From ccf5fbda6efa8c6147da674677685ba6180768e2 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Wed, 10 Jan 2024 14:04:06 -0500 Subject: [PATCH] feat: add generic alpha 3 error --- bdk-ffi/Cargo.lock | 1 + bdk-ffi/Cargo.toml | 1 + bdk-ffi/src/bdk.udl | 95 +++++++++++++++------------------------ bdk-ffi/src/bitcoin.rs | 16 +++---- bdk-ffi/src/descriptor.rs | 4 +- bdk-ffi/src/error.rs | 77 +++++++++++++++++++++++++++++++ bdk-ffi/src/esplora.rs | 12 +++-- bdk-ffi/src/keys.rs | 72 ++++++++++++----------------- bdk-ffi/src/lib.rs | 4 +- bdk-ffi/src/types.rs | 10 ++--- bdk-ffi/src/wallet.rs | 22 ++++----- 11 files changed, 177 insertions(+), 137 deletions(-) diff --git a/bdk-ffi/Cargo.lock b/bdk-ffi/Cargo.lock index e8df64f..eb9f746 100644 --- a/bdk-ffi/Cargo.lock +++ b/bdk-ffi/Cargo.lock @@ -163,6 +163,7 @@ dependencies = [ "assert_matches", "bdk", "bdk_esplora", + "thiserror", "uniffi", ] diff --git a/bdk-ffi/Cargo.toml b/bdk-ffi/Cargo.toml index d2afa2b..9958140 100644 --- a/bdk-ffi/Cargo.toml +++ b/bdk-ffi/Cargo.toml @@ -30,6 +30,7 @@ bdk = { version = "1.0.0-alpha.3", features = ["all-keys", "keys-bip39"] } bdk_esplora = { version = "0.5.0", default-features = false, features = ["std", "blocking"] } uniffi = { version = "=0.25.1" } +thiserror = "1.0.50" [build-dependencies] uniffi = { version = "=0.25.1", features = ["build"] } diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index fe41450..60299a6 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -1,5 +1,20 @@ namespace bdk {}; +// ------------------------------------------------------------------------ +// bdk crate - error module +// ------------------------------------------------------------------------ + +[Error] +enum Alpha3Error { + "Generic" +}; + +[Error] +interface CalculateFeeError { + MissingTxOut(sequence out_points); + NegativeFee(i64 fee); +}; + // ------------------------------------------------------------------------ // bdk crate - types module // ------------------------------------------------------------------------ @@ -36,7 +51,7 @@ dictionary Balance { u64 total; }; -dictionary LocalUtxo { +dictionary LocalOutput { OutPoint outpoint; TxOut txout; KeychainKind keychain; @@ -52,42 +67,6 @@ dictionary TxOut { // bdk crate - wallet module // ------------------------------------------------------------------------ -[Error] -enum BdkError { - "Generic", - "NoRecipients", - "NoUtxosSelected", - "OutputBelowDustLimit", - "InsufficientFunds", - "BnBTotalTriesExceeded", - "BnBNoExactMatch", - "UnknownUtxo", - "TransactionNotFound", - "TransactionConfirmed", - "IrreplaceableTransaction", - "FeeRateTooLow", - "FeeTooLow", - "FeeRateUnavailable", - "MissingKeyOrigin", - "Key", - "ChecksumMismatch", - "SpendingPolicyRequired", - "InvalidPolicyPathError", - "Signer", - "InvalidOutpoint", - "Descriptor", - "Miniscript", - "MiniscriptPsbt", - "Bip32", - "Psbt", -}; - -[Error] -interface CalculateFeeError { - MissingTxOut(sequence out_points); - NegativeFee(i64 fee); -}; - interface FeeRate { f32 as_sat_per_vb(); f32 sat_per_kwu(); @@ -100,7 +79,7 @@ enum ChangeSpendPolicy { }; interface Wallet { - [Name=new_no_persist, Throws=BdkError] + [Name=new_no_persist, Throws=Alpha3Error] constructor(Descriptor descriptor, Descriptor? change_descriptor, Network network); AddressInfo get_address(AddressIndex address_index); @@ -113,10 +92,10 @@ interface Wallet { boolean is_mine([ByRef] Script script); - [Throws=BdkError] + [Throws=Alpha3Error] void apply_update(Update update); - [Throws=BdkError] + [Throws=Alpha3Error] boolean sign(PartiallySignedTransaction psbt); SentAndReceivedValues sent_and_received([ByRef] Transaction tx); @@ -165,7 +144,7 @@ interface TxBuilder { TxBuilder enable_rbf_with_sequence(u32 nsequence); - [Throws=BdkError] + [Throws=Alpha3Error] PartiallySignedTransaction finish([ByRef] Wallet wallet); }; @@ -178,7 +157,7 @@ interface BumpFeeTxBuilder { BumpFeeTxBuilder enable_rbf_with_sequence(u32 nsequence); - [Throws=BdkError] + [Throws=Alpha3Error] PartiallySignedTransaction finish([ByRef] Wallet wallet); }; @@ -189,30 +168,30 @@ interface BumpFeeTxBuilder { interface Mnemonic { constructor(WordCount word_count); - [Name=from_string, Throws=BdkError] + [Name=from_string, Throws=Alpha3Error] constructor(string mnemonic); - [Name=from_entropy, Throws=BdkError] + [Name=from_entropy, Throws=Alpha3Error] constructor(sequence entropy); string as_string(); }; interface DerivationPath { - [Throws=BdkError] + [Throws=Alpha3Error] constructor(string path); }; interface DescriptorSecretKey { constructor(Network network, [ByRef] Mnemonic mnemonic, string? password); - [Name=from_string, Throws=BdkError] + [Name=from_string, Throws=Alpha3Error] constructor(string secret_key); - [Throws=BdkError] + [Throws=Alpha3Error] DescriptorSecretKey derive([ByRef] DerivationPath path); - [Throws=BdkError] + [Throws=Alpha3Error] DescriptorSecretKey extend([ByRef] DerivationPath path); DescriptorPublicKey as_public(); @@ -223,20 +202,20 @@ interface DescriptorSecretKey { }; interface DescriptorPublicKey { - [Name=from_string, Throws=BdkError] + [Name=from_string, Throws=Alpha3Error] constructor(string public_key); - [Throws=BdkError] + [Throws=Alpha3Error] DescriptorPublicKey derive([ByRef] DerivationPath path); - [Throws=BdkError] + [Throws=Alpha3Error] DescriptorPublicKey extend([ByRef] DerivationPath path); string as_string(); }; interface Descriptor { - [Throws=BdkError] + [Throws=Alpha3Error] constructor(string descriptor, Network network); [Name=new_bip44] @@ -275,10 +254,10 @@ interface Descriptor { interface EsploraClient { constructor(string url); - [Throws=BdkError] + [Throws=Alpha3Error] Update scan(Wallet wallet, u64 stop_gap, u64 parallel_requests); - [Throws=BdkError] + [Throws=Alpha3Error] void broadcast([ByRef] Transaction transaction); }; @@ -322,7 +301,7 @@ enum WordCount { }; interface Address { - [Throws=BdkError] + [Throws=Alpha3Error] constructor(string address, Network network); Network network(); @@ -337,7 +316,7 @@ interface Address { }; interface Transaction { - [Throws=BdkError] + [Throws=Alpha3Error] constructor(sequence transaction_bytes); string txid(); @@ -356,7 +335,7 @@ interface Transaction { }; interface PartiallySignedTransaction { - [Throws=BdkError] + [Throws=Alpha3Error] constructor(string psbt_base64); string serialize(); @@ -367,4 +346,4 @@ interface PartiallySignedTransaction { dictionary OutPoint { string txid; u32 vout; -}; \ No newline at end of file +}; diff --git a/bdk-ffi/src/bitcoin.rs b/bdk-ffi/src/bitcoin.rs index 2a24912..8f250d6 100644 --- a/bdk-ffi/src/bitcoin.rs +++ b/bdk-ffi/src/bitcoin.rs @@ -8,11 +8,11 @@ use bdk::bitcoin::Address as BdkAddress; use bdk::bitcoin::OutPoint as BdkOutPoint; use bdk::bitcoin::Transaction as BdkTransaction; use bdk::bitcoin::Txid; -use bdk::Error as BdkError; use std::io::Cursor; use std::str::FromStr; use std::sync::{Arc, Mutex}; +use crate::error::Alpha3Error; #[derive(Clone, Debug, PartialEq, Eq)] pub struct Script(pub(crate) BdkScriptBuf); @@ -71,14 +71,14 @@ pub struct Address { } impl Address { - pub fn new(address: String, network: Network) -> Result { + pub fn new(address: String, network: Network) -> Result { let parsed_address = address .parse::>() - .map_err(|e| BdkError::Generic(e.to_string()))?; + .map_err(|e| Alpha3Error::Generic)?; let network_checked_address = parsed_address .require_network(network.into()) - .map_err(|e| BdkError::Generic(e.to_string()))?; + .map_err(|e| Alpha3Error::Generic)?; Ok(Address { inner: network_checked_address, @@ -151,10 +151,10 @@ pub struct Transaction { } impl Transaction { - pub fn new(transaction_bytes: Vec) -> Result { + pub fn new(transaction_bytes: Vec) -> Result { let mut decoder = Cursor::new(transaction_bytes); let tx: BdkTransaction = BdkTransaction::consensus_decode(&mut decoder) - .map_err(|e| BdkError::Generic(e.to_string()))?; + .map_err(|e| Alpha3Error::Generic)?; Ok(Transaction { inner: tx }) } @@ -230,10 +230,10 @@ pub struct PartiallySignedTransaction { } impl PartiallySignedTransaction { - pub(crate) fn new(psbt_base64: String) -> Result { + pub(crate) fn new(psbt_base64: String) -> Result { let psbt: BdkPartiallySignedTransaction = BdkPartiallySignedTransaction::from_str(&psbt_base64) - .map_err(|e| BdkError::Generic(e.to_string()))?; + .map_err(|e| Alpha3Error::Generic)?; Ok(PartiallySignedTransaction { inner: Mutex::new(psbt), diff --git a/bdk-ffi/src/descriptor.rs b/bdk-ffi/src/descriptor.rs index b86878a..aa020f0 100644 --- a/bdk-ffi/src/descriptor.rs +++ b/bdk-ffi/src/descriptor.rs @@ -1,6 +1,7 @@ use crate::keys::DescriptorPublicKey; use crate::keys::DescriptorSecretKey; use crate::Network; +use crate::error::Alpha3Error; use bdk::bitcoin::bip32::Fingerprint; use bdk::bitcoin::key::Secp256k1; @@ -11,7 +12,6 @@ use bdk::template::{ Bip44, Bip44Public, Bip49, Bip49Public, Bip84, Bip84Public, Bip86, Bip86Public, DescriptorTemplate, }; -use bdk::Error as BdkError; use bdk::KeychainKind; use std::str::FromStr; @@ -23,7 +23,7 @@ pub struct Descriptor { } impl Descriptor { - pub(crate) fn new(descriptor: String, network: Network) -> Result { + pub(crate) fn new(descriptor: String, network: Network) -> Result { let secp = Secp256k1::new(); let (extended_descriptor, key_map) = descriptor.into_wallet_descriptor(&secp, network.into())?; diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index 47a36ab..928f76d 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -4,6 +4,83 @@ use bdk::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError; use std::fmt; +use bdk::descriptor::DescriptorError; +use bdk::wallet::error::{BuildFeeBumpError, CreateTxError}; +use bdk::wallet::tx_builder::{AddUtxoError, AllowShrinkingError}; +use bdk::wallet::{NewError, NewOrLoadError}; +use std::convert::Infallible; + +#[derive(Debug, thiserror::Error)] +pub enum Alpha3Error { + Generic, +} + +impl fmt::Display for Alpha3Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Error in FFI") + } +} + +impl From for Alpha3Error { + fn from(_: DescriptorError) -> Self { + Alpha3Error::Generic + } +} + +impl From for Alpha3Error { + fn from(_: AllowShrinkingError) -> Self { + Alpha3Error::Generic + } +} + +impl From for Alpha3Error { + fn from(_: BuildFeeBumpError) -> Self { + Alpha3Error::Generic + } +} + +impl From> for Alpha3Error { + fn from(_: CreateTxError) -> Self { + Alpha3Error::Generic + } +} + +impl From for Alpha3Error { + fn from(_: AddUtxoError) -> Self { + Alpha3Error::Generic + } +} + +impl From for Alpha3Error { + fn from(_: bdk::bitcoin::bip32::Error) -> Self { + Alpha3Error::Generic + } +} + +// impl From> for TempFfiError { +// fn from(_: FileError<'_>) -> Self { +// TempFfiError::FfiError +// } +// } + +impl From> for Alpha3Error { + fn from(_: NewError) -> Self { + Alpha3Error::Generic + } +} + +// impl From> for Alpha3Error { +// fn from(_: NewOrLoadError) -> Self { +// Alpha3Error::Alpha3Error +// } +// } + +impl From> for Alpha3Error { + fn from(_: CreateTxError) -> Self { + Alpha3Error::Generic + } +} + #[derive(Debug)] pub enum CalculateFeeError { MissingTxOut { out_points: Vec }, diff --git a/bdk-ffi/src/esplora.rs b/bdk-ffi/src/esplora.rs index 0fc3a17..d0be950 100644 --- a/bdk-ffi/src/esplora.rs +++ b/bdk-ffi/src/esplora.rs @@ -1,8 +1,8 @@ use crate::wallet::{Update, Wallet}; +use crate::error::Alpha3Error; use bdk::bitcoin::Transaction as BdkTransaction; use bdk::wallet::Update as BdkUpdate; -use bdk::Error as BdkError; use bdk_esplora::esplora_client::{BlockingClient, Builder}; use bdk_esplora::EsploraExt; @@ -24,7 +24,7 @@ impl EsploraClient { wallet: Arc, stop_gap: u64, parallel_requests: u64, - ) -> Result, BdkError> { + ) -> Result, Alpha3Error> { let wallet = wallet.get_wallet(); let previous_tip = wallet.latest_checkpoint(); @@ -32,10 +32,8 @@ impl EsploraClient { let (update_graph, last_active_indices) = self .0 - .scan_txs_with_keychains( + .full_scan( keychain_spks, - None, - None, stop_gap as usize, parallel_requests as usize, ) @@ -58,11 +56,11 @@ impl EsploraClient { // pub fn sync(); - pub fn broadcast(&self, transaction: &Transaction) -> Result<(), BdkError> { + pub fn broadcast(&self, transaction: &Transaction) -> Result<(), Alpha3Error> { let bdk_transaction: BdkTransaction = transaction.into(); self.0 .broadcast(&bdk_transaction) - .map_err(|e| BdkError::Generic(e.to_string())) + .map_err(|e| Alpha3Error::Generic) } // pub fn estimate_fee(); diff --git a/bdk-ffi/src/keys.rs b/bdk-ffi/src/keys.rs index 45e1935..1609090 100644 --- a/bdk-ffi/src/keys.rs +++ b/bdk-ffi/src/keys.rs @@ -1,4 +1,5 @@ use crate::Network; +use crate::error::Alpha3Error; use bdk::bitcoin::bip32::DerivationPath as BdkDerivationPath; use bdk::bitcoin::key::Secp256k1; @@ -12,7 +13,6 @@ use bdk::keys::{ }; use bdk::miniscript::descriptor::{DescriptorXKey, Wildcard}; use bdk::miniscript::BareCtx; -use bdk::Error as BdkError; use std::ops::Deref; use std::str::FromStr; @@ -35,16 +35,16 @@ impl Mnemonic { Mnemonic { inner: mnemonic } } - pub(crate) fn from_string(mnemonic: String) -> Result { + pub(crate) fn from_string(mnemonic: String) -> Result { BdkMnemonic::from_str(&mnemonic) .map(|m| Mnemonic { inner: m }) - .map_err(|e| BdkError::Generic(e.to_string())) + .map_err(|e| Alpha3Error::Generic) } - pub(crate) fn from_entropy(entropy: Vec) -> Result { + pub(crate) fn from_entropy(entropy: Vec) -> Result { BdkMnemonic::from_entropy(entropy.as_slice()) .map(|m| Mnemonic { inner: m }) - .map_err(|e| BdkError::Generic(e.to_string())) + .map_err(|e| Alpha3Error::Generic) } pub(crate) fn as_string(&self) -> String { @@ -57,12 +57,12 @@ pub(crate) struct DerivationPath { } impl DerivationPath { - pub(crate) fn new(path: String) -> Result { + pub(crate) fn new(path: String) -> Result { BdkDerivationPath::from_str(&path) .map(|x| DerivationPath { inner_mutex: Mutex::new(x), }) - .map_err(|e| BdkError::Generic(e.to_string())) + .map_err(|e| Alpha3Error::Generic) } } @@ -86,22 +86,20 @@ impl DescriptorSecretKey { } } - pub(crate) fn from_string(private_key: String) -> Result { + pub(crate) fn from_string(private_key: String) -> Result { let descriptor_secret_key = BdkDescriptorSecretKey::from_str(private_key.as_str()) - .map_err(|e| BdkError::Generic(e.to_string()))?; + .map_err(|e| Alpha3Error::Generic)?; Ok(Self { inner: descriptor_secret_key, }) } - pub(crate) fn derive(&self, path: &DerivationPath) -> Result, BdkError> { + pub(crate) fn derive(&self, path: &DerivationPath) -> Result, Alpha3Error> { let secp = Secp256k1::new(); let descriptor_secret_key = &self.inner; let path = path.inner_mutex.lock().unwrap().deref().clone(); match descriptor_secret_key { - BdkDescriptorSecretKey::Single(_) => Err(BdkError::Generic( - "Cannot derive from a single key".to_string(), - )), + BdkDescriptorSecretKey::Single(_) => Err(Alpha3Error::Generic), BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { let derived_xprv = descriptor_x_key.xkey.derive_priv(&secp, &path)?; let key_source = match descriptor_x_key.origin.clone() { @@ -118,19 +116,15 @@ impl DescriptorSecretKey { inner: derived_descriptor_secret_key, })) } - BdkDescriptorSecretKey::MultiXPrv(_) => Err(BdkError::Generic( - "Cannot derive from a multi key".to_string(), - )), + BdkDescriptorSecretKey::MultiXPrv(_) => Err(Alpha3Error::Generic), } } - pub(crate) fn extend(&self, path: &DerivationPath) -> Result, BdkError> { + pub(crate) fn extend(&self, path: &DerivationPath) -> Result, Alpha3Error> { let descriptor_secret_key = &self.inner; let path = path.inner_mutex.lock().unwrap().deref().clone(); match descriptor_secret_key { - BdkDescriptorSecretKey::Single(_) => Err(BdkError::Generic( - "Cannot extend from a single key".to_string(), - )), + BdkDescriptorSecretKey::Single(_) => Err(Alpha3Error::Generic), BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { let extended_path = descriptor_x_key.derivation_path.extend(path); let extended_descriptor_secret_key = BdkDescriptorSecretKey::XPrv(DescriptorXKey { @@ -143,9 +137,7 @@ impl DescriptorSecretKey { inner: extended_descriptor_secret_key, })) } - BdkDescriptorSecretKey::MultiXPrv(_) => Err(BdkError::Generic( - "Cannot derive from a multi key".to_string(), - )), + BdkDescriptorSecretKey::MultiXPrv(_) => Err(Alpha3Error::Generic), } } @@ -185,23 +177,21 @@ pub struct DescriptorPublicKey { } impl DescriptorPublicKey { - pub(crate) fn from_string(public_key: String) -> Result { + pub(crate) fn from_string(public_key: String) -> Result { let descriptor_public_key = BdkDescriptorPublicKey::from_str(public_key.as_str()) - .map_err(|e| BdkError::Generic(e.to_string()))?; + .map_err(|e| Alpha3Error::Generic)?; Ok(Self { inner: descriptor_public_key, }) } - pub(crate) fn derive(&self, path: &DerivationPath) -> Result, BdkError> { + pub(crate) fn derive(&self, path: &DerivationPath) -> Result, Alpha3Error> { let secp = Secp256k1::new(); let descriptor_public_key = &self.inner; let path = path.inner_mutex.lock().unwrap().deref().clone(); match descriptor_public_key { - BdkDescriptorPublicKey::Single(_) => Err(BdkError::Generic( - "Cannot derive from a single key".to_string(), - )), + BdkDescriptorPublicKey::Single(_) => Err(Alpha3Error::Generic), BdkDescriptorPublicKey::XPub(descriptor_x_key) => { let derived_xpub = descriptor_x_key.xkey.derive_pub(&secp, &path)?; let key_source = match descriptor_x_key.origin.clone() { @@ -218,19 +208,15 @@ impl DescriptorPublicKey { inner: derived_descriptor_public_key, })) } - BdkDescriptorPublicKey::MultiXPub(_) => Err(BdkError::Generic( - "Cannot derive from a multi xpub".to_string(), - )), + BdkDescriptorPublicKey::MultiXPub(_) => Err(Alpha3Error::Generic), } } - pub(crate) fn extend(&self, path: &DerivationPath) -> Result, BdkError> { + pub(crate) fn extend(&self, path: &DerivationPath) -> Result, Alpha3Error> { let descriptor_public_key = &self.inner; let path = path.inner_mutex.lock().unwrap().deref().clone(); match descriptor_public_key { - BdkDescriptorPublicKey::Single(_) => Err(BdkError::Generic( - "Cannot extend from a single key".to_string(), - )), + BdkDescriptorPublicKey::Single(_) => Err(Alpha3Error::Generic), BdkDescriptorPublicKey::XPub(descriptor_x_key) => { let extended_path = descriptor_x_key.derivation_path.extend(path); let extended_descriptor_public_key = BdkDescriptorPublicKey::XPub(DescriptorXKey { @@ -243,9 +229,7 @@ impl DescriptorPublicKey { inner: extended_descriptor_public_key, })) } - BdkDescriptorPublicKey::MultiXPub(_) => Err(BdkError::Generic( - "Cannot derive from a multi xpub".to_string(), - )), + BdkDescriptorPublicKey::MultiXPub(_) => Err(Alpha3Error::Generic), } } @@ -257,10 +241,10 @@ impl DescriptorPublicKey { #[cfg(test)] mod test { use crate::keys::{DerivationPath, DescriptorPublicKey, DescriptorSecretKey, Mnemonic}; - use crate::BdkError; // use bdk::bitcoin::hashes::hex::ToHex; use bdk::bitcoin::Network; use std::sync::Arc; + use crate::error::Alpha3Error; fn get_inner() -> DescriptorSecretKey { let mnemonic = Mnemonic::from_string("chaos fabric time speed sponsor all flat solution wisdom trophy crack object robot pave observe combine where aware bench orient secret primary cable detect".to_string()).unwrap(); @@ -270,7 +254,7 @@ mod test { fn derive_dsk( key: &DescriptorSecretKey, path: &str, - ) -> Result, BdkError> { + ) -> Result, Alpha3Error> { let path = DerivationPath::new(path.to_string()).unwrap(); key.derive(&path) } @@ -278,7 +262,7 @@ mod test { fn extend_dsk( key: &DescriptorSecretKey, path: &str, - ) -> Result, BdkError> { + ) -> Result, Alpha3Error> { let path = DerivationPath::new(path.to_string()).unwrap(); key.extend(&path) } @@ -286,7 +270,7 @@ mod test { fn derive_dpk( key: &DescriptorPublicKey, path: &str, - ) -> Result, BdkError> { + ) -> Result, Alpha3Error> { let path = DerivationPath::new(path.to_string()).unwrap(); key.derive(&path) } @@ -294,7 +278,7 @@ mod test { fn extend_dpk( key: &DescriptorPublicKey, path: &str, - ) -> Result, BdkError> { + ) -> Result, Alpha3Error> { let path = DerivationPath::new(path.to_string()).unwrap(); key.extend(&path) } diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index ec86e0f..c68a398 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -22,9 +22,10 @@ use crate::keys::DescriptorSecretKey; use crate::keys::Mnemonic; use crate::types::AddressIndex; use crate::types::AddressInfo; +use crate::error::Alpha3Error; use crate::types::Balance; use crate::types::FeeRate; -use crate::types::LocalUtxo; +use crate::types::LocalOutput; use crate::types::ScriptAmount; use crate::wallet::BumpFeeTxBuilder; use crate::wallet::SentAndReceivedValues; @@ -34,7 +35,6 @@ use crate::wallet::Wallet; use bdk::keys::bip39::WordCount; use bdk::wallet::tx_builder::ChangeSpendPolicy; -use bdk::Error as BdkError; use bdk::KeychainKind; uniffi::include_scaffolding!("bdk"); diff --git a/bdk-ffi/src/types.rs b/bdk-ffi/src/types.rs index bfef101..f06affb 100644 --- a/bdk-ffi/src/types.rs +++ b/bdk-ffi/src/types.rs @@ -5,7 +5,7 @@ use bdk::wallet::AddressInfo as BdkAddressInfo; use bdk::wallet::Balance as BdkBalance; use bdk::KeychainKind; -use bdk::LocalUtxo as BdkLocalUtxo; +use bdk::LocalOutput as BdkLocalOutput; use bdk::FeeRate as BdkFeeRate; @@ -113,16 +113,16 @@ impl From for Balance { } } -pub struct LocalUtxo { +pub struct LocalOutput { pub outpoint: OutPoint, pub txout: TxOut, pub keychain: KeychainKind, pub is_spent: bool, } -impl From for LocalUtxo { - fn from(local_utxo: BdkLocalUtxo) -> Self { - LocalUtxo { +impl From for LocalOutput { + fn from(local_utxo: BdkLocalOutput) -> Self { + LocalOutput { outpoint: OutPoint { txid: local_utxo.outpoint.txid.to_string(), vout: local_utxo.outpoint.vout, diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index 0207417..a3bd2c9 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -1,6 +1,6 @@ use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction}; use crate::descriptor::Descriptor; -use crate::error::CalculateFeeError; +use crate::error::{Alpha3Error, CalculateFeeError}; use crate::types::ScriptAmount; use crate::types::{Balance, FeeRate}; use crate::Script; @@ -11,7 +11,7 @@ use bdk::bitcoin::psbt::PartiallySignedTransaction as BdkPartiallySignedTransact use bdk::bitcoin::{OutPoint as BdkOutPoint, Sequence, Txid}; use bdk::wallet::tx_builder::ChangeSpendPolicy; use bdk::wallet::Update as BdkUpdate; -use bdk::{Error as BdkError, FeeRate as BdkFeeRate}; +use bdk::{FeeRate as BdkFeeRate}; use bdk::{SignOptions, Wallet as BdkWallet}; use std::collections::HashSet; @@ -29,7 +29,7 @@ impl Wallet { descriptor: Arc, change_descriptor: Option>, network: Network, - ) -> Result { + ) -> Result { let descriptor = descriptor.as_string_private(); let change_descriptor = change_descriptor.map(|d| d.as_string_private()); @@ -65,10 +65,10 @@ impl Wallet { Balance::from(bdk_balance) } - pub fn apply_update(&self, update: Arc) -> Result<(), BdkError> { + pub fn apply_update(&self, update: Arc) -> Result<(), Alpha3Error> { self.get_wallet() .apply_update(update.0.clone()) - .map_err(|e| BdkError::Generic(e.to_string())) + .map_err(|e| Alpha3Error::Generic) } pub fn is_mine(&self, script: &Script) -> bool { @@ -81,11 +81,11 @@ impl Wallet { &self, psbt: Arc, // sign_options: Option, - ) -> Result { + ) -> Result { let mut psbt = psbt.inner.lock().unwrap(); self.get_wallet() .sign(&mut psbt, SignOptions::default()) - .map_err(|e| BdkError::Generic(e.to_string())) + .map_err(|e| Alpha3Error::Generic) } pub fn sent_and_received(&self, tx: &Transaction) -> SentAndReceivedValues { @@ -465,7 +465,7 @@ impl TxBuilder { pub(crate) fn finish( &self, wallet: &Wallet, - ) -> Result, BdkError> { + ) -> Result, Alpha3Error> { // TODO: I had to change the wallet here to be mutable. Why is that now required with the 1.0 API? let mut wallet = wallet.get_wallet(); let mut tx_builder = wallet.build_tx(); @@ -560,9 +560,9 @@ impl BumpFeeTxBuilder { pub(crate) fn finish( &self, wallet: &Wallet, - ) -> Result, BdkError> { + ) -> Result, Alpha3Error> { let txid = - Txid::from_str(self.txid.as_str()).map_err(|e| BdkError::Generic(e.to_string()))?; + Txid::from_str(self.txid.as_str()).map_err(|e| Alpha3Error::Generic)?; let mut wallet = wallet.get_wallet(); let mut tx_builder = wallet.build_fee_bump(txid)?; tx_builder.fee_rate(BdkFeeRate::from_sat_per_vb(self.fee_rate)); @@ -581,7 +581,7 @@ impl BumpFeeTxBuilder { } let psbt: BdkPartiallySignedTransaction = tx_builder .finish() - .map_err(|e| BdkError::Generic(e.to_string()))?; + .map_err(|e| Alpha3Error::Generic)?; Ok(Arc::new(psbt.into())) }