Skip to content

Commit fb5cfce

Browse files
Add Taproot compiler private-version
1 parent 658a66b commit fb5cfce

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/policy/concrete.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,6 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
159159
}
160160
}
161161

162-
/// Compile [`Policy::Or`] and [`Policy::Threshold`] according to odds
163-
#[cfg(feature = "compiler")]
164-
fn compile_tr_policy(&self) -> Result<TapTree<Pk>, Error> {
165-
let leaf_compilations: Vec<_> = self
166-
.to_tapleaf_prob_vec(1.0)
167-
.into_iter()
168-
.filter(|x| x.1 != Policy::Unsatisfiable)
169-
.map(|(prob, ref policy)| (OrdF64(prob), compiler::best_compilation(policy).unwrap()))
170-
.collect();
171-
let taptree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
172-
Ok(taptree)
173-
}
174-
175162
/// Extract the internal_key from policy tree.
176163
#[cfg(feature = "compiler")]
177164
fn extract_key(self, unspendable_key: Option<Pk>) -> Result<(Pk, Policy<Pk>), Error> {
@@ -229,7 +216,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
229216
/// the probabilitity of satisfaction for the respective branch in the TapTree.
230217
// TODO: We might require other compile errors for Taproot.
231218
#[cfg(feature = "compiler")]
232-
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
219+
pub fn compile_tr_private(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
233220
self.is_valid()?; // Check for validity
234221
match self.is_safe_nonmalleable() {
235222
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
@@ -242,7 +229,18 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
242229
internal_key,
243230
match policy {
244231
Policy::Trivial => None,
245-
policy => Some(policy.compile_tr_policy()?),
232+
policy => {
233+
let leaf_compilations: Vec<_> = policy
234+
.to_tapleaf_prob_vec(1.0)
235+
.into_iter()
236+
.filter(|x| x.1 != Policy::Unsatisfiable)
237+
.map(|(prob, ref pol)| {
238+
(OrdF64(prob), compiler::best_compilation(pol).unwrap())
239+
})
240+
.collect();
241+
let taptree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
242+
Some(taptree)
243+
}
246244
},
247245
)?;
248246
Ok(tree)

src/policy/mod.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,9 @@ mod tests {
376376
let unspendable_key: String = "UNSPENDABLE".to_string();
377377
{
378378
let policy: Concrete<String> = policy_str!("thresh(2,pk(A),pk(B),pk(C),pk(D))");
379-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
379+
let descriptor = policy
380+
.compile_tr_private(Some(unspendable_key.clone()))
381+
.unwrap();
380382

381383
let ms_compilation: Miniscript<String, Tap> = ms_str!("multi_a(2,A,B,C,D)");
382384
let tree: TapTree<String> = TapTree::Leaf(Arc::new(ms_compilation));
@@ -388,7 +390,9 @@ mod tests {
388390
// Trivial multi-node compilation
389391
{
390392
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(C),pk(D)))");
391-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
393+
let descriptor = policy
394+
.compile_tr_private(Some(unspendable_key.clone()))
395+
.unwrap();
392396

393397
let left_ms_compilation: Arc<Miniscript<String, Tap>> =
394398
Arc::new(ms_str!("and_v(v:pk(C),pk(D))"));
@@ -405,7 +409,7 @@ mod tests {
405409
{
406410
// Invalid policy compilation (Duplicate PubKeys)
407411
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(A),pk(D)))");
408-
let descriptor = policy.compile_tr(Some(unspendable_key.clone()));
412+
let descriptor = policy.compile_tr_private(Some(unspendable_key.clone()));
409413

410414
assert_eq!(
411415
descriptor.unwrap_err().to_string(),
@@ -442,7 +446,9 @@ mod tests {
442446
node_policies[6]
443447
)
444448
);
445-
let descriptor = policy.compile_tr(Some(unspendable_key.clone())).unwrap();
449+
let descriptor = policy
450+
.compile_tr_private(Some(unspendable_key.clone()))
451+
.unwrap();
446452

447453
let mut sorted_policy_prob = node_policies
448454
.into_iter()

0 commit comments

Comments
 (0)