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