From e2ccc2ce6ef5f6be12333d9d57d14ebc9f0d7a2c Mon Sep 17 00:00:00 2001 From: Gustavo Chain Date: Mon, 2 Oct 2023 23:13:15 +0200 Subject: [PATCH] test: add Descriptor predicate tests --- .../src/chainhooks/bitcoin/tests.rs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/components/chainhook-sdk/src/chainhooks/bitcoin/tests.rs b/components/chainhook-sdk/src/chainhooks/bitcoin/tests.rs index ff267dab7..fe9b2d12d 100644 --- a/components/chainhook-sdk/src/chainhooks/bitcoin/tests.rs +++ b/components/chainhook-sdk/src/chainhooks/bitcoin/tests.rs @@ -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 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);