Skip to content

Commit

Permalink
replace bitcoinconsensus with miniscript interpeter for tests
Browse files Browse the repository at this point in the history
libbitcoinconsensus is dead -
https://github.com/bitcoin/bitcoin/blob/master/doc/release-notes/release-notes-27.0.md?plain=1#L40-L53. We
used it previously thinking there was no replacement, but
rust-miniscript contains an interpreter:

https://docs.rs/miniscript/latest/miniscript/interpreter/index.html

That interpreter can only verify spends of UTXOs created by a
descriptor, but the BitBox does not support anything else.
  • Loading branch information
benma committed Jul 6, 2024
1 parent 96279dc commit 9d671aa
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 52 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ wasm-bindgen-test = "0.3.42"
tokio = { version = "1", features = ["time", "macros", "rt", "fs"] }
reqwest = "0.12"
url = "2.5"
# Enable this to be able to get coverage using `cargo tarpaulin --features=simulator,tokio --out=Html` without compilation error.
# See https://github.com/rust-bitcoin/rust-bitcoinconsensus/pull/94
# bitcoinconsensus = { git = "https://github.com/benma/rust-bitcoinconsensus.git", rev = "d132eec12cf86038838c45185b083f834dfb7b26", default-features = false }
bitcoinconsensus = { version = "0.106.0", default-features = false }
miniscript = "12.0.0"

[build-dependencies]
Expand Down
41 changes: 3 additions & 38 deletions tests/subtests/test_btc_psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,6 @@ use bitcoin::{
};
use miniscript::psbt::PsbtExt;

// Checks that the psbt is fully signed and valid (all scripts execute correctly).
fn verify_transaction(psbt: Psbt) {
let utxos: Vec<TxOut> = psbt
.iter_funding_utxos()
.map(|utxo| utxo.unwrap())
.cloned()
.collect();

let tx = psbt.extract_tx_unchecked_fee_rate();
let serialized_tx = bitcoin::consensus::encode::serialize(&tx);

let flags = bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT | bitcoinconsensus::VERIFY_TAPROOT;

let utxos_converted: Vec<bitcoinconsensus::Utxo> = utxos
.iter()
.map(|output| bitcoinconsensus::Utxo {
script_pubkey: output.script_pubkey.as_bytes().as_ptr(),
script_pubkey_len: output.script_pubkey.as_bytes().len() as u32,
value: output.value.to_sat() as i64,
})
.collect();

for (idx, output) in utxos.iter().enumerate() {
bitcoinconsensus::verify_with_flags(
output.script_pubkey.as_bytes(),
output.value.to_sat(),
serialized_tx.as_slice(),
Some(&utxos_converted),
idx,
flags,
)
.unwrap();
}
}

pub async fn test(bitbox: &PairedBitBox) {
test_taproot_key_spend(bitbox).await;
test_mixed_spend(bitbox).await;
Expand Down Expand Up @@ -170,7 +135,7 @@ async fn test_taproot_key_spend(bitbox: &PairedBitBox) {
psbt.finalize_mut(&secp).unwrap();

// Verify the signed tx, including that all sigs/witnesses are correct.
verify_transaction(psbt);
miniscript::psbt::interpreter_check(&psbt, &secp).unwrap();
}

// Test signing; mixed input types (p2wpkh, p2wpkh-p2sh, p2tr)
Expand Down Expand Up @@ -315,7 +280,7 @@ async fn test_mixed_spend(bitbox: &PairedBitBox) {
psbt.finalize_mut(&secp).unwrap();

// Verify the signed tx, including that all sigs/witnesses are correct.
verify_transaction(psbt);
miniscript::psbt::interpreter_check(&psbt, &secp).unwrap();
}

async fn test_policy_wsh(bitbox: &PairedBitBox) {
Expand Down Expand Up @@ -469,5 +434,5 @@ async fn test_policy_wsh(bitbox: &PairedBitBox) {
psbt.finalize_mut(&secp).unwrap();

// Verify the signed tx, including that all sigs/witnesses are correct.
verify_transaction(psbt);
miniscript::psbt::interpreter_check(&psbt, &secp).unwrap();
}

0 comments on commit 9d671aa

Please sign in to comment.