Skip to content

Commit 55d25de

Browse files
Add validity and malleability checks.
Testing done.
1 parent 65bcd54 commit 55d25de

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
@@ -237,15 +237,24 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
237237
// TODO: We might require other compile errors for Taproot. Will discuss and update.
238238
#[cfg(feature = "compiler")]
239239
pub fn compile_tr(&self, unspendable_key: Option<Pk>) -> Result<Descriptor<Pk>, Error> {
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)
240+
self.is_valid()?; // Check for validity
241+
match self.is_safe_nonmalleable() {
242+
(false, _) => Err(Error::from(CompilerError::TopLevelNonSafe)),
243+
(_, false) => Err(Error::from(
244+
CompilerError::ImpossibleNonMalleableCompilation,
245+
)),
246+
_ => {
247+
let (internal_key, policy) = self.clone().extract_key(unspendable_key)?;
248+
let tree = Descriptor::new_tr(
249+
internal_key,
250+
match policy {
251+
Policy::Trivial => None,
252+
policy => Some(policy.compile_tr_policy()?),
253+
},
254+
)?;
255+
Ok(tree)
256+
}
257+
}
249258
}
250259

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

src/policy/mod.rs

+11
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)