From 0ab7a6cfc1bfcdc74142c8f88998f260a4967c43 Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Sun, 1 Sep 2024 00:24:00 -0400 Subject: [PATCH 01/13] Android SDK & NDK added --- .github/workflows/precompile_binaries.yml | 6 ++++++ android/build.gradle | 1 - android/src/main/AndroidManifest.xml | 2 +- example/macos/Podfile.lock | 4 ++-- macos/bdk_flutter.podspec | 6 ++++-- rust/cargokit.yaml | 3 +-- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.github/workflows/precompile_binaries.yml b/.github/workflows/precompile_binaries.yml index 0f6bca95..6dfe7c50 100644 --- a/.github/workflows/precompile_binaries.yml +++ b/.github/workflows/precompile_binaries.yml @@ -32,6 +32,12 @@ jobs: - uses: subosito/flutter-action@v2 with: channel: 'stable' + - name: Set up Android SDK + if: (matrix.os == 'ubuntu-20.04') + uses: android-actions/setup-android@v2 + - name: Install Specific NDK + if: (matrix.os == 'ubuntu-20.04') + run: sdkmanager --install "ndk;24.0.8215888" - name: Precompile (with iOS) if: (matrix.os == 'macOS-latest') run: dart run build_tool precompile-binaries -v --manifest-dir=../../rust --repository=LtbLightning/bdk-flutter diff --git a/android/build.gradle b/android/build.gradle index af4681e5..0a24818b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -26,7 +26,6 @@ apply plugin: 'kotlin-android' android { compileSdkVersion 31 - namespace "io.bdk.f.bdk_flutter" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 0bfa73ba..e59e280d 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1 +1 @@ - + diff --git a/example/macos/Podfile.lock b/example/macos/Podfile.lock index 55a89e94..2d921409 100644 --- a/example/macos/Podfile.lock +++ b/example/macos/Podfile.lock @@ -14,9 +14,9 @@ EXTERNAL SOURCES: :path: Flutter/ephemeral SPEC CHECKSUMS: - bdk_flutter: f31096ce6d28094dbbb43d2a3fb130f7c54683df + bdk_flutter: d0437c6116753242241fed48270587542a636d40 FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24 PODFILE CHECKSUM: 6acf97521436d16fc31cd5e1a02000905acdb3ae -COCOAPODS: 1.14.3 +COCOAPODS: 1.15.2 diff --git a/macos/bdk_flutter.podspec b/macos/bdk_flutter.podspec index 5d5b3899..c1ff53ea 100644 --- a/macos/bdk_flutter.podspec +++ b/macos/bdk_flutter.podspec @@ -27,8 +27,10 @@ Pod::Spec.new do |s| } s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', - # Flutter.framework does not contain a i386 slice. - 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386', 'OTHER_LDFLAGS' => '-force_load ${BUILT_PRODUCTS_DIR}/libbdk_flutter.a', + 'DEAD_CODE_STRIPPING' => 'YES', + 'STRIP_INSTALLED_PRODUCT[config=Release][sdk=*][arch=*]' => "YES", + 'STRIP_STYLE[config=Release][sdk=*][arch=*]' => "non-global", + 'DEPLOYMENT_POSTPROCESSING[config=Release][sdk=*][arch=*]' => "YES", } end diff --git a/rust/cargokit.yaml b/rust/cargokit.yaml index 6056237a..4657395e 100644 --- a/rust/cargokit.yaml +++ b/rust/cargokit.yaml @@ -3,5 +3,4 @@ cargo: toolchain: stable precompiled_binaries: url_prefix: https://github.com/LtbLightning/bdk-flutter/releases/download/precompiled_ - public_key: 0e43d5e8452d00db7f3000c18fb1ba796babfcb5dc6306bb0629eff24f8be85b - + public_key: 0e43d5e8452d00db7f3000c18fb1ba796babfcb5dc6306bb0629eff24f8be85b \ No newline at end of file From 4b7abb8e394eb38e93a880b5b854e5c556270187 Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Mon, 2 Sep 2024 09:05:00 -0400 Subject: [PATCH 02/13] flutter pub publish issue resolved --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index aab02e5f..04a0e4c8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,7 +10,7 @@ environment: dependencies: flutter: sdk: flutter - flutter_rust_bridge: ">2.0.0-dev.41 <= 2.0.0" + flutter_rust_bridge: ">=2.0.0 < 2.1.0" ffi: ^2.0.1 freezed_annotation: ^2.2.0 mockito: ^5.4.0 From d8ce141856df43b33a08e78234a6c90796393901 Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Mon, 2 Sep 2024 13:54:00 -0400 Subject: [PATCH 03/13] Rebase From 063feffcee76348ae037d4b2948d6f04b0a5590b Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Mon, 2 Sep 2024 17:08:00 -0400 Subject: [PATCH 04/13] bdk::keys::bip39::Error implemented for BdkError --- rust/src/api/error.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rust/src/api/error.rs b/rust/src/api/error.rs index df88e2ed..9a986ab0 100644 --- a/rust/src/api/error.rs +++ b/rust/src/api/error.rs @@ -361,3 +361,8 @@ impl From for BdkError { BdkError::PsbtParse(value.to_string()) } } +impl From for BdkError { + fn from(value: bdk::keys::bip39::Error) -> Self { + BdkError::Bip39(value.to_string()) + } +} From a13d0b543806b59736884853301f2f1fdf523df2 Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Thu, 5 Sep 2024 17:50:00 -0400 Subject: [PATCH 05/13] handle_mutex exposed to handle lock errors --- rust/src/api/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rust/src/api/mod.rs b/rust/src/api/mod.rs index 73e4799d..03f77c7d 100644 --- a/rust/src/api/mod.rs +++ b/rust/src/api/mod.rs @@ -1,3 +1,7 @@ +use std::{fmt::Debug, sync::Mutex}; + +use error::BdkError; + pub mod blockchain; pub mod descriptor; pub mod error; @@ -5,3 +9,17 @@ pub mod key; pub mod psbt; pub mod types; pub mod wallet; + +pub(crate) fn handle_mutex(lock: &Mutex, operation: F) -> Result +where + T: Debug, + F: FnOnce(&mut T) -> R, +{ + match lock.lock() { + Ok(mut mutex_guard) => Ok(operation(&mut *mutex_guard)), + Err(poisoned) => { + drop(poisoned.into_inner()); + Err(BdkError::Generic("Poison Error!".to_string())) + } + } +} From dd1343f49fabc56242c60cfffae2d082109961e5 Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Fri, 6 Sep 2024 03:03:00 -0400 Subject: [PATCH 06/13] removed all unhandled unwraps --- rust/src/api/key.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/rust/src/api/key.rs b/rust/src/api/key.rs index 9d106e0a..ef55b4c2 100644 --- a/rust/src/api/key.rs +++ b/rust/src/api/key.rs @@ -25,7 +25,12 @@ impl BdkMnemonic { /// Generates Mnemonic with a random entropy pub fn new(word_count: WordCount) -> Result { let generated_key: keys::GeneratedKey<_, BareCtx> = - keys::bip39::Mnemonic::generate((word_count.into(), Language::English)).unwrap(); + (match keys::bip39::Mnemonic::generate((word_count.into(), Language::English)) { + Ok(value) => Ok(value), + Err(Some(err)) => Err(BdkError::Bip39(err.to_string())), + Err(None) => Err(BdkError::Generic("".to_string())), // If + })?; + keys::bip39::Mnemonic::parse_in(Language::English, generated_key.to_string()) .map(|e| e.into()) .map_err(|e| BdkError::Bip39(e.to_string())) @@ -93,10 +98,19 @@ impl BdkDescriptorSecretKey { password: Option, ) -> Result { let mnemonic = (*mnemonic.ptr).clone(); - let xkey: keys::ExtendedKey = (mnemonic, password).into_extended_key().unwrap(); + let xkey: keys::ExtendedKey = (mnemonic, password) + .into_extended_key() + .map_err(|e| BdkError::Key(e.to_string()))?; + let xpriv = if let Some(e) = xkey.into_xprv(network.into()) { + Ok(e) + } else { + Err(BdkError::Generic( + "private data not found in the key!".to_string(), + )) + }; let descriptor_secret_key = keys::DescriptorSecretKey::XPrv(DescriptorXKey { origin: None, - xkey: xkey.into_xprv(network.into()).unwrap(), + xkey: xpriv?, derivation_path: bitcoin::bip32::DerivationPath::master(), wildcard: Wildcard::Unhardened, }); @@ -163,7 +177,10 @@ impl BdkDescriptorSecretKey { #[frb(sync)] pub fn as_public(ptr: BdkDescriptorSecretKey) -> Result { let secp = Secp256k1::new(); - let descriptor_public_key = ptr.ptr.to_public(&secp).unwrap(); + let descriptor_public_key = ptr + .ptr + .to_public(&secp) + .map_err(|e| BdkError::Generic(e.to_string()))?; Ok(descriptor_public_key.into()) } #[frb(sync)] @@ -184,7 +201,8 @@ impl BdkDescriptorSecretKey { } pub fn from_string(secret_key: String) -> Result { - let key = keys::DescriptorSecretKey::from_str(&*secret_key).unwrap(); + let key = keys::DescriptorSecretKey::from_str(&*secret_key) + .map_err(|e| BdkError::Generic(e.to_string()))?; Ok(key.into()) } #[frb(sync)] From 48ba8b31f754b7da40d799fbee239e60af859930 Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Fri, 6 Sep 2024 18:19:00 -0400 Subject: [PATCH 07/13] removed all unhandled unwraps --- rust/src/api/blockchain.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/src/api/blockchain.rs b/rust/src/api/blockchain.rs index c80362e1..ea297051 100644 --- a/rust/src/api/blockchain.rs +++ b/rust/src/api/blockchain.rs @@ -33,7 +33,7 @@ impl BdkBlockchain { socks5: config.socks5, timeout: config.timeout, url: config.url, - stop_gap: usize::try_from(config.stop_gap).unwrap(), + stop_gap: config.stop_gap as usize, validate_domain: config.validate_domain, }) } @@ -42,7 +42,7 @@ impl BdkBlockchain { base_url: config.base_url, proxy: config.proxy, concurrency: config.concurrency, - stop_gap: usize::try_from(config.stop_gap).unwrap(), + stop_gap: config.stop_gap as usize, timeout: config.timeout, }) } From 26271322f7be9323d3b77dfe09578f8c76be2dec Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Sat, 7 Sep 2024 15:07:00 -0400 Subject: [PATCH 08/13] implemeted handle_mutex --- rust/src/api/psbt.rs | 54 ++++--- rust/src/api/wallet.rs | 358 +++++++++++++++++++---------------------- 2 files changed, 199 insertions(+), 213 deletions(-) diff --git a/rust/src/api/psbt.rs b/rust/src/api/psbt.rs index 30c1f6c6..780fae47 100644 --- a/rust/src/api/psbt.rs +++ b/rust/src/api/psbt.rs @@ -8,6 +8,8 @@ use std::str::FromStr; use flutter_rust_bridge::frb; +use super::handle_mutex; + #[derive(Debug)] pub struct BdkPsbt { pub ptr: RustOpaque>, @@ -28,43 +30,51 @@ impl BdkPsbt { } #[frb(sync)] - pub fn as_string(&self) -> String { - let psbt = self.ptr.lock().unwrap().clone(); - psbt.to_string() + pub fn as_string(&self) -> Result { + handle_mutex(&self.ptr, |psbt| psbt.to_string()) } ///Computes the `Txid`. /// Hashes the transaction excluding the segwit data (i. e. the marker, flag bytes, and the witness fields themselves). /// For non-segwit transactions which do not have any segwit data, this will be equal to transaction.wtxid(). #[frb(sync)] - pub fn txid(&self) -> String { - let tx = self.ptr.lock().unwrap().clone().extract_tx(); - let txid = tx.txid(); - txid.to_string() + pub fn txid(&self) -> Result { + handle_mutex(&self.ptr, |psbt| { + psbt.to_owned().extract_tx().txid().to_string() + }) } /// Return the transaction. #[frb(sync)] pub fn extract_tx(ptr: BdkPsbt) -> Result { - let tx = ptr.ptr.lock().unwrap().clone().extract_tx(); - tx.try_into() + handle_mutex(&ptr.ptr, |psbt| { + let tx = psbt.to_owned().extract_tx(); + tx.try_into() + })? } /// Combines this PartiallySignedTransaction with other PSBT as described by BIP 174. /// /// In accordance with BIP 174 this function is commutative i.e., `A.combine(B) == B.combine(A)` pub fn combine(ptr: BdkPsbt, other: BdkPsbt) -> Result { - let other_psbt = other.ptr.lock().unwrap().clone(); - let mut original_psbt = ptr.ptr.lock().unwrap().clone(); + let other_psbt = other + .ptr + .lock() + .map_err(|_| BdkError::Generic("Poison Error!".to_string()))? + .clone(); + let mut original_psbt = ptr + .ptr + .lock() + .map_err(|_| BdkError::Generic("Poison Error!".to_string()))?; original_psbt.combine(other_psbt)?; - Ok(original_psbt.into()) + Ok(original_psbt.to_owned().into()) } /// The total transaction fee amount, sum of input amounts minus sum of output amounts, in Sats. /// If the PSBT is missing a TxOut for an input returns None. #[frb(sync)] - pub fn fee_amount(&self) -> Option { - self.ptr.lock().unwrap().fee_amount() + pub fn fee_amount(&self) -> Result, BdkError> { + handle_mutex(&self.ptr, |psbt| psbt.fee_amount()) } /// The transaction's fee rate. This value will only be accurate if calculated AFTER the @@ -72,20 +82,20 @@ impl BdkPsbt { /// transaction. /// If the PSBT is missing a TxOut for an input returns None. #[frb(sync)] - pub fn fee_rate(&self) -> Option { - self.ptr.lock().unwrap().fee_rate().map(|e| e.into()) + pub fn fee_rate(&self) -> Result, BdkError> { + handle_mutex(&self.ptr, |psbt| psbt.fee_rate().map(|e| e.into())) } ///Serialize as raw binary data #[frb(sync)] - pub fn serialize(&self) -> Vec { - let psbt = self.ptr.lock().unwrap().clone(); - psbt.serialize() + pub fn serialize(&self) -> Result, BdkError> { + handle_mutex(&self.ptr, |psbt| psbt.serialize()) } /// Serialize the PSBT data structure as a String of JSON. #[frb(sync)] - pub fn json_serialize(&self) -> String { - let psbt = self.ptr.lock().unwrap(); - serde_json::to_string(psbt.deref()).unwrap() + pub fn json_serialize(&self) -> Result { + handle_mutex(&self.ptr, |psbt| { + serde_json::to_string(psbt.deref()).map_err(|e| BdkError::Generic(e.to_string())) + })? } } diff --git a/rust/src/api/wallet.rs b/rust/src/api/wallet.rs index fccf5eaa..ec593ada 100644 --- a/rust/src/api/wallet.rs +++ b/rust/src/api/wallet.rs @@ -1,8 +1,21 @@ use crate::api::descriptor::BdkDescriptor; use crate::api::types::{ - AddressIndex, Balance, BdkAddress, BdkScriptBuf, ChangeSpendPolicy, DatabaseConfig, Input, - KeychainKind, LocalUtxo, Network, OutPoint, PsbtSigHashType, RbfValue, ScriptAmount, - SignOptions, TransactionDetails, + AddressIndex, + Balance, + BdkAddress, + BdkScriptBuf, + ChangeSpendPolicy, + DatabaseConfig, + Input, + KeychainKind, + LocalUtxo, + Network, + OutPoint, + PsbtSigHashType, + RbfValue, + ScriptAmount, + SignOptions, + TransactionDetails, }; use std::ops::Deref; use std::str::FromStr; @@ -12,13 +25,14 @@ use crate::api::error::BdkError; use crate::api::psbt::BdkPsbt; use crate::frb_generated::RustOpaque; use bdk::bitcoin::script::PushBytesBuf; -use bdk::bitcoin::{Sequence, Txid}; +use bdk::bitcoin::{ Sequence, Txid }; pub use bdk::blockchain::GetTx; use bdk::database::ConfigurableDatabase; -use std::sync::MutexGuard; use flutter_rust_bridge::frb; +use super::handle_mutex; + #[derive(Debug)] pub struct BdkWallet { pub ptr: RustOpaque>>, @@ -28,7 +42,7 @@ impl BdkWallet { descriptor: BdkDescriptor, change_descriptor: Option, network: Network, - database_config: DatabaseConfig, + database_config: DatabaseConfig ) -> Result { let database = bdk::database::AnyDatabase::from_config(&database_config.into())?; let descriptor: String = descriptor.to_string_private(); @@ -38,27 +52,25 @@ impl BdkWallet { &descriptor, change_descriptor.as_ref(), network.into(), - database, + database )?; Ok(BdkWallet { ptr: RustOpaque::new(std::sync::Mutex::new(wallet)), }) } - pub(crate) fn get_wallet(&self) -> MutexGuard> { - self.ptr.lock().expect("") - } /// Get the Bitcoin network the wallet is using. - #[frb(sync)] - pub fn network(&self) -> Network { - self.get_wallet().network().into() + #[frb(sync)] + pub fn network(&self) -> Result { + handle_mutex(&self.ptr, |w| w.network().into()) } - /// Return whether or not a script is part of this wallet (either internal or external). #[frb(sync)] pub fn is_mine(&self, script: BdkScriptBuf) -> Result { - self.get_wallet() - .is_mine(>::into(script).as_script()) - .map_err(|e| e.into()) + handle_mutex(&self.ptr, |w| { + w.is_mine( + >::into(script).as_script() + ).map_err(|e| e.into()) + })? } /// Return a derived address using the external descriptor, see AddressIndex for available address index selection /// strategies. If none of the keys in the descriptor are derivable (i.e. the descriptor does not end with a * character) @@ -66,12 +78,13 @@ impl BdkWallet { #[frb(sync)] pub fn get_address( ptr: BdkWallet, - address_index: AddressIndex, + address_index: AddressIndex ) -> Result<(BdkAddress, u32), BdkError> { - ptr.get_wallet() - .get_address(address_index.into()) - .map(|e| (e.address.into(), e.index)) - .map_err(|e| e.into()) + handle_mutex(&ptr.ptr, |w| { + w.get_address(address_index.into()) + .map(|e| (e.address.into(), e.index)) + .map_err(|e| e.into()) + })? } /// Return a derived address using the internal (change) descriptor. @@ -84,46 +97,51 @@ impl BdkWallet { #[frb(sync)] pub fn get_internal_address( ptr: BdkWallet, - address_index: AddressIndex, + address_index: AddressIndex ) -> Result<(BdkAddress, u32), BdkError> { - ptr.get_wallet() - .get_internal_address(address_index.into()) - .map(|e| (e.address.into(), e.index)) - .map_err(|e| e.into()) + handle_mutex(&ptr.ptr, |w| { + w.get_internal_address(address_index.into()) + .map(|e| (e.address.into(), e.index)) + .map_err(|e| e.into()) + })? } /// Return the balance, meaning the sum of this wallet’s unspent outputs’ values. Note that this method only operates /// on the internal database, which first needs to be Wallet.sync manually. #[frb(sync)] pub fn get_balance(&self) -> Result { - self.get_wallet() - .get_balance() - .map(|b| b.into()) - .map_err(|e| e.into()) + handle_mutex(&self.ptr, |w| { + w.get_balance() + .map(|b| b.into()) + .map_err(|e| e.into()) + })? } /// Return the list of transactions made and received by the wallet. Note that this method only operate on the internal database, which first needs to be [Wallet.sync] manually. #[frb(sync)] pub fn list_transactions( &self, - include_raw: bool, + include_raw: bool ) -> Result, BdkError> { - let mut transaction_details = vec![]; - for e in self - .get_wallet() - .list_transactions(include_raw)? - .into_iter() - { - transaction_details.push(e.try_into()?); - } - Ok(transaction_details) + handle_mutex(&self.ptr, |wallet| { + let mut transaction_details = vec![]; + + // List transactions and convert them using try_into + for e in wallet.list_transactions(include_raw)?.into_iter() { + transaction_details.push(e.try_into()?); + } + + Ok(transaction_details) + })? } /// Return the list of unspent outputs of this wallet. Note that this method only operates on the internal database, /// which first needs to be Wallet.sync manually. #[frb(sync)] pub fn list_unspent(&self) -> Result, BdkError> { - let unspent: Vec = self.get_wallet().list_unspent()?; - Ok(unspent.into_iter().map(LocalUtxo::from).collect()) + handle_mutex(&self.ptr, |w| { + let unspent: Vec = w.list_unspent()?; + Ok(unspent.into_iter().map(LocalUtxo::from).collect()) + })? } /// Sign a transaction with all the wallet's signers. This function returns an encapsulated bool that @@ -136,91 +154,48 @@ impl BdkWallet { pub fn sign( ptr: BdkWallet, psbt: BdkPsbt, - sign_options: Option, + sign_options: Option ) -> Result { - let mut psbt = psbt.ptr.lock().unwrap(); - ptr.get_wallet() - .sign( - &mut psbt, - sign_options.map(SignOptions::into).unwrap_or_default(), + let mut psbt = psbt.ptr.lock().map_err(|_| BdkError::Generic("Poison Error!".to_string()))?; + handle_mutex(&ptr.ptr, |w| { + w.sign(&mut psbt, sign_options.map(SignOptions::into).unwrap_or_default()).map_err(|e| + e.into() ) - .map_err(|e| e.into()) + })? } /// Sync the internal database with the blockchain. pub fn sync(ptr: BdkWallet, blockchain: &BdkBlockchain) -> Result<(), BdkError> { - let blockchain = blockchain.get_blockchain(); - ptr.get_wallet() - .sync(blockchain.deref(), bdk::SyncOptions::default()) - .map_err(|e| e.into()) + handle_mutex(&ptr.ptr, |w| { + w.sync(blockchain.ptr.deref(), bdk::SyncOptions::default()).map_err(|e| e.into()) + })? } - //TODO recreate verify_tx properly - // pub fn verify_tx(ptr: BdkWallet, tx: BdkTransaction) -> Result<(), BdkError> { - // let serialized_tx = tx.serialize()?; - // let tx: Transaction = (&tx).try_into()?; - // let locked_wallet = ptr.get_wallet(); - // // Loop through all the inputs - // for (index, input) in tx.input.iter().enumerate() { - // let input = input.clone(); - // let txid = input.previous_output.txid; - // let prev_tx = match locked_wallet.database().get_raw_tx(&txid){ - // Ok(prev_tx) => Ok(prev_tx), - // Err(e) => Err(BdkError::VerifyTransaction(format!("The transaction {:?} being spent is not available in the wallet database: {:?} ", txid,e))) - // }; - // if let Some(prev_tx) = prev_tx? { - // let spent_output = match prev_tx.output.get(input.previous_output.vout as usize) { - // Some(output) => Ok(output), - // None => Err(BdkError::VerifyTransaction(format!( - // "Failed to verify transaction: missing output {:?} in tx {:?}", - // input.previous_output.vout, txid - // ))), - // }; - // let spent_output = spent_output?; - // return match bitcoinconsensus::verify( - // &spent_output.clone().script_pubkey.to_bytes(), - // spent_output.value, - // &serialized_tx, - // None, - // index, - // ) { - // Ok(()) => Ok(()), - // Err(e) => Err(BdkError::VerifyTransaction(e.to_string())), - // }; - // } else { - // if tx.is_coin_base() { - // continue; - // } else { - // return Err(BdkError::VerifyTransaction(format!( - // "Failed to verify transaction: missing previous transaction {:?}", - // txid - // ))); - // } - // } - // } - // Ok(()) - // } + ///get the corresponding PSBT Input for a LocalUtxo pub fn get_psbt_input( &self, utxo: LocalUtxo, only_witness_utxo: bool, - sighash_type: Option, + sighash_type: Option ) -> anyhow::Result { - let input = self.get_wallet().get_psbt_input( - utxo.try_into()?, - sighash_type.map(|e| e.into()), - only_witness_utxo, - )?; - input.try_into() + handle_mutex(&self.ptr, |w| { + let input = w.get_psbt_input( + utxo.try_into()?, + sighash_type.map(|e| e.into()), + only_witness_utxo + )?; + input.try_into() + })? } ///Returns the descriptor used to create addresses for a particular keychain. #[frb(sync)] pub fn get_descriptor_for_keychain( ptr: BdkWallet, - keychain: KeychainKind, + keychain: KeychainKind ) -> anyhow::Result { - let wallet = ptr.get_wallet(); - let extended_descriptor = wallet.get_descriptor_for_keychain(keychain.into()); - BdkDescriptor::new(extended_descriptor.to_string(), wallet.network().into()) + handle_mutex(&ptr.ptr, |w| { + let extended_descriptor = w.get_descriptor_for_keychain(keychain.into()); + BdkDescriptor::new(extended_descriptor.to_string(), w.network().into()) + })? } } @@ -230,28 +205,28 @@ pub fn finish_bump_fee_tx_builder( allow_shrinking: Option, wallet: BdkWallet, enable_rbf: bool, - n_sequence: Option, + n_sequence: Option ) -> anyhow::Result<(BdkPsbt, TransactionDetails), BdkError> { - let txid = Txid::from_str(txid.as_str()).unwrap(); - let bdk_wallet = wallet.get_wallet(); - - let mut tx_builder = bdk_wallet.build_fee_bump(txid)?; - tx_builder.fee_rate(bdk::FeeRate::from_sat_per_vb(fee_rate)); - if let Some(allow_shrinking) = &allow_shrinking { - let address = allow_shrinking.ptr.clone(); - let script = address.script_pubkey(); - tx_builder.allow_shrinking(script).unwrap(); - } - if let Some(n_sequence) = n_sequence { - tx_builder.enable_rbf_with_sequence(Sequence(n_sequence)); - } - if enable_rbf { - tx_builder.enable_rbf(); - } - return match tx_builder.finish() { - Ok(e) => Ok((e.0.into(), TransactionDetails::try_from(e.1)?)), - Err(e) => Err(e.into()), - }; + let txid = Txid::from_str(txid.as_str()).map_err(|e| BdkError::PsbtParse(e.to_string()))?; + handle_mutex(&wallet.ptr, |w| { + let mut tx_builder = w.build_fee_bump(txid)?; + tx_builder.fee_rate(bdk::FeeRate::from_sat_per_vb(fee_rate)); + if let Some(allow_shrinking) = &allow_shrinking { + let address = allow_shrinking.ptr.clone(); + let script = address.script_pubkey(); + tx_builder.allow_shrinking(script)?; + } + if let Some(n_sequence) = n_sequence { + tx_builder.enable_rbf_with_sequence(Sequence(n_sequence)); + } + if enable_rbf { + tx_builder.enable_rbf(); + } + return match tx_builder.finish() { + Ok(e) => Ok((e.0.into(), TransactionDetails::try_from(e.1)?)), + Err(e) => Err(e.into()), + }; + })? } pub fn tx_builder_finish( @@ -267,70 +242,71 @@ pub fn tx_builder_finish( drain_wallet: bool, drain_to: Option, rbf: Option, - data: Vec, + data: Vec ) -> anyhow::Result<(BdkPsbt, TransactionDetails), BdkError> { - let binding = wallet.get_wallet(); - - let mut tx_builder = binding.build_tx(); + handle_mutex(&wallet.ptr, |w| { + let mut tx_builder = w.build_tx(); - for e in recipients { - tx_builder.add_recipient(e.script.into(), e.amount); - } - tx_builder.change_policy(change_policy.into()); + for e in recipients { + tx_builder.add_recipient(e.script.into(), e.amount); + } + tx_builder.change_policy(change_policy.into()); - if !utxos.is_empty() { - let bdk_utxos = utxos - .iter() - .map(|e| bdk::bitcoin::OutPoint::try_from(e)) - .collect::, BdkError>>()?; - tx_builder - .add_utxos(bdk_utxos.as_slice()) - .map_err(|e| >::into(e))?; - } - if !un_spendable.is_empty() { - let bdk_unspendable = un_spendable - .iter() - .map(|e| bdk::bitcoin::OutPoint::try_from(e)) - .collect::, BdkError>>()?; - tx_builder.unspendable(bdk_unspendable); - } - if manually_selected_only { - tx_builder.manually_selected_only(); - } - if let Some(sat_per_vb) = fee_rate { - tx_builder.fee_rate(bdk::FeeRate::from_sat_per_vb(sat_per_vb)); - } - if let Some(fee_amount) = fee_absolute { - tx_builder.fee_absolute(fee_amount); - } - if drain_wallet { - tx_builder.drain_wallet(); - } - if let Some(script_) = drain_to { - tx_builder.drain_to(script_.into()); - } - if let Some(utxo) = foreign_utxo { - let foreign_utxo: bdk::bitcoin::psbt::Input = utxo.1.try_into()?; - tx_builder.add_foreign_utxo((&utxo.0).try_into()?, foreign_utxo, utxo.2)?; - } - if let Some(rbf) = &rbf { - match rbf { - RbfValue::RbfDefault => { - tx_builder.enable_rbf(); - } - RbfValue::Value(nsequence) => { - tx_builder.enable_rbf_with_sequence(Sequence(nsequence.to_owned())); + if !utxos.is_empty() { + let bdk_utxos = utxos + .iter() + .map(|e| bdk::bitcoin::OutPoint::try_from(e)) + .collect::, BdkError>>()?; + tx_builder + .add_utxos(bdk_utxos.as_slice()) + .map_err(|e| >::into(e))?; + } + if !un_spendable.is_empty() { + let bdk_unspendable = un_spendable + .iter() + .map(|e| bdk::bitcoin::OutPoint::try_from(e)) + .collect::, BdkError>>()?; + tx_builder.unspendable(bdk_unspendable); + } + if manually_selected_only { + tx_builder.manually_selected_only(); + } + if let Some(sat_per_vb) = fee_rate { + tx_builder.fee_rate(bdk::FeeRate::from_sat_per_vb(sat_per_vb)); + } + if let Some(fee_amount) = fee_absolute { + tx_builder.fee_absolute(fee_amount); + } + if drain_wallet { + tx_builder.drain_wallet(); + } + if let Some(script_) = drain_to { + tx_builder.drain_to(script_.into()); + } + if let Some(utxo) = foreign_utxo { + let foreign_utxo: bdk::bitcoin::psbt::Input = utxo.1.try_into()?; + tx_builder.add_foreign_utxo((&utxo.0).try_into()?, foreign_utxo, utxo.2)?; + } + if let Some(rbf) = &rbf { + match rbf { + RbfValue::RbfDefault => { + tx_builder.enable_rbf(); + } + RbfValue::Value(nsequence) => { + tx_builder.enable_rbf_with_sequence(Sequence(nsequence.to_owned())); + } } } - } - if !data.is_empty() { - let push_bytes = PushBytesBuf::try_from(data.clone()) - .map_err(|_| BdkError::Generic("Failed to convert data to PushBytes".to_string()))?; - tx_builder.add_data(&push_bytes); - } + if !data.is_empty() { + let push_bytes = PushBytesBuf::try_from(data.clone()).map_err(|_| { + BdkError::Generic("Failed to convert data to PushBytes".to_string()) + })?; + tx_builder.add_data(&push_bytes); + } - return match tx_builder.finish() { - Ok(e) => Ok((e.0.into(), TransactionDetails::try_from(&e.1)?)), - Err(e) => Err(e.into()), - }; + return match tx_builder.finish() { + Ok(e) => Ok((e.0.into(), TransactionDetails::try_from(&e.1)?)), + Err(e) => Err(e.into()), + }; + })? } From b322a405c1bdde57fc32b04484e65c29de54bf94 Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Sat, 7 Sep 2024 21:53:00 -0400 Subject: [PATCH 09/13] bindings updated --- lib/src/generated/api/error.dart | 2 +- lib/src/generated/api/wallet.dart | 2 -- lib/src/generated/frb_generated.dart | 14 ++++++------ rust/src/frb_generated.rs | 34 ++++++++++++---------------- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/lib/src/generated/api/error.dart b/lib/src/generated/api/error.dart index c02c6f53..03733e53 100644 --- a/lib/src/generated/api/error.dart +++ b/lib/src/generated/api/error.dart @@ -10,7 +10,7 @@ import 'package:freezed_annotation/freezed_annotation.dart' hide protected; import 'types.dart'; part 'error.freezed.dart'; -// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from` +// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `fmt`, `fmt`, `fmt`, `fmt`, `fmt`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from`, `from` @freezed sealed class AddressError with _$AddressError { diff --git a/lib/src/generated/api/wallet.dart b/lib/src/generated/api/wallet.dart index b08d5dc4..b518c8b8 100644 --- a/lib/src/generated/api/wallet.dart +++ b/lib/src/generated/api/wallet.dart @@ -12,7 +12,6 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; import 'psbt.dart'; import 'types.dart'; -// These functions are ignored because they are not marked as `pub`: `get_wallet` // These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `fmt` Future<(BdkPsbt, TransactionDetails)> finishBumpFeeTxBuilder( @@ -109,7 +108,6 @@ class BdkWallet { onlyWitnessUtxo: onlyWitnessUtxo, sighashType: sighashType); - /// Return whether or not a script is part of this wallet (either internal or external). bool isMine({required BdkScriptBuf script}) => core.instance.api .crateApiWalletBdkWalletIsMine(that: this, script: script); diff --git a/lib/src/generated/frb_generated.dart b/lib/src/generated/frb_generated.dart index 81416fde..cd85e554 100644 --- a/lib/src/generated/frb_generated.dart +++ b/lib/src/generated/frb_generated.dart @@ -1358,7 +1358,7 @@ class coreApiImpl extends coreApiImplPlatform implements coreApi { }, codec: DcoCodec( decodeSuccessData: dco_decode_String, - decodeErrorData: null, + decodeErrorData: dco_decode_bdk_error, ), constMeta: kCrateApiPsbtBdkPsbtAsStringConstMeta, argValues: [that], @@ -1428,7 +1428,7 @@ class coreApiImpl extends coreApiImplPlatform implements coreApi { }, codec: DcoCodec( decodeSuccessData: dco_decode_opt_box_autoadd_u_64, - decodeErrorData: null, + decodeErrorData: dco_decode_bdk_error, ), constMeta: kCrateApiPsbtBdkPsbtFeeAmountConstMeta, argValues: [that], @@ -1451,7 +1451,7 @@ class coreApiImpl extends coreApiImplPlatform implements coreApi { }, codec: DcoCodec( decodeSuccessData: dco_decode_opt_box_autoadd_fee_rate, - decodeErrorData: null, + decodeErrorData: dco_decode_bdk_error, ), constMeta: kCrateApiPsbtBdkPsbtFeeRateConstMeta, argValues: [that], @@ -1495,7 +1495,7 @@ class coreApiImpl extends coreApiImplPlatform implements coreApi { }, codec: DcoCodec( decodeSuccessData: dco_decode_String, - decodeErrorData: null, + decodeErrorData: dco_decode_bdk_error, ), constMeta: kCrateApiPsbtBdkPsbtJsonSerializeConstMeta, argValues: [that], @@ -1518,7 +1518,7 @@ class coreApiImpl extends coreApiImplPlatform implements coreApi { }, codec: DcoCodec( decodeSuccessData: dco_decode_list_prim_u_8_strict, - decodeErrorData: null, + decodeErrorData: dco_decode_bdk_error, ), constMeta: kCrateApiPsbtBdkPsbtSerializeConstMeta, argValues: [that], @@ -1541,7 +1541,7 @@ class coreApiImpl extends coreApiImplPlatform implements coreApi { }, codec: DcoCodec( decodeSuccessData: dco_decode_String, - decodeErrorData: null, + decodeErrorData: dco_decode_bdk_error, ), constMeta: kCrateApiPsbtBdkPsbtTxidConstMeta, argValues: [that], @@ -2411,7 +2411,7 @@ class coreApiImpl extends coreApiImplPlatform implements coreApi { }, codec: DcoCodec( decodeSuccessData: dco_decode_network, - decodeErrorData: null, + decodeErrorData: dco_decode_bdk_error, ), constMeta: kCrateApiWalletBdkWalletNetworkConstMeta, argValues: [that], diff --git a/rust/src/frb_generated.rs b/rust/src/frb_generated.rs index 032d2b13..0cabd3ad 100644 --- a/rust/src/frb_generated.rs +++ b/rust/src/frb_generated.rs @@ -869,9 +869,8 @@ fn wire__crate__api__psbt__bdk_psbt_as_string_impl( }, move || { let api_that = that.cst_decode(); - transform_result_dco::<_, _, ()>((move || { - let output_ok = - Result::<_, ()>::Ok(crate::api::psbt::BdkPsbt::as_string(&api_that))?; + transform_result_dco::<_, _, crate::api::error::BdkError>((move || { + let output_ok = crate::api::psbt::BdkPsbt::as_string(&api_that)?; Ok(output_ok) })()) }, @@ -929,9 +928,8 @@ fn wire__crate__api__psbt__bdk_psbt_fee_amount_impl( }, move || { let api_that = that.cst_decode(); - transform_result_dco::<_, _, ()>((move || { - let output_ok = - Result::<_, ()>::Ok(crate::api::psbt::BdkPsbt::fee_amount(&api_that))?; + transform_result_dco::<_, _, crate::api::error::BdkError>((move || { + let output_ok = crate::api::psbt::BdkPsbt::fee_amount(&api_that)?; Ok(output_ok) })()) }, @@ -948,9 +946,8 @@ fn wire__crate__api__psbt__bdk_psbt_fee_rate_impl( }, move || { let api_that = that.cst_decode(); - transform_result_dco::<_, _, ()>((move || { - let output_ok = - Result::<_, ()>::Ok(crate::api::psbt::BdkPsbt::fee_rate(&api_that))?; + transform_result_dco::<_, _, crate::api::error::BdkError>((move || { + let output_ok = crate::api::psbt::BdkPsbt::fee_rate(&api_that)?; Ok(output_ok) })()) }, @@ -988,9 +985,8 @@ fn wire__crate__api__psbt__bdk_psbt_json_serialize_impl( }, move || { let api_that = that.cst_decode(); - transform_result_dco::<_, _, ()>((move || { - let output_ok = - Result::<_, ()>::Ok(crate::api::psbt::BdkPsbt::json_serialize(&api_that))?; + transform_result_dco::<_, _, crate::api::error::BdkError>((move || { + let output_ok = crate::api::psbt::BdkPsbt::json_serialize(&api_that)?; Ok(output_ok) })()) }, @@ -1007,9 +1003,8 @@ fn wire__crate__api__psbt__bdk_psbt_serialize_impl( }, move || { let api_that = that.cst_decode(); - transform_result_dco::<_, _, ()>((move || { - let output_ok = - Result::<_, ()>::Ok(crate::api::psbt::BdkPsbt::serialize(&api_that))?; + transform_result_dco::<_, _, crate::api::error::BdkError>((move || { + let output_ok = crate::api::psbt::BdkPsbt::serialize(&api_that)?; Ok(output_ok) })()) }, @@ -1026,8 +1021,8 @@ fn wire__crate__api__psbt__bdk_psbt_txid_impl( }, move || { let api_that = that.cst_decode(); - transform_result_dco::<_, _, ()>((move || { - let output_ok = Result::<_, ()>::Ok(crate::api::psbt::BdkPsbt::txid(&api_that))?; + transform_result_dco::<_, _, crate::api::error::BdkError>((move || { + let output_ok = crate::api::psbt::BdkPsbt::txid(&api_that)?; Ok(output_ok) })()) }, @@ -1772,9 +1767,8 @@ fn wire__crate__api__wallet__bdk_wallet_network_impl( }, move || { let api_that = that.cst_decode(); - transform_result_dco::<_, _, ()>((move || { - let output_ok = - Result::<_, ()>::Ok(crate::api::wallet::BdkWallet::network(&api_that))?; + transform_result_dco::<_, _, crate::api::error::BdkError>((move || { + let output_ok = crate::api::wallet::BdkWallet::network(&api_that)?; Ok(output_ok) })()) }, From 8762204e3b33c2c6c2a2286de50d12a2de6d7a2e Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Sun, 8 Sep 2024 05:18:00 -0400 Subject: [PATCH 10/13] esplora url updated --- lib/src/root.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/root.dart b/lib/src/root.dart index 760afe41..d39ff998 100644 --- a/lib/src/root.dart +++ b/lib/src/root.dart @@ -117,13 +117,13 @@ class Blockchain extends BdkBlockchain { } /// [Blockchain] constructor for creating `Esplora` blockchain in `Mutinynet` - /// Esplora url: https://mutinynet.ltbl.io/api + /// Esplora url: https://mutinynet.com/api/ static Future createMutinynet({ int stopGap = 20, }) async { final config = BlockchainConfig.esplora( config: EsploraConfig( - baseUrl: 'https://mutinynet.ltbl.io/api', + baseUrl: 'https://mutinynet.com/api/', stopGap: BigInt.from(stopGap), ), ); From 5f2e5661fefbc7d0094db8a6298c6f589d49abda Mon Sep 17 00:00:00 2001 From: BitcoinZavior Date: Tue, 10 Sep 2024 17:25:00 -0400 Subject: [PATCH 11/13] Rebase From 879d6cbb21e5663baf9d3f45bf049800500fc430 Mon Sep 17 00:00:00 2001 From: Bitcoin Zavior <93057399+BitcoinZavior@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:43:30 -0400 Subject: [PATCH 12/13] Build on push to all branches --- .github/workflows/precompile_binaries.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/precompile_binaries.yml b/.github/workflows/precompile_binaries.yml index 6dfe7c50..5e2fa69d 100644 --- a/.github/workflows/precompile_binaries.yml +++ b/.github/workflows/precompile_binaries.yml @@ -1,6 +1,6 @@ on: push: - branches: [0.31.2, master, main] + branches: [*] name: Precompile Binaries @@ -51,4 +51,4 @@ jobs: working-directory: cargokit/build_tool env: GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} - PRIVATE_KEY: ${{ secrets.CARGOKIT_PRIVATE_KEY }} \ No newline at end of file + PRIVATE_KEY: ${{ secrets.CARGOKIT_PRIVATE_KEY }} From 6cbadd9d7267d0fbb8626dfb403fb5d562046543 Mon Sep 17 00:00:00 2001 From: Bitcoin Zavior <93057399+BitcoinZavior@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:44:46 -0400 Subject: [PATCH 13/13] Build on push to all branches --- .github/workflows/precompile_binaries.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/precompile_binaries.yml b/.github/workflows/precompile_binaries.yml index 5e2fa69d..0804c5b0 100644 --- a/.github/workflows/precompile_binaries.yml +++ b/.github/workflows/precompile_binaries.yml @@ -1,6 +1,6 @@ on: push: - branches: [*] + branches: '*' name: Precompile Binaries