Skip to content

Commit

Permalink
Add timelock related methods on Transaction and TxIn
Browse files Browse the repository at this point in the history
Now we have the two `timelock` types we can implement a few methods on
`Transaction` and `TxIn` that may be useful to users of the library.
  • Loading branch information
tcharding committed May 19, 2022
1 parent 52f19da commit e17235a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/blockdata/transaction.rs
Expand Up @@ -40,7 +40,7 @@ use crate::blockdata::witness::Witness;
use crate::consensus::{encode, Decodable, Encodable};
use crate::consensus::encode::MAX_VEC_SIZE;
use crate::hash_types::{Sighash, Txid, Wtxid};
use crate::VarInt;
use crate::{timelock, VarInt};

#[cfg(doc)]
use crate::util::sighash::SchnorrSighashType;
Expand Down Expand Up @@ -208,6 +208,13 @@ pub struct TxIn {
pub witness: Witness
}

impl TxIn {
/// Returns sequence number as a relative timelock.
pub fn relative_lock_time(&self) -> timelock::Rel {
timelock::Rel::from(self.sequence)
}
}

impl Default for TxIn {
fn default() -> TxIn {
TxIn {
Expand Down Expand Up @@ -609,6 +616,19 @@ 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)
}

/// 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);
Expand Down

0 comments on commit e17235a

Please sign in to comment.