Skip to content

Commit a4cb70b

Browse files
Add Taproot compiler private-version
1 parent 08c6530 commit a4cb70b

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> {
@@ -245,7 +232,7 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
245232
/// the probabilitity of satisfaction for the respective branch in the TapTree.
246233
// TODO: We might require other compile errors for Taproot.
247234
#[cfg(feature = "compiler")]
248-
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
235+
pub fn compile_tr_private(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
249236
self.is_valid()?; // Check for validity
250237
match self.is_safe_nonmalleable() {
251238
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
@@ -258,7 +245,18 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
258245
internal_key,
259246
match policy {
260247
Policy::Trivial => None,
261-
policy => Some(policy.compile_tr_policy()?),
248+
policy => {
249+
let leaf_compilations: Vec<_> = policy
250+
.to_tapleaf_prob_vec(1.0)
251+
.into_iter()
252+
.filter(|x| x.1 != Policy::Unsatisfiable)
253+
.map(|(prob, ref pol)| {
254+
(OrdF64(prob), compiler::best_compilation(pol).unwrap())
255+
})
256+
.collect();
257+
let taptree = with_huffman_tree::<Pk>(leaf_compilations).unwrap();
258+
Some(taptree)
259+
}
262260
},
263261
)?;
264262
Ok(tree)

src/policy/mod.rs

Lines changed: 10 additions & 4 deletions
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)