Skip to content

Commit c0681c5

Browse files
Add validity and malleability checks.
Testing done.
1 parent b7c76cf commit c0681c5

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
@@ -230,15 +230,24 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
230230
// TODO: We might require other compile errors for Taproot.
231231
#[cfg(feature = "compiler")]
232232
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
233-
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
234-
let tree = Descriptor::new_tr(
235-
internal_key,
236-
match policy {
237-
Policy::Trivial => None,
238-
policy => Some(policy.compile_tr_policy()?),
239-
},
240-
)?;
241-
Ok(tree)
233+
self.is_valid()?; // Check for validity
234+
match self.is_safe_nonmalleable() {
235+
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
236+
(_, false) => Err(Error::from(
237+
CompilerError::ImpossibleNonMalleableCompilation,
238+
)),
239+
_ => {
240+
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
241+
let tree = Descriptor::new_tr(
242+
internal_key,
243+
match policy {
244+
Policy::Trivial => None,
245+
policy => Some(policy.compile_tr_policy()?),
246+
},
247+
)?;
248+
Ok(tree)
249+
}
250+
}
242251
}
243252

244253
/// 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)