|
| 1 | +extern crate bitcoin; |
| 2 | +extern crate miniscript; |
| 3 | +extern crate secp256k1; |
| 4 | + |
| 5 | +use std::str::FromStr; |
| 6 | +use std::sync::Arc; |
| 7 | + |
| 8 | +use bitcoin::util::address::WitnessVersion; |
| 9 | +use bitcoin::Network; |
| 10 | +use miniscript::descriptor::{DescriptorType, TapTree}; |
| 11 | +use miniscript::policy::Concrete; |
| 12 | +use miniscript::{Descriptor, Miniscript, Tap}; |
| 13 | +use secp256k1::{rand, KeyPair}; |
| 14 | + |
| 15 | +// Refer to https://github.com/sanket1729/adv_btc_workshop/blob/master/workshop.md#creating-a-taproot-descriptor |
| 16 | +// for a detailed explanation of the policy and it's compilation |
| 17 | + |
| 18 | +const PUBKEY_1: &str = "5102bb3ee77f65a02d79489bebc6e6d86ffc5ff6c4a2ec2b5265a5acb4ba3106"; |
| 19 | +const PUBKEY_2: &str = "3fc863588b1b48ec8e7a077a26112f2356a27ad4717cbb56f289f71a3740eb49"; |
| 20 | +const PUBKEY_3: &str = "8d8ce6639a64a60c02d7d97b1b5bc4ef3b01467f306e0913c275aeb3cc9cb3dc"; |
| 21 | +const PUBKEY_4: &str = "86fe856dc0f9adc12b863d13d03d13dec99bd1112d0425668a6f7237238aa485"; |
| 22 | + |
| 23 | +fn main() { |
| 24 | + let pol_str = format!( |
| 25 | + "or( |
| 26 | + 99@thresh(2, |
| 27 | + pk({}), |
| 28 | + pk({}) |
| 29 | + ),1@or( |
| 30 | + 99@pk({}), |
| 31 | + 1@and(pk({}), |
| 32 | + older(9)) |
| 33 | + ) |
| 34 | + )", |
| 35 | + PUBKEY_1, PUBKEY_2, PUBKEY_3, PUBKEY_4 |
| 36 | + ) |
| 37 | + .replace(&[' ', '\n', '\t'][..], ""); |
| 38 | + |
| 39 | + let pol: Concrete<secp256k1::XOnlyPublicKey> = Concrete::from_str(&pol_str).unwrap(); |
| 40 | + |
| 41 | + // We require secp for generating a random XOnlyPublicKey |
| 42 | + let secp = secp256k1::Secp256k1::new(); |
| 43 | + let key_pair = KeyPair::new(&secp, &mut rand::thread_rng()); |
| 44 | + // Random unspendable XOnlyPublicKey provided for compilation to Taproot Descriptor |
| 45 | + let unspendable_key = secp256k1::XOnlyPublicKey::from_keypair(&key_pair); |
| 46 | + |
| 47 | + let private_desc = pol |
| 48 | + .compile_tr_private(Some(unspendable_key.clone())) |
| 49 | + .unwrap(); |
| 50 | + // println!("{:}", private_desc); |
| 51 | + // let opt_desc = pol.compile_tr(Some(unspendable_key.clone())).unwrap(); |
| 52 | + // println!("{:}", opt_desc); |
| 53 | + |
| 54 | + // Check whether the descriptors are safe. |
| 55 | + assert!(private_desc.sanity_check().is_ok()); |
| 56 | + // assert!(opt_desc.sanity_check().is_ok()); |
| 57 | + |
| 58 | + // Descriptor Type and Version should match respectively for Taproot |
| 59 | + let priv_desc_type = private_desc.desc_type(); |
| 60 | + assert_eq!(priv_desc_type, DescriptorType::Tr); |
| 61 | + // let opt_desc_type = opt_desc.desc_type(); |
| 62 | + // assert_eq!(opt_desc_type, DescriptorType::Tr); |
| 63 | + assert_eq!(priv_desc_type.segwit_version().unwrap(), WitnessVersion::V1); |
| 64 | + // assert_eq!(opt_desc_type.segwit_version().unwrap(), WitnessVersion::V1); |
| 65 | + |
| 66 | + // Compute the bitcoin address and check if it matches |
| 67 | + let network = Network::Bitcoin; |
| 68 | + let priv_addr = private_desc.address(network).unwrap(); |
| 69 | + let expected_addr = bitcoin::Address::from_str( |
| 70 | + "bc1p0y6a05cz95qhj6wlt5fuw7ykh5pq7580hsjkt7tdnd4y8tfv5g0s2w60nu", |
| 71 | + ) |
| 72 | + .unwrap(); |
| 73 | + assert_eq!(priv_addr, expected_addr); |
| 74 | + |
| 75 | + // Computing the underlying script should fail for Taproot Descriptor |
| 76 | + assert!(private_desc.explicit_script().is_err()); |
| 77 | + |
| 78 | + // Max Satisfaction Weight for compilation, corresponding to the script-path spend |
| 79 | + // `multi_a(2,PUBKEY_1,PUBKEY_2) at taptree depth 1, having |
| 80 | + // Max Witness Size = scriptSig len + control_block size + varint(script_size) + script_size + |
| 81 | + // varint(max satisfaction elements) + max satisfaction size |
| 82 | + // = 4 + 65 + 1 + 70 + 1 + 132 |
| 83 | + let max_sat_wt = private_desc.max_satisfaction_weight().unwrap(); |
| 84 | + assert_eq!(max_sat_wt, 273); |
| 85 | + |
| 86 | + if let Descriptor::Tr(p) = private_desc { |
| 87 | + // Check if internal key is correctly inferred as PUBKEY_3 |
| 88 | + assert_eq!(format!("{}", p.internal_key()), PUBKEY_3); |
| 89 | + |
| 90 | + // Check if the constructed TapTree is correctly built |
| 91 | + let expected_taptree: TapTree<bitcoin::XOnlyPublicKey> = TapTree::Tree( |
| 92 | + Arc::new(TapTree::Leaf(Arc::new(Miniscript::<bitcoin::XOnlyPublicKey, Tap>::from_str_insane("and_v(v:pk(86fe856dc0f9adc12b863d13d03d13dec99bd1112d0425668a6f7237238aa485),older(9))").unwrap()))), |
| 93 | + Arc::new(TapTree::Leaf(Arc::new(Miniscript::<bitcoin::XOnlyPublicKey, Tap>::from_str_insane("multi_a(2,5102bb3ee77f65a02d79489bebc6e6d86ffc5ff6c4a2ec2b5265a5acb4ba3106,3fc863588b1b48ec8e7a077a26112f2356a27ad4717cbb56f289f71a3740eb49)").unwrap()))), |
| 94 | + ); |
| 95 | + assert_eq!( |
| 96 | + p.taptree().clone().expect("TapTree expected"), |
| 97 | + expected_taptree |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments