Timelocks

Timelocks are a type of functionality in Bitcoin that restrict the spending of funds until a later time or a specific block height.

Time in this case can either be represented as a UNIX timestamp, block height, or the number of blocks built on top of the block containing the transaction.

Timelocks can be absolute or relative.

Absolute timelocks specify an absolute point in the future when a particular transaction can go through.

This is done using a UNIX timestamp or blockheight.

Relative timelocks on the other hand specify a block depth, or the number of blocks that have been built on top of current one.

There is also the distinction between transaction-level and script-level or UTXO-level timelocks.

As their names imply, transaction-level timelocks impose a timelock on the transaction itself, whereas script-level timelocks can impose timelocks on specific UTXOs, and are specified in Bitcoin scripts.

Transaction-level timelocks were the first implementation of timelocks on bitcoin.

This timelock ensures that a transaction cannot not be included in a valid block until a particular time in the future.

However, the caveat is that since the timelock is only placed on the transaction but not the UTXO that is being spent, the sender could simply send another transaction spending from the same UTXO as the timelocked transaction.

This new transaction will go through first so long as the first transaction is still timelocked.

And so long as it is valid, the new transaction in effect invalidates and overrides the timelocked transaction, since the outputs that the timelocked transaction tries to spend from will have already been used, thereby cancelling out the original timelocked transaction.

In order to prevent this caveat, the time lock can also be placed on the UTXO itself.

The UTXO- level timelock specifies that a particular UTXO cannot be spent until the specified time.

This ensures that the sender of the UTXO cannot override the transaction by sending the UTXO to another address before the timelock is unlocked.

One note when using timelocks is that users must be careful near the expiry time of a timelock.

There is no notion of global time on the blockchain since peers’ clocks on the network could potentially be off.

Therefore, be wary when using absolute timelocks with UNIX timestamps.

A transaction that seems ready to spend given that a specific amount of time has passed might be viewed as premature by others.

Also, blocks are not created at guaranteed intervals either, so any attempts to cancel out of a timelocked transaction should be made a few hours before the timelock, specified in blockheight, expires.

Part 3 Summary