From cbc740c407182b91d2b6ac6528f8b1759cd78d79 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Mon, 19 Dec 2022 12:42:11 -0500 Subject: [PATCH] Refactor: separate psbt struct from lib.rs --- bdk-ffi/src/lib.rs | 76 +++---------------------------------------- bdk-ffi/src/psbt.rs | 75 ++++++++++++++++++++++++++++++++++++++++++ bdk-ffi/src/wallet.rs | 3 +- 3 files changed, 81 insertions(+), 73 deletions(-) create mode 100644 bdk-ffi/src/psbt.rs diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index daa076f..87dcdde 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -1,13 +1,11 @@ -mod wallet; mod psbt; +mod wallet; +use crate::psbt::PartiallySignedTransaction; use crate::wallet::Wallet; use bdk::bitcoin::blockdata::script::Script as BdkScript; -use bdk::bitcoin::hashes::hex::ToHex; use bdk::bitcoin::secp256k1::Secp256k1; use bdk::bitcoin::util::bip32::{DerivationPath as BdkDerivationPath, Fingerprint}; -use bdk::bitcoin::util::psbt::serialize::Serialize; -use bdk::bitcoin::util::psbt::PartiallySignedTransaction as BdkPartiallySignedTransaction; use bdk::bitcoin::Sequence; use bdk::bitcoin::{Address as BdkAddress, Network, OutPoint as BdkOutPoint, Txid}; use bdk::blockchain::any::{AnyBlockchain, AnyBlockchainConfig}; @@ -28,7 +26,6 @@ use bdk::keys::{ KeyMap, }; use bdk::miniscript::BareCtx; -use bdk::psbt::PsbtUtils; use bdk::template::{ Bip44, Bip44Public, Bip49, Bip49Public, Bip84, Bip84Public, DescriptorTemplate, }; @@ -432,72 +429,6 @@ impl fmt::Debug for ProgressHolder { } } -#[derive(Debug)] -pub struct PartiallySignedTransaction { - internal: Mutex, -} - -impl PartiallySignedTransaction { - fn new(psbt_base64: String) -> Result { - let psbt: BdkPartiallySignedTransaction = - BdkPartiallySignedTransaction::from_str(&psbt_base64)?; - Ok(PartiallySignedTransaction { - internal: Mutex::new(psbt), - }) - } - - fn serialize(&self) -> String { - let psbt = self.internal.lock().unwrap().clone(); - psbt.to_string() - } - - fn txid(&self) -> String { - let tx = self.internal.lock().unwrap().clone().extract_tx(); - let txid = tx.txid(); - txid.to_hex() - } - - /// Return the transaction as bytes. - fn extract_tx(&self) -> Vec { - self.internal - .lock() - .unwrap() - .clone() - .extract_tx() - .serialize() - } - - /// 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)` - fn combine( - &self, - other: Arc, - ) -> Result, BdkError> { - let other_psbt = other.internal.lock().unwrap().clone(); - let mut original_psbt = self.internal.lock().unwrap().clone(); - - original_psbt.combine(other_psbt)?; - Ok(Arc::new(PartiallySignedTransaction { - internal: Mutex::new(original_psbt), - })) - } - - /// 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. - fn fee_amount(&self) -> Option { - self.internal.lock().unwrap().fee_amount() - } - - /// The transaction's fee rate. This value will only be accurate if calculated AFTER the - /// `PartiallySignedTransaction` is finalized and all witness/signature data is added to the - /// transaction. - /// If the PSBT is missing a TxOut for an input returns None. - fn fee_rate(&self) -> Option> { - self.internal.lock().unwrap().fee_rate().map(Arc::new) - } -} - /// A Bitcoin address. struct Address { address: BdkAddress, @@ -539,7 +470,7 @@ enum RbfValue { /// The result after calling the TxBuilder finish() function. Contains unsigned PSBT and /// transaction details. pub struct TxBuilderResult { - pub psbt: Arc, + pub(crate) psbt: Arc, pub transaction_details: TransactionDetails, } @@ -1283,6 +1214,7 @@ uniffi::deps::static_assertions::assert_impl_all!(Wallet: Sync, Send); mod test { use crate::*; use assert_matches::assert_matches; + use bdk::bitcoin::hashes::hex::ToHex; use bdk::bitcoin::Address; use bdk::descriptor::DescriptorError::Key; use bdk::keys::KeyError::InvalidNetwork; diff --git a/bdk-ffi/src/psbt.rs b/bdk-ffi/src/psbt.rs new file mode 100644 index 0000000..403f92d --- /dev/null +++ b/bdk-ffi/src/psbt.rs @@ -0,0 +1,75 @@ +use bdk::bitcoin::hashes::hex::ToHex; +use bdk::bitcoin::psbt::serialize::Serialize; +use bdk::bitcoin::util::psbt::PartiallySignedTransaction as BdkPartiallySignedTransaction; +use bdk::psbt::PsbtUtils; +use bdk::Error as BdkError; +use std::str::FromStr; +use std::sync::{Arc, Mutex}; + +use crate::FeeRate; + +#[derive(Debug)] +pub(crate) struct PartiallySignedTransaction { + pub(crate) internal: Mutex, +} + +impl PartiallySignedTransaction { + pub(crate) fn new(psbt_base64: String) -> Result { + let psbt: BdkPartiallySignedTransaction = + BdkPartiallySignedTransaction::from_str(&psbt_base64)?; + Ok(PartiallySignedTransaction { + internal: Mutex::new(psbt), + }) + } + + pub(crate) fn serialize(&self) -> String { + let psbt = self.internal.lock().unwrap().clone(); + psbt.to_string() + } + + pub(crate) fn txid(&self) -> String { + let tx = self.internal.lock().unwrap().clone().extract_tx(); + let txid = tx.txid(); + txid.to_hex() + } + + /// Return the transaction as bytes. + pub(crate) fn extract_tx(&self) -> Vec { + self.internal + .lock() + .unwrap() + .clone() + .extract_tx() + .serialize() + } + + /// 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(crate) fn combine( + &self, + other: Arc, + ) -> Result, BdkError> { + let other_psbt = other.internal.lock().unwrap().clone(); + let mut original_psbt = self.internal.lock().unwrap().clone(); + + original_psbt.combine(other_psbt)?; + Ok(Arc::new(PartiallySignedTransaction { + internal: Mutex::new(original_psbt), + })) + } + + /// 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. + pub(crate) fn fee_amount(&self) -> Option { + self.internal.lock().unwrap().fee_amount() + } + + /// The transaction's fee rate. This value will only be accurate if calculated AFTER the + /// `PartiallySignedTransaction` is finalized and all witness/signature data is added to the + /// transaction. + /// If the PSBT is missing a TxOut for an input returns None. + pub(crate) fn fee_rate(&self) -> Option> { + self.internal.lock().unwrap().fee_rate().map(Arc::new) + } +} diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index 724e9e1..6399c8b 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -5,9 +5,10 @@ use bdk::{Error as BdkError, SignOptions, SyncOptions as BdkSyncOptions, Wallet use std::ops::Deref; use std::sync::{Arc, Mutex, MutexGuard}; +use crate::psbt::PartiallySignedTransaction; use crate::{ AddressIndex, AddressInfo, Balance, Blockchain, DatabaseConfig, Descriptor, LocalUtxo, - NetworkLocalUtxo, PartiallySignedTransaction, Progress, ProgressHolder, TransactionDetails, + NetworkLocalUtxo, Progress, ProgressHolder, TransactionDetails, }; #[derive(Debug)]