|
3 | 3 | //! CheckSigFromStack integration tests
|
4 | 4 | //!
|
5 | 5 |
|
6 |
| -use miniscript::{elements, bitcoin}; |
| 6 | +use miniscript::extensions::{sighash_msg_price_oracle_1, check_sig_price_oracle_1}; |
| 7 | +use miniscript::{elements, bitcoin, TxEnv}; |
7 | 8 | use elements::pset::PartiallySignedTransaction as Psbt;
|
8 | 9 | use elements::sighash::SigHashCache;
|
9 | 10 | use elements::taproot::{LeafVersion, TapLeafHash};
|
@@ -178,16 +179,48 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec
|
178 | 179 | let sig = secp.sign_schnorr_with_aux_rand(&msg, keypair, &aux_rand);
|
179 | 180 | Some(sig)
|
180 | 181 | }
|
| 182 | + |
| 183 | + fn lookup_price_oracle_sig( |
| 184 | + &self, |
| 185 | + pk: &bitcoin::XOnlyPublicKey, |
| 186 | + time: u64, |
| 187 | + ) -> Option<(secp256k1::schnorr::Signature, i64, u64)> { |
| 188 | + let xpk = pk.to_x_only_pubkey(); |
| 189 | + let known_xpks = &self.0.pubdata.x_only_pks; |
| 190 | + let i = known_xpks.iter().position(|&x| x == xpk).unwrap(); |
| 191 | + |
| 192 | + // select a time ahead of the current test time |
| 193 | + let time_signed = time + 1; |
| 194 | + let price = self.0.pubdata.price as u64; |
| 195 | + let sighash_msg = sighash_msg_price_oracle_1(time_signed, price); |
| 196 | + let keypair = &self.0.secretdata.x_only_keypairs[i]; |
| 197 | + let mut aux_rand = [0u8; 32]; |
| 198 | + rand::thread_rng().fill_bytes(&mut aux_rand); |
| 199 | + |
| 200 | + let secp = secp256k1::Secp256k1::new(); |
| 201 | + let sig = secp.sign_schnorr_with_aux_rand(&sighash_msg, keypair, &aux_rand); |
| 202 | + assert!(check_sig_price_oracle_1(&secp, &sig, &xpk, time_signed, price)); |
| 203 | + Some((sig, self.0.pubdata.price, time_signed)) |
| 204 | + } |
181 | 205 | }
|
182 | 206 |
|
183 | 207 | let psbt_sat = PsbtInputSatisfier::new(&psbt, 0);
|
184 | 208 | let csfs_sat = CsfsSatisfier(&testdata);
|
185 | 209 |
|
186 | 210 | let mut tx = psbt.extract_tx().unwrap();
|
| 211 | + let txouts = vec![psbt.inputs()[0].witness_utxo.clone().unwrap()]; |
| 212 | + let extracted_tx = tx.clone(); // Possible to optimize this, but we don't care for this |
| 213 | + // Env requires reference of tx, while satisfaction requires mutable access to inputs. |
| 214 | + let cov_sat = TxEnv::new(&extracted_tx, &txouts, 0).unwrap(); |
187 | 215 | derived_desc
|
188 |
| - .satisfy(&mut tx.input[0], (psbt_sat, csfs_sat)) |
| 216 | + .satisfy(&mut tx.input[0], (psbt_sat, csfs_sat, cov_sat)) |
189 | 217 | .expect("Satisfaction error");
|
190 | 218 |
|
| 219 | + for wit in tx.input[0].witness.script_witness.iter() { |
| 220 | + println!("Witness: {} {:x?}", wit.len(), wit); |
| 221 | + } |
| 222 | + |
| 223 | + |
191 | 224 | // Send the transactions to bitcoin node for mining.
|
192 | 225 | // Regtest mode has standardness checks
|
193 | 226 | // Check whether the node accepts the transactions
|
@@ -222,6 +255,19 @@ fn test_descs(cl: &ElementsD, testdata: &TestData) {
|
222 | 255 | // test combining with other miniscript fragments
|
223 | 256 | let wit = test_desc_satisfy(cl, testdata, "tr(X!,and_b(pk(X2),a:csfs(X1,msg4)))");
|
224 | 257 | assert!(wit.len() == 4);
|
| 258 | + |
| 259 | + // test price oracle 1 |
| 260 | + let price = testdata.pubdata.price; |
| 261 | + let wit = test_desc_satisfy(cl, testdata, &format!("tr(X!,and_v(v:pk(X2),num64_eq(price_oracle1(X1,123213),{})))", price)); |
| 262 | + assert_eq!(wit.len(), 4 + 2); // 4 witness elements + 1 for price oracle + 1 for time |
| 263 | + |
| 264 | + // More complex tests |
| 265 | + test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num64_eq(price_oracle1(X2,1),price_oracle1_w(X3,2))))"); |
| 266 | + test_desc_satisfy(cl, testdata, &format!("tr(X!,and_v(v:pk(X1),num64_eq({},price_oracle1_w(X3,2))))", price)); |
| 267 | + // Different keys and different times |
| 268 | + test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X2),num64_eq(price_oracle1(X3,1),price_oracle1_w(X4,23))))"); |
| 269 | + // Combination with other arith fragments |
| 270 | + test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X2),num64_eq(div(add(price_oracle1(X3,1),price_oracle1_w(X4,2)),2),price_oracle1_w(X5,2))))"); |
225 | 271 | }
|
226 | 272 |
|
227 | 273 | #[test]
|
|
0 commit comments