What is Hashed Timelock Contract (HTLC)?
A Hashed Timelock Contract (HTLC) is a type of smart contract used in blockchain and crypto applications to facilitate time-bound transactions. It is a transactional agreement used in crypto transactions to produce conditional payments.
This smart contract works in such a way that the recipient of a transaction has to acknowledge payment by generating cryptographic proof within a certain timeframe or preset deadline. If they fail to do so within the specific timeframe, the transaction will not take place and will be reversed to the sender.
Atomic swaps, cross-chain trading between cryptocurrencies is often done through the use of HTLCs. In addition, Bitcoin’s lightning network (LN) also utilizes HTLCs in facilitating transactions.
How a Hashed Timelock Contract Works
A Hashed Timelock Contract (HTLC) uses two elements that distinguish it from standard cryptocurrency transactions or ordinary smart contracts.
The first element is the hashlock. A hashlock is a hashed version of a public key generated by the initiator of a transaction. The related private key is then used to unlock the original hash. In HTLC, the sender generates a key and hashes it. The hash is stored in a pre-image that is later revealed to the sender during the final transaction.
The second important element of HTLC is a timelock. HTLCs are programmed to expire after a certain period of time or number of blocks generated, creating a known termination duration. Two different timelocks are used to set time limitations on contracts generated using HTLC. The first one is CheckLockTimeVerify (CLTV). It uses a time base to lock and release tokens.
These time constraints are hardcoded and coins are released only at a specific time and date. The second one is CheckSequenceVerify (CSV). It is not dependent on time. Instead, it uses the number of blocks generated as a measure to keep track of when to finalize a transaction.
Advantages of Hashed Timelock Contracts
- Time-bound transactions
Utilizing a hashed timelock contract system helps to facilitate time-bound transactions. It guarantees the timely execution of transactions which results in the timely execution and receipt of payments.
- Atomic swaps
HTLCs are used in various forms of atomic swaps, or peer-to-peer transfers that do not require an intermediary. An on-chain swap takes place between two different currencies in a cross-chain transaction and the transaction details are recorded directly on each chain. An off-chain swap requires an off-chain platform such as Lightning network to facilitate transfers of the native currency. Use of an HTLC ensures that both parties in an atomic swap fulfill their side of the trade by requiring proof of payment within a specified deadline.
- Trustless system:
As is the case with all smart contracts, trust is not needed to process transactions. This is because the rules are already coded into the contract itself. Hash TimeLock contracts take this one step further by implementing a time limit for recipients to acknowledge payments.