Skip to content

Commit 419944b

Browse files
committed
feat: add code examples for persistence pages
1 parent 2199444 commit 419944b

File tree

13 files changed

+91
-39
lines changed

13 files changed

+91
-39
lines changed

docs/cookbook/persistence/flat-file.md

-3
This file was deleted.

docs/cookbook/persistence/memory.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@ See our page on the [difference between the full scan and sync operations](../sy
1212

1313
The in-memory wallet does _not_ require any additional dependencies beyond the `bdk_wallet` dependency:
1414

15-
```toml
16-
[dependencies]
17-
bdk_wallet = { version = "1.0.0" }
15+
```toml title="Cargo.toml"
16+
--8<-- "examples/rust/persistence/memory/Cargo.toml:deps"
1817
```
1918

2019
To create an in-memory wallet, simply call `create_wallet_no_persist()` on the `Wallet` builder:
2120

22-
```rust
23-
let mut wallet = Wallet::create(EXTERNAL_DESCRIPTOR, INTERNAL_DESCRIPTOR)
24-
.network(Network::Signet)
25-
.create_wallet_no_persist()
26-
.expect("valid wallet");
21+
```rust title="main.rs"
22+
--8<-- "examples/rust/persistence/memory/src/main.rs:create"
2723
```
2824

2925
<br>

docs/cookbook/persistence/sqlite.md

+10-12
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,26 @@ See our page on the [difference between the full scan and sync operations](../sy
1919

2020
The sqlite wallet does not require any additional dependencies above the `bdk_wallet` dependency:
2121

22-
```toml
23-
[dependencies]
24-
bdk_wallet = { version = "1.0.0", features = ["rusqlite"] }
22+
```toml title="Cargo.toml"
23+
--8<-- "examples/rust/persistence/sqlite/Cargo.toml:deps"
2524
```
2625

27-
To create a sqlite-based persisted wallet, simply call the `create_wallet()` with a valid db connection on the wallet builder:
26+
To load an existing sqlite-based persisted wallet use `Wallet::load()`. You may then optionally verify the loaded descriptors match what you expect. If the provided descriptors contain private keys you can also extract these keys into the wallets keystore. Private keys are never stored in the wallet database. You may also verify the wallet network during loading.
2827

2928
```rust
30-
use bdk_wallet::rusqlite::Connection;
29+
--8<-- "examples/rust/persistence/sqlite/src/main.rs:load"
30+
```
31+
32+
If during wallet loading no wallet database file is found you can create a sqlite-based persisted wallet with `Wallet::create()` with a valid db connection and other wallet builder parameters:
3133

32-
let mut conn = Connection::open(file_path)?;
33-
let wallet = Wallet::create(EXTERNAL_DESCRIPTOR, INTERNAL_DESCRIPTOR)
34-
.network(Network::Signet)
35-
.create_wallet(&mut conn)
36-
.expect("valid wallet and db connection");
34+
```rust
35+
--8<-- "examples/rust/persistence/sqlite/src/main.rs:create"
3736
```
3837

3938
After performing an operation that returns data that should be persisted, use the `persist()` method on the wallet:
4039

4140
```rust
42-
let address = wallet.next_unused_address(KeychainKind::External);
43-
wallet.persist(&mut conn)?;
41+
--8<-- "examples/rust/persistence/sqlite/src/main.rs:address"
4442
```
4543

4644
<br>

examples/justfile

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
@default:
22
just --list
33

4-
# build all rust examples
5-
build-rust:
6-
cd rust && cargo build
7-
8-
# format all rust examples
9-
fmt-rust:
10-
cd rust && cargo fmt
4+
# Rust examples: fmt, clippy check, and build
5+
rust:
6+
cd rust && cargo fmt && cargo clippy && cargo build

examples/rust/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[workspace]
22
resolver = "2"
33
members = [
4-
"descriptors", "full-wallet", "memory-wallet", "migrate-version", "quickstart", "seed-phrase",
5-
"syncing/electrum", "syncing/esplora", "syncing/kyoto", "syncing/rpc"
4+
"descriptors", "full-wallet", "migrate-version", "quickstart", "seed-phrase",
5+
"syncing/electrum", "syncing/esplora", "syncing/kyoto", "syncing/rpc",
6+
"persistence/memory", "persistence/sqlite"
67
]
78

89
[workspace.package]

examples/rust/memory-wallet/Cargo.toml examples/rust/persistence/memory/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name = "memory-wallet"
33
version = "0.1.0"
44
edition = "2021"
55

6-
[dependencies]
76
# --8<-- [start:deps]
8-
bdk_wallet = { version = "1.0.0", features = ["keys-bip39"] }
9-
# --8<-- [stop:deps]
10-
anyhow = "1"
7+
[dependencies]
8+
bdk_wallet = { version = "1.0.0" }
9+
# --8<-- [end:deps]
10+
anyhow = "1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "sqlite-wallet"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# --8<-- [start:deps]
7+
[dependencies]
8+
bdk_wallet = { version = "1.0.0", features = ["rusqlite"] }
9+
# --8<-- [end:deps]
10+
anyhow = "1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use bdk_wallet::bitcoin::Network;
2+
use bdk_wallet::rusqlite;
3+
use bdk_wallet::KeychainKind;
4+
use bdk_wallet::Wallet;
5+
6+
// --8<-- [start:descriptors]
7+
// const EXTERNAL_DESCRIPTOR: &str = "[your external descriptor here ...]";
8+
// const INTERNAL_DESCRIPTOR: &str = "[your internal descriptor here ...]";
9+
// Example private descriptors
10+
const EXTERNAL_DESCRIPTOR: &str = "tr(tprv8ZgxMBicQKsPdJuLWWArdBsWjqDA3W5WoREnfdgKEcCQB1FMKfSoaFz9JHZU71HwXAqTsjHripkLM62kUQar14SDD8brsmhFKqVUPXGrZLc/86'/1'/0'/0/*)#fv8tutn2";
11+
const INTERNAL_DESCRIPTOR: &str = "tr(tprv8ZgxMBicQKsPdJuLWWArdBsWjqDA3W5WoREnfdgKEcCQB1FMKfSoaFz9JHZU71HwXAqTsjHripkLM62kUQar14SDD8brsmhFKqVUPXGrZLc/86'/1'/0'/1/*)#ccz2p7rj";
12+
// --8<-- [end:descriptors]
13+
14+
fn main() -> Result<(), anyhow::Error> {
15+
// --8<-- [start:load]
16+
let network = Network::Signet;
17+
let file_path = "test_wallet.sqlite3";
18+
let mut conn = rusqlite::Connection::open(file_path)?;
19+
20+
let wallet_opt = Wallet::load()
21+
.descriptor(KeychainKind::External, Some(EXTERNAL_DESCRIPTOR))
22+
.descriptor(KeychainKind::Internal, Some(INTERNAL_DESCRIPTOR))
23+
.extract_keys() // only needed if using private key descriptors
24+
.check_network(network)
25+
.load_wallet(&mut conn)?;
26+
// --8<-- [end:load]
27+
28+
// --8<-- [start:create]
29+
let mut wallet = match wallet_opt {
30+
Some(wallet) => {
31+
println!("Loaded existing wallet database.");
32+
wallet
33+
}
34+
None => {
35+
println!("Creating new wallet database.");
36+
Wallet::create(EXTERNAL_DESCRIPTOR, INTERNAL_DESCRIPTOR)
37+
.network(network)
38+
.create_wallet(&mut conn)?
39+
}
40+
};
41+
// --8<-- [end:create]
42+
43+
// --8<-- [start:address]
44+
// Reveal a new address from your external keychain
45+
let address = wallet.reveal_next_address(KeychainKind::External);
46+
wallet.persist(&mut conn)?;
47+
// Only share new address with user after successfully persisting wallet
48+
println!("Wallet address[{}]: {}", address.index, address.address);
49+
// --8<-- [end:address]
50+
51+
Ok(())
52+
}

justfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
default:
22
just --list
33

4-
rust:
5-
cd examples/rust && cargo build
4+
# Build mkdocs
5+
build:
6+
mkdocs build
67

8+
# Serve mkdocs
79
serve:
810
mkdocs serve

mkdocs.yml

-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ nav:
9595
- Persistence:
9696
- In-Memory Wallet: cookbook/persistence/memory.md
9797
- SQLite Database: cookbook/persistence/sqlite.md
98-
- Flat File Persistence: cookbook/persistence/flat-file.md
9998
- Keys and Descriptors:
10099
- Creating Descriptors: cookbook/keys-descriptors/descriptors.md
101100
- Seed Phrase to Descriptors: cookbook/keys-descriptors/seed-phrase.md

0 commit comments

Comments
 (0)