Hash Time Locked Contract (HTLC)¶
Overview¶
A Hash Time Locked Contract (HTLC) is a conditional payment mechanism originally introduced by the Lightning Network, and is now widely used in multiple systems including Interledger, Raiden Network, and Sprites channels.
HTLC combines two mechanisms - Hashlock and Timelock - requiring the asset recipient to confirm receipt and generate proof of receipt before a deadline, otherwise the assets are returned to the sender. This mechanism enables decentralized conditional payments without the need for a trusted third-party intermediary.
Core Mechanisms¶
Hashlock - The recipient Eric generates a secret value R - Computes the hash H = Hash(R) - Sends H to the sender Alice, without revealing R - Only providing the correct R can unlock the funds
Timelock - A deadline is set (e.g., 24 hours) - If the recipient fails to provide R before the deadline - Funds are automatically returned to the sender - Prevents funds from being permanently locked
Workflow 1. Preparation Phase: Eric generates R and sends H to Alice 2. Locking Phase: Alice creates an HTLC, locking funds with the condition of providing R or timeout 3. Unlocking Phase: Eric provides R before the deadline and receives the funds 4. Refund Phase: If timeout occurs, Alice can reclaim the funds
Application in the Lightning Network¶
Payment Channel Network HTLC is the key to why the Lightning Network is called a "network": - Alice and Bob have no direct channel - But payment can be completed through an intermediate node Charlie - Each hop uses HTLC to ensure security
Routed Payment Example
- Bob generates secret R and sends H to Alice
- Alice creates an HTLC to Charlie: provide R or refund
- Charlie creates an HTLC to Bob: provide R or refund
- Bob provides R to Charlie and receives the funds
- Charlie uses the same R to claim funds from Alice
- Payment complete: Bob receives the money, Alice gets proof of payment (R)
Atomicity Guarantee - Either the entire payment path succeeds - Or the entire payment fails and refunds occur - Partial success is impossible - Intermediate nodes cannot steal funds
Technical Details¶
HTLC Script In Bitcoin, an HTLC can be represented with the following script:
OP_IF
OP_HASH160 <H> OP_EQUALVERIFY
<Recipient's Public Key> OP_CHECKSIG
OP_ELSE
<Timeout> OP_CHECKLOCKTIMEVERIFY OP_DROP
<Sender's Public Key> OP_CHECKSIG
OP_ENDIF
Timelock Decrement In multi-hop payments, the timelock must decrease with each hop: - Alice → Charlie: 48 hours - Charlie → Bob: 24 hours - Ensures downstream unlocks first, giving upstream time to respond
Use Cases¶
Cross-Chain Atomic Swaps - Alice's Bitcoin ↔ Bob's Ether - Using the same hashlock H - HTLCs created simultaneously on both chains - Either both succeed or both fail
Decentralized Exchanges - Trustless asset exchange - Peer-to-peer trading - Eliminates counterparty risk
Cross-Border Payments - Cross-border transfers through multiple intermediaries - Each hop protected by HTLC - Fast and low-cost
Securities Settlement - Synchronized securities delivery and payment - DvP (Delivery versus Payment) - Reduced settlement risk
Evolution: PTLC¶
The Lightning Network has recently proposed PTLC (Point Time Locked Contracts):
Problems with HTLC - All participants use the same hash H - Intermediate nodes can correlate payments - Insufficient privacy
PTLC Improvements - Uses elliptic curve points instead of hashes - Each hop uses a different point - Better privacy protection - Requires Schnorr signature support (enabled by Taproot)
Security Considerations¶
Time Parameter Settings - Timelock cannot be too short: the recipient needs sufficient time to respond - Timelock cannot be too long: sender's funds locked for too long - Network latency and block confirmation times must be considered
Hash Function Security - Must use collision-resistant hash functions - The secret value R must have sufficient entropy - Prevent hash guessing attacks
Chain Reorganization Risk - Timelocks depend on block height or timestamps - Chain reorganizations may change timelock states - Appropriate confirmation depth is needed
Significance and Impact¶
HTLC is an important innovation in blockchain technology: - Enables trustless conditional payments - Makes payment channel networks possible - Drives Layer 2 solutions such as the Lightning Network - Inspired cross-chain and interoperability solutions
As the "backbone" of the Lightning Network, HTLC enables Bitcoin to achieve fast, low-cost micropayments and is a key component of Bitcoin's scalability solutions.
Recommended Reading¶
- HTLC Principles Explained
- Lightning Network Deep Dive
- HTLC Mechanism Detailed
Related Concepts¶
- Lightning Network
- Payment Channel
- Atomic Swap
- PTLC (Point Time Locked Contract)
- Timelock
- Hashlock