Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify wrappers #17

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
41 changes: 18 additions & 23 deletions src/bitcoin/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use crate::{
};

#[wasm_bindgen]
pub struct Wallet {
wallet: BdkWallet,
}
pub struct Wallet(BdkWallet);

#[wasm_bindgen]
impl Wallet {
Expand All @@ -23,7 +21,7 @@ impl Wallet {
.network(network.into())
.create_wallet_no_persist()?;

Ok(Wallet { wallet })
Ok(Wallet(wallet))
}

pub fn load(changeset: ChangeSet) -> JsResult<Wallet> {
Expand All @@ -34,83 +32,80 @@ impl Wallet {
None => return Err(JsError::new("Failed to load wallet, check the changeset")),
};

Ok(Wallet { wallet })
Ok(Wallet(wallet))
}

pub fn start_full_scan(&self) -> FullScanRequest {
self.wallet.start_full_scan().build().into()
self.0.start_full_scan().build().into()
}

pub fn start_sync_with_revealed_spks(&self) -> SyncRequest {
self.wallet.start_sync_with_revealed_spks().build().into()
self.0.start_sync_with_revealed_spks().build().into()
}

pub fn apply_update(&mut self, update: Update) -> JsResult<()> {
self.apply_update_at(update, (Date::now() / 1000.0) as u64)
}

pub fn apply_update_at(&mut self, update: Update, seen_at: u64) -> JsResult<()> {
self.wallet.apply_update_at(update, seen_at)?;
self.0.apply_update_at(update, seen_at)?;
Ok(())
}

pub fn network(&self) -> Network {
self.wallet.network().into()
self.0.network().into()
}

pub fn balance(&self) -> Balance {
self.wallet.balance().into()
self.0.balance().into()
}

pub fn next_unused_address(&mut self, keychain: KeychainKind) -> AddressInfo {
self.wallet.next_unused_address(keychain.into()).into()
self.0.next_unused_address(keychain.into()).into()
}

pub fn peek_address(&self, keychain: KeychainKind, index: u32) -> AddressInfo {
self.wallet.peek_address(keychain.into(), index).into()
self.0.peek_address(keychain.into(), index).into()
}

pub fn reveal_next_address(&mut self, keychain: KeychainKind) -> AddressInfo {
self.wallet.reveal_next_address(keychain.into()).into()
self.0.reveal_next_address(keychain.into()).into()
}

pub fn reveal_addresses_to(&mut self, keychain: KeychainKind, index: u32) -> Vec<AddressInfo> {
self.wallet
self.0
.reveal_addresses_to(keychain.into(), index)
.map(Into::into)
.collect()
}

pub fn list_unused_addresses(&self, keychain: KeychainKind) -> Vec<AddressInfo> {
self.wallet
.list_unused_addresses(keychain.into())
.map(Into::into)
.collect()
self.0.list_unused_addresses(keychain.into()).map(Into::into).collect()
}

pub fn list_unspent(&self) -> JsResult<Vec<JsValue>> {
self.wallet
self.0
.list_unspent()
.map(|output| to_value(&output).map_err(Into::into))
.collect()
}

pub fn transactions(&self) -> JsResult<Vec<JsValue>> {
self.wallet
self.0
.transactions()
.map(|tx| to_value(&tx.tx_node.tx).map_err(Into::into))
.collect()
}

pub fn latest_checkpoint(&self) -> CheckPoint {
self.wallet.latest_checkpoint().into()
self.0.latest_checkpoint().into()
}

pub fn take_staged(&mut self) -> Option<ChangeSet> {
self.wallet.take_staged().map(Into::into)
self.0.take_staged().map(Into::into)
}

pub fn public_descriptor(&self, keychain: KeychainKind) -> String {
self.wallet.public_descriptor(keychain.into()).to_string()
self.0.public_descriptor(keychain.into()).to_string()
}
}
18 changes: 8 additions & 10 deletions src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,26 @@ use super::KeychainKind;
/// A derived address and the index it was found at.
#[wasm_bindgen]
#[derive(Debug)]
pub struct AddressInfo {
address: BdkAddressInfo,
}
pub struct AddressInfo(BdkAddressInfo);

#[wasm_bindgen]
impl AddressInfo {
/// Child index of this address
#[wasm_bindgen(getter)]
pub fn index(&self) -> u32 {
self.address.index
self.0.index
}

/// Address
#[wasm_bindgen(getter)]
pub fn address(&self) -> String {
self.address.to_string()
self.0.to_string()
}

/// Type of keychain
#[wasm_bindgen(getter)]
pub fn keychain(&self) -> KeychainKind {
self.address.keychain.into()
self.0.keychain.into()
}

/// Gets the address type of the address.
Expand All @@ -39,21 +37,21 @@ impl AddressInfo {
/// None if unknown, non-standard or related to the future witness version.
#[wasm_bindgen(getter)]
pub fn address_type(&self) -> Option<AddressType> {
self.address.address_type().map(Into::into)
self.0.address_type().map(Into::into)
}
}

impl Deref for AddressInfo {
type Target = BdkAddressInfo;

fn deref(&self) -> &Self::Target {
&self.address
&self.0
}
}

impl From<BdkAddressInfo> for AddressInfo {
fn from(address: BdkAddressInfo) -> Self {
AddressInfo { address }
fn from(inner: BdkAddressInfo) -> Self {
AddressInfo(inner)
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/types/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,41 @@ use wasm_bindgen::prelude::wasm_bindgen;
/// arithmetic and conversion to various denominations.
#[wasm_bindgen]
#[derive(Debug)]
pub struct Amount {
amount: BdkAmount,
}
pub struct Amount(BdkAmount);

#[wasm_bindgen]
impl Amount {
/// Gets the number of satoshis in this [`Amount`].
pub fn to_sat(&self) -> u64 {
self.amount.to_sat()
self.0.to_sat()
}

/// Express this [`Amount`] as a floating-point value in Bitcoin.
///
/// Please be aware of the risk of using floating-point numbers.
pub fn to_btc(&self) -> f64 {
self.amount.to_btc()
self.0.to_btc()
}

/// Express this [Amount] as a floating-point value in the given denomination.
///
/// Please be aware of the risk of using floating-point numbers.
pub fn to_float_in(&self, denom: Denomination) -> f64 {
self.amount.to_float_in(denom.into())
self.0.to_float_in(denom.into())
}
}

impl Deref for Amount {
type Target = BdkAmount;

fn deref(&self) -> &Self::Target {
&self.amount
&self.0
}
}

impl From<BdkAmount> for Amount {
fn from(amount: BdkAmount) -> Self {
Amount { amount }
fn from(inner: BdkAmount) -> Self {
Amount(inner)
}
}

Expand Down
22 changes: 10 additions & 12 deletions src/types/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,32 @@ use super::Amount;
/// Balance, differentiated into various categories.
#[wasm_bindgen]
#[derive(Debug, PartialEq, Eq)]
pub struct Balance {
balance: BdkBalance,
}
pub struct Balance(BdkBalance);

#[wasm_bindgen]
impl Balance {
/// All coinbase outputs not yet matured
#[wasm_bindgen(getter)]
pub fn immature(&self) -> Amount {
self.balance.immature.into()
self.0.immature.into()
}

/// Unconfirmed UTXOs generated by a wallet tx
#[wasm_bindgen(getter)]
pub fn trusted_pending(&self) -> Amount {
self.balance.trusted_pending.into()
self.0.trusted_pending.into()
}

/// Unconfirmed UTXOs received from an external wallet
#[wasm_bindgen(getter)]
pub fn untrusted_pending(&self) -> Amount {
self.balance.untrusted_pending.into()
self.0.untrusted_pending.into()
}

/// Confirmed and immediately spendable balance
#[wasm_bindgen(getter)]
pub fn confirmed(&self) -> Amount {
self.balance.confirmed.into()
self.0.confirmed.into()
}

/// Get sum of trusted_pending and confirmed coins.
Expand All @@ -44,26 +42,26 @@ impl Balance {
/// double spending it.
#[wasm_bindgen(getter)]
pub fn trusted_spendable(&self) -> Amount {
self.balance.trusted_spendable().into()
self.0.trusted_spendable().into()
}

/// Get the whole balance visible to the wallet.
#[wasm_bindgen(getter)]
pub fn total(&self) -> Amount {
self.balance.total().into()
self.0.total().into()
}
}

impl Deref for Balance {
type Target = BdkBalance;

fn deref(&self) -> &Self::Target {
&self.balance
&self.0
}
}

impl From<BdkBalance> for Balance {
fn from(balance: BdkBalance) -> Self {
Balance { balance }
fn from(inner: BdkBalance) -> Self {
Balance(inner)
}
}
12 changes: 5 additions & 7 deletions src/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@ use wasm_bindgen::prelude::wasm_bindgen;
/// A reference to a block in the canonical chain.
#[wasm_bindgen]
#[derive(Debug)]
pub struct BlockId {
block_id: BdkBlockId,
}
pub struct BlockId(BdkBlockId);

#[wasm_bindgen]
impl BlockId {
/// The height of the block.
#[wasm_bindgen(getter)]
pub fn height(&self) -> u32 {
self.block_id.height
self.0.height
}

/// The hash of the block.
#[wasm_bindgen(getter)]
pub fn hash(&self) -> String {
self.block_id.hash.to_string()
self.0.hash.to_string()
}
}

impl From<BdkBlockId> for BlockId {
fn from(block_id: BdkBlockId) -> Self {
BlockId { block_id }
fn from(inner: BdkBlockId) -> Self {
BlockId(inner)
}
}
Loading
Loading