Skip to content

Commit c9610e7

Browse files
docs: changes to examples
1 parent 2c8986c commit c9610e7

16 files changed

Lines changed: 1170 additions & 424 deletions

Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,27 @@ name = "esplora_blocking"
7474

7575
[[example]]
7676
name = "bitcoind_rpc"
77+
78+
[[example]]
79+
name = "simple_keyring"
80+
required-features = ["rusqlite"]
81+
82+
[[example]]
83+
name = "multi_keychain_address_generation"
84+
path = "examples/multi_keychain/address_generation.rs"
85+
86+
[[example]]
87+
name = "multi_keychain_wallet"
88+
path = "examples/multi_keychain/wallet.rs"
89+
90+
[[example]]
91+
name = "multi_keychain_balance_tracking"
92+
path = "examples/multi_keychain/balance_tracking.rs"
93+
94+
[[example]]
95+
name = "multi_keychain_migration_guide"
96+
path = "examples/multi_keychain/migration_guide.rs"
97+
98+
[[example]]
99+
name = "multi_keychain_persistence"
100+
path = "examples/multi_keychain/persistence.rs"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ To persist `Wallet` state use a data storage crate that reads and writes [`Chang
7575
* [`bdk_file_store`]: Stores wallet changes in a simple flat file.
7676
* `rusqlite`: Stores wallet changes in a SQLite database.
7777

78-
**Example**
78+
<!-- **Example**
7979
8080
```rust,no_run
8181
use bdk_wallet::rusqlite;
@@ -110,7 +110,7 @@ wallet.persist(&mut conn)?;
110110
111111
println!("Next receive address: {}", address_info.address);
112112
Ok::<_, anyhow::Error>(())
113-
```
113+
``` -->
114114

115115
## Minimum Supported Rust Version (MSRV)
116116

examples/bitcoind_rpc.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
#![allow(unused)]
12
use bdk_bitcoind_rpc::{
23
bitcoincore_rpc::{Auth, Client, RpcApi},
34
Emitter, MempoolEvent,
45
};
56
use bdk_wallet::rusqlite::Connection;
67
use bdk_wallet::{
78
bitcoin::{Block, Network},
8-
KeychainKind, Wallet,
9+
keyring::KeyRing,
10+
KeychainKind, LoadParams, PersistedWallet, Wallet,
911
};
1012
use clap::{self, Parser};
1113
use std::{
@@ -94,29 +96,44 @@ fn main() -> anyhow::Result<()> {
9496

9597
let start_load_wallet = Instant::now();
9698
let mut db = Connection::open(args.db_path)?;
97-
let wallet_opt = Wallet::load()
98-
.descriptor(KeychainKind::External, Some(args.descriptor.clone()))
99-
.descriptor(KeychainKind::Internal, args.change_descriptor.clone())
100-
.extract_keys()
101-
.check_network(args.network)
102-
.load_wallet(&mut db)?;
103-
let mut wallet = match wallet_opt {
99+
100+
let mut params = LoadParams::new()
101+
.check_default(KeychainKind::External)
102+
.check_descriptor(KeychainKind::External, Some(args.descriptor.clone()))
103+
.check_genesis_hash(bitcoin::constants::genesis_block(args.network).block_hash())
104+
.check_network(args.network);
105+
106+
if let Some(desc) = &args.change_descriptor {
107+
params = params.check_descriptor(KeychainKind::Internal, Some(desc.clone()));
108+
}
109+
110+
let mut wallet = match params.load_wallet(&mut db).unwrap() {
104111
Some(wallet) => wallet,
105-
None => match &args.change_descriptor {
106-
Some(change_desc) => Wallet::create(args.descriptor.clone(), change_desc.clone())
107-
.network(args.network)
108-
.create_wallet(&mut db)?,
109-
None => Wallet::create_single(args.descriptor.clone())
110-
.network(args.network)
111-
.create_wallet(&mut db)?,
112-
},
112+
None => {
113+
let mut keyring: KeyRing<KeychainKind> = KeyRing::new(
114+
args.network,
115+
KeychainKind::External,
116+
args.descriptor.clone(),
117+
)
118+
.unwrap();
119+
if let Some(desc) = &args.change_descriptor {
120+
keyring
121+
.add_descriptor(KeychainKind::Internal, desc.clone(), false)
122+
.unwrap();
123+
}
124+
Wallet::create(keyring).create_wallet(&mut db)?
125+
}
113126
};
127+
114128
println!(
115129
"Loaded wallet in {}s",
116130
start_load_wallet.elapsed().as_secs_f32()
117131
);
118132

119-
let address = wallet.reveal_next_address(KeychainKind::External).address;
133+
let address = wallet
134+
.next_unused_address(KeychainKind::External)
135+
.unwrap()
136+
.address;
120137
println!("Wallet address: {address}");
121138

122139
let balance = wallet.balance();

examples/compiler.rs

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
99
// You may not use this file except in accordance with one or both of these
1010
// licenses.
11-
11+
#![allow(unused)]
1212
extern crate bdk_wallet;
13-
extern crate bitcoin;
14-
extern crate miniscript;
15-
extern crate serde_json;
13+
// extern crate bitcoin;
14+
// extern crate miniscript;
15+
// extern crate serde_json;
1616

1717
use std::error::Error;
1818
use std::str::FromStr;
@@ -32,47 +32,52 @@ use bdk_wallet::{KeychainKind, Wallet};
3232
/// This example demonstrates the interaction between a bdk wallet and miniscript policy.
3333
#[allow(clippy::print_stdout)]
3434
fn main() -> Result<(), Box<dyn Error>> {
35-
// We start with a miniscript policy string
36-
let policy_str = "or(
37-
10@thresh(4,
38-
pk(029ffbe722b147f3035c87cb1c60b9a5947dd49c774cc31e94773478711a929ac0),pk(025f05815e3a1a8a83bfbb03ce016c9a2ee31066b98f567f6227df1d76ec4bd143),pk(025625f41e4a065efc06d5019cbbd56fe8c07595af1231e7cbc03fafb87ebb71ec),pk(02a27c8b850a00f67da3499b60562673dcf5fdfb82b7e17652a7ac54416812aefd),pk(03e618ec5f384d6e19ca9ebdb8e2119e5bef978285076828ce054e55c4daf473e2)
39-
),1@and(
40-
older(4209713),
41-
thresh(2,
42-
pk(03deae92101c790b12653231439f27b8897264125ecb2f46f48278603102573165),pk(033841045a531e1adf9910a6ec279589a90b3b8a904ee64ffd692bd08a8996c1aa),pk(02aebf2d10b040eb936a6f02f44ee82f8b34f5c1ccb20ff3949c2b28206b7c1068)
43-
)
44-
)
45-
)"
46-
.replace(&[' ', '\n', '\t'][..], "");
35+
// // We start with a miniscript policy string
36+
// let policy_str = "or(
37+
// 10@thresh(4,
38+
// pk(029ffbe722b147f3035c87cb1c60b9a5947dd49c774cc31e94773478711a929ac0),
39+
// pk(025f05815e3a1a8a83bfbb03ce016c9a2ee31066b98f567f6227df1d76ec4bd143),
40+
// pk(025625f41e4a065efc06d5019cbbd56fe8c07595af1231e7cbc03fafb87ebb71ec),
41+
// pk(02a27c8b850a00f67da3499b60562673dcf5fdfb82b7e17652a7ac54416812aefd),
42+
// pk(03e618ec5f384d6e19ca9ebdb8e2119e5bef978285076828ce054e55c4daf473e2) ),1@and(
43+
// older(4209713),
44+
// thresh(2,
45+
//
46+
// pk(03deae92101c790b12653231439f27b8897264125ecb2f46f48278603102573165),
47+
// pk(033841045a531e1adf9910a6ec279589a90b3b8a904ee64ffd692bd08a8996c1aa),
48+
// pk(02aebf2d10b040eb936a6f02f44ee82f8b34f5c1ccb20ff3949c2b28206b7c1068) )
49+
// )
50+
// )"
51+
// .replace(&[' ', '\n', '\t'][..], "");
4752

48-
println!("Compiling policy: \n{policy_str}");
53+
// println!("Compiling policy: \n{policy_str}");
4954

50-
// Parse the string as a [`Concrete`] type miniscript policy.
51-
let policy = Concrete::<String>::from_str(&policy_str)?;
55+
// // Parse the string as a [`Concrete`] type miniscript policy.
56+
// let policy = Concrete::<String>::from_str(&policy_str)?;
5257

53-
// Create a `wsh` type descriptor from the policy.
54-
// `policy.compile()` returns the resulting miniscript from the policy.
55-
let descriptor = Descriptor::new_wsh(policy.compile()?)?.to_string();
58+
// // Create a `wsh` type descriptor from the policy.
59+
// // `policy.compile()` returns the resulting miniscript from the policy.
60+
// let descriptor = Descriptor::new_wsh(policy.compile()?)?.to_string();
5661

57-
println!("Compiled into Descriptor: \n{descriptor}");
62+
// println!("Compiled into Descriptor: \n{descriptor}");
5863

59-
// Create a new wallet from descriptors
60-
let mut wallet = Wallet::create_single(descriptor)
61-
.network(Network::Regtest)
62-
.create_wallet_no_persist()?;
64+
// // Create a new wallet from descriptors
65+
// let mut wallet = Wallet::create_single(descriptor)
66+
// .network(Network::Regtest)
67+
// .create_wallet_no_persist()?;
6368

64-
println!(
65-
"First derived address from the descriptor: \n{}",
66-
wallet.next_unused_address(KeychainKind::External),
67-
);
69+
// println!(
70+
// "First derived address from the descriptor: \n{}",
71+
// wallet.next_unused_address(KeychainKind::External),
72+
// );
6873

69-
// BDK also has it's own `Policy` structure to represent the spending condition in a more
70-
// human readable json format.
71-
let spending_policy = wallet.policies(KeychainKind::External)?;
72-
println!(
73-
"The BDK spending policy: \n{}",
74-
serde_json::to_string_pretty(&spending_policy)?
75-
);
74+
// // BDK also has it's own `Policy` structure to represent the spending condition in a more
75+
// // human readable json format.
76+
// let spending_policy = wallet.policies(KeychainKind::External)?;
77+
// println!(
78+
// "The BDK spending policy: \n{}",
79+
// serde_json::to_string_pretty(&spending_policy)?
80+
// );
7681

7782
Ok(())
7883
}

0 commit comments

Comments
 (0)