Skip to content

Commit

Permalink
fix(wallet)!: Change method LoadParams::descriptors
Browse files Browse the repository at this point in the history
to just `descriptor` that takes a `KeychainKind` and
`D: IntoWalletDescriptor` and parses the given descriptor and
checks it matches the one loaded. This can be called multiple
times for different keychains.
  • Loading branch information
ValuedMammal committed Aug 9, 2024
1 parent b7ca626 commit 00d4c1f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 13 deletions.
3 changes: 2 additions & 1 deletion crates/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ let network = Network::Testnet;
let descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/0/*)";
let change_descriptor = "wpkh(tprv8ZgxMBicQKsPdcAqYBpzAFwU5yxBUo88ggoBqu1qPcHUfSbKK1sKMLmC7EAk438btHQrSdu3jGGQa6PA71nvH5nkDexhLteJqkM4dQmWF9g/84'/1'/0'/1/*)";
let wallet_opt = Wallet::load()
.descriptors(descriptor, change_descriptor)
.descriptor(KeychainKind::External, descriptor)
.descriptor(KeychainKind::Internal, change_descriptor)
.network(network)
.load_wallet(&mut db)
.expect("wallet");
Expand Down
3 changes: 2 additions & 1 deletion crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ impl Wallet {
/// let mut conn = bdk_wallet::rusqlite::Connection::open(file_path)?;
/// let mut wallet = Wallet::load()
/// // check loaded descriptors matches these values and extract private keys
/// .descriptors(EXTERNAL_DESC, INTERNAL_DESC)
/// .descriptor(KeychainKind::External, EXTERNAL_DESC)
/// .descriptor(KeychainKind::Internal, INTERNAL_DESC)
/// // you can also manually add private keys
/// .keymap(KeychainKind::External, external_keymap)
/// .keymap(KeychainKind::Internal, internal_keymap)
Expand Down
12 changes: 9 additions & 3 deletions crates/wallet/src/wallet/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,18 @@ impl LoadParams {

/// Checks that `descriptor` of `keychain` matches this, and extracts private keys (if
/// available).
pub fn descriptors<D>(mut self, descriptor: D, change_descriptor: D) -> Self
pub fn descriptor<D>(mut self, keychain: KeychainKind, descriptor: D) -> Self
where
D: IntoWalletDescriptor + 'static,
{
self.check_descriptor = Some(make_descriptor_to_extract(descriptor));
self.check_change_descriptor = Some(make_descriptor_to_extract(change_descriptor));
match keychain {
KeychainKind::External => {
self.check_descriptor = Some(make_descriptor_to_extract(descriptor))
}
KeychainKind::Internal => {
self.check_change_descriptor = Some(make_descriptor_to_extract(descriptor))
}
}
self
}

Expand Down
7 changes: 4 additions & 3 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ fn wallet_is_persisted() -> anyhow::Result<()> {
{
let mut db = open_db(&file_path).context("failed to recover db")?;
let wallet = Wallet::load()
.descriptors(external_desc, internal_desc)
.descriptor(KeychainKind::External, external_desc)
.descriptor(KeychainKind::Internal, internal_desc)
.network(Network::Testnet)
.load_wallet(&mut db)?
.expect("wallet must exist");
Expand Down Expand Up @@ -240,7 +241,7 @@ fn wallet_load_checks() -> anyhow::Result<()> {
);
assert_matches!(
Wallet::load()
.descriptors(internal_desc, external_desc)
.descriptor(KeychainKind::External, internal_desc)
.load_wallet(&mut open_db(&file_path)?),
Err(LoadWithPersistError::InvalidChangeSet(LoadError::Mismatch(
LoadMismatch::Descriptor { .. }
Expand Down Expand Up @@ -287,7 +288,7 @@ fn single_descriptor_wallet_persist_and_recover() {
let secp = wallet.secp_ctx();
let (_, keymap) = <Descriptor<DescriptorPublicKey>>::parse_descriptor(secp, desc).unwrap();
let wallet = LoadParams::new()
.keymap(KeychainKind::External, keymap.clone())
.descriptor(KeychainKind::External, desc)
.load_wallet(&mut db)
.unwrap()
.expect("must have loaded changeset");
Expand Down
3 changes: 2 additions & 1 deletion example-crates/wallet_electrum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fn main() -> Result<(), anyhow::Error> {
let mut db = Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), db_path)?;

let wallet_opt = Wallet::load()
.descriptors(EXTERNAL_DESC, INTERNAL_DESC)
.descriptor(KeychainKind::External, EXTERNAL_DESC)
.descriptor(KeychainKind::Internal, INTERNAL_DESC)
.network(NETWORK)
.load_wallet(&mut db)?;
let mut wallet = match wallet_opt {
Expand Down
3 changes: 2 additions & 1 deletion example-crates/wallet_esplora_async/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ async fn main() -> Result<(), anyhow::Error> {
let mut conn = Connection::open(DB_PATH)?;

let wallet_opt = Wallet::load()
.descriptors(EXTERNAL_DESC, INTERNAL_DESC)
.descriptor(KeychainKind::External, EXTERNAL_DESC)
.descriptor(KeychainKind::Internal, INTERNAL_DESC)
.network(NETWORK)
.load_wallet(&mut conn)?;
let mut wallet = match wallet_opt {
Expand Down
3 changes: 2 additions & 1 deletion example-crates/wallet_esplora_blocking/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ fn main() -> Result<(), anyhow::Error> {
let mut db = Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), DB_PATH)?;

let wallet_opt = Wallet::load()
.descriptors(EXTERNAL_DESC, INTERNAL_DESC)
.descriptor(KeychainKind::External, EXTERNAL_DESC)
.descriptor(KeychainKind::Internal, INTERNAL_DESC)
.network(NETWORK)
.load_wallet(&mut db)?;
let mut wallet = match wallet_opt {
Expand Down
5 changes: 3 additions & 2 deletions example-crates/wallet_rpc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bdk_bitcoind_rpc::{
use bdk_wallet::{
bitcoin::{Block, Network, Transaction},
file_store::Store,
Wallet,
KeychainKind, Wallet,
};
use clap::{self, Parser};
use std::{path::PathBuf, sync::mpsc::sync_channel, thread::spawn, time::Instant};
Expand Down Expand Up @@ -89,7 +89,8 @@ fn main() -> anyhow::Result<()> {
let mut db =
Store::<bdk_wallet::ChangeSet>::open_or_create_new(DB_MAGIC.as_bytes(), args.db_path)?;
let wallet_opt = Wallet::load()
.descriptors(args.descriptor.clone(), args.change_descriptor.clone())
.descriptor(KeychainKind::External, args.descriptor.clone())
.descriptor(KeychainKind::Internal, args.change_descriptor.clone())
.network(args.network)
.load_wallet(&mut db)?;
let mut wallet = match wallet_opt {
Expand Down

0 comments on commit 00d4c1f

Please sign in to comment.