Skip to content

Commit

Permalink
Add validity and malleability checks.
Browse files Browse the repository at this point in the history
Testing done.
  • Loading branch information
SarcasticNastik committed May 11, 2022
1 parent 0866807 commit 1c2a80e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,24 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
// TODO: We might require other compile errors for Taproot.
#[cfg(feature = "compiler")]
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
let tree = Descriptor::new_tr(
internal_key,
match policy {
Policy::Trivial => None,
policy => Some(policy.compile_tr_policy()?),
},
)?;
Ok(tree)
self.is_valid()?; // Check for validity
match self.is_safe_nonmalleable() {
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
(_, false) => Err(Error::from(
CompilerError::ImpossibleNonMalleableCompilation,
)),
_ => {
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
let tree = Descriptor::new_tr(
internal_key,
match policy {
Policy::Trivial => None,
policy => Some(policy.compile_tr_policy()?),
},
)?;
Ok(tree)
}
}
}

/// Compile the descriptor into an optimized `Miniscript` representation
Expand Down
11 changes: 11 additions & 0 deletions src/policy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,16 @@ mod tests {
Descriptor::new_tr(unspendable_key.clone(), Some(tree)).unwrap();
assert_eq!(descriptor, expected_descriptor);
}

{
// Invalid policy compilation (Duplicate PubKeys)
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(A),pk(D)))");
let descriptor = policy.compile_tr(Some(unspendable_key.clone()));

assert_eq!(
descriptor.unwrap_err().to_string(),
"Policy contains duplicate keys"
);
}
}
}

0 comments on commit 1c2a80e

Please sign in to comment.