Skip to content

Commit b681e6c

Browse files
committed
feat: replace network example with wallet bootstrap demo
1 parent d2bf08e commit b681e6c

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

examples/network_example.dart

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,71 @@
1+
import 'dart:io';
2+
13
import '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
310
void 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('\nExternal 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('\nWallet 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('\nSyncing 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

Comments
 (0)