@@ -100,6 +100,20 @@ pub enum PolicyError {
100
100
DuplicatePubKeys ,
101
101
}
102
102
103
+ /// Descriptor context for [`Policy`] compilation into a [`Descriptor`]
104
+ pub enum DescriptorCtx < Pk > {
105
+ /// [Bare][`Descriptor::Bare`]
106
+ Bare ,
107
+ /// [Sh][`Descriptor::Sh`]
108
+ Sh ,
109
+ /// [Wsh][`Descriptor::Wsh`]
110
+ Wsh ,
111
+ /// Sh-wrapped [Wsh][`Descriptor::Wsh`]
112
+ ShWsh ,
113
+ /// [Tr][`Descriptor::Tr`], where the Option<Pk> corresponds to the internal_key.
114
+ Tr ( Option < Pk > ) ,
115
+ }
116
+
103
117
impl fmt:: Display for PolicyError {
104
118
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
105
119
match * self {
@@ -289,6 +303,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
289
303
}
290
304
}
291
305
306
+ /// Compile the [`Policy`] into desc_ctx [`Descriptor`]
307
+ #[ cfg( feature = "compiler" ) ]
308
+ pub fn compile_to_descriptor < Ctx : ScriptContext > (
309
+ & self ,
310
+ desc_ctx : DescriptorCtx < Pk > ,
311
+ ) -> Result < Descriptor < Pk > , Error > {
312
+ self . is_valid ( ) ?;
313
+ match self . is_safe_nonmalleable ( ) {
314
+ ( false , _) => Err ( Error :: from ( CompilerError :: TopLevelNonSafe ) ) ,
315
+ ( _, false ) => Err ( Error :: from (
316
+ CompilerError :: ImpossibleNonMalleableCompilation ,
317
+ ) ) ,
318
+ _ => match desc_ctx {
319
+ DescriptorCtx :: Bare => Descriptor :: new_bare ( compiler:: best_compilation ( self ) ?) ,
320
+ DescriptorCtx :: Sh => Descriptor :: new_sh ( compiler:: best_compilation ( self ) ?) ,
321
+ DescriptorCtx :: Wsh => Descriptor :: new_wsh ( compiler:: best_compilation ( self ) ?) ,
322
+ DescriptorCtx :: ShWsh => Descriptor :: new_sh_wsh ( compiler:: best_compilation ( self ) ?) ,
323
+ DescriptorCtx :: Tr ( unspendable_key) => self . compile_tr ( unspendable_key) ,
324
+ } ,
325
+ }
326
+ }
327
+
292
328
/// Compile the descriptor into an optimized `Miniscript` representation
293
329
#[ cfg( feature = "compiler" ) ]
294
330
pub fn compile < Ctx : ScriptContext > ( & self ) -> Result < Miniscript < Pk , Ctx > , CompilerError > {
0 commit comments