From 154fbdd10590505dbb38d020d1945ff8231553e2 Mon Sep 17 00:00:00 2001 From: Aniket-pd Date: Wed, 7 Jan 2026 03:49:57 +0530 Subject: [PATCH 1/3] Ensure ElectrumClient is disposed after sync Moves ElectrumClient instantiation outside the try block and disposes it in a finally block to guarantee proper resource cleanup after Electrum sync, even if an error occurs. --- examples/network_example.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/network_example.dart b/examples/network_example.dart index 17d74dc..3c5d9af 100644 --- a/examples/network_example.dart +++ b/examples/network_example.dart @@ -39,10 +39,10 @@ void main() { stdout.writeln('Persisted staged wallet changes: $persisted'); // 5. Try a quick Electrum sync to fetch history/balances. + ElectrumClient? client; try { stdout.writeln('\nSyncing via Electrum (blockstream.info)…'); - final client = - ElectrumClient('ssl://electrum.blockstream.info:60002', null); + client = ElectrumClient('ssl://electrum.blockstream.info:60002', null); final syncRequest = wallet.startSyncWithRevealedSpks().build(); final update = client.sync_(syncRequest, 100, true); @@ -53,12 +53,13 @@ void main() { stdout.writeln('Confirmed balance: ${balance.confirmed.toSat()} sats'); stdout.writeln('Total balance: ${balance.total.toSat()} sats'); - client.dispose(); } catch (error) { stdout.writeln( 'Electrum sync failed: $error\n' 'Ensure TLS-enabled Electrum access is available, or skip this step.', ); + } finally { + client?.dispose(); } // 6. Clean up FFI handles explicitly so long-lived examples don’t leak. From ffbcde8832df4bba8d48a5f283b1f7a2ad14a36a Mon Sep 17 00:00:00 2001 From: Aniket-pd Date: Wed, 7 Jan 2026 04:09:11 +0530 Subject: [PATCH 2/3] Refactor code formatting for readability Reformatted code in example and test files to improve readability and consistency, including argument alignment and removal of unnecessary line breaks. No functional changes were made. --- examples/network_example.dart | 29 +++++++++++------ test/descriptor_test.dart | 50 ++++++------------------------ test/offline_persistence_test.dart | 4 ++- test/offline_wallet_test.dart | 24 +++++++++----- test/test_constants.dart | 42 ++++++++++--------------- test/wallet_behavior_test.dart | 37 +++++++++++----------- test/wallet_creation_test.dart | 11 ++----- 7 files changed, 88 insertions(+), 109 deletions(-) diff --git a/examples/network_example.dart b/examples/network_example.dart index 3c5d9af..9867ad9 100644 --- a/examples/network_example.dart +++ b/examples/network_example.dart @@ -16,10 +16,16 @@ void main() { // 2. Turn the mnemonic into descriptor keys for external/change paths. final rootKey = DescriptorSecretKey(network, mnemonic, null); - final externalDescriptor = - Descriptor.newBip84(rootKey, KeychainKind.external_, network); - final changeDescriptor = - Descriptor.newBip84(rootKey, KeychainKind.internal, network); + final externalDescriptor = Descriptor.newBip84( + rootKey, + KeychainKind.external_, + network, + ); + final changeDescriptor = Descriptor.newBip84( + rootKey, + KeychainKind.internal, + network, + ); stdout ..writeln('\nExternal descriptor:\n $externalDescriptor') @@ -27,14 +33,20 @@ void main() { // 3. Spin up an in-memory wallet using the descriptors. final persister = Persister.newInMemory(); - final wallet = - Wallet(externalDescriptor, changeDescriptor, network, persister, 25); + final wallet = Wallet( + externalDescriptor, + changeDescriptor, + network, + persister, + 25, + ); stdout.writeln('\nWallet ready on ${wallet.network()}'); // 4. Hand out the next receive address and persist the staged change. final receive = wallet.revealNextAddress(KeychainKind.external_); stdout.writeln( - 'Next receive address (#${receive.index}): ${receive.address.toString()}'); + 'Next receive address (#${receive.index}): ${receive.address.toString()}', + ); final persisted = wallet.persist(persister); stdout.writeln('Persisted staged wallet changes: $persisted'); @@ -52,13 +64,12 @@ void main() { final balance = wallet.balance(); stdout.writeln('Confirmed balance: ${balance.confirmed.toSat()} sats'); stdout.writeln('Total balance: ${balance.total.toSat()} sats'); - } catch (error) { stdout.writeln( 'Electrum sync failed: $error\n' 'Ensure TLS-enabled Electrum access is available, or skip this step.', ); - } finally { + } finally { client?.dispose(); } diff --git a/test/descriptor_test.dart b/test/descriptor_test.dart index ff9768d..719e1cd 100644 --- a/test/descriptor_test.dart +++ b/test/descriptor_test.dart @@ -6,49 +6,19 @@ import 'test_constants.dart'; void main() { group('Descriptor construction', () { test('creates extended WPKH descriptors across networks', () { - expect( - () => buildBip84Descriptor(Network.regtest), - returnsNormally, - ); - expect( - () => buildBip84Descriptor(Network.testnet), - returnsNormally, - ); - expect( - () => buildBip84Descriptor(Network.testnet4), - returnsNormally, - ); - expect( - () => buildBip84Descriptor(Network.signet), - returnsNormally, - ); - expect( - () => buildMainnetBip84Descriptor(), - returnsNormally, - ); + expect(() => buildBip84Descriptor(Network.regtest), returnsNormally); + expect(() => buildBip84Descriptor(Network.testnet), returnsNormally); + expect(() => buildBip84Descriptor(Network.testnet4), returnsNormally); + expect(() => buildBip84Descriptor(Network.signet), returnsNormally); + expect(() => buildMainnetBip84Descriptor(), returnsNormally); }); test('creates extended TR descriptors across networks', () { - expect( - () => buildBip86Descriptor(Network.regtest), - returnsNormally, - ); - expect( - () => buildBip86Descriptor(Network.testnet), - returnsNormally, - ); - expect( - () => buildBip86Descriptor(Network.testnet4), - returnsNormally, - ); - expect( - () => buildBip86Descriptor(Network.signet), - returnsNormally, - ); - expect( - () => buildMainnetBip86Descriptor(), - returnsNormally, - ); + expect(() => buildBip86Descriptor(Network.regtest), returnsNormally); + expect(() => buildBip86Descriptor(Network.testnet), returnsNormally); + expect(() => buildBip86Descriptor(Network.testnet4), returnsNormally); + expect(() => buildBip86Descriptor(Network.signet), returnsNormally); + expect(() => buildMainnetBip86Descriptor(), returnsNormally); }); test('creates non-extended descriptors for all networks', () { diff --git a/test/offline_persistence_test.dart b/test/offline_persistence_test.dart index 61790bd..e394360 100644 --- a/test/offline_persistence_test.dart +++ b/test/offline_persistence_test.dart @@ -44,7 +44,9 @@ void main() { final wallet = Wallet.load( buildDescriptor(persistencePublicDescriptorString, Network.signet), buildDescriptor( - persistencePublicChangeDescriptorString, Network.signet), + persistencePublicChangeDescriptorString, + Network.signet, + ), persister, defaultLookahead, ); diff --git a/test/offline_wallet_test.dart b/test/offline_wallet_test.dart index 420e478..75742a1 100644 --- a/test/offline_wallet_test.dart +++ b/test/offline_wallet_test.dart @@ -6,10 +6,14 @@ import 'test_constants.dart'; void main() { group('Offline wallet', () { test('revealNextAddress yields expected address on multiple networks', () { - final descriptor = - buildDescriptor(offlineDescriptorString, Network.signet); - final changeDescriptor = - buildDescriptor(offlineChangeDescriptorString, Network.signet); + final descriptor = buildDescriptor( + offlineDescriptorString, + Network.signet, + ); + final changeDescriptor = buildDescriptor( + offlineChangeDescriptorString, + Network.signet, + ); final persister = Persister.newInMemory(); final wallet = Wallet( descriptor, @@ -34,10 +38,14 @@ void main() { }); test('new wallet starts with zero balance', () { - final descriptor = - buildDescriptor(offlineDescriptorString, Network.signet); - final changeDescriptor = - buildDescriptor(offlineChangeDescriptorString, Network.signet); + final descriptor = buildDescriptor( + offlineDescriptorString, + Network.signet, + ); + final changeDescriptor = buildDescriptor( + offlineChangeDescriptorString, + Network.signet, + ); final persister = Persister.newInMemory(); final wallet = Wallet( descriptor, diff --git a/test/test_constants.dart b/test/test_constants.dart index 0c6028b..6663333 100644 --- a/test/test_constants.dart +++ b/test/test_constants.dart @@ -37,39 +37,31 @@ const expectedPersistedAddress = "tb1qan3lldunh37ma6c0afeywgjyjgnyc8uz975zl2"; Descriptor buildDescriptor(String descriptor, Network network) => Descriptor(descriptor, network); -Descriptor buildBip84Descriptor(Network network) => Descriptor( - "wpkh($testExtendedPrivKey/$bip84TestReceivePath/*)", - network, - ); +Descriptor buildBip84Descriptor(Network network) => + Descriptor("wpkh($testExtendedPrivKey/$bip84TestReceivePath/*)", network); -Descriptor buildBip84ChangeDescriptor(Network network) => Descriptor( - "wpkh($testExtendedPrivKey/$bip84TestChangePath/*)", - network, - ); +Descriptor buildBip84ChangeDescriptor(Network network) => + Descriptor("wpkh($testExtendedPrivKey/$bip84TestChangePath/*)", network); -Descriptor buildBip86Descriptor(Network network) => Descriptor( - "tr($testExtendedPrivKey/$bip86TestReceivePath/*)", - network, - ); +Descriptor buildBip86Descriptor(Network network) => + Descriptor("tr($testExtendedPrivKey/$bip86TestReceivePath/*)", network); -Descriptor buildBip86ChangeDescriptor(Network network) => Descriptor( - "tr($testExtendedPrivKey/$bip86TestChangePath/*)", - network, - ); +Descriptor buildBip86ChangeDescriptor(Network network) => + Descriptor("tr($testExtendedPrivKey/$bip86TestChangePath/*)", network); Descriptor buildMainnetBip84Descriptor() => Descriptor( - "wpkh($mainnetExtendedPrivKey/$bip84MainnetReceivePath/*)", - Network.bitcoin, - ); + "wpkh($mainnetExtendedPrivKey/$bip84MainnetReceivePath/*)", + Network.bitcoin, +); Descriptor buildMainnetBip86Descriptor() => Descriptor( - "tr($mainnetExtendedPrivKey/$bip86MainnetReceivePath/*)", - Network.bitcoin, - ); + "tr($mainnetExtendedPrivKey/$bip86MainnetReceivePath/*)", + Network.bitcoin, +); Descriptor buildNonExtendedDescriptor(int index) => Descriptor( - "wpkh($testExtendedPrivKey/$bip84TestReceivePath/$index)", - Network.testnet, - ); + "wpkh($testExtendedPrivKey/$bip84TestReceivePath/$index)", + Network.testnet, +); const defaultLookahead = 25; diff --git a/test/wallet_behavior_test.dart b/test/wallet_behavior_test.dart index 981c982..6348912 100644 --- a/test/wallet_behavior_test.dart +++ b/test/wallet_behavior_test.dart @@ -33,23 +33,24 @@ void main() { }); test( - 'single-descriptor wallet returns identical external/internal addresses', - () { - final persister = Persister.newInMemory(); - final wallet = Wallet.createSingle( - buildBip84Descriptor(Network.testnet), - Network.testnet, - persister, - defaultLookahead, - ); - - final externalAddress = wallet.peekAddress(KeychainKind.external_, 0); - final internalAddress = wallet.peekAddress(KeychainKind.internal, 0); - - expect( - externalAddress.address.scriptPubkey().toBytes(), - orderedEquals(internalAddress.address.scriptPubkey().toBytes()), - ); - }); + 'single-descriptor wallet returns identical external/internal addresses', + () { + final persister = Persister.newInMemory(); + final wallet = Wallet.createSingle( + buildBip84Descriptor(Network.testnet), + Network.testnet, + persister, + defaultLookahead, + ); + + final externalAddress = wallet.peekAddress(KeychainKind.external_, 0); + final internalAddress = wallet.peekAddress(KeychainKind.internal, 0); + + expect( + externalAddress.address.scriptPubkey().toBytes(), + orderedEquals(internalAddress.address.scriptPubkey().toBytes()), + ); + }, + ); }); } diff --git a/test/wallet_creation_test.dart b/test/wallet_creation_test.dart index de40f2a..fb464ea 100644 --- a/test/wallet_creation_test.dart +++ b/test/wallet_creation_test.dart @@ -3,10 +3,7 @@ import 'package:test/test.dart'; import 'test_constants.dart'; -Wallet _createWallet( - Descriptor descriptor, - Descriptor changeDescriptor, -) { +Wallet _createWallet(Descriptor descriptor, Descriptor changeDescriptor) { final persister = Persister.newInMemory(); return Wallet( descriptor, @@ -77,10 +74,8 @@ void main() { test('fails for private multipath descriptor', () { expect( - () => buildDescriptor( - privateMultipathDescriptorString, - Network.testnet, - ), + () => + buildDescriptor(privateMultipathDescriptorString, Network.testnet), throwsA(isA()), ); }); From 9557694090adf829eb25561d76b428ea133b7dfe Mon Sep 17 00:00:00 2001 From: Aniket-pd Date: Wed, 7 Jan 2026 16:34:05 +0530 Subject: [PATCH 3/3] Reformat code for improved readability Adjusted indentation in main.dart and test_constants.dart to enhance code clarity and maintain consistent formatting. No functional changes were made. --- bdk_demo/lib/main.dart | 14 +++++++------- test/test_constants.dart | 18 +++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/bdk_demo/lib/main.dart b/bdk_demo/lib/main.dart index bac91e6..d3e9f60 100644 --- a/bdk_demo/lib/main.dart +++ b/bdk_demo/lib/main.dart @@ -101,14 +101,14 @@ class _MyHomePageState extends State { _error != null ? Icons.error_outline : _networkName != null - ? Icons.check_circle - : Icons.network_check, + ? Icons.check_circle + : Icons.network_check, size: 80, color: _error != null ? Colors.red : _networkName != null - ? Colors.green - : Colors.grey, + ? Colors.green + : Colors.grey, ), const SizedBox(height: 20), const Text('BDK bindings status', style: TextStyle(fontSize: 20)), @@ -116,9 +116,9 @@ class _MyHomePageState extends State { Text( 'Network: $_networkName', style: Theme.of(context).textTheme.headlineMedium?.copyWith( - color: Colors.orange, - fontWeight: FontWeight.bold, - ), + color: Colors.orange, + fontWeight: FontWeight.bold, + ), ), if (_descriptorSnippet != null) Padding( diff --git a/test/test_constants.dart b/test/test_constants.dart index 6663333..7fdfd71 100644 --- a/test/test_constants.dart +++ b/test/test_constants.dart @@ -50,18 +50,18 @@ Descriptor buildBip86ChangeDescriptor(Network network) => Descriptor("tr($testExtendedPrivKey/$bip86TestChangePath/*)", network); Descriptor buildMainnetBip84Descriptor() => Descriptor( - "wpkh($mainnetExtendedPrivKey/$bip84MainnetReceivePath/*)", - Network.bitcoin, -); + "wpkh($mainnetExtendedPrivKey/$bip84MainnetReceivePath/*)", + Network.bitcoin, + ); Descriptor buildMainnetBip86Descriptor() => Descriptor( - "tr($mainnetExtendedPrivKey/$bip86MainnetReceivePath/*)", - Network.bitcoin, -); + "tr($mainnetExtendedPrivKey/$bip86MainnetReceivePath/*)", + Network.bitcoin, + ); Descriptor buildNonExtendedDescriptor(int index) => Descriptor( - "wpkh($testExtendedPrivKey/$bip84TestReceivePath/$index)", - Network.testnet, -); + "wpkh($testExtendedPrivKey/$bip84TestReceivePath/$index)", + Network.testnet, + ); const defaultLookahead = 25;