Skip to content

Commit f464e33

Browse files
Add policy to descriptor target compilation method
1 parent 0662d2e commit f464e33

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
@@ -100,6 +100,20 @@ pub enum PolicyError {
100100
DuplicatePubKeys,
101101
}
102102

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+
103117
impl fmt::Display for PolicyError {
104118
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105119
match *self {
@@ -289,6 +303,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
289303
}
290304
}
291305

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

0 commit comments

Comments
 (0)