HTLCs (Hash Time Locked Contracts)
TL;DR
HTLCs are the fundamental smart contract primitive for trustless payments. They lock funds behind a cryptographic secret (Hash Lock) and a fallback expiration timer (Time Lock).
The Two Pillars of an HTLC
An HTLC locks a specific output (UTXO or channel balance) such that it can only be spent if one of two conditions are met:
-
The Hash Lock (Success):
- A secret byte array (the preimage) is generated by the recipient.
- The recipient provides the hash of this secret to the sender.
- The sender locks the funds using a conditional script that effectively says: “This output can be spent by the recipient if and only if they can reveal the original secret that maps to this hash.”
-
The Time Lock (Fallback/Refund):
- If the recipient vanishes or is uncooperative, the funds cannot sit in limbo forever.
- A time lock (typically enforced via
OP_CHECKLOCKTIMEVERIFY/CLTVon-chain, or similar channel logic off-chain) is added. - It dictates: “If a specific block height or time passes without the secret being revealed, the sender can spend this output back to themselves.”
Lightning Network Routing
HTLCs are famous for being the engine of Lightning Network multi-hop payments:
- Suppose Alice wants to pay Carol, but they are only connected via Bob.
- Alice sets up an HTLC paying Bob, contingent on a hash lock.
- Bob sets up a mirror HTLC paying Carol, contingent on the same hash lock.
- Carol pulls the funds from Bob by revealing the secret preimage.
- Once Bob observes the secret, he turns around and uses it to pull the funds from Alice.
The entire transaction chain collapses into a single atomic action: either everyone gets paid, or the time locks expire, and everyone gets refunded.
References: