bitcoin/policies: add unit tests for get_leaf_hash_by_pubkey and taproot_spend_info
What changed, and why it matters
This commit only adds new unit tests for existing Bitcoin Taproot functions. It does not change any production code, fix a bug, or alter security behavior. The tests use fixed example data to ensure future upgrades of a dependency do not accidentally change results.
No security action needed. Treat as routine test coverage improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds two test functions, test_get_leaf_hash_by_pubkey and test_taproot_spend_info, plus required imports (bitcoin::hashes::Hash, hex_lit::hex). It asserts specific expected hashes for key-path and script-path Taproot spending scenarios and an invalid-keypath error case. No runtime logic in policies.rs is modified.
Changed components
src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs (test module only)Inspect captured patch +125 / −0
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs
index af99333..73f869b 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/policies.rs
@@ -767,6 +767,8 @@ mod tests {
use crate::bip32::parse_xpub;
use crate::keystore::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
+ use bitcoin::hashes::Hash as _;
+ use hex_lit::hex;
const SOME_XPUB_1: &str = "tpubDFj9SBQssRHA5EB1ox58mcgF9sB61br9RGz6UrBukcNKmFe4fPgskZ4wigxQ1jSUzLdjnvvDHL8Z6L3ey5Ev5FNNqrDrePxwXsNHiLZhBTc";
const SOME_XPUB_2: &str = "tpubDCmDXtvJLH9yHLNLnGVRoXBvvacvWskjV4hq4WAmGXcRbfa5uaiybZ7kjGRAFbLaoiw1LcwV56H88avibGh7GC7nqqz2Jcs1dWu33cRKYm4";
@@ -1615,6 +1617,129 @@ mod tests {
}
}
+ #[test]
+ fn test_get_leaf_hash_by_pubkey() {
+ mock_unlocked();
+
+ let coin = BtcCoin::Tbtc;
+ let our_key = make_our_key(KEYPATH_ACCOUNT);
+ let policy = make_policy(
+ "tr(@0/**,{pk(@1/**),pk(@2/**)})",
+ &[
+ our_key.clone(),
+ make_key(SOME_XPUB_1),
+ make_key(SOME_XPUB_2),
+ ],
+ );
+ let derived = parse(&mut crate::hal::testing::TestingHal::new(), &policy, coin)
+ .unwrap()
+ .derive(false, 0)
+ .unwrap();
+
+ let Descriptor::Tr(tr) = derived else {
+ panic!("expected tr");
+ };
+
+ // Internal key not present in any leaf script.
+ let internal_key_bytes: [u8; 33] = tr.inner.internal_key().inner.serialize();
+ assert_eq!(tr.get_leaf_hash_by_pubkey(&internal_key_bytes), None);
+
+ // There are exactly two leaf pubkeys (@1 and @2).
+ let leaf_pks: Vec<[u8; 33]> = tr
+ .inner
+ .leaves()
+ .flat_map(|leaf| leaf.miniscript().iter_pk())
+ .map(|pk| pk.inner.serialize())
+ .collect();
+ assert_eq!(leaf_pks.len(), 2);
+
+ assert_eq!(
+ tr.get_leaf_hash_by_pubkey(&leaf_pks[0])
+ .unwrap()
+ .to_byte_array(),
+ hex!("1cc0a4cb1521ffd7aa07e1a30076d57a6daa78289e98545cd69189a56d7a3fba"),
+ );
+ assert_eq!(
+ tr.get_leaf_hash_by_pubkey(&leaf_pks[1])
+ .unwrap()
+ .to_byte_array(),
+ hex!("bd50326268960afb06207a0683f5c3161d295eba424dc28a89e72278d8926b40"),
+ );
+
+ let mut unknown_pk = leaf_pks[0];
+ unknown_pk[32] ^= 1;
+ assert_eq!(tr.get_leaf_hash_by_pubkey(&unknown_pk), None);
+ }
+
+ #[test]
+ fn test_taproot_spend_info() {
+ mock_unlocked();
+
+ let coin = BtcCoin::Tbtc;
+ let our_key = make_our_key(KEYPATH_ACCOUNT);
+ let policy = make_policy(
+ "tr(@0/<0;1>/*,pk(@0/<2;3>/*))",
+ core::slice::from_ref(&our_key),
+ );
+
+ let mut hal = crate::hal::testing::TestingHal::new();
+ let parsed_policy = parse(&mut hal, &policy, coin).unwrap();
+
+ const ADDRESS_INDEX: u32 = 5;
+ let mut xpub_cache = Bip32XpubCache::new(crate::xpubcache::Compute::Once);
+
+ // Internal key results in a key path spend. The internal key is ` @0/<0;1>/*`, so `/0/5`
+ // selects that one as `0` matches the first multipath index of that key.
+ let keypath_internal: Vec<u32> = KEYPATH_ACCOUNT
+ .iter()
+ .copied()
+ .chain([0, ADDRESS_INDEX])
+ .collect();
+ match parsed_policy
+ .taproot_spend_info(&mut hal, &mut xpub_cache, &keypath_internal)
+ .unwrap()
+ {
+ TaprootSpendInfo::KeySpend(tweak) => {
+ assert_eq!(
+ tweak.to_byte_array(),
+ hex!("369416353930adfe3345578e43fc31a8c222905d9d134f3511ea4f3a4ac041a5"),
+ );
+ }
+ _ => panic!("expected key spend"),
+ }
+
+ // Leaf key results in a script path spend. The leaf key is ` @0/<2;3>/*`, so `/2/5`
+ // selects that one as `2` matches the first multipath index of that key.
+ let keypath_leaf: Vec<u32> = KEYPATH_ACCOUNT
+ .iter()
+ .copied()
+ .chain([2, ADDRESS_INDEX])
+ .collect();
+ match parsed_policy
+ .taproot_spend_info(&mut hal, &mut xpub_cache, &keypath_leaf)
+ .unwrap()
+ {
+ TaprootSpendInfo::ScriptSpend(leaf_hash) => {
+ assert_eq!(
+ leaf_hash.to_byte_array(),
+ hex!("00c525c5b70a01ab5fab849dd5a156ea906c75073d15fc5f2f98227b2bb0f5e9"),
+ );
+ }
+ _ => panic!("expected script spend"),
+ }
+
+ // Invalid keypath results in error.
+ let keypath_invalid: Vec<u32> = KEYPATH_ACCOUNT
+ .iter()
+ .copied()
+ .chain([4, ADDRESS_INDEX])
+ .collect();
+ assert!(matches!(
+ parsed_policy.taproot_spend_info(&mut hal, &mut xpub_cache, &keypath_invalid),
+ Err(Error::InvalidInput)
+ ));
+ }
+
#[test]
fn test_get_hash() {
// Fixture below verified with:
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.