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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add regtest assertions for `drain_wallet` + `drain_to` producing a single drain output
- Add regtest assertions for `exclude_unconfirmed` and `exclude_below_confirmations(1)` ignoring trusted pending coins during coin selection
- Audit and refresh Rust and Node development dependencies to their latest compatible releases ([#24](https://github.com/bitcoindevkit/bdk-wasm/issues/24))
- Prototype a declarative macro for tuple-wrapper `Deref`/`From` boilerplate while keeping `wasm_bindgen` getters explicit ([#25](https://github.com/bitcoindevkit/bdk-wasm/issues/25))

### Dependencies

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
mod macros;

pub mod bitcoin;
pub mod types;
mod utils;
Expand Down
27 changes: 27 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
macro_rules! impl_inner_wrapper {
($name:ident, $inner:ty) => {
impl ::std::ops::Deref for $name {
type Target = $inner;

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

impl From<$inner> for $name {
fn from(inner: $inner) -> Self {
Self(inner)
}
}
};

($name:ident, $inner:ty, into_inner) => {
impl_inner_wrapper!($name, $inner);

impl From<$name> for $inner {
fn from(value: $name) -> Self {
value.0
}
}
};
}
24 changes: 2 additions & 22 deletions src/types/amount.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Deref;

use bdk_wallet::{
bitcoin::{Amount as BdkAmount, Denomination as BdkDenomination},
IsDust,
Expand All @@ -18,6 +16,8 @@ use crate::types::{BdkError, BdkErrorCode, ScriptBuf};
#[derive(Clone, Copy, Serialize)]
pub struct Amount(BdkAmount);

impl_inner_wrapper!(Amount, BdkAmount, into_inner);

#[wasm_bindgen]
impl Amount {
pub fn from_btc(btc: f64) -> Result<Self, BdkError> {
Expand Down Expand Up @@ -55,26 +55,6 @@ impl Amount {
}
}

impl Deref for Amount {
type Target = BdkAmount;

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

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

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

impl From<ParseAmountError> for BdkError {
fn from(e: ParseAmountError) -> Self {
use ParseAmountError::*;
Expand Down
18 changes: 2 additions & 16 deletions src/types/balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Deref;

use bdk_wallet::Balance as BdkBalance;
use wasm_bindgen::prelude::wasm_bindgen;

Expand All @@ -10,6 +8,8 @@ use super::Amount;
#[derive(Clone)]
pub struct Balance(BdkBalance);

impl_inner_wrapper!(Balance, BdkBalance);

#[wasm_bindgen]
impl Balance {
/// All coinbase outputs not yet matured
Expand Down Expand Up @@ -51,17 +51,3 @@ impl Balance {
self.0.total().into()
}
}

impl Deref for Balance {
type Target = BdkBalance;

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

impl From<BdkBalance> for Balance {
fn from(inner: BdkBalance) -> Self {
Balance(inner)
}
}
82 changes: 4 additions & 78 deletions src/types/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Deref;

use bdk_wallet::{
chain::{
spk_client::{
Expand All @@ -21,25 +19,7 @@ use super::{ConfirmationBlockTime, Txid};
#[wasm_bindgen]
pub struct SyncRequest(BdkSyncRequest<(KeychainKind, u32)>);

impl Deref for SyncRequest {
type Target = BdkSyncRequest<(KeychainKind, u32)>;

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

impl From<BdkSyncRequest<(KeychainKind, u32)>> for SyncRequest {
fn from(inner: BdkSyncRequest<(KeychainKind, u32)>) -> Self {
SyncRequest(inner)
}
}

impl From<SyncRequest> for BdkSyncRequest<(KeychainKind, u32)> {
fn from(request: SyncRequest) -> Self {
request.0
}
}
impl_inner_wrapper!(SyncRequest, BdkSyncRequest<(KeychainKind, u32)>, into_inner);

/// Data required to perform a spk-based blockchain client full scan.
///
Expand All @@ -50,50 +30,14 @@ impl From<SyncRequest> for BdkSyncRequest<(KeychainKind, u32)> {
#[wasm_bindgen]
pub struct FullScanRequest(BdkFullScanRequest<KeychainKind>);

impl Deref for FullScanRequest {
type Target = BdkFullScanRequest<KeychainKind>;

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

impl From<BdkFullScanRequest<KeychainKind>> for FullScanRequest {
fn from(inner: BdkFullScanRequest<KeychainKind>) -> Self {
FullScanRequest(inner)
}
}

impl From<FullScanRequest> for BdkFullScanRequest<KeychainKind> {
fn from(request: FullScanRequest) -> Self {
request.0
}
}
impl_inner_wrapper!(FullScanRequest, BdkFullScanRequest<KeychainKind>, into_inner);

/// An update to [`Wallet`].
#[wasm_bindgen]
#[derive(Clone)]
pub struct Update(BdkUpdate);

impl Deref for Update {
type Target = BdkUpdate;

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

impl From<BdkUpdate> for Update {
fn from(inner: BdkUpdate) -> Self {
Update(inner)
}
}

impl From<Update> for BdkUpdate {
fn from(update: Update) -> Self {
update.0
}
}
impl_inner_wrapper!(Update, BdkUpdate, into_inner);

impl From<BdkFullScanResponse<KeychainKind>> for Update {
fn from(result: BdkFullScanResponse<KeychainKind>) -> Self {
Expand All @@ -111,13 +55,7 @@ impl From<BdkSyncResponse> for Update {
#[wasm_bindgen]
pub struct ChainPosition(BdkChainPosition<BdkConfirmationBlockTime>);

impl Deref for ChainPosition {
type Target = BdkChainPosition<BdkConfirmationBlockTime>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
impl_inner_wrapper!(ChainPosition, BdkChainPosition<BdkConfirmationBlockTime>, into_inner);

#[wasm_bindgen]
impl ChainPosition {
Expand Down Expand Up @@ -189,15 +127,3 @@ impl ChainPosition {
}
}
}

impl From<BdkChainPosition<BdkConfirmationBlockTime>> for ChainPosition {
fn from(inner: BdkChainPosition<BdkConfirmationBlockTime>) -> Self {
ChainPosition(inner)
}
}

impl From<ChainPosition> for BdkChainPosition<BdkConfirmationBlockTime> {
fn from(chain_position: ChainPosition) -> Self {
chain_position.0
}
}
24 changes: 2 additions & 22 deletions src/types/changeset.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Deref;

use bdk_wallet::{
chain::Merge,
serde_json::{from_str, to_string},
Expand All @@ -14,6 +12,8 @@ use crate::result::JsResult;
#[derive(PartialEq)]
pub struct ChangeSet(BdkChangeSet);

impl_inner_wrapper!(ChangeSet, BdkChangeSet, into_inner);

#[wasm_bindgen]
impl ChangeSet {
/// Merge another [`ChangeSet`] into itself.
Expand All @@ -35,23 +35,3 @@ impl ChangeSet {
Ok(ChangeSet(from_str(val)?))
}
}

impl Deref for ChangeSet {
type Target = BdkChangeSet;

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

impl From<BdkChangeSet> for ChangeSet {
fn from(inner: BdkChangeSet) -> Self {
ChangeSet(inner)
}
}

impl From<ChangeSet> for BdkChangeSet {
fn from(changeset: ChangeSet) -> Self {
changeset.0
}
}
18 changes: 2 additions & 16 deletions src/types/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Deref;

use bdk_wallet::chain::CheckPoint as BdkCheckPoint;
use wasm_bindgen::prelude::wasm_bindgen;

Expand All @@ -13,6 +11,8 @@ use super::BlockId;
#[derive(Clone)]
pub struct CheckPoint(BdkCheckPoint);

impl_inner_wrapper!(CheckPoint, BdkCheckPoint);

#[wasm_bindgen]
impl CheckPoint {
/// Get the [`BlockId`] of the checkpoint.
Expand Down Expand Up @@ -46,17 +46,3 @@ impl CheckPoint {
self.0.get(height).map(Into::into)
}
}

impl Deref for CheckPoint {
type Target = BdkCheckPoint;

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

impl From<BdkCheckPoint> for CheckPoint {
fn from(inner: BdkCheckPoint) -> Self {
CheckPoint(inner)
}
}
42 changes: 3 additions & 39 deletions src/types/fee.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
use bdk_wallet::bitcoin::FeeRate as BdkFeeRate;
use std::{collections::HashMap, ops::Deref};
use std::collections::HashMap;

use wasm_bindgen::prelude::wasm_bindgen;

/// Map where the key is the confirmation target (in number of blocks) and the value is the estimated feerate (in sat/vB).
#[wasm_bindgen]
pub struct FeeEstimates(HashMap<u16, f64>);

impl Deref for FeeEstimates {
type Target = HashMap<u16, f64>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
impl_inner_wrapper!(FeeEstimates, HashMap<u16, f64>, into_inner);

#[wasm_bindgen]
impl FeeEstimates {
Expand All @@ -24,18 +18,6 @@ impl FeeEstimates {
}
}

impl From<HashMap<u16, f64>> for FeeEstimates {
fn from(inner: HashMap<u16, f64>) -> Self {
FeeEstimates(inner)
}
}

impl From<FeeEstimates> for HashMap<u16, f64> {
fn from(fee_estimates: FeeEstimates) -> Self {
fee_estimates.0
}
}

/// Represents fee rate.
///
/// This is an integer newtype representing fee rate in `sat/kwu`. It provides protection against mixing
Expand All @@ -44,13 +26,7 @@ impl From<FeeEstimates> for HashMap<u16, f64> {
#[derive(Clone, Copy)]
pub struct FeeRate(BdkFeeRate);

impl Deref for FeeRate {
type Target = BdkFeeRate;

fn deref(&self) -> &Self::Target {
&self.0
}
}
impl_inner_wrapper!(FeeRate, BdkFeeRate, into_inner);

#[wasm_bindgen]
impl FeeRate {
Expand All @@ -74,15 +50,3 @@ impl FeeRate {
self.0.to_sat_per_vb_floor()
}
}

impl From<BdkFeeRate> for FeeRate {
fn from(inner: BdkFeeRate) -> Self {
FeeRate(inner)
}
}

impl From<FeeRate> for BdkFeeRate {
fn from(fee_rate: FeeRate) -> Self {
fee_rate.0
}
}
Loading
Loading