Skip to content

Commit b64bf26

Browse files
Add validity and malleability checks.
Testing done.
1 parent f097077 commit b64bf26

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/policy/concrete.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,24 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
256256
// TODO: We might require other compile errors for Taproot. Will discuss and update.
257257
#[cfg(feature = "compiler")]
258258
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
259-
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
260-
let tree = Descriptor::new_tr(
261-
internal_key,
262-
match policy {
263-
Policy::Trivial => None,
264-
policy => Some(policy.compile_tr_policy()?),
265-
},
266-
)?;
267-
Ok(tree)
259+
self.is_valid()?; // Check for validity
260+
match self.is_safe_nonmalleable() {
261+
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
262+
(_, false) => Err(Error::from(
263+
CompilerError::ImpossibleNonMalleableCompilation,
264+
)),
265+
_ => {
266+
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
267+
let tree = Descriptor::new_tr(
268+
internal_key,
269+
match policy {
270+
Policy::Trivial => None,
271+
policy => Some(policy.compile_tr_policy()?),
272+
},
273+
)?;
274+
Ok(tree)
275+
}
276+
}
268277
}
269278

270279
/// Compile the descriptor into an optimized `Miniscript` representation

src/policy/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,16 @@ mod tests {
407407
Descriptor::new_tr(unspendable_key.clone(), Some(tree)).unwrap();
408408
assert_eq!(descriptor, expected_descriptor);
409409
}
410+
411+
{
412+
// Invalid policy compilation (Duplicate PubKeys)
413+
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(A),pk(D)))");
414+
let descriptor = policy.compile_tr(Some(unspendable_key.clone()));
415+
416+
assert_eq!(
417+
descriptor.unwrap_err().to_string(),
418+
"Policy contains duplicate keys"
419+
);
420+
}
410421
}
411422
}

0 commit comments

Comments
 (0)