Skip to content

Commit 8a2217f

Browse files
temp 3: sync multiple keychains
1 parent 429ff42 commit 8a2217f

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
resolver = "2"
33
members = [
44
"wallet",
5+
"examples/example_n_keychains",
56
# "examples/example_wallet_electrum",
67
# "examples/example_wallet_esplora_blocking",
78
# "examples/example_wallet_esplora_async",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "example_wallet_esplora_blocking"
3+
version = "0.2.0"
4+
edition = "2021"
5+
publish = false
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
bdk_wallet = { path = "../../wallet" }
11+
bdk_esplora = { version = "0.20", features = ["blocking"] }
12+
anyhow = "1"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use bdk_esplora::{esplora_client, EsploraExt};
2+
use bdk_wallet::chain::DescriptorId;
3+
use bdk_wallet::{KeyRing, Wallet};
4+
use bdk_wallet::bitcoin::Network;
5+
use bdk_wallet::KeychainKind;
6+
7+
const ESPLORA_URL: &str = "http://signet.bitcoindevkit.net";
8+
const STOP_GAP: usize = 5;
9+
const PARALLEL_REQUESTS: usize = 5;
10+
11+
const EXTERNAL_DESC: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
12+
const OTHER_DESC_21: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/21/*)";
13+
const OTHER_DESC_31: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/31/*)";
14+
const OTHER_DESC_41: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/41/*)";
15+
16+
fn main() -> Result<(), anyhow::Error> {
17+
// Create a keyring with a single, default descriptor (aka the KeychainKind::External from the 1.2.0 API)
18+
let mut keyring = KeyRing::new(EXTERNAL_DESC, Network::Signet);
19+
20+
// Add 3 new custom descriptors
21+
keyring.add_other_descriptor(OTHER_DESC_21);
22+
keyring.add_other_descriptor(OTHER_DESC_31);
23+
keyring.add_other_descriptor(OTHER_DESC_41);
24+
25+
let keychain_ids: Vec<DescriptorId> = keyring.list_keychain_ids();
26+
println!("{:?}", keychain_ids);
27+
28+
// Create the wallet and peek addresses on each of the descriptors
29+
let mut wallet: Wallet = Wallet::new(keyring, Network::Signet).create_wallet_no_persist()?;
30+
let address_1 = wallet.peek_address(KeychainKind::Default, 0).unwrap();
31+
let address_2 = wallet.peek_address(KeychainKind::Other(keychain_ids[1]), 0).unwrap();
32+
let address_3 = wallet.peek_address(KeychainKind::Other(keychain_ids[2]), 0).unwrap();
33+
let address_4 = wallet.peek_address(KeychainKind::Other(keychain_ids[3]), 0).unwrap();
34+
35+
println!("Address 1 {:?} at index {:?} on keychain {:?}", address_1.address, address_1.index, address_1.keychain);
36+
println!("Address 2 {:?} at index {:?} on keychain {:?}", address_2.address, address_2.index, address_2.keychain);
37+
println!("Address 3 {:?} at index {:?} on keychain {:?}", address_3.address, address_3.index, address_3.keychain);
38+
println!("Address 4 {:?} at index {:?} on keychain {:?}", address_4.address, address_4.index, address_4.keychain);
39+
40+
let balance = wallet.balance();
41+
println!("Balance before sync {:?}", balance);
42+
43+
let client = esplora_client::Builder::new(ESPLORA_URL).build_blocking();
44+
let full_scan_request = wallet.start_full_scan().build();
45+
let update = client.full_scan(full_scan_request, STOP_GAP, PARALLEL_REQUESTS)?;
46+
wallet.apply_update(update)?;
47+
48+
let new_balance = wallet.balance();
49+
println!("Wallet balance after syncing: {}", new_balance.total());
50+
51+
Ok(())
52+
}

wallet/examples/n_keychains.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@ fn main() -> Result<(), anyhow::Error> {
3232
println!("Address 3 {:?} at index {:?} on keychain {:?}", address_3.address, address_3.index, address_3.keychain);
3333
println!("Address 4 {:?} at index {:?} on keychain {:?}", address_4.address, address_4.index, address_4.keychain);
3434

35+
let balance = wallet.balance();
36+
println!("Balance {:?}", balance);
37+
38+
let full_scan_request = wallet.start_full_scan().build();
39+
40+
3541
Ok(())
3642
}

wallet/src/wallet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ impl std::error::Error for ApplyBlockError {}
286286
pub type WalletTx<'a> = CanonicalTx<'a, Arc<Transaction>, ConfirmationBlockTime>;
287287

288288
impl Wallet {
289-
pub fn new(keychain_set: KeyRing) -> CreateParams {
290-
CreateParams::new_with_keychain_set(keychain_set)
289+
pub fn new(keychain_set: KeyRing, network: Network) -> CreateParams {
290+
CreateParams::new_with_keychain_set(keychain_set, network)
291291
}
292292

293293
/// Build a new single descriptor [`Wallet`].

wallet/src/wallet/params.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ impl CreateParams {
8383

8484
pub fn new_with_keychain_set(
8585
keychain_set: KeyRing,
86+
network: Network,
8687
) -> Self {
8788
Self {
8889
keychain_set,
89-
network: Network::Bitcoin,
90+
network,
9091
genesis_hash: None,
9192
lookahead: DEFAULT_LOOKAHEAD,
9293
}

0 commit comments

Comments
 (0)