Skip to content
Draft
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
49 changes: 38 additions & 11 deletions bdk-ffi/Cargo.lock

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

6 changes: 3 additions & 3 deletions bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bdk-ffi"
version = "2.4.0-alpha.0"
version = "3.0.0-alpha.0"
homepage = "https://bitcoindevkit.org"
repository = "https://github.com/bitcoindevkit/bdk"
edition = "2018"
Expand All @@ -15,10 +15,10 @@ name = "uniffi-bindgen"
path = "uniffi-bindgen.rs"

[dependencies]
bdk_wallet = { version = "2.3.0", features = ["all-keys", "keys-bip39", "rusqlite"] }
bdk_wallet = { version = "=3.0.0", features = ["all-keys", "keys-bip39", "rusqlite"] }
bdk_esplora = { version = "0.22.1", default-features = false, features = ["std", "blocking", "blocking-https-rustls"] }
bdk_electrum = { version = "0.23.2", default-features = false, features = ["use-rustls-ring"] }
bdk_kyoto = { version = "0.15.4" }
bdk_kyoto = { version = "0.16.0" }

uniffi = { version = "=0.30.0", features = ["cli"]}
thiserror = "2.0.17"
Expand Down
31 changes: 18 additions & 13 deletions bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl Descriptor {
#[uniffi::constructor]
pub fn new(descriptor: String, network: Network) -> Result<Self, DescriptorError> {
let secp = Secp256k1::new();
let (extended_descriptor, key_map) = descriptor.into_wallet_descriptor(&secp, network)?;
let (extended_descriptor, key_map) =
descriptor.into_wallet_descriptor(&secp, network.into())?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to get to grips with the NetworkKind type. Are there situations where we'd like to use that instead of the Network for example? Not sure.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t have any concrete ffi facing cases that come to mind, since NetworkKind only has Main and Test... I’ve been thinking of it as mostly an internal/upstream type for apis that only care about that distinction, where bdk-ffi consumers would usually want the full Network to preserve the actual chain. But I need to keep thinking about it more too so im glad you commented on it here

Ok(Self {
extended_descriptor,
key_map,
Expand All @@ -60,8 +61,9 @@ impl Descriptor {
}
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip44(derivable_key, keychain_kind).build(network).unwrap();
let (extended_descriptor, key_map, _) = Bip44(derivable_key, keychain_kind)
.build(network.into())
.unwrap();
Self {
extended_descriptor,
key_map,
Expand Down Expand Up @@ -96,7 +98,7 @@ impl Descriptor {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip44Public(derivable_key, fingerprint, keychain_kind)
.build(network)
.build(network.into())
.map_err(DescriptorError::from)?;

Ok(Self {
Expand Down Expand Up @@ -125,8 +127,9 @@ impl Descriptor {
}
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip49(derivable_key, keychain_kind).build(network).unwrap();
let (extended_descriptor, key_map, _) = Bip49(derivable_key, keychain_kind)
.build(network.into())
.unwrap();
Self {
extended_descriptor,
key_map,
Expand Down Expand Up @@ -161,7 +164,7 @@ impl Descriptor {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip49Public(derivable_key, fingerprint, keychain_kind)
.build(network)
.build(network.into())
.map_err(DescriptorError::from)?;

Ok(Self {
Expand Down Expand Up @@ -190,8 +193,9 @@ impl Descriptor {
}
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip84(derivable_key, keychain_kind).build(network).unwrap();
let (extended_descriptor, key_map, _) = Bip84(derivable_key, keychain_kind)
.build(network.into())
.unwrap();
Self {
extended_descriptor,
key_map,
Expand Down Expand Up @@ -226,7 +230,7 @@ impl Descriptor {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip84Public(derivable_key, fingerprint, keychain_kind)
.build(network)
.build(network.into())
.map_err(DescriptorError::from)?;

Ok(Self {
Expand Down Expand Up @@ -255,8 +259,9 @@ impl Descriptor {
}
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip86(derivable_key, keychain_kind).build(network).unwrap();
let (extended_descriptor, key_map, _) = Bip86(derivable_key, keychain_kind)
.build(network.into())
.unwrap();
Self {
extended_descriptor,
key_map,
Expand Down Expand Up @@ -291,7 +296,7 @@ impl Descriptor {
let derivable_key = descriptor_x_key.xkey;
let (extended_descriptor, key_map, _) =
Bip86Public(derivable_key, fingerprint, keychain_kind)
.build(network)
.build(network.into())
.map_err(DescriptorError::from)?;

Ok(Self {
Expand Down
42 changes: 41 additions & 1 deletion bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use bdk_wallet::descriptor::DescriptorError as BdkDescriptorError;
use bdk_wallet::error::BuildFeeBumpError;
use bdk_wallet::error::CreateTxError as BdkCreateTxError;
use bdk_wallet::keys::bip39::Error as BdkBip39Error;
use bdk_wallet::migration::PreV1MigrationError as BdkPreV1MigrationError;
use bdk_wallet::miniscript::descriptor::DescriptorKeyParseError as BdkDescriptorKeyParseError;
use bdk_wallet::miniscript::psbt::Error as BdkPsbtFinalizeError;
#[allow(deprecated)]
Expand Down Expand Up @@ -585,6 +586,21 @@ pub enum PersistenceError {
Reason { error_message: String },
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum PreV1MigrationError {
#[error("migration helper is only available for sqlite-backed persisters")]
SqliteOnly,

#[error("sqlite migration error: {error_message}")]
Sqlite { error_message: String },

#[error("invalid keychain: {keychain}")]
InvalidKeychain { keychain: String },

#[error("invalid checksum: {error_message}")]
InvalidChecksum { error_message: String },
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum PsbtError {
#[error("invalid magic")]
Expand Down Expand Up @@ -698,6 +714,12 @@ pub enum PsbtParseError {
Base64Encoding { error_message: String },
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum SighashParseError {
#[error("invalid sighash type: {error_message}")]
Invalid { error_message: String },
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum PsbtFinalizeError {
#[error("an input at index {index} is invalid: {reason}")]
Expand Down Expand Up @@ -1333,6 +1355,24 @@ impl From<BdkSqliteError> for PersistenceError {
}
}

impl From<BdkPreV1MigrationError> for PreV1MigrationError {
fn from(error: BdkPreV1MigrationError) -> Self {
match error {
BdkPreV1MigrationError::RusqliteError(error) => PreV1MigrationError::Sqlite {
error_message: error.to_string(),
},
BdkPreV1MigrationError::InvalidKeychain(keychain) => {
PreV1MigrationError::InvalidKeychain { keychain }
}
BdkPreV1MigrationError::InvalidChecksum(error) => {
PreV1MigrationError::InvalidChecksum {
error_message: error.to_string(),
}
}
}
}
}

impl From<bdk_wallet::miniscript::Error> for MiniscriptError {
fn from(error: bdk_wallet::miniscript::Error) -> Self {
use bdk_wallet::miniscript::Error as BdkMiniscriptError;
Expand Down Expand Up @@ -1561,7 +1601,7 @@ impl From<BdkSignerError> for SignerError {
BdkSignerError::MissingKey => SignerError::MissingKey,
BdkSignerError::InvalidKey => SignerError::InvalidKey,
BdkSignerError::UserCanceled => SignerError::UserCanceled,
BdkSignerError::InputIndexOutOfRange => SignerError::InputIndexOutOfRange,
BdkSignerError::InputIndexOutOfRange(_) => SignerError::InputIndexOutOfRange,
BdkSignerError::MissingNonWitnessUtxo => SignerError::MissingNonWitnessUtxo,
BdkSignerError::InvalidNonWitnessUtxo => SignerError::InvalidNonWitnessUtxo,
BdkSignerError::MissingWitnessUtxo => SignerError::MissingWitnessUtxo,
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl DescriptorSecretKey {
let xkey: ExtendedKey = (mnemonic, password).into_extended_key().unwrap();
let descriptor_secret_key = BdkDescriptorSecretKey::XPrv(DescriptorXKey {
origin: None,
xkey: xkey.into_xprv(network).unwrap(),
xkey: xkey.into_xprv(network.into()).unwrap(),
derivation_path: BdkDerivationPath::master(),
wildcard: Wildcard::Unhardened,
});
Expand Down
26 changes: 11 additions & 15 deletions bdk-ffi/src/kyoto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bdk_kyoto::bip157::ServiceFlags;
use bdk_kyoto::builder::Builder as BDKCbfBuilder;
use bdk_kyoto::builder::BuilderExt;
use bdk_kyoto::HeaderCheckpoint;
use bdk_kyoto::LightClient as BDKLightClient;
use bdk_kyoto::Receiver;
use bdk_kyoto::RejectReason;
use bdk_kyoto::Requester;
Expand All @@ -16,7 +15,7 @@ use bdk_kyoto::UnboundedReceiver;
use bdk_kyoto::UpdateSubscriber;
use bdk_kyoto::Warning as Warn;

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -52,7 +51,7 @@ pub struct CbfClient {
sender: Arc<Requester>,
info_rx: Mutex<Receiver<bdk_kyoto::Info>>,
warning_rx: Mutex<UnboundedReceiver<bdk_kyoto::Warning>>,
update_rx: Mutex<UpdateSubscriber>,
update_rx: Mutex<UpdateSubscriber<bdk_kyoto::wallets::Single>>,
}

/// A [`CbfNode`] gathers transactions for a [`Wallet`].
Expand Down Expand Up @@ -238,27 +237,24 @@ impl CbfBuilder {
if let Some(proxy) = &self.socks5_proxy {
let port = proxy.port;
let addr = proxy.address.inner;
builder = builder.socks5_proxy((addr, port));
builder = builder.socks5_proxy(SocketAddr::new(addr, port));
}

let BDKLightClient {
requester,
info_subscriber,
warning_subscriber,
update_subscriber,
node,
} = builder
let (client, logging, update_subscriber) = builder
.build_with_wallet(&wallet, scan_type)
.expect("networks match by definition");
.expect("networks match by definition")
.subscribe();
let (client, node) = client.managed_start();
let requester = client.requester();

let node = CbfNode {
node: std::sync::Mutex::new(Some(node)),
};

let client = CbfClient {
sender: Arc::new(requester),
info_rx: Mutex::new(info_subscriber),
warning_rx: Mutex::new(warning_subscriber),
info_rx: Mutex::new(logging.info_subscriber),
warning_rx: Mutex::new(logging.warning_subscriber),
update_rx: Mutex::new(update_subscriber),
};

Expand Down Expand Up @@ -308,7 +304,7 @@ impl CbfClient {
pub async fn broadcast(&self, transaction: &Transaction) -> Result<Arc<Wtxid>, CbfError> {
let tx = transaction.into();
self.sender
.broadcast_random(tx)
.broadcast_tx(tx)
.await
.map_err(From::from)
.map(|wtxid| Arc::new(Wtxid(wtxid)))
Expand Down
Loading
Loading