Skip to content

Commit 0572f2b

Browse files
Add Taproot compiler private-version
1 parent 2b13694 commit 0572f2b

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/policy/concrete.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,6 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
175175
}
176176
}
177177

178-
/// Compile [`Policy::Or`] and [`Policy::Threshold`] according to odds
179-
#[cfg(feature = "compiler")]
180-
fn compile_tr_policy(&self) -> Result<TapTree<Pk>, Error> {
181-
let leaf_compilations: Vec<_> = self
182-
.to_tapleaf_prob_vec(1.0)
183-
.into_iter()
184-
.filter(|x| x.1 != Policy::Unsatisfiable)
185-
.map(|(prob, ref policy)| (OrdF64(prob), compiler::best_compilation(policy).unwrap()))
186-
.collect();
187-
let taptree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
188-
Ok(taptree)
189-
}
190-
191178
/// Extract the internal_key from policy tree.
192179
#[cfg(feature = "compiler")]
193180
fn extract_key(self, unspendable_key: Option<Pk>) -> Result<(Pk, Policy<Pk>), Error> {
@@ -244,7 +231,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
244231
/// the probabilitity of satisfaction for the respective branch in the TapTree.
245232
// TODO: We might require other compile errors for Taproot.
246233
#[cfg(feature = "compiler")]
247-
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
234+
pub fn compile_tr_private(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
248235
self.is_valid()?; // Check for validity
249236
match self.is_safe_nonmalleable() {
250237
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
@@ -257,7 +244,18 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
257244
internal_key,
258245
match policy {
259246
Policy::Trivial => None,
260-
policy => Some(policy.compile_tr_policy()?),
247+
policy => {
248+
let leaf_compilations: Vec<_> = policy
249+
.to_tapleaf_prob_vec(1.0)
250+
.into_iter()
251+
.filter(|x| x.1 != Policy::Unsatisfiable)
252+
.map(|(prob, ref pol)| {
253+
(OrdF64(prob), compiler::best_compilation(pol).unwrap())
254+
})
255+
.collect();
256+
let taptree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
257+
Some(taptree)
258+
}
261259
},
262260
)?;
263261
Ok(tree)

src/policy/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ mod tests {
373373
let unspendable_key: String = "UNSPENDABLE".to_string();
374374
{
375375
let policy: Concrete<String> = policy_str!("thresh(2,pk(A),pk(B),pk(C),pk(D))");
376-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
376+
let descriptor = policy
377+
.compile_tr_private(Some(unspendable_key.clone()))
378+
.unwrap();
377379

378380
let ms_compilation: Miniscript<String, Tap> = ms_str!("multi_a(2,A,B,C,D)");
379381
let tree: TapTree<String> = TapTree::Leaf(Arc::new(ms_compilation));
@@ -385,7 +387,9 @@ mod tests {
385387
// Trivial multi-node compilation
386388
{
387389
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(C),pk(D)))");
388-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
390+
let descriptor = policy
391+
.compile_tr_private(Some(unspendable_key.clone()))
392+
.unwrap();
389393

390394
let left_ms_compilation: Arc<Miniscript<String, Tap>> =
391395
Arc::new(ms_str!("and_v(v:pk(C),pk(D))"));
@@ -402,7 +406,7 @@ mod tests {
402406
{
403407
// Invalid policy compilation (Duplicate PubKeys)
404408
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(A),pk(D)))");
405-
let descriptor = policy.compile_tr(Some(unspendable_key.clone()));
409+
let descriptor = policy.compile_tr_private(Some(unspendable_key.clone()));
406410

407411
assert_eq!(
408412
descriptor.unwrap_err().to_string(),
@@ -439,7 +443,9 @@ mod tests {
439443
node_policies[6]
440444
)
441445
);
442-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
446+
let descriptor = policy
447+
.compile_tr_private(Some(unspendable_key.clone()))
448+
.unwrap();
443449

444450
let mut sorted_policy_prob = node_policies
445451
.into_iter()

0 commit comments

Comments
 (0)