diff --git a/src/bdk.udl b/src/bdk.udl index dc2a996..2d8061c 100644 --- a/src/bdk.udl +++ b/src/bdk.udl @@ -144,7 +144,7 @@ interface OnlineWallet { [Throws=BdkError] void sync(BdkProgress progress_update, u32? max_address_param); [Throws=BdkError] - string broadcast([ByRef] PartiallySignedBitcoinTransaction psbt); + Transaction broadcast([ByRef] PartiallySignedBitcoinTransaction psbt); }; interface PartiallySignedBitcoinTransaction { diff --git a/src/lib.rs b/src/lib.rs index 1510fee..63505de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,6 +70,8 @@ pub struct TransactionDetails { } type Confirmation = ConfirmationTime; + +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Transaction { Unconfirmed { details: TransactionDetails, @@ -80,6 +82,29 @@ pub enum Transaction { }, } +impl From<&bdk::TransactionDetails> for TransactionDetails { + fn from(x: &bdk::TransactionDetails) -> TransactionDetails { + TransactionDetails { + fees: x.fee, + id: x.txid.to_string(), + received: x.received, + sent: x.sent, + } + } +} + +impl From<&bdk::TransactionDetails> for Transaction { + fn from(x: &bdk::TransactionDetails) -> Transaction { + match x.confirmation_time.clone() { + Some(confirmation) => Transaction::Confirmed { + details: TransactionDetails::from(x), + confirmation, + }, + None => Transaction::Unconfirmed { details: TransactionDetails::from(x) }, + } + } +} + trait OfflineWalletOperations: WalletHolder { fn get_new_address(&self) -> String { self.get_wallet() @@ -115,24 +140,7 @@ trait OfflineWalletOperations: WalletHolder { fn get_transactions(&self) -> Result, Error> { let transactions = self.get_wallet().list_transactions(true)?; - Ok(transactions - .iter() - .map(|x| -> Transaction { - let details = TransactionDetails { - fees: x.fee, - id: x.txid.to_string(), - received: x.received, - sent: x.sent, - }; - match x.confirmation_time.clone() { - Some(confirmation) => Transaction::Confirmed { - details, - confirmation, - }, - None => Transaction::Unconfirmed { details }, - } - }) - .collect()) + Ok(transactions.iter().map(Transaction::from).collect()) } } @@ -175,6 +183,7 @@ impl Progress for BdkProgressHolder { struct PartiallySignedBitcoinTransaction { internal: Mutex, + details: bdk::TransactionDetails, } impl PartiallySignedBitcoinTransaction { @@ -187,7 +196,7 @@ impl PartiallySignedBitcoinTransaction { let wallet = online_wallet.get_wallet(); match Address::from_str(&recipient) { Ok(address) => { - let (psbt, _) = { + let (psbt, details) = { let mut builder = wallet.build_tx(); builder.add_recipient(address.script_pubkey(), amount); if let Some(sat_per_vb) = fee_rate { @@ -197,6 +206,7 @@ impl PartiallySignedBitcoinTransaction { }; Ok(PartiallySignedBitcoinTransaction { internal: Mutex::new(psbt), + details, }) } Err(..) => Err(BdkError::Generic( @@ -266,10 +276,13 @@ impl OnlineWallet { .sync(BdkProgressHolder { progress_update }, max_address_param) } - fn broadcast<'a>(&self, psbt: &'a PartiallySignedBitcoinTransaction) -> Result { + fn broadcast<'a>( + &self, + psbt: &'a PartiallySignedBitcoinTransaction, + ) -> Result { let tx = psbt.internal.lock().unwrap().clone().extract_tx(); - let tx_id = self.get_wallet().broadcast(tx)?; - Ok(tx_id.to_string()) + self.get_wallet().broadcast(tx)?; + Ok(Transaction::from(&psbt.details)) } }