Skip to content

Commit

Permalink
feat: 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 15, 2024
1 parent 11c7646 commit b06cb6b
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 112 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 @@ -27,7 +27,7 @@ bdk_electrum = { git = "https://github.com/thunderbiscuit/bdk/", package = "bdk_
bdk_sqlite = { version = "0.2.0" }
bdk_bitcoind_rpc = { version = "0.12.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
41 changes: 10 additions & 31 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,6 @@ interface FromScriptError {
OtherFromScriptErr();
};

[Error]
interface ParseAmountError {
OutOfRange();
TooPrecise();
MissingDigits();
InputTooLarge();
InvalidCharacter(string error_message);
OtherParseAmountErr();
};

[Error]
interface PersistenceError {
Write(string error_message);
Expand Down Expand Up @@ -605,14 +595,6 @@ dictionary SentAndReceivedValues {
// bdk_wallet crate - bitcoin re-exports
// ------------------------------------------------------------------------

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

enum WordCount {
"Words12",
"Words15",
Expand Down Expand Up @@ -688,18 +670,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 @@ -722,8 +692,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
58 changes: 21 additions & 37 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bdk_esplora::esplora_client::{Error as BdkEsploraError, Error};
use bdk_sqlite::Error as BdkSqliteError;
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 @@ -396,27 +395,27 @@ pub enum InspectError {
RequestAlreadyConsumed,
}

#[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,
}
// #[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,
// }

#[derive(Debug, thiserror::Error)]
pub enum PersistenceError {
Expand Down Expand Up @@ -1058,21 +1057,6 @@ impl From<BdkFromScriptError> for FromScriptError {
}
}

impl From<BdkParseAmountError> for ParseAmountError {
fn from(error: BdkParseAmountError) -> Self {
match error {
BdkParseAmountError::OutOfRange(_) => ParseAmountError::OutOfRange,
BdkParseAmountError::TooPrecise(_) => ParseAmountError::TooPrecise,
BdkParseAmountError::MissingDigits(_) => ParseAmountError::MissingDigits,
BdkParseAmountError::InputTooLarge(_) => ParseAmountError::InputTooLarge,
BdkParseAmountError::InvalidCharacter(c) => ParseAmountError::InvalidCharacter {
error_message: c.to_string(),
},
_ => ParseAmountError::OtherParseAmountErr,
}
}
}

impl From<std::io::Error> for PersistenceError {
fn from(error: std::io::Error) -> Self {
PersistenceError::Write {
Expand Down
6 changes: 3 additions & 3 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod types;
mod wallet;

use crate::bitcoin::Address;
use crate::bitcoin::Amount;
// use crate::bitcoin::Amount;
use crate::bitcoin::FeeRate;
use crate::bitcoin::OutPoint;
use crate::bitcoin::Psbt;
Expand All @@ -32,7 +32,6 @@ use crate::error::ExtractTxError;
use crate::error::FeeRateError;
use crate::error::FromScriptError;
use crate::error::InspectError;
use crate::error::ParseAmountError;
use crate::error::PersistenceError;
use crate::error::PsbtError;
use crate::error::PsbtParseError;
Expand Down Expand Up @@ -64,9 +63,10 @@ use crate::wallet::TxBuilder;
use crate::wallet::Update;
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::wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::KeychainKind;
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::InspectError;

use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
Expand All @@ -11,6 +11,7 @@ use bdk_wallet::wallet::AddressInfo as BdkAddressInfo;
use bdk_wallet::wallet::Balance as BdkBalance;
use bdk_wallet::KeychainKind;
use bdk_wallet::LocalOutput as BdkLocalOutput;
use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;

use bdk_electrum::bdk_chain::CombinedChangeSet;
Expand Down
4 changes: 2 additions & 2 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::bitcoin::Amount;
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
Expand All @@ -20,6 +19,7 @@ use bdk_wallet::wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::wallet::Update as BdkUpdate;
use bdk_wallet::Wallet as BdkWallet;
use bdk_wallet::{KeychainKind, SignOptions};
use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;

use std::collections::HashSet;
Expand Down Expand Up @@ -169,7 +169,7 @@ pub struct SentAndReceivedValues {

pub struct Update(pub(crate) BdkUpdate);

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct TxBuilder {
pub(crate) recipients: Vec<(BdkScriptBuf, BdkAmount)>,
pub(crate) utxos: Vec<OutPoint>,
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 b06cb6b

Please sign in to comment.