2023-02-14 13:54:07 +11:00
|
|
|
#![allow(unused)]
|
2024-06-25 11:00:17 -04:00
|
|
|
use bdk_chain::{BlockId, ConfirmationTime, ConfirmationTimeHeightAnchor, TxGraph};
|
2024-06-28 09:07:36 -04:00
|
|
|
use bdk_wallet::{
|
|
|
|
wallet::{Update, Wallet},
|
|
|
|
KeychainKind, LocalOutput,
|
|
|
|
};
|
2023-10-16 19:51:53 +11:00
|
|
|
use bitcoin::{
|
2024-06-28 09:07:36 -04:00
|
|
|
hashes::Hash, transaction, Address, Amount, BlockHash, FeeRate, Network, OutPoint, Transaction,
|
|
|
|
TxIn, TxOut, Txid,
|
2023-10-16 19:51:53 +11:00
|
|
|
};
|
2023-07-26 19:46:40 -05:00
|
|
|
use std::str::FromStr;
|
2023-02-14 13:54:07 +11:00
|
|
|
|
2024-03-27 13:34:18 -04:00
|
|
|
/// Return a fake wallet that appears to be funded for testing.
|
|
|
|
///
|
|
|
|
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
|
|
|
|
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
|
|
|
|
/// sats are the transaction fee.
|
2024-03-26 21:57:10 -04:00
|
|
|
pub fn get_funded_wallet_with_change(descriptor: &str, change: &str) -> (Wallet, bitcoin::Txid) {
|
2024-06-01 00:06:20 -05:00
|
|
|
let mut wallet = Wallet::new(descriptor, change, Network::Regtest).unwrap();
|
2024-03-26 21:57:10 -04:00
|
|
|
let receive_address = wallet.peek_address(KeychainKind::External, 0).address;
|
2023-07-26 19:46:40 -05:00
|
|
|
let sendto_address = Address::from_str("bcrt1q3qtze4ys45tgdvguj66zrk4fu6hq3a3v9pfly5")
|
|
|
|
.expect("address")
|
|
|
|
.require_network(Network::Regtest)
|
|
|
|
.unwrap();
|
2023-02-14 13:54:07 +11:00
|
|
|
|
2023-07-26 19:46:40 -05:00
|
|
|
let tx0 = Transaction {
|
2023-10-16 19:51:53 +11:00
|
|
|
version: transaction::Version::ONE,
|
2023-07-19 15:27:48 +02:00
|
|
|
lock_time: bitcoin::absolute::LockTime::ZERO,
|
2023-07-26 19:46:40 -05:00
|
|
|
input: vec![TxIn {
|
|
|
|
previous_output: OutPoint {
|
|
|
|
txid: Txid::all_zeros(),
|
|
|
|
vout: 0,
|
|
|
|
},
|
|
|
|
script_sig: Default::default(),
|
|
|
|
sequence: Default::default(),
|
|
|
|
witness: Default::default(),
|
|
|
|
}],
|
2023-02-14 13:54:07 +11:00
|
|
|
output: vec![TxOut {
|
2023-10-16 19:51:53 +11:00
|
|
|
value: Amount::from_sat(76_000),
|
2024-03-26 21:57:10 -04:00
|
|
|
script_pubkey: receive_address.script_pubkey(),
|
2023-07-26 19:46:40 -05:00
|
|
|
}],
|
|
|
|
};
|
|
|
|
|
|
|
|
let tx1 = Transaction {
|
2023-10-16 19:51:53 +11:00
|
|
|
version: transaction::Version::ONE,
|
2023-07-26 19:46:40 -05:00
|
|
|
lock_time: bitcoin::absolute::LockTime::ZERO,
|
|
|
|
input: vec![TxIn {
|
|
|
|
previous_output: OutPoint {
|
deps(bdk): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
deps(chain): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
fix(chain): use `minimal_non_dust()` instead of `dust_value()`
fix(chain): use `compute_txid()` instead of `txid`
deps(testenv): bump `electrsd` to `0.28.0`
deps(electrum): bump `electrum-client` to `0.20.0`
fix(electrum): use `compute_txid()` instead of `txid`
deps(esplora): bump `esplora-client` to `0.8.0`
deps(bitcoind_rpc): bump `bitcoin` to `0.32.0`, `bitcoincore-rpc` to
`0.19.0`
fix(bitcoind_rpc): use `compute_txid()` instead of `txid`
fix(nursery/tmp_plan): use proper `sighash` errors, and fix the expected
`Signature` fields
fix(sqlite): use `compute_txid()` instead of `txid`
deps(hwi): bump `hwi` to `0.9.0`
deps(wallet): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
fix(wallet): use `compute_txid()` and `minimal_non_dust()`
- update to use `compute_txid()` instead of deprecated `txid()`
- update to use `minimal_non_dust()` instead of `dust_value()`
- remove unused `bitcoin::hex::FromHex`.
fix(wallet): uses `.into` conversion on `Network` for `NetworkKind`
- uses `.into()` when appropriate, otherwise use the explicit
`NetworkKind`, and it's `.is_mainnet()` method.
fix(wallet): add P2wpkh, Taproot, InputsIndex errors to `SignerError`
fix(wallet): fields on taproot, and ecdsa `Signature` structure
fix(wallet/wallet): convert `Weight` to `usize` for now
- converts the `bitcoin-units::Weight` type to `usize` with help of
`to_wu()` method.
- it should be updated/refactored in the future to handle the `Weight`
type throughout the code instead of current `usize`, only converting
it for now.
- allows the usage of deprecated `is_provably_unspendable()`, needs
further discussion if suggested `is_op_return` is suitable.
- update the expect field to `signature`, as it was renamed from `sig`.
fix(wallet/wallet): use `is_op_return` instead of
`is_provably_unspendable`
fix(wallet/wallet): use `relative::Locktime` instead of `Sequence`
fix(wallet/descriptor): use `ParsePublicKeyError`
fix(wallet/descriptor): use `.into()` to convert from `AbsLockTime` and
`RelLockTime` to `absolute::LockTime` and `relative::LockTime`
fix(wallet/wallet): use `Message::from_digest()` instead of relying on
deprecated `ThirtyTwoByteHash` trait.
fix(wallet/descriptor+wallet): expect `Threshold` type, and handle it
internally
fix(wallet/wallet): remove `0x` prefix from expected `TxId` display
fix(examples): use `compute_txid()` instead of `txid`
fix(ci): remove usage of `bitcoin/no-std` feature
- remove comment: `# The `no-std` feature it's implied when the `std` feature is disabled.`
2024-05-22 18:34:30 -03:00
|
|
|
txid: tx0.compute_txid(),
|
2023-07-26 19:46:40 -05:00
|
|
|
vout: 0,
|
|
|
|
},
|
|
|
|
script_sig: Default::default(),
|
|
|
|
sequence: Default::default(),
|
|
|
|
witness: Default::default(),
|
2023-02-14 13:54:07 +11:00
|
|
|
}],
|
2023-07-26 19:46:40 -05:00
|
|
|
output: vec![
|
|
|
|
TxOut {
|
2023-10-16 19:51:53 +11:00
|
|
|
value: Amount::from_sat(50_000),
|
2024-03-26 21:57:10 -04:00
|
|
|
script_pubkey: receive_address.script_pubkey(),
|
2023-07-26 19:46:40 -05:00
|
|
|
},
|
|
|
|
TxOut {
|
2023-10-16 19:51:53 +11:00
|
|
|
value: Amount::from_sat(25_000),
|
2023-07-26 19:46:40 -05:00
|
|
|
script_pubkey: sendto_address.script_pubkey(),
|
|
|
|
},
|
|
|
|
],
|
2023-02-14 13:54:07 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
wallet
|
|
|
|
.insert_checkpoint(BlockId {
|
|
|
|
height: 1_000,
|
|
|
|
hash: BlockHash::all_zeros(),
|
|
|
|
})
|
|
|
|
.unwrap();
|
2023-07-26 19:46:40 -05:00
|
|
|
wallet
|
|
|
|
.insert_checkpoint(BlockId {
|
|
|
|
height: 2_000,
|
|
|
|
hash: BlockHash::all_zeros(),
|
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-20 07:01:05 -04:00
|
|
|
|
|
|
|
wallet.insert_tx(tx0.clone());
|
|
|
|
insert_anchor_from_conf(
|
|
|
|
&mut wallet,
|
|
|
|
tx0.compute_txid(),
|
|
|
|
ConfirmationTime::Confirmed {
|
|
|
|
height: 1_000,
|
|
|
|
time: 100,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
wallet.insert_tx(tx1.clone());
|
|
|
|
insert_anchor_from_conf(
|
|
|
|
&mut wallet,
|
|
|
|
tx1.compute_txid(),
|
|
|
|
ConfirmationTime::Confirmed {
|
|
|
|
height: 2_000,
|
|
|
|
time: 200,
|
|
|
|
},
|
|
|
|
);
|
2023-02-14 13:54:07 +11:00
|
|
|
|
deps(bdk): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
deps(chain): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
fix(chain): use `minimal_non_dust()` instead of `dust_value()`
fix(chain): use `compute_txid()` instead of `txid`
deps(testenv): bump `electrsd` to `0.28.0`
deps(electrum): bump `electrum-client` to `0.20.0`
fix(electrum): use `compute_txid()` instead of `txid`
deps(esplora): bump `esplora-client` to `0.8.0`
deps(bitcoind_rpc): bump `bitcoin` to `0.32.0`, `bitcoincore-rpc` to
`0.19.0`
fix(bitcoind_rpc): use `compute_txid()` instead of `txid`
fix(nursery/tmp_plan): use proper `sighash` errors, and fix the expected
`Signature` fields
fix(sqlite): use `compute_txid()` instead of `txid`
deps(hwi): bump `hwi` to `0.9.0`
deps(wallet): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
fix(wallet): use `compute_txid()` and `minimal_non_dust()`
- update to use `compute_txid()` instead of deprecated `txid()`
- update to use `minimal_non_dust()` instead of `dust_value()`
- remove unused `bitcoin::hex::FromHex`.
fix(wallet): uses `.into` conversion on `Network` for `NetworkKind`
- uses `.into()` when appropriate, otherwise use the explicit
`NetworkKind`, and it's `.is_mainnet()` method.
fix(wallet): add P2wpkh, Taproot, InputsIndex errors to `SignerError`
fix(wallet): fields on taproot, and ecdsa `Signature` structure
fix(wallet/wallet): convert `Weight` to `usize` for now
- converts the `bitcoin-units::Weight` type to `usize` with help of
`to_wu()` method.
- it should be updated/refactored in the future to handle the `Weight`
type throughout the code instead of current `usize`, only converting
it for now.
- allows the usage of deprecated `is_provably_unspendable()`, needs
further discussion if suggested `is_op_return` is suitable.
- update the expect field to `signature`, as it was renamed from `sig`.
fix(wallet/wallet): use `is_op_return` instead of
`is_provably_unspendable`
fix(wallet/wallet): use `relative::Locktime` instead of `Sequence`
fix(wallet/descriptor): use `ParsePublicKeyError`
fix(wallet/descriptor): use `.into()` to convert from `AbsLockTime` and
`RelLockTime` to `absolute::LockTime` and `relative::LockTime`
fix(wallet/wallet): use `Message::from_digest()` instead of relying on
deprecated `ThirtyTwoByteHash` trait.
fix(wallet/descriptor+wallet): expect `Threshold` type, and handle it
internally
fix(wallet/wallet): remove `0x` prefix from expected `TxId` display
fix(examples): use `compute_txid()` instead of `txid`
fix(ci): remove usage of `bitcoin/no-std` feature
- remove comment: `# The `no-std` feature it's implied when the `std` feature is disabled.`
2024-05-22 18:34:30 -03:00
|
|
|
(wallet, tx1.compute_txid())
|
2023-02-14 13:54:07 +11:00
|
|
|
}
|
|
|
|
|
2024-03-27 13:34:18 -04:00
|
|
|
/// Return a fake wallet that appears to be funded for testing.
|
|
|
|
///
|
|
|
|
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
|
|
|
|
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
|
|
|
|
/// sats are the transaction fee.
|
|
|
|
///
|
|
|
|
/// Note: the change descriptor will have script type `p2wpkh`. If passing some other script type
|
|
|
|
/// as argument, make sure you're ok with getting a wallet where the keychains have potentially
|
|
|
|
/// different script types. Otherwise, use `get_funded_wallet_with_change`.
|
2023-02-15 12:52:10 +11:00
|
|
|
pub fn get_funded_wallet(descriptor: &str) -> (Wallet, bitcoin::Txid) {
|
2024-03-26 21:57:10 -04:00
|
|
|
let change = get_test_wpkh_change();
|
|
|
|
get_funded_wallet_with_change(descriptor, change)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_funded_wallet_wpkh() -> (Wallet, bitcoin::Txid) {
|
|
|
|
get_funded_wallet_with_change(get_test_wpkh(), get_test_wpkh_change())
|
2023-02-15 12:52:10 +11:00
|
|
|
}
|
|
|
|
|
2023-02-14 13:54:07 +11:00
|
|
|
pub fn get_test_wpkh() -> &'static str {
|
|
|
|
"wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)"
|
|
|
|
}
|
|
|
|
|
2024-03-26 21:57:10 -04:00
|
|
|
pub fn get_test_wpkh_with_change_desc() -> (&'static str, &'static str) {
|
|
|
|
(
|
|
|
|
"wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)",
|
|
|
|
get_test_wpkh_change(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_test_wpkh_change() -> &'static str {
|
|
|
|
"wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/0)"
|
|
|
|
}
|
|
|
|
|
2023-02-14 13:54:07 +11:00
|
|
|
pub fn get_test_single_sig_csv() -> &'static str {
|
|
|
|
// and(pk(Alice),older(6))
|
|
|
|
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),older(6)))"
|
|
|
|
}
|
|
|
|
|
|
|
|
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))))"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_test_single_sig_cltv() -> &'static str {
|
|
|
|
// and(pk(Alice),after(100000))
|
|
|
|
"wsh(and_v(v:pk(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW),after(100000)))"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_test_tr_single_sig() -> &'static str {
|
|
|
|
"tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG)"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_test_tr_with_taptree() -> &'static str {
|
|
|
|
"tr(b511bd5771e47ee27558b1765e87b541668304ec567721c7b880edc0a010da55,{pk(cPZzKuNmpuUjD1e8jUU4PVzy2b5LngbSip8mBsxf4e7rSFZVb4Uh),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_test_tr_with_taptree_both_priv() -> &'static str {
|
|
|
|
"tr(b511bd5771e47ee27558b1765e87b541668304ec567721c7b880edc0a010da55,{pk(cPZzKuNmpuUjD1e8jUU4PVzy2b5LngbSip8mBsxf4e7rSFZVb4Uh),pk(cNaQCDwmmh4dS9LzCgVtyy1e1xjCJ21GUDHe9K98nzb689JvinGV)})"
|
|
|
|
}
|
|
|
|
|
|
|
|
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))})"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_test_tr_single_sig_xprv() -> &'static str {
|
|
|
|
"tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/*)"
|
|
|
|
}
|
|
|
|
|
2024-03-26 21:57:10 -04:00
|
|
|
pub fn get_test_tr_single_sig_xprv_with_change_desc() -> (&'static str, &'static str) {
|
|
|
|
("tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/0/*)",
|
|
|
|
"tr(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/1/*)")
|
|
|
|
}
|
|
|
|
|
2023-02-14 13:54:07 +11:00
|
|
|
pub fn get_test_tr_with_taptree_xprv() -> &'static str {
|
|
|
|
"tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG,{pk(tprv8ZgxMBicQKsPdDArR4xSAECuVxeX1jwwSXR4ApKbkYgZiziDc4LdBy2WvJeGDfUSE4UT4hHhbgEwbdq8ajjUHiKDegkwrNU6V55CxcxonVN/*),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_test_tr_dup_keys() -> &'static str {
|
|
|
|
"tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG,{pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})"
|
|
|
|
}
|
2023-11-16 18:09:20 -05:00
|
|
|
|
|
|
|
/// Construct a new [`FeeRate`] from the given raw `sat_vb` feerate. This is
|
|
|
|
/// useful in cases where we want to create a feerate from a `f64`, as the
|
|
|
|
/// traditional [`FeeRate::from_sat_per_vb`] method will only accept an integer.
|
|
|
|
///
|
|
|
|
/// **Note** this 'quick and dirty' conversion should only be used when the input
|
|
|
|
/// parameter has units of `satoshis/vbyte` **AND** is not expected to overflow,
|
|
|
|
/// or else the resulting value will be inaccurate.
|
2023-11-16 18:45:21 -05:00
|
|
|
pub fn feerate_unchecked(sat_vb: f64) -> FeeRate {
|
2023-11-16 18:09:20 -05:00
|
|
|
// 1 sat_vb / 4wu_vb * 1000kwu_wu = 250 sat_kwu
|
|
|
|
let sat_kwu = (sat_vb * 250.0).ceil() as u64;
|
|
|
|
FeeRate::from_sat_per_kwu(sat_kwu)
|
|
|
|
}
|
2024-05-20 07:01:05 -04:00
|
|
|
|
|
|
|
/// Simulates confirming a tx with `txid` at the specified `position` by inserting an anchor
|
|
|
|
/// at the lowest height in local chain that is greater or equal to `position`'s height,
|
|
|
|
/// assuming the confirmation time matches `ConfirmationTime::Confirmed`.
|
|
|
|
pub fn insert_anchor_from_conf(wallet: &mut Wallet, txid: Txid, position: ConfirmationTime) {
|
|
|
|
if let ConfirmationTime::Confirmed { height, time } = position {
|
|
|
|
// anchor tx to checkpoint with lowest height that is >= position's height
|
|
|
|
let anchor = wallet
|
|
|
|
.local_chain()
|
|
|
|
.range(height..)
|
|
|
|
.last()
|
|
|
|
.map(|anchor_cp| ConfirmationTimeHeightAnchor {
|
|
|
|
anchor_block: anchor_cp.block_id(),
|
|
|
|
confirmation_height: height,
|
|
|
|
confirmation_time: time,
|
|
|
|
})
|
|
|
|
.expect("confirmation height cannot be greater than tip");
|
|
|
|
|
2024-06-25 11:00:17 -04:00
|
|
|
let mut graph = TxGraph::default();
|
|
|
|
let _ = graph.insert_anchor(txid, anchor);
|
|
|
|
wallet
|
|
|
|
.apply_update(Update {
|
|
|
|
graph,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-20 07:01:05 -04:00
|
|
|
}
|
|
|
|
}
|