Bitcoin Time locks
Last week at Qala Africa we were tasked with picking a topic from a collection of topics, researching it, and then presenting it. I picked Time locks and was paired with Collin. I choose time locks because that was a concept I didn’t quite understand at the time, this gave me the opportunity to research properly; I learned a lot and I will be sharing what I learned with you.
What are Time locks in Bitcoin?
A time lock is a locking mechanism that restricts the spending of Bitcoins until a particular time or block height in the future. This means that you can lock some Satoshis and specify some time in the future when those Satoshis could be spent. Just like a father sending some Satoshis to his child now (2022) but specifying in the transaction that the child can only spend the satoshis in the year 2040.
Classifying Time locks
Time locks can be classified using three basic attributes: Location, Target, and Unit.
Location: This is the place in which time lock is applied; it could be applied at the transaction level, that’s locking the whole transaction, or it could be applied at the script level whereby a particular UTXO or Output is locked until a particular time or block height in the future.
Target: This refers to the time in which the time lock is meant to expire, i.e. the coin will be available for spending. This could be an Absolute or Relative time. Absolute time is a definite time, for example, “The meeting would end by 04:00 pm” is an absolute time specifying that the meeting definitely ends by that time, but the relative time depends on other conditions to get the time, for example, “The meeting would last for 3 hours” this means that no matter when the meeting starts the meeting would be ending after 3 hours. In relation to Bitcoin, this means that a coin can be locked to an absolute time in the future and it can be locked relative to when the transaction or UTXO is mined into a block.
Units: This refers to the unit of measurement the timelock uses. In Bitcoin, they can be measured based on the number of blocks or the number of seconds.
Bitcoin Time locks
There are currently four locks at the time of writing this article, they are nLocktime, nSequence, OP_CHECKLOCKTIMEVERIFY(OP_CLTV), and OP_CHECKSEQUENCEVERIFY(OP_CSV). Two of them are script-level locks and two are transaction-level locks.
nLocktime
This is a transaction level and an absolute time lock. This was the initial time lock that was shipped with the First Bitcoin core client that was written by Satoshi. A transaction has the lock_time field by default and the current Bitcoin Core reference sets the value to the <Current Block Height + 1> to prevent fee sniping. The nLocktime locks a transaction until a particular time in the future, which means the transaction would not be a valid transaction and cannot be propagated until the specified time, this has a major drawback. Imagine I construct a transaction with a particular Unspent Transaction Output (UTXO) from a previous transaction and set the nlocktime to a time in the future, I can only send that transaction to you but cannot share it across the network because it would be regarded as an invalid transaction. The drawback is you could keep holding to that transaction waiting for when it would be valid, but from my end, I can (double) spend the UTXO I used in that transaction in another transaction and if that gets mined into a block, the transaction I sent to you becomes invalid and you lose the funds tired to that transaction, a solution to this is the OP_CHECKLOCKTIMEVERIFY.
CLTV
OP_CHECKLOCKTIMEVERIFY is a script level and absolute time lock. This was introduced as a Soft fork into Bitcoin in BIP 65, this is used to lock UTXOs instead of the whole transaction. What this means is, continuing from our previous example, instead of locking the whole transaction this time I only lock the output I am sending to you until a particular time in the future. This protects you from me double-spending the UTXO I used in constructing the transaction for you as it is eligible to be mined immediately, it just cannot be spent immediately. Another thing to be careful about is locking UTXO to some time far in the future as that would keep your funds locked up and you can’t use it.
How this works is that the transaction you are creating to spend an output locked by CLTV must have a nlocktime greater than or equal to the lock-time specified in the output. The script checks to compare the nlocktime of the transaction and that of the output, if nlocktime is less than the CLTV value the transaction is invalid, if it is equal or greater the transaction is valid, and remember a transaction with nlocktime is valid only when the current block height or time is equal to or greater than the nlocktime value.
I would like to bring your attention to the fact that although CLTV is a script-level transaction it still relies on the nLocktime value as its time source, that's the same nLocktime that’s used for transaction-level lock. Although, both locks use the nLocktime value, however the script-level CLTV enables the locking transaction to be valid (and therefore mined) immediately.
Get Bamidele Oluwatobi’s stories in your inbox
Join Medium for free to get updates from this writer.
Note: If the Script is locked in seconds then the nlocktime must be in seconds and if locked in block height the nlocktime must be in block height or else the transaction is invalid.
| |**Locking tx** | **Spending tx** |
|-----------|------------------------|------------------------|
| nLocktime | invalid until timelock | invalid until timelock |
| CLTV | valid immediately | nvalid until timelock |nSequence
This is a transaction level and relative time lock. This was introduced in BIP 68 and was activated as a soft fork in mid-2016. This helps lock the whole transaction relative to when the UTXO you are trying to spend was mined into a block, and this is specified in every input but it is a transaction-level lock.
Note: All nSequence value in transaction input must be less than or equal to the time it is relative to, in order for the transaction to be valid.
CSV
OP_CHECKSEQUENCEVERIFY is a script level and relative time lock. This was described in BIP 112 and was soft-forked in mid-2016. It is very similar to CLTV as they both lock UTXOs and they work in the same way ensuring UTXOs are not being spent until the time specified in the future, just that this is a relative time lock.
How it works is, when creating a transaction just like in the case of CLTV, the nSequence value should be equal to or greater than the CSV value. The script checks that the nSequence value is greater or equal to the CSV value to make the transaction valid. If that’s not the case, the transaction is considered invalid.
A Simple Summary Table
| | **Location** | **Target** | **Units** |
|-----------|--------------|------------|--------------|
| nLocktime | Transaction | Absolute | Block | Time |
| CLTV | Script | Absolute | Block | Time |
| nSequence | Transaction | Relative | Block | Time |
| CSV | Script | Relative | Block | Time |
Use cases for time lock today
Prevent Fee sniping: Fee sniping is when a miner tries to steal the fees for a transaction by trying to re-mine one or more previous blocks in order to steal fees from miners who have previously mined that block. Currently, it’s still theoretical because of the available block subsidy, but this might be an issue when the block subsidy runs out/is severely reduced. To prevent this, lock_times are set to <Current Block Height + 1>. This can combat fee-sniping because the transaction would be invalid in an earlier block that a miner might try to re-mine, as the transaction is only valid for the next block that is to be created.
Lightning Network HTLC: HTLC stands for Hash Time-Locked Contracts, this is how payments are made between channels in the Lightning network. Time locks help to ensure that HTLC does not last forever; if the payment has not been settled within the timeout, the HTLC can be claimed back by the sender.
Degrading Multi-Sig Transactions: Timelocks can also be useful for multi-Sig transactions. A typical example is a company creates a multi-sig transaction and specifies that 2 private keys of specific Managers should be able to spend the funds immediately at any point in time, but a second clause could allow the company lawyer to spend the funds after 2 months if the funds have not been spent as a backup.
Conclusion
Thanks for reading through, I hope you had a great time reading about Bitcoin Time locks, they are so interesting😊😊. I will write an article soon on how to create time-locked transactions with Node JS. Cheers🥂
References
Bitcoin’s Time locks: https://medium.com/summa-technology/bitcoins-time-locks-27e0c362d7a1










