Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 2 additions & 69 deletions src/types/address.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;
use wasm_bindgen::prelude::wasm_bindgen;

use crate::{
result::JsResult,
types::{BdkError, BdkErrorCode},
types::{BdkError, BdkErrorCode, ScriptBuf},
};

use super::{KeychainKind, Network};
Expand Down Expand Up @@ -143,73 +143,6 @@ impl From<ParseError> 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<Self> {
let script = BdkScriptBuf::from_hex(s)?;
Ok(script.into())
}

pub fn from_bytes(bytes: Vec<u8>) -> 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<u8> {
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<BdkScriptBuf> for ScriptBuf {
fn from(inner: BdkScriptBuf) -> Self {
ScriptBuf(inner)
}
}

impl From<ScriptBuf> for BdkScriptBuf {
fn from(script_buf: ScriptBuf) -> Self {
script_buf.0
}
}

/// The different types of addresses.
#[wasm_bindgen]
#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down
13 changes: 11 additions & 2 deletions src/types/amount.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod keychain;
mod network;
mod output;
mod psbt;
mod script;
mod slip10;
mod transaction;

Expand All @@ -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::*;
88 changes: 88 additions & 0 deletions src/types/script.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
let script = BdkScriptBuf::from_hex(s)?;
Ok(script.into())
}

pub fn from_bytes(bytes: Vec<u8>) -> 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<u8> {
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<BdkScriptBuf> for ScriptBuf {
fn from(inner: BdkScriptBuf) -> Self {
ScriptBuf(inner)
}
}

impl From<ScriptBuf> for BdkScriptBuf {
fn from(script_buf: ScriptBuf) -> Self {
script_buf.0
}
}