}
}
}
+
+#[cfg(test)]
+mod test {
+
+ use crate::descriptor;
+ use crate::descriptor::{ExtractPolicy, ToWalletDescriptor};
+
+ use super::*;
+ use crate::descriptor::policy::SatisfiableItem::{Multisig, Signature, Thresh};
+ use crate::keys::{DescriptorKey, ToDescriptorKey};
+ use crate::wallet::signer::SignersContainer;
+ use bitcoin::secp256k1::{All, Secp256k1};
+ use bitcoin::util::bip32;
+ use bitcoin::util::bip32::ChildNumber;
+ use bitcoin::Network;
+ use std::str::FromStr;
+ use std::sync::Arc;
+
+ const TPRV0_STR:&str = "tprv8ZgxMBicQKsPdZXrcHNLf5JAJWFAoJ2TrstMRdSKtEggz6PddbuSkvHKM9oKJyFgZV1B7rw8oChspxyYbtmEXYyg1AjfWbL3ho3XHDpHRZf";
+ const TPRV1_STR:&str = "tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N";
+
+ const PATH: &str = "m/44'/1'/0'/0";
+
+ fn setup_keys<Ctx: ScriptContext>(
+ tprv: &str,
+ ) -> (DescriptorKey<Ctx>, DescriptorKey<Ctx>, Fingerprint) {
+ let secp: Secp256k1<All> = Secp256k1::new();
+
+ let path = bip32::DerivationPath::from_str(PATH).unwrap();
+
+ let tprv = bip32::ExtendedPrivKey::from_str(tprv).unwrap();
+ let tpub = bip32::ExtendedPubKey::from_private(&secp, &tprv);
+ let fingerprint = tprv.fingerprint(&secp);
+
+ let prvkey = (tprv, path.clone()).to_descriptor_key().unwrap();
+ let pubkey = (tpub, path).to_descriptor_key().unwrap();
+
+ (prvkey, pubkey, fingerprint)
+ }
+
+ // test ExtractPolicy trait for simple descriptors; wpkh(), sh(multi())
+
+ #[test]
+ fn test_extract_policy_for_wpkh() {
+ let (prvkey, pubkey, fingerprint) = setup_keys(TPRV0_STR);
+
+ let desc = descriptor!(wpkh(pubkey)).unwrap();
+
+ println!("desc = {:?}", desc); // TODO remove
+
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Signature(pk_or_f) if &pk_or_f.fingerprint.unwrap() == &fingerprint)
+ );
+ assert!(matches!(&policy.contribution, Satisfaction::None));
+
+ let desc = descriptor!(wpkh(prvkey)).unwrap();
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Signature(pk_or_f) if &pk_or_f.fingerprint.unwrap() == &fingerprint)
+ );
+ assert!(
+ matches!(&policy.contribution, Satisfaction::Complete {condition} if condition.csv == None && condition.timelock == None)
+ );
+ }
+
+ // 2 pub keys descriptor, required 2 prv keys
+ #[test]
+ fn test_extract_policy_for_sh_multi_partial_0of2() {
+ let (_prvkey0, pubkey0, fingerprint0) = setup_keys(TPRV0_STR);
+ let (_prvkey1, pubkey1, fingerprint1) = setup_keys(TPRV1_STR);
+
+ let desc = descriptor!(sh(multi 2, pubkey0, pubkey1)).unwrap();
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Multisig { keys, threshold } if threshold == &2
+ && &keys[0].fingerprint.unwrap() == &fingerprint0
+ && &keys[1].fingerprint.unwrap() == &fingerprint1)
+ );
+
+ // TODO should this be "Satisfaction::None" since we have no prv keys?
+ // TODO should items and conditions not be empty?
+ assert!(
+ matches!(&policy.contribution, Satisfaction::Partial { n, m, items, conditions} if n == &2
+ && m == &2
+ && items.is_empty()
+ && conditions.is_empty()
+ )
+ );
+ }
+
+ // 1 prv and 1 pub key descriptor, required 2 prv keys
+ #[test]
+ fn test_extract_policy_for_sh_multi_partial_1of2() {
+ let (prvkey0, _pubkey0, fingerprint0) = setup_keys(TPRV0_STR);
+ let (_prvkey1, pubkey1, fingerprint1) = setup_keys(TPRV1_STR);
+
+ let desc = descriptor!(sh(multi 2, prvkey0, pubkey1)).unwrap();
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Multisig { keys, threshold } if threshold == &2
+ && &keys[0].fingerprint.unwrap() == &fingerprint0
+ && &keys[1].fingerprint.unwrap() == &fingerprint1)
+ );
+
+ // TODO should this be "Satisfaction::Partial" since we have only one of two prv keys?
+ assert!(
+ matches!(&policy.contribution, Satisfaction::PartialComplete { n, m, items, conditions} if n == &2
+ && m == &2
+ && items.len() == 2
+ && conditions.contains_key(&vec![0,1])
+ )
+ );
+ }
+
+ // 1 prv and 1 pub key descriptor, required 1 prv keys
+ #[test]
+ fn test_extract_policy_for_sh_multi_complete_1of2() {
+ let (_prvkey0, pubkey0, fingerprint0) = setup_keys(TPRV0_STR);
+ let (prvkey1, _pubkey1, fingerprint1) = setup_keys(TPRV1_STR);
+
+ let desc = descriptor!(sh(multi 1, pubkey0, prvkey1)).unwrap();
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Multisig { keys, threshold } if threshold == &1
+ && &keys[0].fingerprint.unwrap() == &fingerprint0
+ && &keys[1].fingerprint.unwrap() == &fingerprint1)
+ );
+ assert!(
+ matches!(&policy.contribution, Satisfaction::PartialComplete { n, m, items, conditions} if n == &2
+ && m == &1
+ && items.len() == 2
+ && conditions.contains_key(&vec![0])
+ && conditions.contains_key(&vec![1])
+ )
+ );
+ }
+
+ // 2 prv keys descriptor, required 2 prv keys
+ #[test]
+ fn test_extract_policy_for_sh_multi_complete_2of2() {
+ let (prvkey0, _pubkey0, fingerprint0) = setup_keys(TPRV0_STR);
+ let (prvkey1, _pubkey1, fingerprint1) = setup_keys(TPRV1_STR);
+
+ let desc = descriptor!(sh(multi 2, prvkey0, prvkey1)).unwrap();
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Multisig { keys, threshold } if threshold == &2
+ && &keys[0].fingerprint.unwrap() == &fingerprint0
+ && &keys[1].fingerprint.unwrap() == &fingerprint1)
+ );
+
+ assert!(
+ matches!(&policy.contribution, Satisfaction::PartialComplete { n, m, items, conditions} if n == &2
+ && m == &2
+ && items.len() == 2
+ && conditions.contains_key(&vec![0,1])
+ )
+ );
+ }
+
+ // test ExtractPolicy trait with extended and single keys
+
+ #[test]
+ fn test_extract_policy_for_single_wpkh() {
+ let (prvkey, pubkey, fingerprint) = setup_keys(TPRV0_STR);
+
+ let desc = descriptor!(wpkh(pubkey)).unwrap();
+ println!("desc = {:?}", desc); // TODO remove
+
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let single_key = wallet_desc.derive(&[ChildNumber::from_normal_idx(0).unwrap()]);
+ println!("single_key = {:?}", single_key); // TODO remove
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = single_key
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Signature(pk_or_f) if &pk_or_f.fingerprint.unwrap() == &fingerprint)
+ );
+ assert!(matches!(&policy.contribution, Satisfaction::None));
+
+ let desc = descriptor!(wpkh(prvkey)).unwrap();
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let single_key = wallet_desc.derive(&[ChildNumber::from_normal_idx(0).unwrap()]);
+
+ println!("single_key = {:?}", single_key); // TODO remove
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = single_key
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Signature(pk_or_f) if &pk_or_f.fingerprint.unwrap() == &fingerprint)
+ );
+ assert!(
+ matches!(&policy.contribution, Satisfaction::Complete {condition} if condition.csv == None && condition.timelock == None)
+ );
+ }
+
+ // single key, 1 prv and 1 pub key descriptor, required 1 prv keys
+ #[test]
+ fn test_extract_policy_for_single_wsh_multi_complete_1of2() {
+ let (_prvkey0, pubkey0, fingerprint0) = setup_keys(TPRV0_STR);
+ let (prvkey1, _pubkey1, fingerprint1) = setup_keys(TPRV1_STR);
+
+ let desc = descriptor!(sh(multi 1, pubkey0, prvkey1)).unwrap();
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let single_key = wallet_desc.derive(&[ChildNumber::from_normal_idx(0).unwrap()]);
+ println!("single_key = {:?}", single_key); // TODO remove
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = single_key
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Multisig { keys, threshold } if threshold == &1
+ && &keys[0].fingerprint.unwrap() == &fingerprint0
+ && &keys[1].fingerprint.unwrap() == &fingerprint1)
+ );
+ assert!(
+ matches!(&policy.contribution, Satisfaction::PartialComplete { n, m, items, conditions } if n == &2
+ && m == &1
+ && items.len() == 2
+ && conditions.contains_key(&vec![0])
+ && conditions.contains_key(&vec![1])
+ )
+ );
+ }
+
+ // test ExtractPolicy trait with descriptors containing timelocks in a thresh()
+
+ #[test]
+ fn test_extract_policy_for_wsh_multi_timelock() {
+ let (prvkey0, _pubkey0, _fingerprint0) = setup_keys(TPRV0_STR);
+ let (_prvkey1, pubkey1, _fingerprint1) = setup_keys(TPRV1_STR);
+ let sequence = 50;
+
+ let desc = descriptor!(wsh (
+ thresh 2, (pk prvkey0), (+s pk pubkey1), (+s+d+v older sequence)
+ ))
+ .unwrap();
+
+ println!("desc = {:?}", desc); // TODO remove
+
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+ assert!(
+ matches!(&policy.item, Thresh { items, threshold } if items.len() == 3 && threshold == &2)
+ );
+
+ assert!(
+ matches!(&policy.contribution, Satisfaction::PartialComplete { n, m, items, conditions } if n == &3
+ && m == &2
+ && items.len() == 3
+ && conditions.get(&vec![0,1]).unwrap().iter().next().unwrap().csv.is_none()
+ && conditions.get(&vec![0,2]).unwrap().iter().next().unwrap().csv == Some(sequence)
+ && conditions.get(&vec![1,2]).unwrap().iter().next().unwrap().csv == Some(sequence)
+ )
+ );
+ }
+
+ // - mixed timelocks should fail
+
+ #[test]
+ fn test_extract_policy_for_wsh_mixed_timelocks() {
+ let (prvkey0, _pubkey0, _fingerprint0) = setup_keys(TPRV0_STR);
+
+ let locktime_threshold = 500000000; // if less than this means block number, else block time in seconds
+ let locktime_blocks = 100;
+ let locktime_seconds = locktime_blocks + locktime_threshold;
+
+ let desc = descriptor!(sh (and_v (+v pk prvkey0), (and_v (+v after locktime_seconds), (after locktime_blocks)))).unwrap();
+
+ println!("desc = {:?}", desc); // TODO remove
+
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+
+ // TODO how should this fail with mixed timelocks?
+ }
+
+ // - multiple timelocks of the same type should be correctly merged together
+
+ #[test]
+ fn test_extract_policy_for_multiple_same_timelocks() {
+ let (prvkey0, _pubkey0, _fingerprint0) = setup_keys(TPRV0_STR);
+
+ let locktime_blocks0 = 100;
+ let locktime_blocks1 = 200;
+
+ let desc = descriptor!(sh (and_v (+v pk prvkey0), (and_v (+v after locktime_blocks0), (after locktime_blocks1)))).unwrap();
+
+ println!("desc = {:?}", desc); // TODO remove
+
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+
+ let (prvkey1, _pubkey1, _fingerprint1) = setup_keys(TPRV0_STR);
+
+ let locktime_seconds0 = 500000100;
+ let locktime_seconds1 = 500000200;
+
+ let desc = descriptor!(sh (and_v (+v pk prvkey1), (and_v (+v after locktime_seconds0), (after locktime_seconds1)))).unwrap();
+
+ println!("desc = {:?}", desc); // TODO remove
+
+ let (wallet_desc, keymap) = desc.to_wallet_descriptor(Network::Testnet).unwrap();
+
+ let signers_container = Arc::new(SignersContainer::from(keymap));
+ let policy = wallet_desc
+ .extract_policy(signers_container)
+ .unwrap()
+ .unwrap();
+
+ println!("desc policy = {:?}", policy); // TODO remove
+
+ // TODO how should this merge timelocks?
+ }
+}