Locktime (nLockTime)
A field that stops a transaction being mined before a given block height or time.
Basically
nLockTime sets the earliest time or block number when your Bitcoin transaction can be mined. Until then, it cannot go into a block.
Key Takeaways
- nLockTime is a 4-byte field in every Bitcoin transaction. It sets the earliest block height or Unix timestamp at which the transaction can be mined and is a core building block for timelocks on Bitcoin.
- Values below 500,000,000 represent a block height, while values at or above 500,000,000 represent a Unix timestamp. Bitcoin Core sets nLockTime by default to prevent fee sniping.
- nLockTime alone is a transaction-level restriction that the sender can bypass by creating a new transaction. OP_CHECKLOCKTIMEVERIFY (BIP 65) extends it into a consensus-enforced script-level lock that enables trustless time-locked contracts.
What Is Locktime (nLockTime)?
Locktime, technically called nLockTime, is a field in every Bitcoin transaction. It occupies the last 4 bytes of the raw transaction data as a 32-bit unsigned integer in little-endian byte order, and a non-zero value tells the network not to include the transaction in a block until a specified point in time has passed.
nLockTime was part of Satoshi Nakamoto's original Bitcoin implementation (v0.1). Satoshi envisioned it working alongside the nSequence field so parties could create a transaction with a future lock time and update it by broadcasting new versions with higher sequence numbers. The original replacement mechanism was never fully realised, but nLockTime has become a foundational primitive for Bitcoin Script programmability and time-locked contracts.
How It Works
Every Bitcoin transaction contains an nLockTime value. A value of 0 allows the transaction into any block without restriction, while a non-zero value is interpreted according to whether it falls above or below a specific threshold.
Dual Interpretation
The threshold is exactly 500,000,000 (hex 0x1DCD6500). Block 500,000,000 will not be reached for approximately 9,500 years at Bitcoin's 10-minute block interval, so the threshold provides an unambiguous separation between the two modes.
Timestamp-based locks have used the Median Time Past (MTP) for comparison since BIP 113 activation in 2016. MTP is the median timestamp of the 11 blocks preceding the block being validated, and it increases monotonically to prevent miners from manipulating their block timestamp to include time-locked transactions early.
The nSequence Gate
nLockTime enforcement depends on the nSequence fields of the transaction's inputs. The transaction is considered "final" and consensus ignores nLockTime entirely if every input has nSequence set to 0xFFFFFFFF, the maximum value called SEQUENCE_FINAL. At least one input must have nSequence below 0xFFFFFFFF for nLockTime to be enforced.
Bitcoin Core checks this through its IsFinalTx() function. Nodes will neither relay nor mine a transaction whose nLockTime has not yet been satisfied.
Fee Sniping Prevention
Automatic fee sniping prevention is the most common use of nLockTime today. Since Bitcoin Core 0.11.0, merged December 2014 via PR #2340, the wallet sets nLockTime on every new transaction to the current block height and sets all input nSequence values to 0xFFFFFFFE.
Fee sniping is an attack where a miner with significant hashrate attempts to re-mine a recent block to capture its high-fee transactions. Without nLockTime protection, the miner could orphan the current best block and include those transactions in their own competing version. Transactions locked to the current block height cannot be included when a miner re-mines a previous block because they are valid only at the current height or later.
Bitcoin Core adds a random offset with 10% probability for privacy. It subtracts a random number between 0 and 99 instead of setting nLockTime to exactly the current block height, preventing observers from determining exactly when a transaction was created from its nLockTime value. BIP 326 extends this concept to Taproot transactions.
Relationship to OP_CHECKLOCKTIMEVERIFY
nLockTime alone has an important limitation because it is a transaction-level restriction, not an output-level one. The holder of a private key can always create a new transaction without any nLockTime restriction to spend the same outputs, so nothing stops the key holder from bypassing the lock.
OP_CHECKLOCKTIMEVERIFY (CLTV), introduced in BIP 65 and activated in late 2015, solves this by enforcing timelocks at the script level. CLTV is an opcode embedded in the output's spending conditions that compares a value on the stack against the spending transaction's nLockTime field. The script fails if the transaction's nLockTime is less than the value specified in the script.
CLTV transforms nLockTime from a voluntary, sender-imposed delay into a consensus-enforced, output-level lock. The output cannot be spent before the specified time, regardless of who holds the private key or what transaction they construct. The Bitcoin Timelocks: CLTV and CSV research article provides a deeper look at both absolute and relative timelocks.
Relative Timelocks: nSequence and OP_CSV
nLockTime provides absolute timelocks based on a specific block height or timestamp, while OP_CHECKSEQUENCEVERIFY (CSV) and BIP 68 enable relative timelocks based on the age of the output being spent. BIP 68 repurposes the nSequence field with new consensus-enforced semantics and uses bit flags to signal block-based or time-based relative delays with 512-second granularity. Together, these mechanisms form the complete timelock toolkit on Bitcoin.
Time-Locked Contracts
CLTV combined with nLockTime enables contracts where funds become spendable only after a future date. A common pattern uses multiple spending paths, with one path requiring multiple signatures available immediately and a fallback path using a single signature available only after a CLTV timelock expires. This powers wallet recovery mechanisms, vesting schedules and inheritance planning.
Hashed Time-Locked Contracts (HTLCs)
HTLCs combine hash locks with timelocks to enable trustless conditional payments. The timelock component uses CLTV to ensure that the sender can reclaim the funds if the recipient fails to claim them by revealing a preimage before the deadline. HTLCs are the core mechanism powering the Lightning Network and atomic swaps.
Payment Channel Dispute Resolution
Lightning Network commitment transactions use timelocks for dispute resolution. A force-closed channel subjects the broadcasting party's funds to a timelock delay, giving the counterparty time to broadcast a justice transaction if the closure was fraudulent. Layer 2 protocols like Spark build on these timelock primitives to enable efficient off-chain transactions while preserving self-custodial security guarantees.
Scheduled and Deferred Payments
A sender can create and sign a transaction with a future nLockTime, then share it with the recipient. The recipient holds a valid, signed transaction that cannot be broadcast until the specified time. This provides a simple form of scheduled payment, though the sender retains the ability to double-spend the inputs before the lock time is reached.
Transaction-Level vs. Script-Level Enforcement
A bare nLockTime restriction without CLTV in the output script offers no guarantee to anyone other than the sender. The sender can always create a competing transaction without nLockTime and broadcast it before the locked version becomes valid. CLTV must be used in the output's spending script for enforceable timelocks.
Timestamp Precision Limitations
Timestamp-based nLockTime introduces imprecision because the comparison uses Median Time Past. MTP typically lags actual wall-clock time by approximately one hour, and miners have some flexibility in the timestamps they include in block headers. Applications requiring precise timing should account for this variance or use block-height-based locks instead.
Type Mismatch
A CLTV script specifying a block height cannot be satisfied by a transaction with a timestamp-based nLockTime, and vice versa. Both lock time types must be in the block height range or both must be in the timestamp range. A type mismatch causes the script to fail unconditionally.
Interaction with Replace-By-Fee
Transactions using nLockTime for fee sniping prevention set nSequence to 0xFFFFFFFE, which does not signal Replace-By-Fee (RBF). RBF signalling requires nSequence below 0xFFFFFFFD. Developers building applications that use both timelocks and RBF must carefully choose nSequence values to enable the desired behaviour, and the fee market dynamics research article provides a broader look at how fees interact with Bitcoin's mempool.
This glossary entry is for informational purposes only and does not constitute financial or investment advice. Always do your own research before using any protocol or technology.