1+ import 'dart:io' ;
2+
13import 'package:bdk_dart/bdk.dart' ;
24
5+ /// Run with: `dart run examples/network_example.dart`
6+ ///
7+ /// Prerequisites:
8+ /// * Generated bindings (`scripts/generate_bindings.sh` )
9+ /// * Native library (`libbdkffi.*` ) discoverable by the Dart process
310void main () {
4- // Create a NetworkExample instance
5- final networkHelper = NetworkExample ();
6-
7- // Get the Signet network
8- final signetNetwork = networkHelper.get_signet ();
9-
10- // Convert it to string - should print "Signet"
11- final networkName = networkHelper.network_name (signetNetwork);
12-
13- print ("Network: $networkName " );
14- }
11+ final network = Network .testnet;
12+
13+ // 1. Create fresh seed material.
14+ final mnemonic = Mnemonic (WordCount .words12);
15+ stdout.writeln ('Mnemonic: $mnemonic ' );
16+
17+ // 2. Turn the mnemonic into descriptor keys for external/change paths.
18+ final rootKey = DescriptorSecretKey (network, mnemonic, null );
19+ final externalDescriptor =
20+ Descriptor .newBip84 (rootKey, KeychainKind .external_, network);
21+ final changeDescriptor =
22+ Descriptor .newBip84 (rootKey, KeychainKind .internal, network);
23+
24+ stdout
25+ ..writeln ('\n External descriptor:\n $externalDescriptor ' )
26+ ..writeln ('Change descriptor:\n $changeDescriptor ' );
27+
28+ // 3. Spin up an in-memory wallet using the descriptors.
29+ final persister = Persister .newInMemory ();
30+ final wallet =
31+ Wallet (externalDescriptor, changeDescriptor, network, persister, 25 );
32+ stdout.writeln ('\n Wallet ready on ${wallet .network ()}' );
33+
34+ // 4. Hand out the next receive address and persist the staged change.
35+ final receive = wallet.revealNextAddress (KeychainKind .external_);
36+ stdout.writeln (
37+ 'Next receive address (#${receive .index }): ${receive .address .toString ()}' );
38+ final persisted = wallet.persist (persister);
39+ stdout.writeln ('Persisted staged wallet changes: $persisted ' );
40+
41+ // 5. Try a quick Electrum sync to fetch history/balances.
42+ try {
43+ stdout.writeln ('\n Syncing via Electrum (blockstream.info)…' );
44+ final client =
45+ ElectrumClient ('ssl://electrum.blockstream.info:60002' , null );
46+ final syncRequest = wallet.startSyncWithRevealedSpks ().build ();
47+ final update = client.sync_ (syncRequest, 100 , true );
48+
49+ wallet.applyUpdate (update);
50+ wallet.persist (persister);
51+
52+ final balance = wallet.balance ();
53+ stdout.writeln ('Confirmed balance: ${balance .confirmed .toSat ()} sats' );
54+ stdout.writeln ('Total balance: ${balance .total .toSat ()} sats' );
55+
56+ client.dispose ();
57+ } catch (error) {
58+ stdout.writeln (
59+ 'Electrum sync failed: $error \n '
60+ 'Ensure TLS-enabled Electrum access is available, or skip this step.' ,
61+ );
62+ }
63+
64+ // 6. Clean up FFI handles explicitly so long-lived examples don’t leak.
65+ wallet.dispose ();
66+ persister.dispose ();
67+ externalDescriptor.dispose ();
68+ changeDescriptor.dispose ();
69+ rootKey.dispose ();
70+ mnemonic.dispose ();
71+ }
0 commit comments