Skip to content

Commit

Permalink
test: add Descriptor predicate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qustavo committed Oct 2, 2023
1 parent 624d09a commit e2ccc2c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions components/chainhook-sdk/src/chainhooks/bitcoin/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@ fn test_opreturn_evaluation(script_pubkey: &str, rule: MatchingRule, matches: bo
script_pubkey_evaluation(OutputPredicate::OpReturn(rule), script_pubkey, matches)
}

// Descriptor test cases have been taken from
// https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md#examples
// To generate the address run:
// `bdk-cli -n testnet wallet --descriptor <descriptor> get_new_address`
#[test_case(
"tb1q0ht9tyks4vh7p5p904t340cr9nvahy7um9zdem",
"wpkh(02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9)";
"Descriptor: P2WPKH"
)]
#[test_case(
"2NBtBzAJ84E3sTy1KooEHYVwmMhUVdJAyEa",
"sh(wpkh(03fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556))";
"Descriptor: P2SH-P2WPKH"
)]
#[test_case(
"tb1qwu7hp9vckakyuw6htsy244qxtztrlyez4l7qlrpg68v6drgvj39qya5jch",
"wsh(multi(2,03a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7,03774ae7f858a9411e5ef4246b70c65aac5649980be5c17891bbec17895da008cb,03d01115d548e7561b15c38f004d734633687cf4419620095bc5b0f47070afe85a))";
"Descriptor: P2WSH 2-of-3 multisig output"
)]
fn test_descriptor_evaluation(addr: &str, expr: &str) {
// turn the address into a script_pubkey with a 0x prefix, as expected by the evaluator.
let script_pubkey = Address::from_str(addr).unwrap().script_pubkey();
let matching_script_pubkey = format!("0x{}", hex::encode(script_pubkey));

let rule = DescriptorMatchingRule {
expression: expr.to_string(),
// TODO: test ranges
range: None,
};

// matching against the script_pubkey generated from the address should match.
script_pubkey_evaluation(
OutputPredicate::Descriptor(rule.clone()),
&matching_script_pubkey,
true,
);

// matching against a fake script_pubkey should not match.
script_pubkey_evaluation(OutputPredicate::Descriptor(rule.clone()), "0xffff", false);
}

// script_pubkey_evaluation is a helper that evaluates a a script_pubkey against a transaction predicate.
fn script_pubkey_evaluation(output: OutputPredicate, script_pubkey: &str, matches: bool) {
let predicate = BitcoinPredicateType::Outputs(output);
Expand Down

0 comments on commit e2ccc2c

Please sign in to comment.