From 823bb39fc1682b42b0e4a33d6a9030f82668656a Mon Sep 17 00:00:00 2001 From: valued mammal Date: Thu, 31 Oct 2024 16:41:41 -0400 Subject: [PATCH] feat(wallet): add module `test_utils` The common test utils are moved to a public `test_utils` module behind a new test-utils feature flag --- crates/wallet/Cargo.toml | 3 +- crates/wallet/src/lib.rs | 2 ++ .../{tests/common.rs => src/test_utils.rs} | 30 ++++++++++++++----- crates/wallet/tests/psbt.rs | 3 +- crates/wallet/tests/wallet.rs | 4 +-- 5 files changed, 29 insertions(+), 13 deletions(-) rename crates/wallet/{tests/common.rs => src/test_utils.rs} (90%) diff --git a/crates/wallet/Cargo.toml b/crates/wallet/Cargo.toml index 18f72337..859c712a 100644 --- a/crates/wallet/Cargo.toml +++ b/crates/wallet/Cargo.toml @@ -35,13 +35,14 @@ all-keys = ["keys-bip39"] keys-bip39 = ["bip39"] rusqlite = ["bdk_chain/rusqlite"] file_store = ["bdk_file_store"] +test-utils = ["std"] [dev-dependencies] lazy_static = "1.4" assert_matches = "1.5.0" tempfile = "3" bdk_chain = { path = "../chain", features = ["rusqlite"] } -bdk_wallet = { path = ".", features = ["rusqlite", "file_store"] } +bdk_wallet = { path = ".", features = ["rusqlite", "file_store", "test-utils"] } bdk_file_store = { path = "../file_store" } anyhow = "1" rand = "^0.8" diff --git a/crates/wallet/src/lib.rs b/crates/wallet/src/lib.rs index 40167a39..d17cc468 100644 --- a/crates/wallet/src/lib.rs +++ b/crates/wallet/src/lib.rs @@ -28,6 +28,8 @@ pub extern crate serde_json; pub mod descriptor; pub mod keys; pub mod psbt; +#[cfg(feature = "test-utils")] +pub mod test_utils; mod types; mod wallet; diff --git a/crates/wallet/tests/common.rs b/crates/wallet/src/test_utils.rs similarity index 90% rename from crates/wallet/tests/common.rs rename to crates/wallet/src/test_utils.rs index 59acbb11..94a11fea 100644 --- a/crates/wallet/tests/common.rs +++ b/crates/wallet/src/test_utils.rs @@ -1,11 +1,15 @@ -#![allow(unused)] -use bdk_chain::{tx_graph, BlockId, ChainPosition, ConfirmationBlockTime, TxGraph}; -use bdk_wallet::{CreateParams, KeychainKind, LocalOutput, Update, Wallet}; +//! `bdk_wallet` test utilities + +use alloc::string::ToString; +use core::str::FromStr; + +use bdk_chain::{tx_graph, BlockId, ConfirmationBlockTime}; use bitcoin::{ hashes::Hash, transaction, Address, Amount, BlockHash, FeeRate, Network, OutPoint, Transaction, TxIn, TxOut, Txid, }; -use std::str::FromStr; + +use crate::{KeychainKind, Update, Wallet}; /// Return a fake wallet that appears to be funded for testing. /// @@ -128,14 +132,17 @@ pub fn get_funded_wallet(descriptor: &str) -> (Wallet, bitcoin::Txid) { get_funded_wallet_with_change(descriptor, change) } +/// Get funded segwit wallet pub fn get_funded_wallet_wpkh() -> (Wallet, bitcoin::Txid) { get_funded_wallet_with_change(get_test_wpkh(), get_test_wpkh_change()) } +/// `wpkh` single key descriptor pub fn get_test_wpkh() -> &'static str { "wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)" } +/// `wpkh` descriptor and change descriptor pub fn get_test_wpkh_with_change_desc() -> (&'static str, &'static str) { ( "wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)", @@ -143,54 +150,63 @@ pub fn get_test_wpkh_with_change_desc() -> (&'static str, &'static str) { ) } +/// `wpkh` change descriptor fn get_test_wpkh_change() -> &'static str { "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/0)" } +/// `wsh` descriptor with policy `and(pk(A),older(6))` pub fn get_test_single_sig_csv() -> &'static str { - // and(pk(Alice),older(6)) "wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),older(6)))" } +/// `wsh` descriptor with policy `or(pk(A),and(pk(B),older(144)))` pub fn get_test_a_or_b_plus_csv() -> &'static str { - // or(pk(Alice),and(pk(Bob),older(144))) "wsh(or_d(pk(cRjo6jqfVNP33HhSS76UhXETZsGTZYx8FMFvR9kpbtCSV1PmdZdu),and_v(v:pk(cMnkdebixpXMPfkcNEjjGin7s94hiehAH4mLbYkZoh9KSiNNmqC8),older(144))))" } +/// `wsh` descriptor with policy `and(pk(A),after(100000))` pub fn get_test_single_sig_cltv() -> &'static str { - // and(pk(Alice),after(100000)) "wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(100000)))" } +/// taproot single key descriptor pub fn get_test_tr_single_sig() -> &'static str { "tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG)" } +/// taproot descriptor with taptree pub fn get_test_tr_with_taptree() -> &'static str { "tr(b511bd5771e47ee27558b1765e87b541668304ec567721c7b880edc0a010da55,{pk(cPZzKuNmpuUjD1e8jUU4PVzy2b5LngbSip8mBsxf4e7rSFZVb4Uh),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})" } +/// taproot descriptor with private key taptree pub fn get_test_tr_with_taptree_both_priv() -> &'static str { "tr(b511bd5771e47ee27558b1765e87b541668304ec567721c7b880edc0a010da55,{pk(cPZzKuNmpuUjD1e8jUU4PVzy2b5LngbSip8mBsxf4e7rSFZVb4Uh),pk(cNaQCDwmmh4dS9LzCgVtyy1e1xjCJ21GUDHe9K98nzb689JvinGV)})" } +/// taproot descriptor where one key appears in two script paths pub fn get_test_tr_repeated_key() -> &'static str { "tr(b511bd5771e47ee27558b1765e87b541668304ec567721c7b880edc0a010da55,{and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(100)),and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(200))})" } +/// taproot xpriv descriptor pub fn get_test_tr_single_sig_xprv() -> &'static str { "tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/*)" } +/// taproot xpriv and change descriptor pub fn get_test_tr_single_sig_xprv_with_change_desc() -> (&'static str, &'static str) { ("tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/0/*)", "tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/1/*)") } +/// taproot descriptor with taptree pub fn get_test_tr_with_taptree_xprv() -> &'static str { "tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG,{pk(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/*),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})" } +/// taproot descriptor with duplicate script paths pub fn get_test_tr_dup_keys() -> &'static str { "tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG,{pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})" } diff --git a/crates/wallet/tests/psbt.rs b/crates/wallet/tests/psbt.rs index 155bb143..cbd097c8 100644 --- a/crates/wallet/tests/psbt.rs +++ b/crates/wallet/tests/psbt.rs @@ -1,8 +1,7 @@ use bdk_wallet::bitcoin::{Amount, FeeRate, Psbt, TxIn}; +use bdk_wallet::test_utils::*; use bdk_wallet::{psbt, KeychainKind, SignOptions}; use core::str::FromStr; -mod common; -use common::*; // from bip 174 const PSBT_STR: &str = "cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIAAAA"; diff --git a/crates/wallet/tests/wallet.rs b/crates/wallet/tests/wallet.rs index abf9a9c9..825dd930 100644 --- a/crates/wallet/tests/wallet.rs +++ b/crates/wallet/tests/wallet.rs @@ -12,6 +12,7 @@ use bdk_wallet::descriptor::{calc_checksum, DescriptorError, IntoWalletDescripto use bdk_wallet::error::CreateTxError; use bdk_wallet::psbt::PsbtUtils; use bdk_wallet::signer::{SignOptions, SignerError}; +use bdk_wallet::test_utils::*; use bdk_wallet::tx_builder::AddForeignUtxoError; use bdk_wallet::{AddressInfo, Balance, ChangeSet, Wallet, WalletPersister, WalletTx}; use bdk_wallet::{KeychainKind, LoadError, LoadMismatch, LoadWithPersistError}; @@ -30,9 +31,6 @@ use miniscript::{descriptor::KeyMap, Descriptor, DescriptorPublicKey}; use rand::rngs::StdRng; use rand::SeedableRng; -mod common; -use common::*; - fn receive_output( wallet: &mut Wallet, value: u64, -- 2.49.0