Skip to content

Commit a7a1b9a

Browse files
committed
miniscript: use RelLockTime in Satisfaction
Gets rid of a couple unwraps from the last commit :)
1 parent fc6bfa8 commit a7a1b9a

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

src/descriptor/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,7 @@ impl Descriptor<DefiniteDescriptorKey> {
512512
descriptor: self,
513513
template: stack,
514514
absolute_timelock: satisfaction.absolute_timelock.map(Into::into),
515-
// unwrap to be removed in a later commit
516-
relative_timelock: satisfaction
517-
.relative_timelock
518-
.map(|s| s.to_relative_lock_time().unwrap()),
515+
relative_timelock: satisfaction.relative_timelock.map(Into::into),
519516
})
520517
} else {
521518
Err(self)
@@ -544,9 +541,7 @@ impl Descriptor<DefiniteDescriptorKey> {
544541
template: stack,
545542
absolute_timelock: satisfaction.absolute_timelock.map(Into::into),
546543
// unwrap to be removed in a later commit
547-
relative_timelock: satisfaction
548-
.relative_timelock
549-
.map(|s| s.to_relative_lock_time().unwrap()),
544+
relative_timelock: satisfaction.relative_timelock.map(Into::into),
550545
})
551546
} else {
552547
Err(self)

src/miniscript/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ mod tests {
13551355

13561356
#[test]
13571357
fn template_timelocks() {
1358-
use crate::AbsLockTime;
1358+
use crate::{AbsLockTime, RelLockTime};
13591359
let key_present = bitcoin::PublicKey::from_str(
13601360
"0327a6ed0e71b451c79327aa9e4a6bb26ffb1c0056abc02c25e783f6096b79bb4f",
13611361
)
@@ -1381,28 +1381,28 @@ mod tests {
13811381
(
13821382
format!("or_d(pk({}),and_v(v:pk({}),older(12960)))", key_missing, key_present),
13831383
None,
1384-
Some(bitcoin::Sequence(12960)),
1384+
Some(RelLockTime::from_height(12960)),
13851385
),
13861386
(
13871387
format!(
13881388
"thresh(3,pk({}),s:pk({}),snl:older(10),snl:after(11))",
13891389
key_present, key_missing
13901390
),
13911391
Some(AbsLockTime::from_consensus(11).unwrap()),
1392-
Some(bitcoin::Sequence(10)),
1392+
Some(RelLockTime::from_height(10)),
13931393
),
13941394
(
13951395
format!("and_v(v:and_v(v:pk({}),older(10)),older(20))", key_present),
13961396
None,
1397-
Some(bitcoin::Sequence(20)),
1397+
Some(RelLockTime::from_height(20)),
13981398
),
13991399
(
14001400
format!(
14011401
"andor(pk({}),older(10),and_v(v:pk({}),older(20)))",
14021402
key_present, key_missing
14031403
),
14041404
None,
1405-
Some(bitcoin::Sequence(10)),
1405+
Some(RelLockTime::from_height(10)),
14061406
),
14071407
];
14081408

src/miniscript/satisfy.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use super::context::SigType;
1818
use crate::plan::AssetProvider;
1919
use crate::prelude::*;
2020
use crate::util::witness_size;
21-
use crate::{AbsLockTime, Miniscript, MiniscriptKey, ScriptContext, Terminal, ToPublicKey};
21+
use crate::{
22+
AbsLockTime, Miniscript, MiniscriptKey, RelLockTime, ScriptContext, Terminal, ToPublicKey,
23+
};
2224

2325
/// Type alias for 32 byte Preimage.
2426
pub type Preimage32 = [u8; 32];
@@ -117,6 +119,12 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for Sequence {
117119
}
118120
}
119121

122+
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for RelLockTime {
123+
fn check_older(&self, n: relative::LockTime) -> bool {
124+
<relative::LockTime as Satisfier<Pk>>::check_older(&(*self).into(), n)
125+
}
126+
}
127+
120128
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for relative::LockTime {
121129
fn check_older(&self, n: relative::LockTime) -> bool { n.is_implied_by(*self) }
122130
}
@@ -876,7 +884,7 @@ pub struct Satisfaction<T> {
876884
/// The absolute timelock used by this satisfaction
877885
pub absolute_timelock: Option<AbsLockTime>,
878886
/// The relative timelock used by this satisfaction
879-
pub relative_timelock: Option<Sequence>,
887+
pub relative_timelock: Option<RelLockTime>,
880888
}
881889

882890
impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
@@ -1278,19 +1286,19 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfaction<Placeholder<Pk>> {
12781286
}
12791287
Terminal::Older(t) => {
12801288
// unwrap to be removed in a later commit
1281-
let (stack, relative_timelock) =
1282-
if stfr.check_older(t.to_relative_lock_time().unwrap()) {
1283-
(Witness::empty(), Some(t))
1284-
} else if root_has_sig {
1285-
// If the root terminal has signature, the
1286-
// signature covers the nLockTime and nSequence
1287-
// values. The sender of the transaction should
1288-
// take care that it signs the value such that the
1289-
// timelock is not met
1290-
(Witness::Impossible, None)
1291-
} else {
1292-
(Witness::Unavailable, None)
1293-
};
1289+
let t = <RelLockTime as core::convert::TryFrom<_>>::try_from(t).unwrap();
1290+
let (stack, relative_timelock) = if stfr.check_older(t.into()) {
1291+
(Witness::empty(), Some(t))
1292+
} else if root_has_sig {
1293+
// If the root terminal has signature, the
1294+
// signature covers the nLockTime and nSequence
1295+
// values. The sender of the transaction should
1296+
// take care that it signs the value such that the
1297+
// timelock is not met
1298+
(Witness::Impossible, None)
1299+
} else {
1300+
(Witness::Unavailable, None)
1301+
};
12941302
Satisfaction { stack, has_sig: false, relative_timelock, absolute_timelock: None }
12951303
}
12961304
Terminal::Ripemd160(ref h) => Satisfaction {

0 commit comments

Comments
 (0)