Skip to content

Commit 72fde20

Browse files
committed
tr: Implement Simplicity spending info
Simplicity is converted into Bitcoin Script just like Miniscript. Each policy fragment is converted into its CMR. Spending policy fragments will be handled by a later commit.
1 parent 4ffcc00 commit 72fde20

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/descriptor/tr.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,9 @@ impl<Pk: MiniscriptKey, Ext: Extension> Tr<Pk, Ext> {
266266
TaprootSpendInfo::new_key_spend(&secp, self.internal_key.to_x_only_pubkey(), None)
267267
} else {
268268
let mut builder = TaprootBuilder::new();
269-
for (depth, ms) in self.iter_scripts() {
270-
let script = ms.as_miniscript().unwrap().encode();
269+
for (depth, script) in self.iter_scripts() {
271270
builder = builder
272-
.add_leaf(depth, script)
271+
.add_leaf_with_ver(depth, script.encode(), script.version())
273272
.expect("Computing spend data on a valid Tree should always succeed");
274273
}
275274
// Assert builder cannot error here because we have a well formed descriptor
@@ -460,6 +459,19 @@ impl<'a, Pk: MiniscriptKey, Ext: Extension> TapLeafScript<'a, Pk, Ext> {
460459
}
461460
}
462461

462+
impl<'a, Pk: ToPublicKey, Ext: ParseableExt> TapLeafScript<'a, Pk, Ext> {
463+
/// Encode the leaf script as Bitcoin script (witness script).
464+
pub fn encode(&self) -> Script {
465+
match self {
466+
TapLeafScript::Miniscript(ms) => ms.encode(),
467+
TapLeafScript::Simplicity(sim) => {
468+
let commit = sim.serialize_no_witness();
469+
Script::from(commit.cmr().as_ref().to_vec())
470+
}
471+
}
472+
}
473+
}
474+
463475
/// Iterator over the leaves of a tap tree.
464476
///
465477
/// Each leaf consists is a pair of (depth, script).

0 commit comments

Comments
 (0)