miniscript/primitives/
absolute_locktime.rs1use core::{cmp, fmt};
6
7use bitcoin::absolute;
8
9pub const MAX_ABSOLUTE_LOCKTIME: u32 = 0x8000_0000;
11
12pub const MIN_ABSOLUTE_LOCKTIME: u32 = 1;
18
19#[derive(Debug, PartialEq)]
21pub struct AbsLockTimeError {
22 value: u32,
23}
24
25impl fmt::Display for AbsLockTimeError {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 if self.value < MIN_ABSOLUTE_LOCKTIME {
28 f.write_str("absolute locktimes in Miniscript have a minimum value of 1")
29 } else {
30 debug_assert!(self.value > MAX_ABSOLUTE_LOCKTIME);
31 write!(
32 f,
33 "absolute locktimes in Miniscript have a maximum value of 0x{:08x}; got 0x{:08x}",
34 MAX_ABSOLUTE_LOCKTIME, self.value
35 )
36 }
37 }
38}
39
40#[cfg(feature = "std")]
41impl std::error::Error for AbsLockTimeError {
42 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
47pub struct AbsLockTime(absolute::LockTime);
48
49impl AbsLockTime {
50 pub fn from_consensus(n: u32) -> Result<Self, AbsLockTimeError> {
52 if n >= MIN_ABSOLUTE_LOCKTIME && n <= MAX_ABSOLUTE_LOCKTIME {
53 Ok(AbsLockTime(absolute::LockTime::from_consensus(n)))
54 } else {
55 Err(AbsLockTimeError { value: n })
56 }
57 }
58
59 pub fn to_consensus_u32(self) -> u32 { self.0.to_consensus_u32() }
65
66 pub fn is_block_height(&self) -> bool { self.0.is_block_height() }
68
69 pub fn is_block_time(&self) -> bool { self.0.is_block_time() }
71}
72
73impl From<AbsLockTime> for absolute::LockTime {
74 fn from(lock_time: AbsLockTime) -> absolute::LockTime { lock_time.0 }
75}
76
77impl cmp::PartialOrd for AbsLockTime {
78 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) }
79}
80
81impl cmp::Ord for AbsLockTime {
82 fn cmp(&self, other: &Self) -> cmp::Ordering {
83 let this = self.0.to_consensus_u32();
84 let that = other.0.to_consensus_u32();
85 this.cmp(&that)
86 }
87}
88
89impl fmt::Display for AbsLockTime {
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
91}