From 620d65e21730fc4f97cf72152ed12961e5f0ff57 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 00:43:26 +0530 Subject: [PATCH 1/9] Allow cloning transaction --- src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 1510fee..05614f1 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, From f4e9af18b53d5e2bb5444cc7770a911eb5bf1145 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 00:43:55 +0530 Subject: [PATCH 2/9] Add a way to convert TransactionDetails to Transaction --- src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 05614f1..9bc441f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,6 +82,22 @@ pub enum Transaction { }, } +fn to_transaction(x: &bdk::TransactionDetails) -> 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 }, + } +} + trait OfflineWalletOperations: WalletHolder { fn get_new_address(&self) -> String { self.get_wallet() From d190008f2ca1da3c202b1e616dd552006224d9bc Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 00:45:02 +0530 Subject: [PATCH 3/9] Add details to PSBT --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9bc441f..a7a159b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -193,6 +193,7 @@ impl Progress for BdkProgressHolder { struct PartiallySignedBitcoinTransaction { internal: Mutex, + details: bdk::TransactionDetails, } impl PartiallySignedBitcoinTransaction { @@ -205,7 +206,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 { @@ -215,6 +216,7 @@ impl PartiallySignedBitcoinTransaction { }; Ok(PartiallySignedBitcoinTransaction { internal: Mutex::new(psbt), + details, }) } Err(..) => Err(BdkError::Generic( From 683a817c5543f0b59e8090e65a50d6b4ed58a6b6 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 00:45:16 +0530 Subject: [PATCH 4/9] Simplify --- src/lib.rs | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a7a159b..ffa133a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,24 +133,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(to_transaction).collect()) } } From 578771ffe1ff80ae37e9390f10f540f186da7aea Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 00:45:27 +0530 Subject: [PATCH 5/9] Return transaction on broadcast --- src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ffa133a..944197e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -269,10 +269,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(to_transaction(&psbt.details)) } } From de47771c1240a187b1338bc7e097363ebe29af05 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 00:45:40 +0530 Subject: [PATCH 6/9] Update Wallet::broadcast API --- src/bdk.udl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 { From e738126bedff23e8a7f094b28927fb2a9c390fe4 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 00:45:50 +0530 Subject: [PATCH 7/9] Fix demo --- bindings/bdk-kotlin/demo/src/main/kotlin/Main.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bindings/bdk-kotlin/demo/src/main/kotlin/Main.kt b/bindings/bdk-kotlin/demo/src/main/kotlin/Main.kt index a02f843..45cd0a8 100644 --- a/bindings/bdk-kotlin/demo/src/main/kotlin/Main.kt +++ b/bindings/bdk-kotlin/demo/src/main/kotlin/Main.kt @@ -72,12 +72,12 @@ fun main(args: Array) { println("Press Enter to return funds") readLine() println("Creating a PSBT with recipient $recipient and amount $amount satoshis...") - val transaction = PartiallySignedBitcoinTransaction(wallet, recipient, amount) + val psbt = PartiallySignedBitcoinTransaction(wallet, recipient, amount, null) println("Signing the transaction...") - wallet.sign(transaction) + wallet.sign(psbt) println("Broadcasting the signed transaction...") - val transactionId = wallet.broadcast(transaction) - println("Broadcasted transaction with id $transactionId") + val transaction = wallet.broadcast(psbt) + println("Broadcasted transaction $transaction") val take = 5 println("Listing latest $take transactions...") wallet @@ -87,3 +87,4 @@ fun main(args: Array) { .forEach { println(it) } println("Final wallet balance: ${wallet.getBalance()}") } + From 87a8af9457a256790427a77d509b16e5a8492256 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 01:08:50 +0530 Subject: [PATCH 8/9] Use From trait for conversion --- src/lib.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 944197e..3ad57a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,19 +82,21 @@ pub enum Transaction { }, } -fn to_transaction(x: &bdk::TransactionDetails) -> 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 }, +impl From<&bdk::TransactionDetails> for Transaction { + fn from(x: &bdk::TransactionDetails) -> 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 }, + } } } @@ -133,7 +135,7 @@ trait OfflineWalletOperations: WalletHolder { fn get_transactions(&self) -> Result, Error> { let transactions = self.get_wallet().list_transactions(true)?; - Ok(transactions.iter().map(to_transaction).collect()) + Ok(transactions.iter().map(Transaction::from).collect()) } } @@ -275,7 +277,7 @@ impl OnlineWallet { ) -> Result { let tx = psbt.internal.lock().unwrap().clone().extract_tx(); self.get_wallet().broadcast(tx)?; - Ok(to_transaction(&psbt.details)) + Ok(Transaction::from(&psbt.details)) } } From bfe38d9890ef0fbf18d02a8aa3de44008305a328 Mon Sep 17 00:00:00 2001 From: Sudarsan Balaji Date: Fri, 5 Nov 2021 01:13:45 +0530 Subject: [PATCH 9/9] Use From trait for conversion --- src/lib.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3ad57a2..63505de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,20 +82,25 @@ pub enum Transaction { }, } -impl From<&bdk::TransactionDetails> for Transaction { - fn from(x: &bdk::TransactionDetails) -> Transaction { - let details = TransactionDetails { +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, + details: TransactionDetails::from(x), confirmation, }, - None => Transaction::Unconfirmed { details }, + None => Transaction::Unconfirmed { details: TransactionDetails::from(x) }, } } }