Skip to content

Commit 1a67189

Browse files
Add policy to descriptor target compilation method
1 parent 219119d commit 1a67189

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/policy/concrete.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ pub enum PolicyError {
106106
DuplicatePubKeys,
107107
}
108108

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+
109123
impl error::Error for PolicyError {}
110124

111125
impl fmt::Display for PolicyError {
@@ -490,6 +504,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
490504
}
491505
}
492506

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+
493529
/// Compile the descriptor into an optimized `Miniscript` representation
494530
#[cfg(feature = "compiler")]
495531
pub fn compile<Ctx: ScriptContext>(&self) -> Result<Miniscript<Pk, Ctx>, CompilerError> {

0 commit comments

Comments
 (0)