diff --git a/src/types/address.rs b/src/types/address.rs index 04ca220..64e8f28 100644 --- a/src/types/address.rs +++ b/src/types/address.rs @@ -1,7 +1,7 @@ use std::{ops::Deref, str::FromStr}; use bdk_wallet::{ - bitcoin::{Address as BdkAddress, AddressType as BdkAddressType, Network as BdkNetwork, ScriptBuf as BdkScriptBuf}, + bitcoin::{Address as BdkAddress, AddressType as BdkAddressType, Network as BdkNetwork}, AddressInfo as BdkAddressInfo, }; use bitcoin::address::ParseError; @@ -9,7 +9,7 @@ use wasm_bindgen::prelude::wasm_bindgen; use crate::{ result::JsResult, - types::{BdkError, BdkErrorCode}, + types::{BdkError, BdkErrorCode, ScriptBuf}, }; use super::{KeychainKind, Network}; @@ -143,73 +143,6 @@ impl From for BdkError { } } -/// An owned, growable script. -/// -/// `ScriptBuf` is the most common script type that has the ownership over the contents of the -/// script. It has a close relationship with its borrowed counterpart, [`Script`]. -#[wasm_bindgen] -#[derive(Clone)] -pub struct ScriptBuf(BdkScriptBuf); - -impl Deref for ScriptBuf { - type Target = BdkScriptBuf; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -#[wasm_bindgen] -impl ScriptBuf { - pub fn from_hex(s: &str) -> JsResult { - let script = BdkScriptBuf::from_hex(s)?; - Ok(script.into()) - } - - pub fn from_bytes(bytes: Vec) -> Self { - BdkScriptBuf::from_bytes(bytes).into() - } - - #[allow(clippy::inherent_to_string)] - #[wasm_bindgen(js_name = toString)] - pub fn to_string(&self) -> String { - self.0.to_string() - } - - pub fn as_bytes(&self) -> Vec { - self.0.as_bytes().to_vec() - } - - pub fn to_asm_string(&self) -> String { - self.0.to_asm_string() - } - - pub fn to_hex_string(&self) -> String { - self.0.to_hex_string() - } - - pub fn is_op_return(&self) -> bool { - self.0.is_op_return() - } - - #[wasm_bindgen(js_name = clone)] - pub fn js_clone(&self) -> ScriptBuf { - self.clone() - } -} - -impl From for ScriptBuf { - fn from(inner: BdkScriptBuf) -> Self { - ScriptBuf(inner) - } -} - -impl From for BdkScriptBuf { - fn from(script_buf: ScriptBuf) -> Self { - script_buf.0 - } -} - /// The different types of addresses. #[wasm_bindgen] #[derive(Clone, Copy, PartialEq, Eq)] diff --git a/src/types/amount.rs b/src/types/amount.rs index 93402d1..f20ebbe 100644 --- a/src/types/amount.rs +++ b/src/types/amount.rs @@ -1,11 +1,14 @@ use std::ops::Deref; -use bdk_wallet::bitcoin::{Amount as BdkAmount, Denomination as BdkDenomination}; +use bdk_wallet::{ + bitcoin::{Amount as BdkAmount, Denomination as BdkDenomination}, + IsDust, +}; use bitcoin::amount::ParseAmountError; use serde::Serialize; use wasm_bindgen::prelude::wasm_bindgen; -use crate::types::{BdkError, BdkErrorCode}; +use crate::types::{BdkError, BdkErrorCode, ScriptBuf}; /// Amount /// @@ -44,6 +47,12 @@ impl Amount { pub fn to_float_in(&self, denom: Denomination) -> f64 { self.0.to_float_in(denom.into()) } + + /// Check whether or not a value is below dust limit + /// for a given script + pub fn is_dust(&self, script: ScriptBuf) -> bool { + self.0.is_dust(&script) + } } impl Deref for Amount { diff --git a/src/types/mod.rs b/src/types/mod.rs index 505f8b1..2df2338 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -12,6 +12,7 @@ mod keychain; mod network; mod output; mod psbt; +mod script; mod slip10; mod transaction; @@ -29,5 +30,6 @@ pub use keychain::*; pub use network::*; pub use output::*; pub use psbt::*; +pub use script::*; pub use slip10::*; pub use transaction::*; diff --git a/src/types/script.rs b/src/types/script.rs new file mode 100644 index 0000000..bdaf011 --- /dev/null +++ b/src/types/script.rs @@ -0,0 +1,88 @@ +use std::ops::Deref; + +use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf; +use wasm_bindgen::prelude::wasm_bindgen; + +use crate::{ + result::JsResult, + types::{Amount, FeeRate}, +}; + +/// An owned, growable script. +/// +/// `ScriptBuf` is the most common script type that has the ownership over the contents of the +/// script. It has a close relationship with its borrowed counterpart, [`Script`]. +#[wasm_bindgen] +#[derive(Clone)] +pub struct ScriptBuf(BdkScriptBuf); + +impl Deref for ScriptBuf { + type Target = BdkScriptBuf; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +#[wasm_bindgen] +impl ScriptBuf { + pub fn from_hex(s: &str) -> JsResult { + let script = BdkScriptBuf::from_hex(s)?; + Ok(script.into()) + } + + pub fn from_bytes(bytes: Vec) -> Self { + BdkScriptBuf::from_bytes(bytes).into() + } + + #[allow(clippy::inherent_to_string)] + #[wasm_bindgen(js_name = toString)] + pub fn to_string(&self) -> String { + self.0.to_string() + } + + pub fn as_bytes(&self) -> Vec { + self.0.as_bytes().to_vec() + } + + pub fn to_asm_string(&self) -> String { + self.0.to_asm_string() + } + + pub fn to_hex_string(&self) -> String { + self.0.to_hex_string() + } + + pub fn is_op_return(&self) -> bool { + self.0.is_op_return() + } + + #[wasm_bindgen(js_name = clone)] + pub fn js_clone(&self) -> ScriptBuf { + self.clone() + } + + /// Returns the minimum value an output with this script should have in order to be + /// broadcastable on today's Bitcoin network. + pub fn minimal_non_dust(&self) -> Amount { + self.0.minimal_non_dust().into() + } + + /// Returns the minimum value an output with this script should have in order to be + /// broadcastable on today's Bitcoin network. + pub fn minimal_non_dust_custom(&self, dust_relay_fee: FeeRate) -> Amount { + self.0.minimal_non_dust_custom(dust_relay_fee.into()).into() + } +} + +impl From for ScriptBuf { + fn from(inner: BdkScriptBuf) -> Self { + ScriptBuf(inner) + } +} + +impl From for BdkScriptBuf { + fn from(script_buf: ScriptBuf) -> Self { + script_buf.0 + } +}