Skip to content

Commit 2f1ae91

Browse files
committed
wip: Use LockTime
1 parent 0ac479f commit 2f1ae91

File tree

18 files changed

+98
-106
lines changed

18 files changed

+98
-106
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ compiler = []
1515
trace = []
1616
unstable = []
1717
default = []
18-
use-serde = ["serde", "bitcoin/use-serde"]
18+
use-serde = ["serde", "bitcoin/serde"]
1919
rand = ["bitcoin/rand"]
2020

2121
[dependencies]
22-
bitcoin = "0.28.0"
22+
bitcoin = { git = "https://github.com/tcharding/rust-bitcoin", branch = "05-17-timelock" }
2323
serde = { version = "1.0", optional = true}
2424

2525
[dev-dependencies]
2626
bitcoind = {version = "0.26.1", features=["22_0"]}
2727
actual-rand = { package = "rand", version = "0.8.4"}
28-
bitcoin = { version = "0.28", features = ["rand"]}
28+
bitcoin = { git = "https://github.com/tcharding/rust-bitcoin", branch = "05-17-timelock", features = ["rand"] }
2929

3030
[[example]]
3131
name = "htlc"

examples/sign_multisig.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::collections::HashMap;
1818
use std::str::FromStr;
1919

2020
use bitcoin::blockdata::witness::Witness;
21-
use bitcoin::secp256k1;
21+
use bitcoin::{secp256k1, LockTime};
2222

2323
fn main() {
2424
let mut tx = spending_transaction();
@@ -91,7 +91,7 @@ fn main() {
9191
fn spending_transaction() -> bitcoin::Transaction {
9292
bitcoin::Transaction {
9393
version: 2,
94-
lock_time: 0,
94+
lock_time: LockTime::from(0),
9595
input: vec![bitcoin::TxIn {
9696
previous_output: Default::default(),
9797
script_sig: bitcoin::Script::new(),

examples/verify_tx.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use std::str::FromStr;
1818

19+
use bitcoin::LockTime;
1920
use bitcoin::consensus::Decodable;
2021
use bitcoin::secp256k1::{self, Secp256k1};
2122
use bitcoin::util::sighash;
@@ -33,7 +34,7 @@ fn main() {
3334
&spk_input_1,
3435
&tx.input[0].script_sig,
3536
&tx.input[0].witness,
36-
0,
37+
LockTime::from(0),
3738
0,
3839
)
3940
.unwrap();

src/descriptor/tr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
262262
TaprootBuilderError::EmptyTree => {
263263
unreachable!("Taptree is a well formed tree with atleast 1 element")
264264
}
265+
_ => panic!("Unknown error, non_exhaustive catch all"),
265266
},
266267
}
267268
};

src/interpreter/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use super::BitcoinKey;
2626
pub enum Error {
2727
/// Could not satisfy, absolute locktime not met
2828
AbsoluteLocktimeNotMet(u32),
29+
/// Could not satisfy, lock time values are different units
30+
AbsoluteLocktimeComparisonInvalid(u32, u32),
2931
/// Cannot Infer a taproot descriptor
3032
/// Key spends cannot infer the internal key of the descriptor
3133
/// Inferring script spends is possible, but is hidden nodes are currently
@@ -188,6 +190,7 @@ impl fmt::Display for Error {
188190
Error::VerifyFailed => {
189191
f.write_str("Expected Satisfied Boolean at stack top for VERIFY")
190192
}
193+
_ => f.write_str("Unknown error, non_exhaustive catch all"),
191194
}
192195
}
193196
}
@@ -234,6 +237,7 @@ impl error::Error for Error {
234237
Secp(e) => Some(e),
235238
SchnorrSig(e) => Some(e),
236239
SighashError(e) => Some(e),
240+
_ => None // non_exhaustive catch all.
237241
}
238242
}
239243
}

src/interpreter/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::str::FromStr;
2626
use bitcoin::blockdata::witness::Witness;
2727
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d};
2828
use bitcoin::util::{sighash, taproot};
29-
use bitcoin::{self, secp256k1, TxOut};
29+
use bitcoin::{self, secp256k1, TxOut, LockTime};
3030

3131
use crate::miniscript::context::NoChecks;
3232
use crate::miniscript::ScriptContext;
@@ -48,7 +48,7 @@ pub struct Interpreter<'txin> {
4848
/// For non-Taproot spends, the scriptCode; for Taproot script-spends, this
4949
/// is the leaf script; for key-spends it is `None`.
5050
script_code: Option<bitcoin::Script>,
51-
age: u32,
51+
age: LockTime,
5252
height: u32,
5353
}
5454

@@ -169,7 +169,7 @@ impl<'txin> Interpreter<'txin> {
169169
spk: &bitcoin::Script,
170170
script_sig: &'txin bitcoin::Script,
171171
witness: &'txin Witness,
172-
age: u32,
172+
age: LockTime,
173173
height: u32,
174174
) -> Result<Self, Error> {
175175
let (inner, stack, script_code) = inner::from_txdata(spk, script_sig, witness)?;
@@ -492,7 +492,7 @@ pub enum SatisfiedConstraint {
492492
///Absolute Timelock for CLTV.
493493
AbsoluteTimelock {
494494
/// The value of Absolute timelock
495-
time: u32,
495+
time: LockTime,
496496
},
497497
}
498498

@@ -527,7 +527,7 @@ pub struct Iter<'intp, 'txin: 'intp> {
527527
public_key: Option<&'intp BitcoinKey>,
528528
state: Vec<NodeEvaluationState<'intp>>,
529529
stack: Stack<'txin>,
530-
age: u32,
530+
age: LockTime,
531531
height: u32,
532532
has_errored: bool,
533533
}
@@ -1130,7 +1130,7 @@ mod tests {
11301130
n_evaluated: 0,
11311131
n_satisfied: 0,
11321132
}],
1133-
age: 1002,
1133+
age: LockTime::from(1002),
11341134
height: 1002,
11351135
has_errored: false,
11361136
}
@@ -1197,7 +1197,7 @@ mod tests {
11971197
let after_satisfied: Result<Vec<SatisfiedConstraint>, Error> = constraints.collect();
11981198
assert_eq!(
11991199
after_satisfied.unwrap(),
1200-
vec![SatisfiedConstraint::AbsoluteTimelock { time: 1000 }]
1200+
vec![SatisfiedConstraint::AbsoluteTimelock { time: LockTime::from(1000) }]
12011201
);
12021202

12031203
//Check Older

src/interpreter/stack.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use bitcoin;
1818
use bitcoin::blockdata::{opcodes, script};
1919
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
20+
use bitcoin::{locktime, LockTime};
2021

2122
use super::error::PkEvalErrInner;
2223
use super::{
@@ -230,14 +231,16 @@ impl<'txin> Stack<'txin> {
230231
/// booleans
231232
pub(super) fn evaluate_after(
232233
&mut self,
233-
n: &u32,
234-
age: u32,
234+
n: &LockTime,
235+
age: LockTime,
235236
) -> Option<Result<SatisfiedConstraint, Error>> {
236-
if age >= *n {
237-
self.push(Element::Satisfied);
238-
Some(Ok(SatisfiedConstraint::AbsoluteTimelock { time: *n }))
239-
} else {
240-
Some(Err(Error::AbsoluteLocktimeNotMet(*n)))
237+
match locktime::is_satisfied(*n, age) {
238+
Ok(true) => {
239+
self.push(Element::Satisfied);
240+
Some(Ok(SatisfiedConstraint::AbsoluteTimelock { time: *n }))
241+
},
242+
Ok(false) => Some(Err(Error::AbsoluteLocktimeNotMet(n.to_u32()))),
243+
Err(_) => Some(Err(Error::AbsoluteLocktimeComparisonInvalid(n.to_u32(), age.to_u32()))),
241244
}
242245
}
243246

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ pub mod interpreter;
104104
pub mod miniscript;
105105
pub mod policy;
106106
pub mod psbt;
107-
pub mod timelock;
108107

109108
mod util;
110109

src/miniscript/astelem.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use std::str::FromStr;
2323
use std::sync::Arc;
2424
use std::{fmt, str};
2525

26+
use bitcoin::LockTime;
2627
use bitcoin::blockdata::{opcodes, script};
2728
use bitcoin::hashes::hex::FromHex;
2829
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
@@ -491,7 +492,7 @@ where
491492
expression::terminal(&top.args[0], |x| Pk::Hash::from_str(x).map(Terminal::PkH))
492493
}
493494
("after", 1) => expression::terminal(&top.args[0], |x| {
494-
expression::parse_num(x).map(Terminal::After)
495+
expression::parse_num(x).map(|x| Terminal::After(LockTime::from(x)))
495496
}),
496497
("older", 1) => expression::terminal(&top.args[0], |x| {
497498
expression::parse_num(x).map(Terminal::Older)
@@ -653,7 +654,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
653654
.push_slice(&Pk::hash_to_hash160(hash)[..])
654655
.push_opcode(opcodes::all::OP_EQUALVERIFY),
655656
Terminal::After(t) => builder
656-
.push_int(t as i64)
657+
.push_int(t.to_u32() as i64)
657658
.push_opcode(opcodes::all::OP_CLTV),
658659
Terminal::Older(t) => builder.push_int(t as i64).push_opcode(opcodes::all::OP_CSV),
659660
Terminal::Sha256(h) => builder
@@ -788,7 +789,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
788789
match *self {
789790
Terminal::PkK(ref pk) => Ctx::pk_len(pk),
790791
Terminal::PkH(..) => 24,
791-
Terminal::After(n) => script_num_size(n as usize) + 1,
792+
Terminal::After(n) => script_num_size(n.to_u32() as usize) + 1,
792793
Terminal::Older(n) => script_num_size(n as usize) + 1,
793794
Terminal::Sha256(..) => 33 + 6,
794795
Terminal::Hash256(..) => 33 + 6,

src/miniscript/decode.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
2626

2727
use crate::miniscript::lex::{Token as Tk, TokenIter};
2828
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
29+
use crate::bitcoin::locktime::LockTime;
2930
use crate::miniscript::types::extra_props::ExtData;
3031
use crate::miniscript::types::{Property, Type};
3132
use crate::miniscript::ScriptContext;
@@ -133,7 +134,7 @@ pub enum Terminal<Pk: MiniscriptKey, Ctx: ScriptContext> {
133134
PkH(Pk::Hash),
134135
// timelocks
135136
/// `n CHECKLOCKTIMEVERIFY`
136-
After(u32),
137+
After(LockTime),
137138
/// `n CHECKSEQUENCEVERIFY`
138139
Older(u32),
139140
// hashlocks
@@ -388,7 +389,7 @@ pub fn parse<Ctx: ScriptContext>(
388389
Tk::CheckSequenceVerify, Tk::Num(n)
389390
=> term.reduce0(Terminal::Older(n))?,
390391
Tk::CheckLockTimeVerify, Tk::Num(n)
391-
=> term.reduce0(Terminal::After(n))?,
392+
=> term.reduce0(Terminal::After(LockTime::from(n)))?,
392393
// hashlocks
393394
Tk::Equal => match_token!(
394395
tokens,

src/miniscript/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ mod tests {
978978
));
979979
assert_eq!(
980980
ms.unwrap_err().to_string(),
981-
"unexpected «Key hex decoding error: bad hex string length 64 (expected 66)»"
981+
"unexpected «key hex decoding error»"
982982
);
983983
Tapscript::from_str_insane(&format!(
984984
"pk(2788ee41e76f4f3af603da5bc8fa22997bc0344bb0f95666ba6aaff0242baa99)"

src/miniscript/satisfy.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ use std::collections::{BTreeMap, HashMap};
2222
use std::sync::Arc;
2323
use std::{cmp, i64, mem};
2424

25-
use bitcoin;
25+
use bitcoin::LockTime;
2626
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d};
2727
use bitcoin::secp256k1::XOnlyPublicKey;
2828
use bitcoin::util::taproot::{ControlBlock, LeafVersion, TapLeafHash};
2929

3030
use crate::miniscript::limits::{
31-
LOCKTIME_THRESHOLD, SEQUENCE_LOCKTIME_DISABLE_FLAG, SEQUENCE_LOCKTIME_TYPE_FLAG,
31+
SEQUENCE_LOCKTIME_DISABLE_FLAG, SEQUENCE_LOCKTIME_TYPE_FLAG,
3232
};
3333
use crate::util::witness_size;
3434
use crate::{Miniscript, MiniscriptKey, ScriptContext, Terminal, ToPublicKey};
@@ -115,7 +115,7 @@ pub trait Satisfier<Pk: MiniscriptKey + ToPublicKey> {
115115
}
116116

117117
/// Assert whether a absolute locktime is satisfied
118-
fn check_after(&self, _: u32) -> bool {
118+
fn check_after(&self, _: LockTime) -> bool {
119119
false
120120
}
121121
}
@@ -148,21 +148,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for Older {
148148
}
149149
}
150150

151-
/// Newtype around `u32` which implements `Satisfier` using `n` as an
152-
/// absolute locktime
153-
pub struct After(pub u32);
154-
155-
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for After {
156-
fn check_after(&self, n: u32) -> bool {
157-
// if n > self.0; we will be returning false anyways
158-
if n < LOCKTIME_THRESHOLD && self.0 >= LOCKTIME_THRESHOLD {
159-
false
160-
} else {
161-
n <= self.0
162-
}
163-
}
164-
}
165-
166151
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for HashMap<Pk, bitcoin::EcdsaSig> {
167152
fn lookup_ecdsa_sig(&self, key: &Pk) -> Option<bitcoin::EcdsaSig> {
168153
self.get(key).copied()
@@ -277,7 +262,7 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier<Pk>> Satisfier<Pk> for &'
277262
(**self).check_older(t)
278263
}
279264

280-
fn check_after(&self, t: u32) -> bool {
265+
fn check_after(&self, t: LockTime) -> bool {
281266
(**self).check_after(t)
282267
}
283268
}
@@ -339,7 +324,7 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier<Pk>> Satisfier<Pk> for &'
339324
(**self).check_older(t)
340325
}
341326

342-
fn check_after(&self, t: u32) -> bool {
327+
fn check_after(&self, t: LockTime) -> bool {
343328
(**self).check_after(t)
344329
}
345330
}
@@ -483,7 +468,7 @@ macro_rules! impl_tuple_satisfier {
483468
false
484469
}
485470

486-
fn check_after(&self, n: u32) -> bool {
471+
fn check_after(&self, n: LockTime) -> bool {
487472
let &($(ref $ty,)*) = self;
488473
$(
489474
if $ty.check_after(n) {

src/miniscript/types/extra_props.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
use std::cmp;
55
use std::iter::once;
66

7+
use bitcoin::LockTime;
8+
79
use super::{Error, ErrorKind, Property, ScriptContext};
810
use crate::miniscript::context::SigType;
911
use crate::miniscript::limits::{
10-
LOCKTIME_THRESHOLD, SEQUENCE_LOCKTIME_DISABLE_FLAG, SEQUENCE_LOCKTIME_TYPE_FLAG,
12+
SEQUENCE_LOCKTIME_DISABLE_FLAG, SEQUENCE_LOCKTIME_TYPE_FLAG,
1113
};
1214
use crate::{script_num_size, MiniscriptKey, Terminal};
1315

@@ -337,9 +339,9 @@ impl Property for ExtData {
337339
unreachable!()
338340
}
339341

340-
fn from_after(t: u32) -> Self {
342+
fn from_after(t: LockTime) -> Self {
341343
ExtData {
342-
pk_cost: script_num_size(t as usize) + 1,
344+
pk_cost: script_num_size(t.to_u32() as usize) + 1,
343345
has_free_verify: false,
344346
ops: OpLimits::new(1, Some(0), None),
345347
stack_elem_count_sat: Some(0),
@@ -349,8 +351,8 @@ impl Property for ExtData {
349351
timelock_info: TimelockInfo {
350352
csv_with_height: false,
351353
csv_with_time: false,
352-
cltv_with_height: t < LOCKTIME_THRESHOLD,
353-
cltv_with_time: t >= LOCKTIME_THRESHOLD,
354+
cltv_with_height: t.is_height(),
355+
cltv_with_time: t.is_time(),
354356
contains_combination: false,
355357
},
356358
exec_stack_elem_count_sat: Some(1), // <t>
@@ -933,7 +935,7 @@ impl Property for ExtData {
933935
// Note that for CLTV this is a limitation not of Bitcoin but Miniscript. The
934936
// number on the stack would be a 5 bytes signed integer but Miniscript's B type
935937
// only consumes 4 bytes from the stack.
936-
if t == 0 || (t & SEQUENCE_LOCKTIME_DISABLE_FLAG) != 0 {
938+
if t.to_u32() == 0 || (t.to_u32() & SEQUENCE_LOCKTIME_DISABLE_FLAG) != 0 {
937939
return Err(Error {
938940
fragment: fragment.clone(),
939941
error: ErrorKind::InvalidTime,

0 commit comments

Comments
 (0)