Skip to content

Commit

Permalink
refactor: use network and amount types from bitcoin-ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Aug 30, 2024
1 parent d0793dc commit a021d7c
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 86 deletions.
3 changes: 2 additions & 1 deletion bdk-ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bdk_esplora = { version = "0.17.0", default-features = false, features = ["std",
bdk_electrum = { version = "0.17.0", default-features = false, features = ["use-rustls-ring"] }
bdk_bitcoind_rpc = { version = "0.14.0" }
bitcoin-internals = { version = "0.2.0", features = ["alloc"] }
bitcoin-ffi = { git = "https://github.com/thunderbiscuit/bitcoin-ffi.git", tag = "v0.1.1" }
bitcoin-ffi = { git = "https://github.com/rustaceanrob/bitcoin-ffi.git", branch = "another-type" }

uniffi = { version = "=0.28.0" }
thiserror = "1.0.58"
Expand Down
31 changes: 10 additions & 21 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,6 @@ dictionary SentAndReceivedValues {
// bdk_wallet crate - bitcoin re-exports
// ------------------------------------------------------------------------

[NonExhaustive]
enum Network {
"Bitcoin",
"Testnet",
"Signet",
"Regtest",
};

enum WordCount {
"Words12",
"Words15",
Expand Down Expand Up @@ -716,18 +708,6 @@ dictionary OutPoint {
u32 vout;
};

interface Amount {
[Name=from_sat]
constructor(u64 from_sat);

[Name=from_btc, Throws=ParseAmountError]
constructor(f64 from_btc);

u64 to_sat();

f64 to_btc();
};

interface FeeRate {
[Name=from_sat_per_vb, Throws=FeeRateError]
constructor(u64 sat_per_vb);
Expand All @@ -750,8 +730,17 @@ dictionary TxIn {
};

// ------------------------------------------------------------------------
// types defined in external crate rust-bitcoin-ffi
// types defined in external crate bitcoin-ffi
// ------------------------------------------------------------------------

[ExternalInterface="bitcoin_ffi"]
typedef extern Script;

[External="bitcoin_ffi"]
typedef extern Network;

[ExternalInterface="bitcoin_ffi"]
typedef extern Amount;

[ExternalInterface="bitcoin_ffi"]
typedef extern ParseAmountError;
70 changes: 35 additions & 35 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use crate::error::{

use bdk_bitcoind_rpc::bitcoincore_rpc::jsonrpc::serde_json;
use bdk_wallet::bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bdk_wallet::bitcoin::amount::ParseAmountError;
// use bdk_wallet::bitcoin::amount::ParseAmountError;
use bdk_wallet::bitcoin::consensus::encode::serialize;
use bdk_wallet::bitcoin::consensus::Decodable;
use bdk_wallet::bitcoin::io::Cursor;
use bdk_wallet::bitcoin::psbt::ExtractTxError;
use bdk_wallet::bitcoin::Address as BdkAddress;
use bdk_wallet::bitcoin::Amount as BdkAmount;
// use bdk_wallet::bitcoin::Amount as BdkAmount;
use bdk_wallet::bitcoin::FeeRate as BdkFeeRate;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::bitcoin::OutPoint as BdkOutPoint;
Expand All @@ -26,39 +26,39 @@ use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Amount(pub(crate) BdkAmount);

impl Amount {
pub fn from_sat(sat: u64) -> Self {
Amount(BdkAmount::from_sat(sat))
}

pub fn from_btc(btc: f64) -> Result<Self, ParseAmountError> {
let bdk_amount = BdkAmount::from_btc(btc).map_err(ParseAmountError::from)?;
Ok(Amount(bdk_amount))
}

pub fn to_sat(&self) -> u64 {
self.0.to_sat()
}

pub fn to_btc(&self) -> f64 {
self.0.to_btc()
}
}

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

impl From<BdkAmount> for Amount {
fn from(amount: BdkAmount) -> Self {
Amount(amount)
}
}
// #[derive(Clone, Debug, PartialEq, Eq)]
// pub struct Amount(pub(crate) BdkAmount);
//
// impl Amount {
// pub fn from_sat(sat: u64) -> Self {
// Amount(BdkAmount::from_sat(sat))
// }
//
// pub fn from_btc(btc: f64) -> Result<Self, ParseAmountError> {
// let bdk_amount = BdkAmount::from_btc(btc).map_err(ParseAmountError::from)?;
// Ok(Amount(bdk_amount))
// }
//
// pub fn to_sat(&self) -> u64 {
// self.0.to_sat()
// }
//
// pub fn to_btc(&self) -> f64 {
// self.0.to_btc()
// }
// }
//
// impl From<Amount> for BdkAmount {
// fn from(amount: Amount) -> Self {
// amount.0
// }
// }
//
// impl From<BdkAmount> for Amount {
// fn from(amount: BdkAmount) -> Self {
// Amount(amount)
// }
// }

// #[derive(Clone, Debug, PartialEq, Eq)]
// pub struct Script(pub(crate) BdkScriptBuf);
Expand Down
43 changes: 21 additions & 22 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use bdk_electrum::electrum_client::Error as BdkElectrumError;
use bdk_esplora::esplora_client::{Error as BdkEsploraError, Error};
use bdk_wallet::bitcoin::address::FromScriptError as BdkFromScriptError;
use bdk_wallet::bitcoin::address::ParseError as BdkParseError;
use bdk_wallet::bitcoin::amount::ParseAmountError as BdkParseAmountError;
use bdk_wallet::bitcoin::bip32::Error as BdkBip32Error;
use bdk_wallet::bitcoin::consensus::encode::Error as BdkEncodeError;
use bdk_wallet::bitcoin::psbt::Error as BdkPsbtError;
Expand Down Expand Up @@ -408,7 +407,7 @@ pub enum RequestBuilderError {
RequestAlreadyConsumed,
}

#[derive(Debug, thiserror::Error)]
// #[derive(Debug, thiserror::Error)]
pub enum LoadWithPersistError {
#[error("sqlite persistence error: {error_message}")]
Persist { error_message: String },
Expand All @@ -421,26 +420,26 @@ pub enum LoadWithPersistError {
}

#[derive(Debug, thiserror::Error)]
pub enum ParseAmountError {
#[error("amount out of range")]
OutOfRange,

#[error("amount has a too high precision")]
TooPrecise,

#[error("the input has too few digits")]
MissingDigits,

#[error("the input is too large")]
InputTooLarge,

#[error("invalid character: {error_message}")]
InvalidCharacter { error_message: String },

// Has to handle non-exhaustive
#[error("unknown parse amount error")]
OtherParseAmountErr,
}
// pub enum ParseAmountError {
// #[error("amount out of range")]
// OutOfRange,
//
// #[error("amount has a too high precision")]
// TooPrecise,
//
// #[error("the input has too few digits")]
// MissingDigits,
//
// #[error("the input is too large")]
// InputTooLarge,
//
// #[error("invalid character: {error_message}")]
// InvalidCharacter { error_message: String },
//
// // Has to handle non-exhaustive
// #[error("unknown parse amount error")]
// OtherParseAmountErr,
// }

#[derive(Debug, thiserror::Error)]
pub enum PersistenceError {
Expand Down
5 changes: 2 additions & 3 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod types;
mod wallet;

use crate::bitcoin::Address;
use crate::bitcoin::Amount;
use crate::bitcoin::FeeRate;
use crate::bitcoin::OutPoint;
use crate::bitcoin::Psbt;
Expand All @@ -33,7 +32,6 @@ use crate::error::ExtractTxError;
use crate::error::FeeRateError;
use crate::error::FromScriptError;
use crate::error::LoadWithPersistError;
use crate::error::ParseAmountError;
use crate::error::PersistenceError;
use crate::error::PsbtError;
use crate::error::PsbtParseError;
Expand Down Expand Up @@ -68,9 +66,10 @@ use crate::wallet::SentAndReceivedValues;
use crate::wallet::TxBuilder;
use crate::wallet::Wallet;

use bitcoin_ffi::Amount;
use bitcoin_ffi::Network;
use bitcoin_ffi::Script;

use bdk_wallet::bitcoin::Network;
use bdk_wallet::keys::bip39::WordCount;
use bdk_wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::ChangeSet;
Expand Down
3 changes: 2 additions & 1 deletion bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bitcoin::{Address, Amount, OutPoint, Transaction, TxOut};
use crate::bitcoin::{Address, OutPoint, Transaction, TxOut};
use crate::error::RequestBuilderError;

use bdk_core::spk_client::SyncItem;
Expand All @@ -15,6 +15,7 @@ use bdk_wallet::AddressInfo as BdkAddressInfo;
use bdk_wallet::Balance as BdkBalance;
use bdk_wallet::KeychainKind;
use bdk_wallet::LocalOutput as BdkLocalOutput;
use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;
use bdk_wallet::Update as BdkUpdate;

Expand Down
3 changes: 2 additions & 1 deletion bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use bdk_wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::PersistedWallet;
use bdk_wallet::Wallet as BdkWallet;
use bdk_wallet::{KeychainKind, SignOptions};
use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;

use std::borrow::BorrowMut;
Expand Down Expand Up @@ -185,7 +186,7 @@ pub struct SentAndReceivedValues {
pub received: Arc<Amount>,
}

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct TxBuilder {
pub(crate) add_global_xpubs: bool,
pub(crate) recipients: Vec<(BdkScriptBuf, BdkAmount)>,
Expand Down
2 changes: 1 addition & 1 deletion bdk-jvm/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ test-offline:
./gradlew test -P excludeConnectedTests

test-specific TEST:
./gradlew test --tests {{TEST}}
./gradlew test --tests {{TEST}}

0 comments on commit a021d7c

Please sign in to comment.