@@ -73,41 +73,43 @@ To persist `Wallet` state use a data storage crate that reads and writes [`Chang
73
73
** Implementations**
74
74
75
75
* [ ` bdk_file_store ` ] : Stores wallet changes in a simple flat file.
76
+ * ` rusqlite ` : Stores wallet changes in a SQLite database.
76
77
77
78
** Example**
78
79
79
80
``` rust,no_run
80
- use bdk_wallet::{bitcoin::Network, KeychainKind, ChangeSet, Wallet};
81
+ use bdk_wallet::rusqlite;
82
+ use bdk_wallet::{KeychainKind, Wallet};
81
83
82
- // Open or create a new file store for wallet data.
83
- let (mut db, _changeset) =
84
- bdk_file_store::Store::<ChangeSet>::load_or_create(b"magic_bytes", "/tmp/my_wallet.db")
85
- .expect("create store");
84
+ // Open or create a new SQLite database for wallet data.
85
+ let db_path = "my_wallet.sqlite";
86
+ let mut conn = rusqlite::Connection::open(db_path)?;
86
87
87
- // Create a wallet with initial wallet data read from the file store.
88
- let network = Network::Testnet;
88
+ let network = bitcoin::Network::Testnet;
89
89
let descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/0/*)";
90
90
let change_descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/1/*)";
91
- let wallet_opt = Wallet::load()
91
+
92
+ let mut wallet = match Wallet::load()
92
93
.descriptor(KeychainKind::External, Some(descriptor))
93
94
.descriptor(KeychainKind::Internal, Some(change_descriptor))
94
95
.extract_keys()
95
96
.check_network(network)
96
- .load_wallet(&mut db)
97
- .expect("wallet");
98
- let mut wallet = match wallet_opt {
97
+ .load_wallet(&mut conn)?
98
+ {
99
99
Some(wallet) => wallet,
100
100
None => Wallet::create(descriptor, change_descriptor)
101
101
.network(network)
102
- .create_wallet(&mut db)
103
- .expect("wallet"),
102
+ .create_wallet(&mut conn)?,
104
103
};
105
104
106
- // Get a new address to receive bitcoin.
107
- let receive_address = wallet.reveal_next_address(KeychainKind::External);
108
- // Persist staged wallet data changes to the file store.
109
- wallet.persist(&mut db).expect("persist");
110
- println!("Your new receive address is: {}", receive_address.address);
105
+ // Get a new address to receive bitcoin!
106
+ let address_info = wallet.reveal_next_address(KeychainKind::External);
107
+
108
+ // Persist new wallet state to database.
109
+ wallet.persist(&mut conn)?;
110
+
111
+ println!("Next receive address: {}", address_info.address);
112
+ Ok::<_, anyhow::Error>(())
111
113
```
112
114
113
115
## Minimum Supported Rust Version (MSRV)
@@ -134,8 +136,8 @@ just test
134
136
135
137
Licensed under either of
136
138
137
- * Apache License, Version 2.0, ([ LICENSE-APACHE] ( ../../ LICENSE-APACHE) or < https://www.apache.org/licenses/LICENSE-2.0 > )
138
- * MIT license ([ LICENSE-MIT] ( ../../ LICENSE-MIT) or < https://opensource.org/licenses/MIT > )
139
+ * Apache License, Version 2.0, ([ LICENSE-APACHE] ( LICENSE-APACHE ) or < https://www.apache.org/licenses/LICENSE-2.0 > )
140
+ * MIT license ([ LICENSE-MIT] ( LICENSE-MIT ) or < https://opensource.org/licenses/MIT > )
139
141
140
142
at your option.
141
143
@@ -146,13 +148,14 @@ submitted for inclusion in the work by you, as defined in the Apache-2.0
146
148
license, shall be dual licensed as above, without any additional terms or
147
149
conditions.
148
150
149
- [ `Wallet` ] : https://docs.rs/bdk_wallet/latest/bdk_wallet/wallet/struct.Wallet.html
151
+ [ `Wallet` ] : https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html
152
+ [ `ChangeSet` ] : https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.ChangeSet.html
150
153
[ `bdk` ] : https://github.com/bitcoindevkit/bdk
151
154
[ `bdk_wallet` ] : https://docs.rs/bdk_wallet/latest
152
155
[ `bdk_chain` ] : https://docs.rs/bdk_chain/latest
153
156
[ `bdk_file_store` ] : https://docs.rs/bdk_file_store/latest
154
157
[ `bdk_electrum` ] : https://docs.rs/bdk_electrum/latest
155
158
[ `bdk_esplora` ] : https://docs.rs/bdk_esplora/latest
156
159
[ `bdk_bitcoind_rpc` ] : https://docs.rs/bdk_bitcoind_rpc/latest
157
- [ `rust-bitcoin` ] : https://docs.rs/miniscript /latest/bitcoin/index.html
160
+ [ `rust-bitcoin` ] : https://docs.rs/bitcoin /latest/bitcoin/
158
161
[ `rust-miniscript` ] : https://docs.rs/miniscript/latest/miniscript/index.html
0 commit comments