From 3c51cd282924ec68f713ca903c36e350810cf933 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 17 May 2022 08:29:28 +1000 Subject: [PATCH] WIP: Add timelock related methods on Transaction Now we have the two `timelock` types we can implement a few methods on `Transaction` that may be useful to users of the library. --- src/blockdata/transaction.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 47e5623414..a0eedb088f 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -40,7 +40,7 @@ use blockdata::witness::Witness; use consensus::{encode, Decodable, Encodable}; use consensus::encode::MAX_VEC_SIZE; use hash_types::{Sighash, Txid, Wtxid}; -use VarInt; +use {timelock, VarInt}; #[cfg(doc)] use util::sighash::SchnorrSighashType; @@ -609,6 +609,24 @@ impl Transaction { pub fn is_explicitly_rbf(&self) -> bool { self.input.iter().any(|input| input.sequence < (0xffffffff - 1)) } + + /// Returns the nLockTime as an absolute timelock. + pub fn absolute_lock_time(&self) -> timelock::Abs { + timelock::Abs::from(self.lock_time) + } + + /// Return the nSequence for `input_index` as a relative timelock. + pub fn relative_lock_time(&self, input_index: usize) -> timelock::Rel { + timelock::Rel::from(self.input[input_index].sequence) + } + + /// Returns `true` if this transactions nLockTime should be ignored. + pub fn ignore_lock_time(&self) -> bool { + if self.lock_time == 0 { + return true; + } + self.input.iter().all(|input| input.sequence == 0xffffffff) + } } impl_consensus_encoding!(TxOut, value, script_pubkey);