Skip to content

Commit cac7815

Browse files
Add absolute timelocks
1 parent 7bbc3e4 commit cac7815

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/policy/semantic.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,34 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
499499
ret
500500
}
501501

502+
/// Helper function for recursion in `absolute timelocks`
503+
pub fn real_absolute_timelocks(&self) -> Vec<u32> {
504+
match *self {
505+
Policy::Unsatisfiable
506+
| Policy::Trivial
507+
| Policy::KeyHash(..)
508+
| Policy::Sha256(..)
509+
| Policy::Hash256(..)
510+
| Policy::Ripemd160(..)
511+
| Policy::Hash160(..) => vec![],
512+
Policy::Older(..) => vec![],
513+
Policy::After(t) => vec![t],
514+
Policy::Threshold(_, ref subs) => subs.iter().fold(vec![], |mut acc, x| {
515+
acc.extend(x.real_absolute_timelocks());
516+
acc
517+
}),
518+
}
519+
}
520+
521+
/// Returns a list of all absolute timelocks, not including 0,
522+
/// which appear in the policy
523+
pub fn absolute_timelocks(&self) -> Vec<u32> {
524+
let mut ret = self.real_absolute_timelocks();
525+
ret.sort();
526+
ret.dedup();
527+
ret
528+
}
529+
502530
/// Filter a policy by eliminating relative timelock constraints
503531
/// that are not satisfied at the given age.
504532
pub fn at_age(mut self, time: u32) -> Policy<Pk> {
@@ -604,13 +632,15 @@ mod tests {
604632
let policy = StringPolicy::from_str("pkh()").unwrap();
605633
assert_eq!(policy, Policy::KeyHash("".to_owned()));
606634
assert_eq!(policy.relative_timelocks(), vec![]);
635+
assert_eq!(policy.absolute_timelocks(), vec![]);
607636
assert_eq!(policy.clone().at_age(0), policy.clone());
608637
assert_eq!(policy.clone().at_age(10000), policy.clone());
609638
assert_eq!(policy.n_keys(), 1);
610639
assert_eq!(policy.minimum_n_keys(), 1);
611640

612641
let policy = StringPolicy::from_str("older(1000)").unwrap();
613642
assert_eq!(policy, Policy::Older(1000));
643+
assert_eq!(policy.absolute_timelocks(), vec![]);
614644
assert_eq!(policy.relative_timelocks(), vec![1000]);
615645
assert_eq!(policy.clone().at_age(0), Policy::Unsatisfiable);
616646
assert_eq!(policy.clone().at_age(999), Policy::Unsatisfiable);

0 commit comments

Comments
 (0)