From 1e0e712bb0c6b3808d7318342769e33cfa966e06 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 7 Feb 2023 16:31:02 +1100 Subject: [PATCH] Add push_* methods for lock times Lock times are u32 and can necessitate encoding using 5 bytes. As such they are "special". Add methods `push_lock_time` and `push_sequence` for pushing absolute lock times and sequence numbers. We do not push relative locktimes because they are only 16 bits from the original sequence number. --- bitcoin/src/blockdata/script/builder.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/bitcoin/src/blockdata/script/builder.rs b/bitcoin/src/blockdata/script/builder.rs index a69b19eb..cecdcce2 100644 --- a/bitcoin/src/blockdata/script/builder.rs +++ b/bitcoin/src/blockdata/script/builder.rs @@ -7,8 +7,10 @@ use core::fmt; use secp256k1::XOnlyPublicKey; +use crate::blockdata::locktime::absolute; use crate::blockdata::opcodes::{self, all::*}; use crate::blockdata::script::{write_scriptint, opcode_to_verify, Script, ScriptBuf}; +use crate::blockdata::transaction::Sequence; use crate::key::PublicKey; use crate::prelude::*; @@ -106,6 +108,16 @@ impl Builder { } } + /// Adds instructions to push an absolute lock time onto the stack. + pub fn push_lock_time(self, lock_time: absolute::LockTime) -> Builder { + self.push_int(lock_time.to_consensus_u32().into()) + } + + /// Adds instructions to push a sequence number onto the stack. + pub fn push_sequence(self, sequence: Sequence) -> Builder { + self.push_int(sequence.to_consensus_u32().into()) + } + /// Converts the `Builder` into `ScriptBuf`. pub fn into_script(self) -> ScriptBuf { self.0