Home › Glossary › HTLC

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.

BIPs that define HTLC

  • BIP 199 (HTLC): Hashed Time-Locked Contract transactions: Closed. Defines the hashed time-locked contract (HTLC), a script where funds can be claimed by revealing a secret before a deadline or reclaimed by the sender afterward. HTLCs are a core building block for…
  • BIP 65: OP_CHECKLOCKTIMEVERIFY: Deployed. Adds OP_CHECKLOCKTIMEVERIFY, a script command that keeps coins unspendable until a chosen date or block height. It enables time-locked savings, escrow with deadlines, and payment channels such as…
  • BIP 112: CHECKSEQUENCEVERIFY: Deployed. Introduces CHECKSEQUENCEVERIFY, a script instruction that lets coins be locked until a certain amount of time has passed since they were received. This 'relative timelock' is a key building block…

Related terms

  • Lightning Network: The Lightning Network is a layer-2 payment network on Bitcoin: users pay through chains of off-chain channels and settle on-chain only when needed.
  • Timelock: A timelock is a rule that stops bitcoin from being spent until a set block height or time, or until the coin has aged a set amount since it confirmed.
  • OP_CHECKLOCKTIMEVERIFY (CLTV): OP_CHECKLOCKTIMEVERIFY (CLTV, BIP 65) is an opcode that keeps an output unspendable until a chosen block height or time: an absolute timelock.
  • OP_CHECKSEQUENCEVERIFY (CSV): OP_CHECKSEQUENCEVERIFY (CSV, BIP 112) is an opcode that blocks spending an output until it has aged a set number of blocks or time since it confirmed.

Learn more

Guides