@@ -106,6 +106,20 @@ pub enum PolicyError {
106
106
DuplicatePubKeys ,
107
107
}
108
108
109
+ /// Descriptor context for [`Policy`] compilation into a [`Descriptor`]
110
+ pub enum DescriptorCtx < Pk > {
111
+ /// Bare
112
+ Bare ,
113
+ /// Sh
114
+ Sh ,
115
+ /// Wsh
116
+ Wsh ,
117
+ /// ShWsh
118
+ ShWsh ,
119
+ /// Tr
120
+ Tr ( Option < Pk > ) ,
121
+ }
122
+
109
123
impl error:: Error for PolicyError { }
110
124
111
125
impl fmt:: Display for PolicyError {
@@ -490,6 +504,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
490
504
}
491
505
}
492
506
507
+ /// Compile the [`Policy`] into desc_ctx [`Descriptor`]
508
+ #[ cfg( feature = "compiler" ) ]
509
+ pub fn compile_to_descriptor < Ctx : ScriptContext > (
510
+ & self ,
511
+ desc_ctx : DescriptorCtx < Pk > ,
512
+ ) -> Result < Descriptor < Pk > , Error > {
513
+ self . is_valid ( ) ?;
514
+ match self . is_safe_nonmalleable ( ) {
515
+ ( false , _) => Err ( Error :: from ( CompilerError :: TopLevelNonSafe ) ) ,
516
+ ( _, false ) => Err ( Error :: from (
517
+ CompilerError :: ImpossibleNonMalleableCompilation ,
518
+ ) ) ,
519
+ _ => match desc_ctx {
520
+ DescriptorCtx :: Bare => Descriptor :: new_bare ( compiler:: best_compilation ( self ) ?) ,
521
+ DescriptorCtx :: Sh => Descriptor :: new_sh ( compiler:: best_compilation ( self ) ?) ,
522
+ DescriptorCtx :: Wsh => Descriptor :: new_wsh ( compiler:: best_compilation ( self ) ?) ,
523
+ DescriptorCtx :: ShWsh => Descriptor :: new_sh_wsh ( compiler:: best_compilation ( self ) ?) ,
524
+ DescriptorCtx :: Tr ( unspendable_key) => self . compile_tr ( unspendable_key) ,
525
+ } ,
526
+ }
527
+ }
528
+
493
529
/// Compile the descriptor into an optimized `Miniscript` representation
494
530
#[ cfg( feature = "compiler" ) ]
495
531
pub fn compile < Ctx : ScriptContext > ( & self ) -> Result < Miniscript < Pk , Ctx > , CompilerError > {
0 commit comments