# 13 - nLocktime

The nLocktime value is used for creating payment channels. If a transaction has any inputs with non-final nSequence values (e.g. nSequence < 0xffffffff) and an nLocktime in the future, it is considered a payment channel, and cannot be put into a block until either the nLocktime expires, or the transaction is updated to have all final inputs.

This can be useful if an output must be created that can't be spent before a particular time. The nSequence can be checked to be non-final, and the nLocktime can be checked to be later than a pre-set time/date value.

<figure><img src="/files/rUP3Wi661iyt0Q0EA1jX" alt=""><figcaption></figcaption></figure>

#### Example: Check channel status

<table><thead><tr><th width="237.33333333333331">Stack</th><th width="279">Script</th><th>Description</th></tr></thead><tbody><tr><td>&#x3C;nSequence>&#x3C;r_tx_preimg></td><td>...</td><td>nSequence is left on the stack. R_tx_preimg has had version, hash_prevouts, hash_nSequence, hash_outpoints, script, value, nSequence and hash_outputs removed</td></tr><tr><td>&#x3C;nSequence> &#x3C;r_tx_preimg></td><td>OP_4</td><td>nLocktime is 4 bytes</td></tr><tr><td>&#x3C;nSequence> &#x3C;r_tx_preimg> 0x04</td><td>OP_SPLIT</td><td>Split nSequence</td></tr><tr><td>&#x3C;nSequence> &#x3C;nLocktime> &#x3C;rr_tx_preimg></td><td>OP_SWAP</td><td>Swap nLocktime to top of stack</td></tr><tr><td>&#x3C;nSequence> &#x3C;rr_tx_preimg>&#x3C;nLocktime></td><td>0x00</td><td>add 0x00 (push 0x00 to stack, do not use OP_FALSE)</td></tr><tr><td>&#x3C;nSequence> &#x3C;rr_tx_preimg>&#x3C;nLocktime> 0x00</td><td>OP_CAT</td><td>add two leading zeroes as MSB so locktime will be interpreted as a positive integer</td></tr><tr><td>&#x3C;nSequence> &#x3C;rr_tx_preimg> &#x3C;nLocktime+00></td><td>&#x3C;expiry></td><td>Load pre-set expiry time to stack. this is in Unix epoch time, rather than block-height.</td></tr><tr><td>&#x3C;nSequence> &#x3C;rr_tx_preimg> &#x3C;nLocktime+00> &#x3C;expiry></td><td>OP_GREATERTHANOREQUAL</td><td>Check that the nLocktime is at or after the pre-set expiry</td></tr><tr><td>&#x3C;nSequence> &#x3C;rr_tx_preimg> &#x3C;result></td><td>OP_VERIFY</td><td>If expiry is valid, the script will continue</td></tr><tr><td>&#x3C;nSequence>&#x3C;rr_tx_preimg></td><td>OP_SWAP</td><td>Swap nLocktime to top of stack</td></tr><tr><td>&#x3C;rr_tx_preimg>&#x3C;nSequence></td><td>0x00</td><td>Push 0x00 onto the stack</td></tr><tr><td>&#x3C;rr_tx_preimg>&#x3C;nSequence> 0x00</td><td>OP_CAT</td><td>Attach 00 to front of nSequence to ensure it is interpreted as a positive integer by the validation engine</td></tr><tr><td>&#x3C;r_tx_preimg>&#x3C;nSequence+00></td><td>0xffffffff00</td><td>load finalised nSequence to stack. Value is little-endian so leading zeroes/MSB are on the right</td></tr><tr><td>&#x3C;r_tx_preimg>&#x3C;nSequence+00> 0xffffffff00</td><td>OP_LESSTHAN</td><td>Check nSequence is less than final</td></tr><tr><td>&#x3C;r_tx_preimg> &#x3C;result></td><td>OP_VERIFY</td><td>If the input is non-final, the script can continue</td></tr><tr><td>&#x3C;r_tx_preimg></td><td>...</td><td>rest of script</td></tr></tbody></table>

In this script, we first check that the nLocktime is set to a time in the future, before also checking whether the input is non-final. By doing both of these things, we can validate that the transaction will remain in an open payment channel until the nLocktime has expired, replicating and extending the functionality of removed opcode OP\_CHECKLOCKTIMEVERIFY.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/introduction-to-bitcoin-script/chapter-5-op_push_tx/13-nlocktime.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
