@@ -108,6 +108,20 @@ pub enum PolicyError {
108108 DuplicatePubKeys ,
109109}
110110
111+ /// Descriptor context for [`Policy`] compilation into a [`Descriptor`]
112+ pub enum DescriptorCtx < Pk > {
113+ /// Bare
114+ Bare ,
115+ /// Sh
116+ Sh ,
117+ /// Wsh
118+ Wsh ,
119+ /// ShWsh
120+ ShWsh ,
121+ /// Tr
122+ Tr ( Option < Pk > ) ,
123+ }
124+
111125impl error:: Error for PolicyError { }
112126
113127impl fmt:: Display for PolicyError {
@@ -316,6 +330,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
316330 }
317331 }
318332
333+ /// Compile the [`Policy`] into desc_ctx [`Descriptor`]
334+ #[ cfg( feature = "compiler" ) ]
335+ pub fn compile_to_descriptor < Ctx : ScriptContext > (
336+ & self ,
337+ desc_ctx : DescriptorCtx < Pk > ,
338+ ) -> Result < Descriptor < Pk > , Error > {
339+ self . is_valid ( ) ?;
340+ match self . is_safe_nonmalleable ( ) {
341+ ( false , _) => Err ( Error :: from ( CompilerError :: TopLevelNonSafe ) ) ,
342+ ( _, false ) => Err ( Error :: from (
343+ CompilerError :: ImpossibleNonMalleableCompilation ,
344+ ) ) ,
345+ _ => match desc_ctx {
346+ DescriptorCtx :: Bare => Descriptor :: new_bare ( compiler:: best_compilation ( self ) ?) ,
347+ DescriptorCtx :: Sh => Descriptor :: new_sh ( compiler:: best_compilation ( self ) ?) ,
348+ DescriptorCtx :: Wsh => Descriptor :: new_wsh ( compiler:: best_compilation ( self ) ?) ,
349+ DescriptorCtx :: ShWsh => Descriptor :: new_sh_wsh ( compiler:: best_compilation ( self ) ?) ,
350+ DescriptorCtx :: Tr ( unspendable_key) => self . compile_tr ( unspendable_key) ,
351+ } ,
352+ }
353+ }
354+
319355 /// Compile the descriptor into an optimized `Miniscript` representation
320356 #[ cfg( feature = "compiler" ) ]
321357 pub fn compile < Ctx : ScriptContext > ( & self ) -> Result < Miniscript < Pk , Ctx > , CompilerError > {
0 commit comments