Skip to content

Commit 771d7c1

Browse files
committed
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
1 parent c3a997c commit 771d7c1

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

crates/wallet/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ all-keys = ["keys-bip39"]
3535
keys-bip39 = ["bip39"]
3636
rusqlite = ["bdk_chain/rusqlite"]
3737
file_store = ["bdk_file_store"]
38+
test-utils = []
3839

3940
[dev-dependencies]
4041
lazy_static = "1.4"
4142
assert_matches = "1.5.0"
4243
tempfile = "3"
4344
bdk_chain = { path = "../chain", features = ["rusqlite"] }
44-
bdk_wallet = { path = ".", features = ["rusqlite", "file_store"] }
45+
bdk_wallet = { path = ".", features = ["rusqlite", "file_store", "test-utils"] }
4546
bdk_file_store = { path = "../file_store" }
4647
anyhow = "1"
4748
rand = "^0.8"

crates/wallet/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub extern crate serde_json;
2828
pub mod descriptor;
2929
pub mod keys;
3030
pub mod psbt;
31+
#[cfg(feature = "test-utils")]
32+
pub mod test_utils;
3133
mod types;
3234
mod wallet;
3335

crates/wallet/tests/common.rs renamed to crates/wallet/src/test_utils.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
#![allow(unused)]
2-
use bdk_chain::{tx_graph, BlockId, ChainPosition, ConfirmationBlockTime, TxGraph};
3-
use bdk_wallet::{CreateParams, KeychainKind, LocalOutput, Update, Wallet};
1+
//! `bdk_wallet` test utilities
2+
3+
use alloc::string::ToString;
4+
use core::str::FromStr;
5+
6+
use bdk_chain::{tx_graph, BlockId, ConfirmationBlockTime};
47
use bitcoin::{
58
hashes::Hash, transaction, Address, Amount, BlockHash, FeeRate, Network, OutPoint, Transaction,
69
TxIn, TxOut, Txid,
710
};
8-
use std::str::FromStr;
11+
12+
use crate::{KeychainKind, Update, Wallet};
913

1014
/// Return a fake wallet that appears to be funded for testing.
1115
///
@@ -128,69 +132,81 @@ pub fn get_funded_wallet(descriptor: &str) -> (Wallet, bitcoin::Txid) {
128132
get_funded_wallet_with_change(descriptor, change)
129133
}
130134

135+
/// Get funded segwit wallet
131136
pub fn get_funded_wallet_wpkh() -> (Wallet, bitcoin::Txid) {
132137
get_funded_wallet_with_change(get_test_wpkh(), get_test_wpkh_change())
133138
}
134139

140+
/// `wpkh` single key descriptor
135141
pub fn get_test_wpkh() -> &'static str {
136142
"wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)"
137143
}
138144

145+
/// `wpkh` descriptor and change descriptor
139146
pub fn get_test_wpkh_with_change_desc() -> (&'static str, &'static str) {
140147
(
141148
"wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)",
142149
get_test_wpkh_change(),
143150
)
144151
}
145152

153+
/// `wpkh` change descriptor
146154
fn get_test_wpkh_change() -> &'static str {
147155
"wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/0)"
148156
}
149157

158+
/// `wsh` descriptor with policy `and(pk(A),older(6))`
150159
pub fn get_test_single_sig_csv() -> &'static str {
151-
// and(pk(Alice),older(6))
152160
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),older(6)))"
153161
}
154162

163+
/// `wsh` descriptor with policy `or(pk(A),and(pk(B),older(144)))`
155164
pub fn get_test_a_or_b_plus_csv() -> &'static str {
156-
// or(pk(Alice),and(pk(Bob),older(144)))
157165
"wsh(or_d(pk(cRjo6jqfVNP33HhSS76UhXETZsGTZYx8FMFvR9kpbtCSV1PmdZdu),and_v(v:pk(cMnkdebixpXMPfkcNEjjGin7s94hiehAH4mLbYkZoh9KSiNNmqC8),older(144))))"
158166
}
159167

168+
/// `wsh` descriptor with policy `and(pk(A),after(100000))`
160169
pub fn get_test_single_sig_cltv() -> &'static str {
161-
// and(pk(Alice),after(100000))
162170
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(100000)))"
163171
}
164172

173+
/// taproot single key descriptor
165174
pub fn get_test_tr_single_sig() -> &'static str {
166175
"tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG)"
167176
}
168177

178+
/// taproot descriptor with taptree
169179
pub fn get_test_tr_with_taptree() -> &'static str {
170180
"tr(b511bd5771e47ee27558b1765e87b541668304ec567721c7b880edc0a010da55,{pk(cPZzKuNmpuUjD1e8jUU4PVzy2b5LngbSip8mBsxf4e7rSFZVb4Uh),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})"
171181
}
172182

183+
/// taproot descriptor with private key taptree
173184
pub fn get_test_tr_with_taptree_both_priv() -> &'static str {
174185
"tr(b511bd5771e47ee27558b1765e87b541668304ec567721c7b880edc0a010da55,{pk(cPZzKuNmpuUjD1e8jUU4PVzy2b5LngbSip8mBsxf4e7rSFZVb4Uh),pk(cNaQCDwmmh4dS9LzCgVtyy1e1xjCJ21GUDHe9K98nzb689JvinGV)})"
175186
}
176187

188+
/// taproot descriptor where one key appears in two script paths
177189
pub fn get_test_tr_repeated_key() -> &'static str {
178190
"tr(b511bd5771e47ee27558b1765e87b541668304ec567721c7b880edc0a010da55,{and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(100)),and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(200))})"
179191
}
180192

193+
/// taproot xpriv descriptor
181194
pub fn get_test_tr_single_sig_xprv() -> &'static str {
182195
"tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/*)"
183196
}
184197

198+
/// taproot xpriv and change descriptor
185199
pub fn get_test_tr_single_sig_xprv_with_change_desc() -> (&'static str, &'static str) {
186200
("tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/0/*)",
187201
"tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/1/*)")
188202
}
189203

204+
/// taproot descriptor with taptree
190205
pub fn get_test_tr_with_taptree_xprv() -> &'static str {
191206
"tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG,{pk(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/*),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})"
192207
}
193208

209+
/// taproot desc with duplicate script paths
194210
pub fn get_test_tr_dup_keys() -> &'static str {
195211
"tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG,{pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})"
196212
}

crates/wallet/tests/psbt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use bdk_wallet::bitcoin::{Amount, FeeRate, Psbt, TxIn};
2+
use bdk_wallet::test_utils::*;
23
use bdk_wallet::{psbt, KeychainKind, SignOptions};
34
use core::str::FromStr;
4-
mod common;
5-
use common::*;
65

76
// from bip 174
87
const PSBT_STR: &str = "cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIAAAA";

crates/wallet/tests/wallet.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use bdk_wallet::descriptor::{calc_checksum, DescriptorError, IntoWalletDescripto
1212
use bdk_wallet::error::CreateTxError;
1313
use bdk_wallet::psbt::PsbtUtils;
1414
use bdk_wallet::signer::{SignOptions, SignerError};
15+
use bdk_wallet::test_utils::*;
1516
use bdk_wallet::tx_builder::AddForeignUtxoError;
1617
use bdk_wallet::{AddressInfo, Balance, ChangeSet, Wallet, WalletPersister, WalletTx};
1718
use bdk_wallet::{KeychainKind, LoadError, LoadMismatch, LoadWithPersistError};
@@ -30,9 +31,6 @@ use miniscript::{descriptor::KeyMap, Descriptor, DescriptorPublicKey};
3031
use rand::rngs::StdRng;
3132
use rand::SeedableRng;
3233

33-
mod common;
34-
use common::*;
35-
3634
fn receive_output(
3735
wallet: &mut Wallet,
3836
value: u64,

0 commit comments

Comments
 (0)