From bc47538c2b0e126fbea20de9c27f4a6aea61d711 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Sun, 3 Mar 2024 11:00:55 +0100 Subject: [PATCH] add taproot calls in big executable --- examples/big.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/examples/big.rs b/examples/big.rs index 71f97d0eb..02492d00f 100644 --- a/examples/big.rs +++ b/examples/big.rs @@ -10,8 +10,8 @@ //! use std::{collections::HashMap, str::FromStr}; -use bitcoin::ecdsa; -use miniscript::{descriptor::Wsh, policy::{Concrete, Liftable}, psbt::PsbtExt, DefiniteDescriptorKey, Descriptor, DescriptorPublicKey, MiniscriptKey}; +use bitcoin::{ecdsa, XOnlyPublicKey}; +use miniscript::{descriptor::Wsh, policy::{Concrete, Liftable}, psbt::PsbtExt, translate_hash_fail, DefiniteDescriptorKey, Descriptor, DescriptorPublicKey, MiniscriptKey, TranslatePk, Translator}; use secp256k1::Secp256k1; fn main() { let empty = "".to_string(); @@ -47,6 +47,16 @@ fn main() { let d = miniscript::Descriptor::::from_str(&i).unwrap(); let sigs = HashMap::::new(); d.satisfy(&mut tx.input[0], &sigs).unwrap(); + + let pol = Concrete::::from_str(&i).unwrap(); + let desc = pol.compile_tr(Some("UNSPENDABLE_KEY".to_string())).unwrap(); + println!("{}", desc); + let pk_map =HashMap::new(); + let mut t = StrPkTranslator { pk_map }; + let real_desc = desc.translate_pk(&mut t).unwrap(); + println!("{}", real_desc); + let addr = real_desc.address(bitcoin::Network::Bitcoin).unwrap(); + println!("{}", addr); } fn use_descriptor(d: Descriptor) { @@ -54,4 +64,19 @@ fn use_descriptor(d: Descriptor) { println!("{:?}", d); println!("{:?}", d.desc_type()); println!("{:?}", d.sanity_check()); +} + + +struct StrPkTranslator { + pk_map: HashMap, +} + +impl Translator for StrPkTranslator { + fn pk(&mut self, pk: &String) -> Result { + self.pk_map.get(pk).copied().ok_or(()) + } + + // We don't need to implement these methods as we are not using them in the policy. + // Fail if we encounter any hash fragments. See also translate_hash_clone! macro. + translate_hash_fail!(String, XOnlyPublicKey, ()); } \ No newline at end of file