# HTLCs: hash time-locked contracts

An HTLC pays someone who reveals a secret before a deadline and otherwise refunds the sender. It makes multi-hop Lightning payments trustless. See how.

Canonical page: https://mybitcoinexplorer.com/learn/htlc
Topic: Lightning Network
Published: 2026-10-29
Updated: 2026-10-30


## Pay for a secret
An HTLC is a conditional payment: "this money goes to you if you reveal the secret whose hash is H **before** time T; otherwise it comes back to me."

> Diagram: An HTLC has two ways out — HTLC: locked to hash H → (knows secret)*Receiver claims: reveal the preimage → (or after T)Sender refund: timelock expires

It combines a **hash lock** ([hash](https://mybitcoinexplorer.com/learn/hash-functions) check) and a **time lock** ([CLTV](https://mybitcoinexplorer.com/learn/timelocks-cltv-csv)).

## Making routing trustless
1. Bob creates a random **secret** (the preimage) and gives Alice its hash in an [invoice](https://mybitcoinexplorer.com/learn/lightning-invoices).
1. Alice offers Carol an HTLC locked to that hash. Carol offers Dave one, Dave offers Bob one, each with a slightly **shorter deadline**.
1. Bob reveals the secret to claim Dave's HTLC. Dave now knows it and claims Carol's. Carol claims Alice's.

> Diagram: The same hash locks every hop; the secret settles them in reverse — Alice,Carol,Dave,Bob

Because each hop can only claim from the previous hop with the secret they just learned, nobody can take money without passing the payment on.

## The preimage is a receipt
Once Alice sees the preimage, she has proof Bob was paid: only Bob knew it.


## HTLCs beyond Lightning
The same hash-plus-timeout pattern powers **atomic swaps**: two people trade coins on two different chains, or on-chain for Lightning, without trusting each other. Either both sides get paid, or both get refunded.

## The script, simplified
~~~
IF
  <hash of secret> OP_SHA256 check, then the receiver's signature
ELSE
  <timeout> OP_CHECKLOCKTIMEVERIFY, then the sender's signature
ENDIF
~~~


## Key takeaways

- An HTLC pays for revealing a secret before a deadline, or refunds after it.
- Every hop is locked to the same hash, with decreasing deadlines.
- The revealed preimage is proof of payment.

## Common questions

### Why do deadlines decrease along the route?

So each intermediary has time to claim upstream after being paid downstream.

### Are HTLCs on-chain?

Usually not. They live inside commitment transactions and only appear on-chain if a channel force-closes mid-payment.

### What is a preimage?

The secret value whose hash locks the payment; revealing it claims the payment and proves it was made.

### What happens if the receiver never reveals the secret?

After the timeout, the sender takes the funds back.

## Related lessons

- [Lightning routing](https://mybitcoinexplorer.com/learn/lightning-routing)
- [Timelocks: CLTV and CSV](https://mybitcoinexplorer.com/learn/timelocks-cltv-csv)
- [Lightning invoices](https://mybitcoinexplorer.com/learn/lightning-invoices)
- [Payment channels](https://mybitcoinexplorer.com/learn/payment-channels)
