@@ -108,6 +108,20 @@ pub enum PolicyError {
108
108
DuplicatePubKeys ,
109
109
}
110
110
111
+ /// Descriptor context for [`Policy`] compilation into a [`Descriptor`]
112
+ pub enum DescriptorCtx < Pk > {
113
+ /// [Bare][`Descriptor::Bare`]
114
+ Bare ,
115
+ /// [Sh][`Descriptor::Sh`]
116
+ Sh ,
117
+ /// [Wsh][`Descriptor::Wsh`]
118
+ Wsh ,
119
+ /// Sh-wrapped [Wsh][`Descriptor::Wsh`]
120
+ ShWsh ,
121
+ /// [Tr][`Descriptor::Tr`], where the Option<Pk> corresponds to the internal_key.
122
+ Tr ( Option < Pk > ) ,
123
+ }
124
+
111
125
impl fmt:: Display for PolicyError {
112
126
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
113
127
match * self {
@@ -356,6 +370,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
356
370
}
357
371
}
358
372
373
+ /// Compile the [`Policy`] into desc_ctx [`Descriptor`]
374
+ #[ cfg( feature = "compiler" ) ]
375
+ pub fn compile_to_descriptor < Ctx : ScriptContext > (
376
+ & self ,
377
+ desc_ctx : DescriptorCtx < Pk > ,
378
+ ) -> Result < Descriptor < Pk > , Error > {
379
+ self . is_valid ( ) ?;
380
+ match self . is_safe_nonmalleable ( ) {
381
+ ( false , _) => Err ( Error :: from ( CompilerError :: TopLevelNonSafe ) ) ,
382
+ ( _, false ) => Err ( Error :: from (
383
+ CompilerError :: ImpossibleNonMalleableCompilation ,
384
+ ) ) ,
385
+ _ => match desc_ctx {
386
+ DescriptorCtx :: Bare => Descriptor :: new_bare ( compiler:: best_compilation ( self ) ?) ,
387
+ DescriptorCtx :: Sh => Descriptor :: new_sh ( compiler:: best_compilation ( self ) ?) ,
388
+ DescriptorCtx :: Wsh => Descriptor :: new_wsh ( compiler:: best_compilation ( self ) ?) ,
389
+ DescriptorCtx :: ShWsh => Descriptor :: new_sh_wsh ( compiler:: best_compilation ( self ) ?) ,
390
+ DescriptorCtx :: Tr ( unspendable_key) => self . compile_tr ( unspendable_key) ,
391
+ } ,
392
+ }
393
+ }
394
+
359
395
/// Compile the descriptor into an optimized `Miniscript` representation
360
396
#[ cfg( feature = "compiler" ) ]
361
397
pub fn compile < Ctx : ScriptContext > ( & self ) -> Result < Miniscript < Pk , Ctx > , CompilerError > {
0 commit comments