license = "MIT OR Apache-2.0"
repository = "https://github.com/bitcoindevkit/bdk"
documentation = "https://docs.rs/bdk_sqlite"
-description = "A simple SQLite based implementation of PersistBackend for Bitcoin Dev Kit."
+description = "A simple SQLite relational database client for persisting bdk_chain data."
keywords = ["bitcoin", "persist", "persistence", "bdk", "sqlite"]
authors = ["Bitcoin Dev Kit Developers"]
readme = "README.md"
## Persistence
-To persist `Wallet` state data on disk use an implementation of the [`PersistBackend`] trait.
+To persist `Wallet` state data use a data store crate that reads and writes [`bdk_chain::CombinedChangeSet`].
**Implementations**
-* [`bdk_file_store`]: A simple flat-file implementation of [`PersistBackend`].
-* [`bdk_sqlite`]: A simple sqlite implementation of [`PersistBackend`].
+* [`bdk_file_store`]: Stores wallet changes in a simple flat file.
+* [`bdk_sqlite`]: Stores wallet changes in a SQLite relational database file.
**Example**
<!-- compile_fail because outpoint and txout are fake variables -->
-```rust,compile_fail
-use bdk_wallet::{bitcoin::Network, wallet::{ChangeSet, Wallet}};
+```rust,no_run
+use bdk_wallet::{bitcoin::Network, KeychainKind, wallet::{ChangeSet, Wallet}};
fn main() {
- // Create a new file `Store`.
- let mut db = bdk_file_store::Store::<ChangeSet>::open_or_create_new(b"magic_bytes", "path/to/my_wallet.db").expect("create store");
+ // Open or create a new file store for wallet data.
+ let mut db =
+ bdk_file_store::Store::<ChangeSet>::open_or_create_new(b"magic_bytes", "/tmp/my_wallet.db")
+ .expect("create store");
+ // Create a wallet with initial wallet data read from the file store.
let descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/0/*)";
let change_descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/1/*)";
- let changeset = db.load_changes().expect("changeset loaded");
- let mut wallet = Wallet::new_or_load(descriptor, change_descriptor, changeset, Network::Testnet).expect("create or load wallet");
-
- // Insert a single `TxOut` at `OutPoint` into the wallet.
- let _ = wallet.insert_txout(outpoint, txout);
- wallet.commit_to(&mut db).expect("must commit changes to database");
+ let changeset = db.aggregate_changesets().expect("changeset loaded");
+ let mut wallet =
+ Wallet::new_or_load(descriptor, change_descriptor, changeset, Network::Testnet)
+ .expect("create or load wallet");
+
+ // Get a new address to receive bitcoin.
+ let receive_address = wallet.reveal_next_address(KeychainKind::External);
+ // Persist staged wallet data changes to the file store.
+ let staged_changeset = wallet.take_staged();
+ if let Some(changeset) = staged_changeset {
+ db.append_changeset(&changeset)
+ .expect("must commit changes to database");
+ }
+ println!("Your new receive address is: {}", receive_address.address);
}
```
conditions.
[`Wallet`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/wallet/struct.Wallet.html
-[`PersistBackend`]: https://docs.rs/bdk_chain/latest/bdk_chain/persist/trait.PersistBackend.html
[`bdk_chain`]: https://docs.rs/bdk_chain/latest
[`bdk_file_store`]: https://docs.rs/bdk_file_store/latest
[`bdk_sqlite`]: https://docs.rs/bdk_sqlite/latest