|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use bitcoin::hashes::{hash160, sha256}; |
| 5 | +use bitcoin::util::address::WitnessVersion; |
| 6 | +use bitcoin::Network; |
| 7 | +use miniscript::descriptor::DescriptorType; |
| 8 | +use miniscript::policy::Concrete; |
| 9 | +use miniscript::{Descriptor, Miniscript, Tap, TranslatePk, Translator}; |
| 10 | +use secp256k1::{rand, KeyPair}; |
| 11 | + |
| 12 | +// Refer to https://github.com/sanket1729/adv_btc_workshop/blob/master/workshop.md#creating-a-taproot-descriptor |
| 13 | +// for a detailed explanation of the policy and it's compilation |
| 14 | + |
| 15 | +struct StrPkTranslator { |
| 16 | + pk_map: HashMap<String, bitcoin::XOnlyPublicKey>, |
| 17 | +} |
| 18 | + |
| 19 | +impl Translator<String, bitcoin::XOnlyPublicKey, ()> for StrPkTranslator { |
| 20 | + fn pk(&mut self, pk: &String) -> Result<bitcoin::XOnlyPublicKey, ()> { |
| 21 | + self.pk_map.get(pk).copied().ok_or(()) |
| 22 | + } |
| 23 | + |
| 24 | + fn pkh(&mut self, _pkh: &String) -> Result<hash160::Hash, ()> { |
| 25 | + unreachable!("Policy doesn't contain any pkh fragment"); |
| 26 | + } |
| 27 | + |
| 28 | + fn sha256(&mut self, _sha256: &String) -> Result<sha256::Hash, ()> { |
| 29 | + unreachable!("Policy does not contain any sha256 fragment"); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +fn main() { |
| 34 | + let pol_str = "or( |
| 35 | + 99@thresh(2, |
| 36 | + pk(hA), pk(S) |
| 37 | + ),1@or( |
| 38 | + 99@pk(Ca), |
| 39 | + 1@and(pk(In), older(9)) |
| 40 | + ) |
| 41 | + )" |
| 42 | + .replace(&[' ', '\n', '\t'][..], ""); |
| 43 | + |
| 44 | + let pol: Concrete<String> = Concrete::from_str(&pol_str).unwrap(); |
| 45 | + |
| 46 | + let desc = pol.compile_tr(Some("UNSPENDABLE_KEY".to_string())).unwrap(); |
| 47 | + |
| 48 | + let expected_desc = |
| 49 | + Descriptor::<String>::from_str("tr(Ca,{and_v(v:pk(In),older(9)),multi_a(2,hA,S)})") |
| 50 | + .unwrap(); |
| 51 | + assert_eq!(desc, expected_desc); |
| 52 | + |
| 53 | + // Check whether the descriptors are safe. |
| 54 | + assert!(desc.sanity_check().is_ok()); |
| 55 | + |
| 56 | + // Descriptor Type and Version should match respectively for Taproot |
| 57 | + let desc_type = desc.desc_type(); |
| 58 | + assert_eq!(desc_type, DescriptorType::Tr); |
| 59 | + assert_eq!(desc_type.segwit_version().unwrap(), WitnessVersion::V1); |
| 60 | + |
| 61 | + if let Descriptor::Tr(ref p) = desc { |
| 62 | + // Check if internal key is correctly inferred as Ca |
| 63 | + // assert_eq!(p.internal_key(), &pubkeys[2]); |
| 64 | + assert_eq!(p.internal_key(), "Ca"); |
| 65 | + |
| 66 | + // Iterate through scripts |
| 67 | + let mut iter = p.iter_scripts(); |
| 68 | + assert_eq!( |
| 69 | + iter.next().unwrap(), |
| 70 | + ( |
| 71 | + 1u8, |
| 72 | + &Miniscript::<String, Tap>::from_str("and_v(vc:pk_k(In),older(9))").unwrap() |
| 73 | + ) |
| 74 | + ); |
| 75 | + assert_eq!( |
| 76 | + iter.next().unwrap(), |
| 77 | + ( |
| 78 | + 1u8, |
| 79 | + &Miniscript::<String, Tap>::from_str("multi_a(2,hA,S)").unwrap() |
| 80 | + ) |
| 81 | + ); |
| 82 | + assert_eq!(iter.next(), None); |
| 83 | + } |
| 84 | + |
| 85 | + let mut pk_map = HashMap::new(); |
| 86 | + |
| 87 | + // We require secp for generating a random XOnlyPublicKey |
| 88 | + let secp = secp256k1::Secp256k1::new(); |
| 89 | + let key_pair = KeyPair::new(&secp, &mut rand::thread_rng()); |
| 90 | + // Random unspendable XOnlyPublicKey provided for compilation to Taproot Descriptor |
| 91 | + let unspendable_pubkey = bitcoin::XOnlyPublicKey::from_keypair(&key_pair); |
| 92 | + |
| 93 | + pk_map.insert("UNSPENDABLE_KEY".to_string(), unspendable_pubkey); |
| 94 | + let pubkeys = hardcoded_xonlypubkeys(); |
| 95 | + pk_map.insert("hA".to_string(), pubkeys[0]); |
| 96 | + pk_map.insert("S".to_string(), pubkeys[1]); |
| 97 | + pk_map.insert("Ca".to_string(), pubkeys[2]); |
| 98 | + pk_map.insert("In".to_string(), pubkeys[3]); |
| 99 | + let mut t = StrPkTranslator { pk_map }; |
| 100 | + |
| 101 | + let real_desc = desc.translate_pk(&mut t).unwrap(); |
| 102 | + |
| 103 | + // Max Satisfaction Weight for compilation, corresponding to the script-path spend |
| 104 | + // `multi_a(2,PUBKEY_1,PUBKEY_2) at taptree depth 1, having |
| 105 | + // Max Witness Size = scriptSig len + control_block size + varint(script_size) + script_size + |
| 106 | + // varint(max satisfaction elements) + max satisfaction size |
| 107 | + // = 4 + 65 + 1 + 70 + 1 + 132 |
| 108 | + let max_sat_wt = real_desc.max_satisfaction_weight().unwrap(); |
| 109 | + assert_eq!(max_sat_wt, 273); |
| 110 | + |
| 111 | + // Compute the bitcoin address and check if it matches |
| 112 | + let network = Network::Bitcoin; |
| 113 | + let addr = real_desc.address(network).unwrap(); |
| 114 | + let expected_addr = bitcoin::Address::from_str( |
| 115 | + "bc1pcc8ku64slu3wu04a6g376d2s8ck9y5alw5sus4zddvn8xgpdqw2swrghwx", |
| 116 | + ) |
| 117 | + .unwrap(); |
| 118 | + assert_eq!(addr, expected_addr); |
| 119 | +} |
| 120 | + |
| 121 | +fn hardcoded_xonlypubkeys() -> Vec<bitcoin::XOnlyPublicKey> { |
| 122 | + let serialized_keys: [[u8; 32]; 4] = [ |
| 123 | + [ |
| 124 | + 22, 37, 41, 4, 57, 254, 191, 38, 14, 184, 200, 133, 111, 226, 145, 183, 245, 112, 100, |
| 125 | + 42, 69, 210, 146, 60, 179, 170, 174, 247, 231, 224, 221, 52, |
| 126 | + ], |
| 127 | + [ |
| 128 | + 194, 16, 47, 19, 231, 1, 0, 143, 203, 11, 35, 148, 101, 75, 200, 15, 14, 54, 222, 208, |
| 129 | + 31, 205, 191, 215, 80, 69, 214, 126, 10, 124, 107, 154, |
| 130 | + ], |
| 131 | + [ |
| 132 | + 202, 56, 167, 245, 51, 10, 193, 145, 213, 151, 66, 122, 208, 43, 10, 17, 17, 153, 170, |
| 133 | + 29, 89, 133, 223, 134, 220, 212, 166, 138, 2, 152, 122, 16, |
| 134 | + ], |
| 135 | + [ |
| 136 | + 50, 23, 194, 4, 213, 55, 42, 210, 67, 101, 23, 3, 195, 228, 31, 70, 127, 79, 21, 188, |
| 137 | + 168, 39, 134, 58, 19, 181, 3, 63, 235, 103, 155, 213, |
| 138 | + ], |
| 139 | + ]; |
| 140 | + let mut keys: Vec<bitcoin::XOnlyPublicKey> = vec![]; |
| 141 | + for idx in 0..4 { |
| 142 | + keys.push(bitcoin::XOnlyPublicKey::from_slice(&serialized_keys[idx][..]).unwrap()); |
| 143 | + } |
| 144 | + keys |
| 145 | +} |
0 commit comments