@@ -26,6 +26,10 @@ use crate::{
26
26
Threshold , ToPublicKey , TranslateErr , Translator ,
27
27
} ;
28
28
29
+ mod taptree;
30
+
31
+ pub use self :: taptree:: TapTreeIterItem ;
32
+
29
33
/// A Taproot Tree representation.
30
34
// Hidden leaves are not yet supported in descriptor spec. Conceptually, it should
31
35
// be simple to integrate those here, but it is best to wait on core for the exact syntax.
@@ -226,10 +230,10 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
226
230
TaprootSpendInfo :: new_key_spend ( & secp, self . internal_key . to_x_only_pubkey ( ) , None )
227
231
} else {
228
232
let mut builder = TaprootBuilder :: new ( ) ;
229
- for ( depth , ms ) in self . iter_scripts ( ) {
230
- let script = ms . encode ( ) ;
233
+ for leaf in self . iter_scripts ( ) {
234
+ let script = leaf . miniscript ( ) . encode ( ) ;
231
235
builder = builder
232
- . add_leaf ( depth, script)
236
+ . add_leaf ( leaf . depth ( ) , script)
233
237
. expect ( "Computing spend data on a valid Tree should always succeed" ) ;
234
238
}
235
239
// Assert builder cannot error here because we have a well formed descriptor
@@ -245,8 +249,8 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
245
249
246
250
/// Checks whether the descriptor is safe.
247
251
pub fn sanity_check ( & self ) -> Result < ( ) , Error > {
248
- for ( _depth , ms ) in self . iter_scripts ( ) {
249
- ms . sanity_check ( ) ?;
252
+ for leaf in self . iter_scripts ( ) {
253
+ leaf . miniscript ( ) . sanity_check ( ) ?;
250
254
}
251
255
Ok ( ( ) )
252
256
}
@@ -276,11 +280,11 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
276
280
277
281
let wu = tree
278
282
. iter ( )
279
- . filter_map ( |( depth , ms ) | {
280
- let script_size = ms . script_size ( ) ;
281
- let max_sat_elems = ms . max_satisfaction_witness_elements ( ) . ok ( ) ?;
282
- let max_sat_size = ms . max_satisfaction_size ( ) . ok ( ) ?;
283
- let control_block_size = control_block_len ( depth) ;
283
+ . filter_map ( |leaf | {
284
+ let script_size = leaf . miniscript ( ) . script_size ( ) ;
285
+ let max_sat_elems = leaf . miniscript ( ) . max_satisfaction_witness_elements ( ) . ok ( ) ?;
286
+ let max_sat_size = leaf . miniscript ( ) . max_satisfaction_size ( ) . ok ( ) ?;
287
+ let control_block_size = control_block_len ( leaf . depth ( ) ) ;
284
288
285
289
// stack varint difference (+1 for ctrl block, witness script already included)
286
290
let stack_varint_diff = varint_len ( max_sat_elems + 1 ) - varint_len ( 0 ) ;
@@ -326,11 +330,11 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
326
330
} ;
327
331
328
332
tree. iter ( )
329
- . filter_map ( |( depth , ms ) | {
330
- let script_size = ms . script_size ( ) ;
331
- let max_sat_elems = ms . max_satisfaction_witness_elements ( ) . ok ( ) ?;
332
- let max_sat_size = ms . max_satisfaction_size ( ) . ok ( ) ?;
333
- let control_block_size = control_block_len ( depth) ;
333
+ . filter_map ( |leaf | {
334
+ let script_size = leaf . miniscript ( ) . script_size ( ) ;
335
+ let max_sat_elems = leaf . miniscript ( ) . max_satisfaction_witness_elements ( ) . ok ( ) ?;
336
+ let max_sat_size = leaf . miniscript ( ) . max_satisfaction_size ( ) . ok ( ) ?;
337
+ let control_block_size = control_block_len ( leaf . depth ( ) ) ;
334
338
Some (
335
339
// scriptSig len byte
336
340
4 +
@@ -473,7 +477,7 @@ impl<'a, Pk> Iterator for TapTreeIter<'a, Pk>
473
477
where
474
478
Pk : MiniscriptKey + ' a ,
475
479
{
476
- type Item = ( u8 , & ' a Miniscript < Pk , Tap > ) ;
480
+ type Item = TapTreeIterItem < ' a , Pk > ;
477
481
478
482
fn next ( & mut self ) -> Option < Self :: Item > {
479
483
while let Some ( ( depth, last) ) = self . stack . pop ( ) {
@@ -482,7 +486,7 @@ where
482
486
self . stack . push ( ( depth + 1 , right) ) ;
483
487
self . stack . push ( ( depth + 1 , left) ) ;
484
488
}
485
- TapTree :: Leaf ( ref ms) => return Some ( ( depth , ms ) ) ,
489
+ TapTree :: Leaf ( ref ms) => return Some ( TapTreeIterItem { node : ms , depth } ) ,
486
490
}
487
491
}
488
492
None
@@ -629,7 +633,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
629
633
fn for_each_key < ' a , F : FnMut ( & ' a Pk ) -> bool > ( & ' a self , mut pred : F ) -> bool {
630
634
let script_keys_res = self
631
635
. iter_scripts ( )
632
- . all ( |( _d , ms ) | ms . for_each_key ( & mut pred) ) ;
636
+ . all ( |leaf| leaf . miniscript ( ) . for_each_key ( & mut pred) ) ;
633
637
script_keys_res && pred ( & self . internal_key )
634
638
}
635
639
}
@@ -673,14 +677,14 @@ where
673
677
absolute_timelock : None ,
674
678
} ;
675
679
let mut min_wit_len = None ;
676
- for ( _depth , ms ) in desc. iter_scripts ( ) {
680
+ for leaf in desc. iter_scripts ( ) {
677
681
let mut satisfaction = if allow_mall {
678
- match ms . build_template ( provider) {
682
+ match leaf . miniscript ( ) . build_template ( provider) {
679
683
s @ Satisfaction { stack : Witness :: Stack ( _) , .. } => s,
680
684
_ => continue , // No witness for this script in tr descriptor, look for next one
681
685
}
682
686
} else {
683
- match ms . build_template_mall ( provider) {
687
+ match leaf . miniscript ( ) . build_template_mall ( provider) {
684
688
s @ Satisfaction { stack : Witness :: Stack ( _) , .. } => s,
685
689
_ => continue , // No witness for this script in tr descriptor, look for next one
686
690
}
@@ -690,7 +694,7 @@ where
690
694
_ => unreachable ! ( ) ,
691
695
} ;
692
696
693
- let leaf_script = ( ms . encode ( ) , LeafVersion :: TapScript ) ;
697
+ let leaf_script = ( leaf . compute_script ( ) , LeafVersion :: TapScript ) ;
694
698
let control_block = spend_info
695
699
. control_block ( & leaf_script)
696
700
. expect ( "Control block must exist in script map for every known leaf" ) ;
0 commit comments