Skip to content

Commit

Permalink
Add taproot compiler example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
SarcasticNastik committed May 24, 2022
1 parent 4e7d7c7 commit 83a685d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ name = "psbt"

[[example]]
name = "xpub_descriptors"

[[example]]
name = "taproot"
required-features = ["compiler"]
44 changes: 44 additions & 0 deletions examples/taproot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
extern crate bitcoin;
extern crate miniscript;

use std::str::FromStr;

#[cfg(feature = "compiler")]
use miniscript::policy::Concrete;
use miniscript::Descriptor;

fn main() {
let policies_str = [
"or(1@pk(A),1@pk(B))",
"or(1@and(pk(A),pk(B)),1@pk(C))",
"or(9@or(10@or(1@pk(A),100@pk(D)),1@pk(B)),1@pk(C))",
"or(1@or(1@or(1@pk(A),1@pk(D)),1@pk(B)),1@pk(C))",
"or(1@pk(A),1@or(1@and(pk(B),pk(C)),1@and(pk(D),older(10))))",
"thresh(1,or(1@pk(A),1@pk(B)),or(1@pk(C),1@or(1@and(pk(E),pk(F)),1@pk(D))))",
"thresh(1,pk(Z),and(pk(A),or(1@pk(B),1@pk(C))),pk(D),or(1@or(1@pk(G),1@pk(E)),1@and(pk(F),pk(X))))",
"thresh(1,and(pk(A),or(1@pk(B),1@pk(C))),or(10@pk(D),7@pk(E)),or(2@or(1@pk(F),1@and(pk(G),pk(H))),7@pk(I)))"
];
let unspendable_key = "UNSPENDABLE_KEY".to_string();
let policies: Vec<Concrete<String>> = policies_str
.into_iter()
.map(|x| Concrete::from_str(x).unwrap())
.collect();
let private_comp: Vec<Descriptor<String>> = policies
.iter()
.map(|pol| {
pol.compile_tr_private(Some(unspendable_key.clone()))
.unwrap()
})
.collect();
let default_comp: Vec<Descriptor<String>> = policies
.iter()
.map(|pol| pol.compile_tr(Some(unspendable_key.clone())).unwrap())
.collect();

for idx in 0..policies.len() {
println!("Example {}", idx + 1);
println!("Policy: {}", policies_str[idx]);
println!("Private compilation: {}", private_comp[idx]);
println!("Default compilation: {}\n", default_comp[idx]);
}
}

0 comments on commit 83a685d

Please sign in to comment.