Skip to content

Commit e1b1d92

Browse files
Add validity and malleability checks.
Testing done.
1 parent e7f1a67 commit e1b1d92

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/policy/concrete.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,24 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
246246
// TODO: We might require other compile errors for Taproot.
247247
#[cfg(feature = "compiler")]
248248
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
249-
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
250-
let tree = Descriptor::new_tr(
251-
internal_key,
252-
match policy {
253-
Policy::Trivial => None,
254-
policy => Some(policy.compile_tr_policy()?),
255-
},
256-
)?;
257-
Ok(tree)
249+
self.is_valid()?; // Check for validity
250+
match self.is_safe_nonmalleable() {
251+
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
252+
(_, false) => Err(Error::from(
253+
CompilerError::ImpossibleNonMalleableCompilation,
254+
)),
255+
_ => {
256+
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
257+
let tree = Descriptor::new_tr(
258+
internal_key,
259+
match policy {
260+
Policy::Trivial => None,
261+
policy => Some(policy.compile_tr_policy()?),
262+
},
263+
)?;
264+
Ok(tree)
265+
}
266+
}
258267
}
259268

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

src/policy/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -401,5 +401,16 @@ mod tests {
401401
Descriptor::new_tr(unspendable_key.clone(), Some(tree)).unwrap();
402402
assert_eq!(descriptor, expected_descriptor);
403403
}
404+
405+
{
406+
// Invalid policy compilation (Duplicate PubKeys)
407+
let policy: Concrete<String> = policy_str!("or(and(pk(A),pk(B)),and(pk(A),pk(D)))");
408+
let descriptor = policy.compile_tr(Some(unspendable_key.clone()));
409+
410+
assert_eq!(
411+
descriptor.unwrap_err().to_string(),
412+
"Policy contains duplicate keys"
413+
);
414+
}
404415
}
405416
}

0 commit comments

Comments
 (0)