Skip to content

Commit c4dbe42

Browse files
committed
feat(wallet): add LoadParams::two_path_descriptor method
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
1 parent cb2c7ed commit c4dbe42

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/test_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ pub fn get_test_wpkh_and_change_desc() -> (&'static str, &'static str) {
147147
"wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)")
148148
}
149149

150+
/// `wpkh` two-path descriptor
151+
pub fn get_test_two_path_wpkh() -> &'static str {
152+
"wpkh(tpubDDks68wKK1xKaVVVbNmXUAx68K1K817M6KwjvjEyCrjdU7xMvjKnfYAtZjfZcrfPfGFzqmibuVqMzKJGbBnK7mo7WSJri8Y9QgM7aNQ3fCp/<0;1>/*)"
153+
}
154+
150155
/// `wsh` descriptor with policy `and(pk(A),older(6))`
151156
pub fn get_test_single_sig_csv() -> &'static str {
152157
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),older(6)))"

src/wallet/params.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,28 @@ impl LoadParams {
270270
self
271271
}
272272

273+
/// Checks that the provided two-path descriptor matches exactly what is loaded for both the
274+
/// external and internal keychains.
275+
///
276+
/// # Note
277+
///
278+
/// You must also specify [`extract_keys`](Self::extract_keys) if you wish to add a signer
279+
/// for an expected descriptor containing secrets.
280+
pub fn two_path_descriptor<D>(mut self, expected_descriptor: D) -> Self
281+
where
282+
D: IntoWalletDescriptor + Send + Clone + 'static,
283+
{
284+
let external: DescriptorToExtract =
285+
make_two_path_descriptor_to_extract(expected_descriptor.clone(), 0);
286+
let internal: DescriptorToExtract =
287+
make_two_path_descriptor_to_extract(expected_descriptor, 1);
288+
289+
self.check_descriptor = Some(Some(external));
290+
self.check_change_descriptor = Some(Some(internal));
291+
292+
self
293+
}
294+
273295
/// Checks that the given network matches the one loaded from persistence.
274296
pub fn check_network(mut self, network: Network) -> Self {
275297
self.check_network = Some(network);

tests/persisted_wallet.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,31 @@ fn single_descriptor_wallet_persist_and_recover() {
427427
);
428428
}
429429

430+
#[test]
431+
fn two_path_descriptor_wallet_persist_and_recover() {
432+
use bdk_chain::rusqlite;
433+
434+
let temp_dir = tempfile::tempdir().unwrap();
435+
let db_path = temp_dir.path().join("wallet.db");
436+
let mut db = rusqlite::Connection::open(db_path).unwrap();
437+
438+
let two_path_descriptor = get_test_two_path_wpkh();
439+
let mut wallet = Wallet::create_from_two_path_descriptor(two_path_descriptor)
440+
.network(Network::Testnet4)
441+
.create_wallet(&mut db)
442+
.unwrap();
443+
let _ = wallet.reveal_addresses_to(KeychainKind::External, 2);
444+
assert!(wallet.persist(&mut db).unwrap());
445+
446+
let loaded = Wallet::load()
447+
.two_path_descriptor(two_path_descriptor)
448+
.check_network(Network::Testnet4)
449+
.load_wallet(&mut db)
450+
.unwrap()
451+
.expect("wallet must exist");
452+
assert_eq!(loaded.derivation_index(KeychainKind::External), Some(2));
453+
}
454+
430455
#[test]
431456
fn wallet_changeset_is_persisted() {
432457
persist_wallet_changeset("store.db", |path| {

0 commit comments

Comments
 (0)