Skip to content

Commit

Permalink
Add policy to descriptor target compilation method
Browse files Browse the repository at this point in the history
  • Loading branch information
SarcasticNastik committed May 4, 2022
1 parent 989176b commit 63ee549
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ pub enum PolicyError {
DuplicatePubKeys,
}

/// Descriptor context for [`Policy`] compilation into a [`Descriptor`]
pub enum DescriptorCtx<Pk> {
/// Bare
Bare,
/// Sh
Sh,
/// Wsh
Wsh,
/// ShWsh
ShWsh,
/// Tr
Tr(Option<Pk>),
}

impl error::Error for PolicyError {}

impl fmt::Display for PolicyError {
Expand Down Expand Up @@ -316,6 +330,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
}
}

/// Compile the [`Policy`] into desc_ctx [`Descriptor`]
#[cfg(feature = "compiler")]
pub fn compile_to_descriptor<Ctx: ScriptContext>(
&self,
desc_ctx: DescriptorCtx<Pk>,
) -> Result<Descriptor<Pk>, Error> {
self.is_valid()?;
match self.is_safe_nonmalleable() {
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
(_, false) => Err(Error::from(
CompilerError::ImpossibleNonMalleableCompilation,
)),
_ => match desc_ctx {
DescriptorCtx::Bare => Descriptor::new_bare(compiler::best_compilation(self)?),
DescriptorCtx::Sh => Descriptor::new_sh(compiler::best_compilation(self)?),
DescriptorCtx::Wsh => Descriptor::new_wsh(compiler::best_compilation(self)?),
DescriptorCtx::ShWsh => Descriptor::new_sh_wsh(compiler::best_compilation(self)?),
DescriptorCtx::Tr(unspendable_key) => self.compile_tr(unspendable_key),
},
}
}

/// Compile the descriptor into an optimized `Miniscript` representation
#[cfg(feature = "compiler")]
pub fn compile<Ctx: ScriptContext>(&self) -> Result<Miniscript<Pk, Ctx>, CompilerError> {
Expand Down

0 comments on commit 63ee549

Please sign in to comment.