Skip to content

Commit 00c9b50

Browse files
Add policy to descriptor target compilation method
1 parent 7828b38 commit 00c9b50

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/policy/concrete.rs

+36
Original file line numberDiff line numberDiff line change
@@ -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][`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+
111125
impl fmt::Display for PolicyError {
112126
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113127
match *self {
@@ -356,6 +370,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
356370
}
357371
}
358372

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

0 commit comments

Comments
 (0)