Hash Time Locked Contracts (HTLCs) are a type of smart contract that enable trustless, conditional cryptocurrency payments between parties using two cryptographic mechanisms: a hash lock (requiring the recipient to reveal a secret to claim funds) and a time lock (returning funds to the sender if unclaimed by a deadline). HTLCs are the foundational technology enabling the Bitcoin Lightning Network and cross-chain atomic swaps.
THE TWO LOCKING MECHANISMS
Hash Lock: The sender locks funds with the cryptographic hash of a secret (called a preimage). The recipient can only claim the funds by revealing the original secret, proving they know the preimage without the sender having to trust them to claim.
Time Lock: If the recipient has not claimed the funds by a specified block height or timestamp, the sender can reclaim their funds. This prevents funds from being locked indefinitely if the recipient fails to act.
HOW HTLC ENABLES TRUSTLESS EXCHANGE
Alice wants to send 1 BTC to Bob without trusting an intermediary. Bob generates a secret S and sends Alice the hash H(S). Alice locks 1 BTC in an HTLC: "pay to Bob if he reveals S within 24 hours; otherwise return to Alice." Bob, knowing S, reveals it to claim the Bitcoin. If Bob never claims, Alice gets her Bitcoin back after 24 hours.
THE BITCOIN LIGHTNING NETWORKHTLCs
make the Lightning Network's payment channels possible. When you open a Lightning channel, funds are locked in an HTLC-based multi-signature contract. Payments route through chains of HTLCs across multiple channels. Alice pays Bob through Carol without trusting Carol. Carol can only claim her routing fee by forwarding the payment correctly. If she fails or cheats, the HTLCs time out and funds return to their rightful owners.
ATOMIC SWAPS: CROSS-CHAIN HTLCSHTLCs
enable atomic swaps, direct exchange of cryptocurrencies on different blockchains without an exchange intermediary. Alice locks BTC on Bitcoin with H(S). Bob locks equivalent ETH on Ethereum with H(S). Alice reveals S to claim the ETH, simultaneously revealing S to Bob who uses it to claim the BTC. Either both swaps complete, or both time out, it is atomic (indivisible).