What Are HTLCs and How Do They Work in Crypto Payments?
An HTLC is a smart contract that releases crypto only when the right secret is shown before a deadline. It powers Lightning payments and atomic swaps.
On this page
Short answer
An HTLC, or hashed timelock contract, is a smart contract that holds crypto and releases it only when the recipient shows a secret that matches a hash, before a set deadline. If the secret never appears, the funds return to the sender, which is why HTLCs make trustless crypto payments possible.
What is an HTLC?
An HTLC is a smart contract that locks cryptocurrency behind two conditions: a hash lock and a time lock. The hash lock says the funds can only be claimed by someone who knows a secret whose hash matches the one in the contract. The time lock says the funds return to the sender if nobody claims them before a deadline.
The idea is simple. The sender picks a secret, hashes it, and puts the hash and a deadline into the contract. The receiver can only claim the funds by revealing the secret, and revealing it proves they are the right person. Nobody needs to trust anyone else, because the contract enforces the rules.
HTLCs matter because crypto payments often involve people who do not know each other. Without a lock like this, one side would have to pay first and hope the other side follows through. The HTLC removes that risk for both sides at once.
How does the hash lock work?
The hash lock works by hiding a secret behind its hash. A hash is a one-way fingerprint: anyone can check that a secret matches a hash, but nobody can work out the secret from the hash alone. The contract stores the hash, and the receiver must present the secret that produces it.
| Part | What it does | Who knows it |
|---|---|---|
| Secret | The password that claims the funds | Only the receiver, at first |
| Hash | The fingerprint stored in the contract | Everyone can see it |
| Deadline | The time after which funds return | Everyone can see it |
When the receiver claims the funds, the secret becomes public on the chain, because the claim transaction includes it. That is by design: in a chain of payments, each person who claims reveals the same secret to the next person, which is how the payment moves along. The secret itself is only a random value, so revealing it after the payment harms nobody.
How does the time lock protect both sides?
The time lock protects both sides by giving the payment a deadline. If the receiver never shows the secret, the sender can take the funds back after the deadline passes. Nobody's money is stuck forever because the other side went quiet.
The deadline also creates urgency. The receiver knows the funds will return to the sender if they wait too long, so they claim promptly. In a chain of payments, the deadlines are staggered: each hop has a slightly shorter deadline than the one before it, so nobody is left holding funds they cannot pass on.
Picking the deadline is a trade-off. A short deadline means funds return quickly if something fails, but it leaves little time to fix a problem. A long deadline is forgiving but ties up funds for longer. Payment networks usually set deadlines in hours, not days, for this reason.
How do HTLCs power Lightning payments?
HTLCs power Lightning payments because the Lightning Network moves bitcoin off the main chain through payment channels, and HTLCs are what make those channels safe. When you pay through Lightning, your payment hops through several channels, and an HTLC in each channel guarantees that either every hop settles or none of them does.
The trick is the shared secret. The final receiver creates a secret and its hash, and the same hash is used in every HTLC along the route. Each hop can only claim its funds by revealing the secret, so the payment either completes all the way to the receiver or fails cleanly and every hop gets its funds back.
This is what lets Lightning be fast and cheap. The hops happen off-chain, with no waiting for block confirmations, and the main chain is only used if something goes wrong. For the wallet side of this, the Bitcoin Lightning Network explains how channels and routing fit together.
Building the wallet side is covered by how to develop a Lightning Network wallet, which covers channel setup and key handling.
How do HTLCs make atomic swaps possible?
HTLCs make atomic swaps possible because they let two people on different blockchains trade without trusting each other or a middleman. An atomic swap is all-or-nothing: either both sides get what they agreed, or both get their funds back.
The swap uses the same secret on both chains. Alice locks her bitcoin in an HTLC on the Bitcoin chain, and Bob locks his ether in an HTLC on the Ethereum chain, both using the hash of Alice's secret. Alice claims Bob's ether by revealing her secret, which then lets Bob claim Alice's bitcoin with the same secret.
If either side stops, the time locks return the funds. Alice cannot take Bob's ether without giving him the secret he needs, and Bob cannot take Alice's bitcoin first. The trade either happens in full or not at all, which is why it is called atomic.
How do you use HTLCs in a crypto payment product?
You use HTLCs in a crypto payment product by building them into the payment flow, not by asking users to handle secrets themselves. The wallet or service creates the secret, sets the deadlines, and watches the chain for claims and refunds.
- Define the payment route. Decide which channels or chains the payment crosses and what each hop needs.
- Generate the secret and hash. Create a random secret for each payment and store its hash in every HTLC along the route.
- Set staggered deadlines. Give each hop a deadline slightly shorter than the one before it, so funds never get stuck.
- Watch for claims and refunds. Monitor the chain so a claim reveals the secret and a missed deadline triggers a refund.
- Handle failures. When a hop fails, release the locked funds and retry the payment along a different route.
- Test on testnets first. Run the full flow on test networks before real funds are involved.
Most teams do not write HTLC logic from scratch. Lightning libraries and swap protocols already implement it, so the product work is in routing, monitoring and failure handling. For the gateway side, crypto payment gateway development covers how payment products are put together.
Example (hypothetical): a cross-chain swap of bitcoin for ether
Example (hypothetical): Alice wants to swap 0.5 bitcoin for ether, and Bob agrees to a rate of 1 bitcoin for 20 ether. Alice creates a secret and locks 0.5 bitcoin in an HTLC on the Bitcoin chain, with a deadline of 24 hours. Bob locks 10 ether in an HTLC on the Ethereum chain, with a deadline of 12 hours, using the same hash.
Alice claims Bob's 10 ether by revealing her secret, and the claim is visible on the chain. Bob then uses the same secret to claim Alice's 0.5 bitcoin before his 24-hour deadline. The arithmetic checks: 0.5 bitcoin at 20 ether each is 10 ether, which is exactly what Bob locked.
If Alice never claims, Bob's 10 ether returns to him after 12 hours, and Alice's 0.5 bitcoin returns to her after 24 hours. Neither side loses funds because the other side went quiet, which is the whole point of the lock.
What are the common mistakes?
The most common HTLC mistakes are setting deadlines too short, reusing a secret across payments, and building a payment flow that cannot handle a failed hop.
- Deadlines too short. A hop that runs out of time before it can claim will fail the whole payment, even when everyone acted correctly.
- Reusing a secret. A secret used for two payments can link them and let one claim pay for the other. Generate a fresh secret every time.
- No failure handling. A payment that fails mid-route must release its locked funds and retry, or money sits locked until the deadline.
- Ignoring fees. Each hop and each on-chain claim costs a fee, so a long route can cost more than the payment is worth.
- Assuming instant settlement. Off-chain hops are fast, but a dispute that goes on-chain waits for block confirmations like any other transaction.
Key takeaways
- An HTLC locks crypto behind a hash lock and a time lock, so either the payment completes or the funds return
- The hash lock means only someone who knows the secret can claim the funds
- The time lock means funds are never stuck forever if the other side goes quiet
- Lightning payments and atomic swaps both rely on the same shared secret across hops
- Use a fresh secret per payment and stagger deadlines along the route
Frequently asked questions
What does HTLC stand for?
HTLC stands for hashed timelock contract. It is a smart contract that releases cryptocurrency only when the recipient shows a secret matching a stored hash, before a set deadline. If the deadline passes, the funds return to the sender.
Are HTLCs only used on Bitcoin?
No. HTLCs work on any chain that supports hash locks and time locks, including Bitcoin, Ethereum and most smart contract chains. They are best known from the Bitcoin Lightning Network, but the same idea powers cross-chain atomic swaps.
What happens if the receiver never claims the funds?
The funds return to the sender once the deadline passes. The sender can then claim a refund from the contract. No funds are lost, though they are locked until the deadline, which is why deadlines are usually set in hours.
Is the secret safe once it is revealed?
Yes, for that payment. The secret is a random value created for one payment only, and revealing it after the payment is part of how the payment settles. A fresh secret is generated for every new payment, so an old secret is useless.
How are HTLCs different from a normal escrow?
A normal escrow needs a trusted third party to hold the funds and decide who gets them. An HTLC replaces that third party with a contract: the rules are enforced by the chain itself, so neither side needs to trust the other or anyone else.
Do users need to understand HTLCs to pay with Lightning?
No. Wallets and payment services handle the secret, the deadlines and the monitoring behind the scenes. Users just send and receive, while the HTLCs make each hop safe without them seeing the details.