|
| 1 | +//! # Miniscript integration test file format |
| 2 | +//! |
| 3 | +//! This file has custom parsing for miniscripts that enables satisfier to spend transaction |
| 4 | +//! |
| 5 | +//! K : Compressed key available |
| 6 | +//! K!: Compressed key with corresponding secret key unknown |
| 7 | +//! X: X-only key available |
| 8 | +//! X!: X-only key with corresponding secret key unknown |
| 9 | +//! |
| 10 | +//! Example: |
| 11 | +//! pk(K1)/pkh(X1)/multi(n,...K3,...) represents a compressed key 'K1'/(X-only key 'X1') whose private key in known by the wallet |
| 12 | +//! pk(K2!)/pkh(K3!)/multi(n,...K5!,...) represents a key 'K' whose private key is NOT known to the test wallet |
| 13 | +//! sha256(H)/hash256(H)/ripemd160(H)/hash160(H) is hash node whose preimage is known to wallet |
| 14 | +//! sha256(H!)/hash256(H!)/ripemd160(H!)/hash160(H!) is hash node whose preimage is *NOT* known to wallet |
| 15 | +//! timelocks are taken from the transaction value. |
| 16 | +//! |
| 17 | +//! The keys/hashes are automatically translated so that the tests knows how to satisfy things that don't end with ! |
| 18 | +//! |
| 19 | +
|
| 20 | +use std::str::FromStr; |
| 21 | + |
| 22 | +use actual_rand as rand; |
| 23 | +use bitcoin::hashes::hex::ToHex; |
| 24 | +use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash}; |
| 25 | +use bitcoin::secp256k1; |
| 26 | +use miniscript::descriptor::{SinglePub, SinglePubKey}; |
| 27 | +use miniscript::{Descriptor, DescriptorPublicKey, Miniscript, ScriptContext, TranslatePk}; |
| 28 | +use rand::RngCore; |
| 29 | + |
| 30 | +#[derive(Clone, Debug)] |
| 31 | +pub struct PubData { |
| 32 | + pub pks: Vec<bitcoin::PublicKey>, |
| 33 | + pub x_only_pks: Vec<bitcoin::XOnlyPublicKey>, |
| 34 | + pub sha256: sha256::Hash, |
| 35 | + pub hash256: sha256d::Hash, |
| 36 | + pub ripemd160: ripemd160::Hash, |
| 37 | + pub hash160: hash160::Hash, |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Debug, Clone)] |
| 41 | +pub struct SecretData { |
| 42 | + pub sks: Vec<bitcoin::secp256k1::SecretKey>, |
| 43 | + pub x_only_keypairs: Vec<bitcoin::KeyPair>, |
| 44 | + pub sha256_pre: [u8; 32], |
| 45 | + pub hash256_pre: [u8; 32], |
| 46 | + pub ripemd160_pre: [u8; 32], |
| 47 | + pub hash160_pre: [u8; 32], |
| 48 | +} |
| 49 | +#[derive(Debug, Clone)] |
| 50 | +pub struct TestData { |
| 51 | + pub pubdata: PubData, |
| 52 | + pub secretdata: SecretData, |
| 53 | +} |
| 54 | + |
| 55 | +// Setup (sk, pk) pairs |
| 56 | +fn setup_keys( |
| 57 | + n: usize, |
| 58 | +) -> ( |
| 59 | + Vec<bitcoin::secp256k1::SecretKey>, |
| 60 | + Vec<miniscript::bitcoin::PublicKey>, |
| 61 | + Vec<bitcoin::KeyPair>, |
| 62 | + Vec<bitcoin::XOnlyPublicKey>, |
| 63 | +) { |
| 64 | + let secp_sign = secp256k1::Secp256k1::signing_only(); |
| 65 | + let mut sk = [0; 32]; |
| 66 | + let mut sks = vec![]; |
| 67 | + let mut pks = vec![]; |
| 68 | + for i in 1..n + 1 { |
| 69 | + sk[0] = i as u8; |
| 70 | + sk[1] = (i >> 8) as u8; |
| 71 | + sk[2] = (i >> 16) as u8; |
| 72 | + |
| 73 | + let sk = secp256k1::SecretKey::from_slice(&sk[..]).expect("secret key"); |
| 74 | + let pk = miniscript::bitcoin::PublicKey { |
| 75 | + inner: secp256k1::PublicKey::from_secret_key(&secp_sign, &sk), |
| 76 | + compressed: true, |
| 77 | + }; |
| 78 | + pks.push(pk); |
| 79 | + sks.push(sk); |
| 80 | + } |
| 81 | + |
| 82 | + let mut x_only_keypairs = vec![]; |
| 83 | + let mut x_only_pks = vec![]; |
| 84 | + |
| 85 | + for i in 0..n { |
| 86 | + let keypair = bitcoin::KeyPair::from_secret_key(&secp_sign, sks[i]); |
| 87 | + let xpk = bitcoin::XOnlyPublicKey::from_keypair(&keypair); |
| 88 | + x_only_keypairs.push(keypair); |
| 89 | + x_only_pks.push(xpk); |
| 90 | + } |
| 91 | + (sks, pks, x_only_keypairs, x_only_pks) |
| 92 | +} |
| 93 | + |
| 94 | +impl TestData { |
| 95 | + // generate a fixed data for n keys |
| 96 | + pub(crate) fn new_fixed_data(n: usize) -> Self { |
| 97 | + let (sks, pks, x_only_keypairs, x_only_pks) = setup_keys(n); |
| 98 | + let sha256_pre = [0x12 as u8; 32]; |
| 99 | + let sha256 = sha256::Hash::hash(&sha256_pre); |
| 100 | + let hash256_pre = [0x34 as u8; 32]; |
| 101 | + let hash256 = sha256d::Hash::hash(&hash256_pre); |
| 102 | + let hash160_pre = [0x56 as u8; 32]; |
| 103 | + let hash160 = hash160::Hash::hash(&hash160_pre); |
| 104 | + let ripemd160_pre = [0x78 as u8; 32]; |
| 105 | + let ripemd160 = ripemd160::Hash::hash(&ripemd160_pre); |
| 106 | + |
| 107 | + let pubdata = PubData { |
| 108 | + pks, |
| 109 | + sha256, |
| 110 | + hash256, |
| 111 | + ripemd160, |
| 112 | + hash160, |
| 113 | + x_only_pks, |
| 114 | + }; |
| 115 | + let secretdata = SecretData { |
| 116 | + sks, |
| 117 | + sha256_pre, |
| 118 | + hash256_pre, |
| 119 | + ripemd160_pre, |
| 120 | + hash160_pre, |
| 121 | + x_only_keypairs, |
| 122 | + }; |
| 123 | + Self { |
| 124 | + pubdata, |
| 125 | + secretdata, |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// Obtain an insecure random public key with unknown secret key for testing |
| 131 | +pub fn random_pk(mut seed: u8) -> bitcoin::PublicKey { |
| 132 | + loop { |
| 133 | + let mut data = [0; 33]; |
| 134 | + for byte in &mut data[..] { |
| 135 | + *byte = seed; |
| 136 | + // totally a rng |
| 137 | + seed = seed.wrapping_mul(41).wrapping_add(53); |
| 138 | + } |
| 139 | + data[0] = 2 + (data[0] >> 7); |
| 140 | + if let Ok(key) = bitcoin::PublicKey::from_slice(&data[..33]) { |
| 141 | + return key; |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/// Parse an insane miniscript into a miniscript with the format described above at file header |
| 147 | +pub fn parse_insane_ms<Ctx: ScriptContext>( |
| 148 | + ms: &str, |
| 149 | + pubdata: &PubData, |
| 150 | +) -> Miniscript<DescriptorPublicKey, Ctx> { |
| 151 | + let ms = subs_hash_frag(ms, pubdata); |
| 152 | + let ms = |
| 153 | + Miniscript::<String, Ctx>::from_str_insane(&ms).expect("only parsing valid minsicripts"); |
| 154 | + let mut i = 0; |
| 155 | + let mut j = pubdata.pks.len(); |
| 156 | + let ms = ms.translate_pk_infallible( |
| 157 | + &mut |pk_str: &String| { |
| 158 | + let avail = !pk_str.ends_with("!"); |
| 159 | + if avail { |
| 160 | + i = i + 1; |
| 161 | + if pk_str.starts_with("K") { |
| 162 | + DescriptorPublicKey::Single(SinglePub { |
| 163 | + origin: None, |
| 164 | + key: SinglePubKey::FullKey(pubdata.pks[i]), |
| 165 | + }) |
| 166 | + } else if pk_str.starts_with("X") { |
| 167 | + DescriptorPublicKey::Single(SinglePub { |
| 168 | + origin: None, |
| 169 | + key: SinglePubKey::XOnly(pubdata.x_only_pks[i]), |
| 170 | + }) |
| 171 | + } else { |
| 172 | + // Parse any other keys as known to allow compatibility with existing tests |
| 173 | + DescriptorPublicKey::Single(SinglePub { |
| 174 | + origin: None, |
| 175 | + key: SinglePubKey::FullKey(pubdata.pks[i]), |
| 176 | + }) |
| 177 | + } |
| 178 | + } else { |
| 179 | + DescriptorPublicKey::Single(SinglePub { |
| 180 | + origin: None, |
| 181 | + key: SinglePubKey::FullKey(random_pk(59)), |
| 182 | + }) |
| 183 | + } |
| 184 | + }, |
| 185 | + &mut |pk_str: &String| { |
| 186 | + let avail = !pk_str.ends_with("!"); |
| 187 | + if avail { |
| 188 | + j = j - 1; |
| 189 | + if pk_str.starts_with("K") { |
| 190 | + DescriptorPublicKey::Single(SinglePub { |
| 191 | + origin: None, |
| 192 | + key: SinglePubKey::FullKey(pubdata.pks[j]), |
| 193 | + }) |
| 194 | + } else if pk_str.starts_with("X") { |
| 195 | + DescriptorPublicKey::Single(SinglePub { |
| 196 | + origin: None, |
| 197 | + key: SinglePubKey::XOnly(pubdata.x_only_pks[j]), |
| 198 | + }) |
| 199 | + } else { |
| 200 | + // Parse any other keys as known to allow compatibility with existing tests |
| 201 | + DescriptorPublicKey::Single(SinglePub { |
| 202 | + origin: None, |
| 203 | + key: SinglePubKey::FullKey(pubdata.pks[j]), |
| 204 | + }) |
| 205 | + } |
| 206 | + } else { |
| 207 | + DescriptorPublicKey::Single(SinglePub { |
| 208 | + origin: None, |
| 209 | + key: SinglePubKey::FullKey(random_pk(59)), |
| 210 | + }) |
| 211 | + } |
| 212 | + }, |
| 213 | + ); |
| 214 | + ms |
| 215 | +} |
| 216 | + |
| 217 | +pub fn parse_test_desc(desc: &str, pubdata: &PubData) -> Descriptor<DescriptorPublicKey> { |
| 218 | + let desc = subs_hash_frag(desc, pubdata); |
| 219 | + let desc = |
| 220 | + Descriptor::<String>::from_str(&desc).expect("only parsing valid and sane descriptors"); |
| 221 | + let mut i = 0; |
| 222 | + let mut j = pubdata.pks.len(); |
| 223 | + let desc: Result<_, ()> = desc.translate_pk( |
| 224 | + &mut |pk_str: &'_ String| { |
| 225 | + let avail = !pk_str.ends_with("!"); |
| 226 | + if avail { |
| 227 | + i = i + 1; |
| 228 | + if pk_str.starts_with("K") { |
| 229 | + Ok(DescriptorPublicKey::Single(SinglePub { |
| 230 | + origin: None, |
| 231 | + key: SinglePubKey::FullKey(pubdata.pks[i]), |
| 232 | + })) |
| 233 | + } else if pk_str.starts_with("X") { |
| 234 | + Ok(DescriptorPublicKey::Single(SinglePub { |
| 235 | + origin: None, |
| 236 | + key: SinglePubKey::XOnly(pubdata.x_only_pks[i]), |
| 237 | + })) |
| 238 | + } else { |
| 239 | + panic!("Key must start with either K or X") |
| 240 | + } |
| 241 | + } else { |
| 242 | + Ok(DescriptorPublicKey::Single(SinglePub { |
| 243 | + origin: None, |
| 244 | + key: SinglePubKey::FullKey(random_pk(59)), |
| 245 | + })) |
| 246 | + } |
| 247 | + }, |
| 248 | + &mut |pkh_str: &'_ String| { |
| 249 | + let avail = !pkh_str.ends_with("!"); |
| 250 | + if avail { |
| 251 | + j = j - 1; |
| 252 | + if pkh_str.starts_with("K") { |
| 253 | + Ok(DescriptorPublicKey::Single(SinglePub { |
| 254 | + origin: None, |
| 255 | + key: SinglePubKey::FullKey(pubdata.pks[j]), |
| 256 | + })) |
| 257 | + } else if pkh_str.starts_with("X") { |
| 258 | + Ok(DescriptorPublicKey::Single(SinglePub { |
| 259 | + origin: None, |
| 260 | + key: SinglePubKey::XOnly(pubdata.x_only_pks[j]), |
| 261 | + })) |
| 262 | + } else { |
| 263 | + panic!("Key must start with either K or X") |
| 264 | + } |
| 265 | + } else { |
| 266 | + Ok(DescriptorPublicKey::Single(SinglePub { |
| 267 | + origin: None, |
| 268 | + key: SinglePubKey::FullKey(random_pk(61)), |
| 269 | + })) |
| 270 | + } |
| 271 | + }, |
| 272 | + ); |
| 273 | + desc.expect("Translate must succeed") |
| 274 | +} |
| 275 | + |
| 276 | +// substitute hash fragments in the string as the per rules |
| 277 | +fn subs_hash_frag(ms: &str, pubdata: &PubData) -> String { |
| 278 | + let ms = ms.replace( |
| 279 | + "sha256(H)", |
| 280 | + &format!("sha256({})", &pubdata.sha256.to_hex()), |
| 281 | + ); |
| 282 | + let ms = ms.replace( |
| 283 | + "hash256(H)", |
| 284 | + &format!("hash256({})", &pubdata.hash256.into_inner().to_hex()), |
| 285 | + ); |
| 286 | + let ms = ms.replace( |
| 287 | + "ripemd160(H)", |
| 288 | + &format!("ripemd160({})", &pubdata.ripemd160.to_hex()), |
| 289 | + ); |
| 290 | + let ms = ms.replace( |
| 291 | + "hash160(H)", |
| 292 | + &format!("hash160({})", &pubdata.hash160.to_hex()), |
| 293 | + ); |
| 294 | + |
| 295 | + let mut rand_hash32 = [0u8; 32]; |
| 296 | + rand::thread_rng().fill_bytes(&mut rand_hash32); |
| 297 | + |
| 298 | + let mut rand_hash20 = [0u8; 20]; |
| 299 | + rand::thread_rng().fill_bytes(&mut rand_hash20); |
| 300 | + let ms = ms.replace("sha256(H!)", &format!("sha256({})", rand_hash32.to_hex())); |
| 301 | + let ms = ms.replace("hash256(H!)", &format!("hash256({})", rand_hash32.to_hex())); |
| 302 | + let ms = ms.replace( |
| 303 | + "ripemd160(H!)", |
| 304 | + &format!("ripemd160({})", rand_hash20.to_hex()), |
| 305 | + ); |
| 306 | + let ms = ms.replace("hash160(H!)", &format!("hash160({})", rand_hash20.to_hex())); |
| 307 | + ms |
| 308 | +} |
0 commit comments