From 27c10a75246854403faf4a73a05f900a2bcd2d9a Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 7 Dec 2023 13:24:39 -0600 Subject: [PATCH] feat: add transactions method on wallet --- .../org/bitcoindevkit/LiveWalletTest.kt | 9 +++++++ bdk-ffi/src/bdk.udl | 2 ++ bdk-ffi/src/wallet.rs | 25 +++++++++++++++++++ .../org/bitcoindevkit/LiveWalletTest.kt | 9 +++++++ bdk-python/tests/test_live_wallet.py | 11 +++++++- .../BitcoinDevKitTests/LiveWalletTests.swift | 9 +++++++ 6 files changed, 64 insertions(+), 1 deletion(-) diff --git a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt index 6b6b5b91..b9a7d54a 100644 --- a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt +++ b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt @@ -19,6 +19,15 @@ class LiveWalletTest { println("Balance: $balance") assert(wallet.getBalance().total > 0uL) + + println("Transactions count: ${wallet.transactions().count()}") + val transactions = wallet.transactions().take(3) + for (tx in transactions) { + val sentAndReceived = wallet.sentAndReceived(tx) + println("Transaction: ${tx.txid()}") + println("Sent ${sentAndReceived.sent}") + println("Received ${sentAndReceived.received}") + } } @Test diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 1229779e..f6935093 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -109,6 +109,8 @@ interface Wallet { boolean sign(PartiallySignedTransaction psbt); SentAndReceivedValues sent_and_received([ByRef] Transaction tx); + + sequence transactions(); }; interface Update {}; diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index c6f2c6ae..709edab7 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -98,6 +98,31 @@ impl Wallet { let (sent, received): (u64, u64) = self.get_wallet().sent_and_received(&tx.clone().into()); SentAndReceivedValues { sent, received } } + + pub fn transactions(&self) -> Vec> { + self.get_wallet() + .transactions() + .filter_map(|tx| { + let transaction = tx.tx_node.tx.clone(); + + let is_mine_input = transaction.input.iter().any(|input| { + let script = Script::from(input.script_sig.clone()); + self.is_mine(Arc::new(script)) + }); + + let is_mine_output = transaction.output.iter().any(|output| { + let script = Script::from(output.script_pubkey.clone()); + self.is_mine(Arc::new(script)) + }); + + if is_mine_input || is_mine_output { + Some(Arc::new(transaction.into())) + } else { + None + } + }) + .collect() + } } pub struct SentAndReceivedValues { diff --git a/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveWalletTest.kt b/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveWalletTest.kt index ff709f80..dda5b9fa 100644 --- a/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveWalletTest.kt +++ b/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveWalletTest.kt @@ -15,6 +15,15 @@ class LiveWalletTest { println("Balance: ${wallet.getBalance().total}") assert(wallet.getBalance().total > 0uL) + + println("Transactions count: ${wallet.transactions().count()}") + val transactions = wallet.transactions().take(3) + for (tx in transactions) { + val sentAndReceived = wallet.sentAndReceived(tx) + println("Transaction: ${tx.txid()}") + println("Sent ${sentAndReceived.sent}") + println("Received ${sentAndReceived.received}") + } } @Test diff --git a/bdk-python/tests/test_live_wallet.py b/bdk-python/tests/test_live_wallet.py index cb3b5461..a99180a7 100644 --- a/bdk-python/tests/test_live_wallet.py +++ b/bdk-python/tests/test_live_wallet.py @@ -23,7 +23,16 @@ def test_synced_balance(self): self.assertGreater(wallet.get_balance().total, 0) - def test_broadcast_transaction(self): + print(f"Transactions count: {len(wallet.transactions())}") + transactions = wallet.transactions()[:3] + for tx in transactions: + sent_and_received = wallet.sent_and_received(tx) + print(f"Transaction: {tx.txid()}") + print(f"Sent {sent_and_received.sent}") + print(f"Received {sent_and_received.received}") + + +def test_broadcast_transaction(self): descriptor: bdk.Descriptor = bdk.Descriptor( "wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", bdk.Network.TESTNET diff --git a/bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift b/bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift index 87565986..b936ea95 100644 --- a/bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift +++ b/bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift @@ -21,6 +21,15 @@ final class LiveWalletTests: XCTestCase { try wallet.applyUpdate(update: update) XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0)) + + print("Transactions count: \(wallet.transactions().count)") + let transactions = wallet.transactions().prefix(3) + for tx in transactions { + let sentAndReceived = wallet.sentAndReceived(tx: tx) + print("Transaction: \(tx.txid())") + print("Sent \(sentAndReceived.sent)") + print("Received \(sentAndReceived.received)") + } } func testBroadcastTransaction() throws {