What is HTLC (hashed timelock contract)?

A conditional payment that releases funds to whoever reveals a secret before a deadline, and refunds the sender afterwards.

Not yet verifiedHow we verify

3 min read

In this entry

A conditional payment that releases funds to whoever reveals a secret before a deadline, and refunds the sender afterwards.

It is the primitive that makes routed Lightning payments safe: each hop is paid only if the next hop was, and every hop can reclaim its money if the payment stalls (source: the Lightning BOLT specifications). The same construction underlies simple cross-chain atomic swaps, where two people trade coins on different chains without trusting each other.

Most users never see one directly. It runs underneath a Lightning wallet or a swap interface, and its behavior surfaces only when something goes wrong, as a payment that appears stuck for hours or a channel that force-closes on its own.

How it works

Two ingredients combine. A hash lock says funds can be claimed by anyone who presents a value that hashes to a published digest. A time lock says that if nobody does so before a stated block height, the sender can take the funds back. Together they create a payment that either completes or unwinds, with no state in between where the money is lost.

A routed payment through several hops works like this.

  1. The recipient generates a random secret and gives the sender its hash, typically inside an invoice.
  2. The sender offers the first hop a payment locked to that hash, with a deadline.
  3. Each hop offers the next hop the same conditional payment, with a shorter deadline than the one it received, so an intermediary always has time to react after being paid.
  4. The recipient reveals the secret to claim the final hop, which lets that hop claim from the previous one, and the secret propagates backwards until the sender's hop settles.
  5. If the secret is never revealed, every deadline expires in order and every hop is refunded.

The risks that remain are about timing rather than trust. A routing node that goes offline mid-payment can force its channels to close on chain, which costs fees and locks funds until the timelock passes. And because deadlines shorten along the route, a long path holds a sender's funds pending for longer.

Example

Illustrative walkthrough with three parties. You pay a merchant 40,000 satoshis through one intermediary. The merchant creates a secret and sends you its hash. You offer the intermediary a payment claimable with that secret, expiring in 144 blocks, roughly a day. The intermediary offers the merchant the same payment less a routing fee, expiring in 72 blocks. The merchant reveals the secret and takes the funds, the intermediary uses the same secret to take yours, and everyone is settled in seconds. If the merchant had gone offline instead, nothing would have been claimed, and after 72 and then 144 blocks both offers would refund automatically.

Why it matters when you buy

A handful of exchanges support Lightning withdrawals for Bitcoin, which can be dramatically cheaper than an on-chain send for small amounts, and a stalled payment there is usually a timelock unwinding rather than lost funds. Compare withdrawal costs on the fee comparison before choosing a route, and see the chain pages for base-layer confirmation times.

Questions

Why is my Lightning payment stuck?

A hop along the route stopped responding, so the conditional payment is waiting for either the secret or its deadline. Wallets usually resolve it automatically once the timelock expires.

Can an intermediary steal the payment?

Not by design. A hop can only claim from the previous hop by presenting the same secret it had to pay forward, so it never profits without completing its side.

Do these work between different blockchains?

Yes, provided both chains support hash locks and time locks, which is how simple atomic swaps are built. In practice the coordination and timing requirements have kept them niche.