HTLC
Also called: hashed time-locked contract, hash time locked contract, hashlock, BIP 199.
An HTLC (hashed time-locked contract) pays whoever reveals a secret before a deadline and refunds the sender after it; Lightning routes payments with them.
A hashed time-locked contract (HTLC) is a script with two ways to spend:
- Hashlock: the recipient can claim the coins by revealing a secret value (the preimage) whose hash is written in the script.
- Timelock: if that doesn't happen before a deadline, the sender can take the coins back.
BIP 199, by Sean Bowe and Daira Hopwood, gives the script form:
OP_IF
[HASHOP] <digest> OP_EQUALVERIFY OP_DUP OP_HASH160 <seller pubkey hash>
OP_ELSE
<num> [TIMEOUTOP] OP_DROP OP_DUP OP_HASH160 <buyer pubkey hash>
OP_ENDIF
OP_EQUALVERIFY
OP_CHECKSIG
HASHOP is OP_SHA256 or OP_HASH160, and TIMEOUTOP is OP_CHECKSEQUENCEVERIFY or OP_CHECKLOCKTIMEVERIFY.
How HTLCs route Lightning payments
On the Lightning Network, say Alice pays Carol through Bob. Carol picks a secret and gives Alice its hash. Alice offers Bob an HTLC for that hash; Bob offers Carol one for the same hash with an earlier deadline. Carol reveals the secret to claim from Bob, and Bob uses it to claim from Alice. Since the same secret unlocks every hop, either everyone is paid or everyone is refunded, without trusting Bob. The revealed preimage also works as proof of payment.
Other uses and limits
BIP 199 also mentions atomic swaps and zero-knowledge contingent payments, where a secret is sold for bitcoin. One drawback is that every hop of a payment uses the same hash, which can link them. Point time-locked contracts (PTLCs), built on Schnorr signatures, are a proposed successor. See timelocks for the deadline side.