@@ -37,6 +37,7 @@ pub enum TapTree<Pk: MiniscriptKey, Ext: Extension = NoExt> {
37
37
// are of Leafversion::default
38
38
Leaf ( Arc < Miniscript < Pk , Tap , Ext > > ) ,
39
39
/// A taproot leaf denoting a spending condition in terms of Simplicity
40
+ #[ cfg( feature = "simplicity" ) ]
40
41
SimplicityLeaf ( Arc < simplicity:: Policy < Pk > > ) ,
41
42
}
42
43
@@ -119,7 +120,9 @@ impl<Pk: MiniscriptKey, Ext: Extension> TapTree<Pk, Ext> {
119
120
TapTree :: Tree ( ref left_tree, ref right_tree) => {
120
121
1 + max ( left_tree. taptree_height ( ) , right_tree. taptree_height ( ) )
121
122
}
122
- TapTree :: Leaf ( ..) | TapTree :: SimplicityLeaf ( ..) => 0 ,
123
+ TapTree :: Leaf ( ..) => 0 ,
124
+ #[ cfg( feature = "simplicity" ) ]
125
+ TapTree :: SimplicityLeaf ( ..) => 0 ,
123
126
}
124
127
}
125
128
@@ -138,8 +141,10 @@ impl<Pk: MiniscriptKey, Ext: Extension> TapTree<Pk, Ext> {
138
141
Q : MiniscriptKey ,
139
142
Ext : Extension ,
140
143
{
144
+ #[ cfg( feature = "simplicity" ) ]
141
145
struct SimTranslator < ' a , T > ( & ' a mut T ) ;
142
146
147
+ #[ cfg( feature = "simplicity" ) ]
143
148
impl < ' a , Pk , T , Q , Error > simplicity:: Translator < Pk , Q , Error > for SimTranslator < ' a , T >
144
149
where
145
150
Pk : MiniscriptKey ,
@@ -161,6 +166,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> TapTree<Pk, Ext> {
161
166
Arc :: new ( r. translate_helper ( t) ?) ,
162
167
) ,
163
168
TapTree :: Leaf ( ms) => TapTree :: Leaf ( Arc :: new ( ms. translate_pk ( t) ?) ) ,
169
+ #[ cfg( feature = "simplicity" ) ]
164
170
TapTree :: SimplicityLeaf ( sim) => TapTree :: SimplicityLeaf ( Arc :: new ( sim. translate ( & mut SimTranslator ( t) ) ?) )
165
171
} ;
166
172
Ok ( frag)
@@ -179,6 +185,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> TapTree<Pk, Ext> {
179
185
Arc :: new ( r. translate_ext_helper ( t) ?) ,
180
186
) ,
181
187
TapTree :: Leaf ( ms) => TapTree :: Leaf ( Arc :: new ( ms. translate_ext ( t) ?) ) ,
188
+ #[ cfg( feature = "simplicity" ) ]
182
189
TapTree :: SimplicityLeaf ( sim) => TapTree :: SimplicityLeaf ( Arc :: clone ( sim) ) ,
183
190
} ;
184
191
Ok ( frag)
@@ -190,6 +197,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> fmt::Display for TapTree<Pk, Ext> {
190
197
match self {
191
198
TapTree :: Tree ( ref left, ref right) => write ! ( f, "{{{},{}}}" , * left, * right) ,
192
199
TapTree :: Leaf ( ref script) => write ! ( f, "{}" , * script) ,
200
+ #[ cfg( feature = "simplicity" ) ]
193
201
TapTree :: SimplicityLeaf ( ref policy) => write ! ( f, "sim{{{}}}" , policy) ,
194
202
}
195
203
}
@@ -200,6 +208,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> fmt::Debug for TapTree<Pk, Ext> {
200
208
match self {
201
209
TapTree :: Tree ( ref left, ref right) => write ! ( f, "{{{:?},{:?}}}" , * left, * right) ,
202
210
TapTree :: Leaf ( ref script) => write ! ( f, "{:?}" , * script) ,
211
+ #[ cfg( feature = "simplicity" ) ]
203
212
TapTree :: SimplicityLeaf ( ref policy) => write ! ( f, "{:?}" , policy) ,
204
213
}
205
214
}
@@ -288,6 +297,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> Tr<Pk, Ext> {
288
297
match script {
289
298
TapLeafScript :: Miniscript ( ms) => ms. sanity_check ( ) ?,
290
299
// TODO: Add sanity check for Simplicity policies
300
+ #[ cfg( feature = "simplicity" ) ]
291
301
TapLeafScript :: Simplicity ( ..) => { } ,
292
302
}
293
303
}
@@ -436,6 +446,7 @@ pub enum TapLeafScript<'a, Pk: MiniscriptKey, Ext: Extension> {
436
446
/// Miniscript leaf
437
447
Miniscript ( & ' a Miniscript < Pk , Tap , Ext > ) ,
438
448
/// Simplicity leaf
449
+ #[ cfg( feature = "simplicity" ) ]
439
450
Simplicity ( & ' a simplicity:: Policy < Pk > )
440
451
}
441
452
@@ -444,11 +455,13 @@ impl<'a, Pk: MiniscriptKey, Ext: Extension> TapLeafScript<'a, Pk, Ext> {
444
455
pub fn as_miniscript ( & self ) -> Option < & ' a Miniscript < Pk , Tap , Ext > > {
445
456
match self {
446
457
TapLeafScript :: Miniscript ( ms) => Some ( ms) ,
458
+ #[ cfg( feature = "simplicity" ) ]
447
459
_ => None ,
448
460
}
449
461
}
450
462
451
463
/// Get the Simplicity policy at the leaf, if it exists.
464
+ #[ cfg( feature = "simplicity" ) ]
452
465
pub fn as_simplicity ( & self ) -> Option < & ' a simplicity:: Policy < Pk > > {
453
466
match self {
454
467
TapLeafScript :: Simplicity ( sim) => Some ( sim) ,
@@ -460,6 +473,7 @@ impl<'a, Pk: MiniscriptKey, Ext: Extension> TapLeafScript<'a, Pk, Ext> {
460
473
pub fn version ( & self ) -> LeafVersion {
461
474
match self {
462
475
TapLeafScript :: Miniscript ( ..) => LeafVersion :: default ( ) ,
476
+ #[ cfg( feature = "simplicity" ) ]
463
477
TapLeafScript :: Simplicity ( ..) => simplicity:: leaf_version ( ) ,
464
478
}
465
479
}
@@ -469,6 +483,7 @@ impl<'a, Pk: MiniscriptKey, Ext: Extension> TapLeafScript<'a, Pk, Ext> {
469
483
match self {
470
484
TapLeafScript :: Miniscript ( ms) => ms. script_size ( ) ,
471
485
// Simplicity's witness script is always a 32-byte CMR
486
+ #[ cfg( feature = "simplicity" ) ]
472
487
TapLeafScript :: Simplicity ( ..) => 32 ,
473
488
}
474
489
}
@@ -482,6 +497,7 @@ impl<'a, Pk: MiniscriptKey, Ext: Extension> TapLeafScript<'a, Pk, Ext> {
482
497
// (1) Encoded program+witness
483
498
// (2) CMR program
484
499
// The third element is the control block, which is not counted by this method.
500
+ #[ cfg( feature = "simplicity" ) ]
485
501
TapLeafScript :: Simplicity ( ..) => Ok ( 2 ) ,
486
502
}
487
503
}
@@ -493,6 +509,7 @@ impl<'a, Pk: MiniscriptKey, Ext: Extension> TapLeafScript<'a, Pk, Ext> {
493
509
// There is currently no way to bound the Simplicity witness size without producing one
494
510
// We mark the witness size as malleable since it depends on the chosen spending path
495
511
// TODO: Add method to simplicity::Policy and use it here
512
+ #[ cfg( feature = "simplicity" ) ]
496
513
TapLeafScript :: Simplicity ( ..) => Err ( Error :: AnalysisError ( crate :: AnalysisError :: Malleable ) )
497
514
}
498
515
}
@@ -501,6 +518,7 @@ impl<'a, Pk: MiniscriptKey, Ext: Extension> TapLeafScript<'a, Pk, Ext> {
501
518
pub fn iter_pk ( & self ) -> Box < dyn Iterator < Item =Pk > + ' a > {
502
519
match self {
503
520
TapLeafScript :: Miniscript ( ms) => Box :: new ( ms. iter_pk ( ) ) ,
521
+ #[ cfg( feature = "simplicity" ) ]
504
522
TapLeafScript :: Simplicity ( sim) => Box :: new ( sim. iter_pk ( ) ) ,
505
523
}
506
524
}
@@ -511,6 +529,7 @@ impl<'a, Pk: ToPublicKey, Ext: ParseableExt> TapLeafScript<'a, Pk, Ext> {
511
529
pub fn encode ( & self ) -> Script {
512
530
match self {
513
531
TapLeafScript :: Miniscript ( ms) => ms. encode ( ) ,
532
+ #[ cfg( feature = "simplicity" ) ]
514
533
TapLeafScript :: Simplicity ( sim) => {
515
534
Script :: from ( sim. cmr ( ) . as_ref ( ) . to_vec ( ) )
516
535
}
@@ -522,6 +541,7 @@ impl<'a, Pk: ToPublicKey, Ext: ParseableExt> TapLeafScript<'a, Pk, Ext> {
522
541
match self {
523
542
TapLeafScript :: Miniscript ( ms) => ms. satisfy_malleable ( satisfier) ,
524
543
// There doesn't (yet?) exist a malleable satisfaction of Simplicity policy
544
+ #[ cfg( feature = "simplicity" ) ]
525
545
TapLeafScript :: Simplicity ( ..) => self . satisfy ( satisfier) ,
526
546
}
527
547
}
@@ -530,6 +550,7 @@ impl<'a, Pk: ToPublicKey, Ext: ParseableExt> TapLeafScript<'a, Pk, Ext> {
530
550
pub fn satisfy < S : Satisfier < Pk > > ( & self , satisfier : S ) -> Result < Vec < Vec < u8 > > , Error > {
531
551
match self {
532
552
TapLeafScript :: Miniscript ( ms) => ms. satisfy ( satisfier) ,
553
+ #[ cfg( feature = "simplicity" ) ]
533
554
TapLeafScript :: Simplicity ( sim) => {
534
555
let satisfier = crate :: simplicity:: SatisfierWrapper :: new ( satisfier) ;
535
556
let program = sim. satisfy ( & satisfier) . map_err ( |_| Error :: CouldNotSatisfy ) ?;
@@ -577,6 +598,7 @@ where
577
598
TapTree :: Leaf ( ref ms) => {
578
599
return Some ( ( depth, TapLeafScript :: Miniscript ( ms) ) )
579
600
} ,
601
+ #[ cfg( feature = "simplicity" ) ]
580
602
TapTree :: SimplicityLeaf ( ref sim) => {
581
603
return Some ( ( depth, TapLeafScript :: Simplicity ( sim) ) )
582
604
}
@@ -593,6 +615,7 @@ impl_block_str!(
593
615
// Helper function to parse taproot script path
594
616
fn parse_tr_script_spend( tree: & expression:: Tree , ) -> Result <TapTree <Pk , Ext >, Error > {
595
617
match tree {
618
+ #[ cfg( feature = "simplicity" ) ]
596
619
expression:: Tree { name, args } if * name == "sim" && args. len( ) == 1 => {
597
620
let policy = crate :: simplicity:: PolicyWrapper :: <Pk >:: from_str( args[ 0 ] . name) ?;
598
621
Ok ( TapTree :: SimplicityLeaf ( Arc :: new( policy. 0 ) ) )
@@ -771,6 +794,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> Liftable<Pk> for TapTree<Pk, Ext> {
771
794
Ok ( Policy :: Threshold ( 1 , vec ! [ lift_helper( l) ?, lift_helper( r) ?] ) )
772
795
}
773
796
TapTree :: Leaf ( ref leaf) => leaf. lift ( ) ,
797
+ #[ cfg( feature = "simplicity" ) ]
774
798
TapTree :: SimplicityLeaf ( ..) => panic ! ( "FIXME: Cannot lift Simplicity policy to Miniscript semantic policy" ) ,
775
799
}
776
800
}
@@ -802,6 +826,7 @@ impl<Pk: MiniscriptKey, Ext: Extension> ForEachKey<Pk> for Tr<Pk, Ext> {
802
826
. all ( |( _d, script) | {
803
827
match script {
804
828
TapLeafScript :: Miniscript ( ms) => ms. for_each_key ( & mut pred) ,
829
+ #[ cfg( feature = "simplicity" ) ]
805
830
TapLeafScript :: Simplicity ( sim) => crate :: simplicity:: for_each_key ( sim, & mut pred) ,
806
831
}
807
832
} ) ;
@@ -976,17 +1001,20 @@ mod tests {
976
1001
& [ TapLeafScript :: Miniscript ( & ms) ]
977
1002
) ;
978
1003
979
- // Simplicity key spend
980
- let sim = simplicity:: Policy :: Key ( "a" . to_string ( ) ) ;
981
- verify_from_str (
982
- "eltr(internal,sim{pk(a)})#duhmnzmm" , "internal" ,
983
- & [ TapLeafScript :: Simplicity ( & sim) ]
984
- ) ;
985
-
986
- // Mixed Miniscript and Simplicity
987
- verify_from_str (
988
- "eltr(internal,{pk(a),sim{pk(a)}})#7vmfhpaj" , "internal" ,
989
- & [ TapLeafScript :: Miniscript ( & ms) , TapLeafScript :: Simplicity ( & sim) ]
990
- ) ;
1004
+ #[ cfg( feature = "simplicity" ) ]
1005
+ {
1006
+ // Simplicity key spend
1007
+ let sim = simplicity:: Policy :: Key ( "a" . to_string ( ) ) ;
1008
+ verify_from_str (
1009
+ "eltr(internal,sim{pk(a)})#duhmnzmm" , "internal" ,
1010
+ & [ TapLeafScript :: Simplicity ( & sim) ]
1011
+ ) ;
1012
+
1013
+ // Mixed Miniscript and Simplicity
1014
+ verify_from_str (
1015
+ "eltr(internal,{pk(a),sim{pk(a)}})#7vmfhpaj" , "internal" ,
1016
+ & [ TapLeafScript :: Miniscript ( & ms) , TapLeafScript :: Simplicity ( & sim) ]
1017
+ ) ;
1018
+ }
991
1019
}
992
1020
}
0 commit comments