Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
LABS
Glossary

Hash Time-Locked Contract (HTLC)

A Hash Time-Locked Contract (HTLC) is a smart contract that uses a cryptographic hash and a timelock to enable conditional, trust-minimized payments for atomic swaps and cross-chain transfers.
Chainscore © 2026
definition
BLOCKCHAIN PROTOCOL

What is a Hash Time-Locked Contract (HTLC)?

A foundational cryptographic primitive enabling conditional, trust-minimized payments across blockchain networks.

A Hash Time-Locked Contract (HTLC) is a type of smart contract that enables conditional payments by cryptographically linking the transfer of funds to the presentation of a secret preimage within a specified time window. It is a core mechanism for enabling atomic swaps and facilitating off-chain payment channels in networks like the Lightning Network. The contract uses two primary locks: a hashlock, which requires the recipient to provide the cryptographic proof (the preimage) that generates a known hash, and a timelock, which allows the original sender to reclaim the funds if the secret is not revealed before a deadline.

The operation of an HTLC follows a precise sequence. First, the sender creates a cryptographic hash (e.g., SHA-256) of a secret and embeds this hash into the payment condition. The locked funds are then sent to a contract address that can only be claimed by revealing the secret's preimage. The recipient must discover or obtain this preimage to unlock and claim the funds. Crucially, the contract also includes a refund path: if the recipient fails to provide the preimage before the timelock expires, the funds are automatically returned to the sender. This creates a secure, self-enforcing agreement without requiring a trusted third party.

HTLCs are the backbone of interoperability and scalability solutions. Their most prominent application is in cross-chain atomic swaps, allowing users to exchange cryptocurrencies across different blockchains (e.g., Bitcoin for Litecoin) without centralized exchanges. Within a single blockchain, they are essential for payment channel networks, where they enable multi-hop payments by creating a chain of contingent HTLCs across participants. This allows value to be routed through intermediaries who never gain custody of the funds, only facilitating the secret's propagation to trigger the conditional transfers along the path.

While powerful, HTLCs have limitations. The fixed timelocks can create liquidity constraints and require careful coordination of timeout periods across a payment route. They are also susceptible to hash exhaustion attacks if weak hashes are used, and to griefing attacks where a malicious participant can temporarily lock up a counterparty's capital without intending to complete the trade. Newer protocols, such as Point Time-Locked Contracts (PTLCs), aim to address these issues by using Schnorr signatures and adaptor signatures, offering greater efficiency, privacy, and reduced on-chain footprint compared to the hash-based model.

how-it-works
MECHANISM

How Does an HTLC Work?

A technical breakdown of the conditional payment mechanism that enables trust-minimized cross-chain and off-chain transactions.

A Hash Time-Locked Contract (HTLC) is a conditional smart contract that facilitates a trust-minimized exchange by requiring the recipient to cryptographically prove knowledge of a secret within a set timeframe. It operates on two core cryptographic primitives: a hashlock and a timelock. The hashlock is a condition that can only be fulfilled by revealing the preimage—the original data that produces a specific cryptographic hash. The timelock, enforced by the blockchain, sets a strict deadline for this revelation, after which the transaction can be refunded to the original sender.

The process begins when Party A initiates an HTLC, locking funds in a contract that specifies a payment hash and an expiry block height. To claim these funds, Party B must present the correct preimage, which proves they possess the secret. This act of revealing the preimage simultaneously unlocks the funds for Party B and publicly discloses the secret on the blockchain. This public disclosure is crucial, as it allows subsequent parties in a payment route to also claim their conditional payments, enabling multi-hop transactions without requiring direct trust between all participants.

The timelock provides critical security for all parties. If Party B fails to produce the preimage before the contract expires, the funds are automatically refunded to Party A. This mechanism ensures that capital is never permanently locked. Timelocks are typically implemented using CheckLockTimeVerify (CLTV) or CheckSequenceVerify (CSV) opcodes in Bitcoin, or similar native functions in other smart contract platforms. The sequential nature of timelocks—where each hop in a route has a slightly earlier expiry—ensures the entire atomic swap or payment channel update either completes successfully or rolls back completely.

HTLCs are the foundational building block for several key blockchain scalability and interoperability solutions. They are the core mechanism enabling Lightning Network payments, where they facilitate secure, off-chain routing across multiple nodes. In atomic swaps, HTLCs allow for the cross-chain exchange of different cryptocurrencies without a centralized intermediary. Their deterministic and verifiable logic also makes them integral to more complex cross-chain communication protocols, where proving knowledge of a secret on one chain can trigger an action on another.

key-features
MECHANISM DEEP DIVE

Key Features of HTLCs

Hash Time-Locked Contracts (HTLCs) are a fundamental cryptographic primitive enabling conditional, trust-minimized value transfer across blockchains or payment channels. Their core features enforce atomicity and security for cross-chain swaps and Lightning Network payments.

01

Cryptographic Hashlock

The hashlock is a cryptographic condition that locks funds. The sender generates a secret preimage and publishes its cryptographic hash (e.g., SHA-256). Funds can only be claimed by any party who reveals the correct preimage that hashes to the published value. This creates a provable secret that enables conditional payment across untrusted parties.

02

Time-Based Expiry (Timelock)

The timelock is a safety mechanism that sets a deadline for the transaction. It is enforced via a CHECKLOCKTIMEVERIFY (CLTV) or CHECKSEQUENCEVERIFY (CSV) opcode in Bitcoin Script. If the recipient fails to claim the funds by revealing the preimage before the timelock expires, the funds are refunded to the original sender. This prevents funds from being locked indefinitely.

03

Atomicity Guarantee

HTLCs enable atomic swaps, where two cross-chain transactions either both succeed or both fail. The sequence is:

  • Alice locks BTC in an HTLC with Bob's hash.
  • Bob, to claim it, must reveal the preimage.
  • Alice uses that revealed preimage to claim Bob's locked ETH in a parallel HTLC. This hash revelation ensures the entire exchange is atomic; one party cannot take the funds without completing their side of the deal.
04

Trust-Minimized Intermediaries

HTLCs allow for the creation of payment channels (like the Lightning Network) where intermediaries can forward payments without custody of funds. Each hop in the route is a separate HTLC with the same hashlock but progressively shorter timelocks. An intermediary can only claim funds from an upstream party if they have already paid the downstream party, eliminating counterparty risk.

05

Script-Based Implementation

On Bitcoin, HTLCs are implemented using a Bitcoin Script that combines hash and time conditions. A typical redeem script logic is: IF (signature AND hash preimage) ELSE (timelock AND signature). On Ethereum, they are implemented as smart contracts with similar conditional logic using keccak256 for the hash and block numbers for the timelock.

06

Use Cases & Limitations

Primary Use Cases:

  • Cross-chain atomic swaps between cryptocurrencies.
  • Off-chain payment channels (Lightning, Raiden).
  • Cross-chain bridges for asset transfers.

Key Limitations:

  • Requires temporal coordination due to timelocks.
  • Vulnerable to griefing attacks if a party broadcasts an old state.
  • Liquidity must be locked for the duration, creating opportunity cost.
visual-explainer
MECHANISM EXPLAINER

Visualizing an Atomic Swap with HTLCs

A step-by-step walkthrough of how a Hash Time-Locked Contract (HTLC) enables a trustless cryptocurrency exchange between two parties on different blockchains, ensuring the swap is either completed entirely or not at all.

An atomic swap is a peer-to-peer exchange of cryptocurrencies across different blockchains, executed without a trusted third party. The core mechanism enabling this is the Hash Time-Locked Contract (HTLC), a type of smart contract that uses a cryptographic hash and a timelock to create conditional payments. The process ensures atomicity: the entire transaction sequence either completes successfully for both parties, or funds are returned to their original owners, eliminating counterparty risk. This is visualized as a coordinated, multi-step dance between two blockchains.

The swap begins when Party A, who wants to trade Bitcoin for Party B's Litecoin, generates a secret random number (a preimage) and computes its cryptographic hash. Party A then creates an HTLC on the Bitcoin blockchain, locking funds with two conditions: the funds can be claimed by anyone who reveals the preimage that produces the known hash, or they can be refunded to Party A after a specified timelock expires. Party A sends this hash to Party B, who cannot yet claim the Bitcoin because they do not know the secret preimage.

Upon receiving the hash, Party B creates a corresponding HTLC on the Litecoin blockchain, locking their Litecoin with the same hash. This contract has a shorter timelock than the Bitcoin one. To claim the Litecoin, Party A must now reveal the secret preimage, which they happily do, successfully claiming the Litecoin. Crucially, by revealing the preimage on the Litecoin chain, Party B learns the secret. Party B can now use that preimage to claim the Bitcoin from the original HTLC before its longer timelock expires. If any step fails, the timelocks ensure funds are automatically refunded, making the swap atomic.

ecosystem-usage
HASH TIME-LOCKED CONTRACT (HTLC)

Ecosystem Usage & Protocols

A Hash Time-Locked Contract (HTLC) is a smart contract that enables conditional payments across blockchains or payment channels, using a cryptographic hash and a time constraint to secure transactions. It is the foundational protocol for trustless atomic swaps and the Lightning Network.

01

Core Mechanism: Hash & Time Lock

An HTLC enforces payment with two conditions:

  • Hash Lock: The recipient must provide the cryptographic preimage (secret data) that produces a specific hash to claim the funds.
  • Time Lock: If the preimage is not revealed before a set block height or timestamp, the funds are refunded to the sender. This creates a secure, self-executing escrow without a trusted third party.
02

Enabling Atomic Swaps

HTLCs power cross-chain atomic swaps, allowing direct peer-to-peer cryptocurrency trades between different blockchains (e.g., Bitcoin for Litecoin). The process is atomic: either the entire swap completes successfully when the secret is revealed, or all funds are refunded after the time lock expires, eliminating counterparty risk.

03

Foundation of Payment Channels

The Lightning Network and similar Layer 2 scaling solutions use HTLCs as their core routing protocol. They enable off-chain micropayments by creating a path of linked HTLCs across multiple nodes. Each hop in the path is secured by the same hash lock, allowing funds to flow conditionally without settling every transaction on the base layer blockchain.

04

Real-World Example: Lightning Payment

Alice wants to pay Carol 0.01 BTC via the Lightning Network, routing through Bob.

  1. Carol generates a secret R and sends its hash H to Alice.
  2. Alice creates an HTLC to Bob, locked with H and a 24-hour timeout.
  3. Bob creates an HTLC to Carol with the same H and a 12-hour timeout.
  4. Carol reveals R to claim funds from Bob, which automatically reveals it to Alice via the protocol.
  5. Bob uses R to claim funds from Alice. If any step fails, time locks refund all parties.
05

Security Considerations

While highly secure, HTLCs have specific risk parameters:

  • Hash Strength: Relies on the cryptographic security of the hash function (e.g., SHA-256).
  • Time Lock Coordination: Critical to set refund timeouts correctly to prevent funds from being stuck or stolen. The recipient's time lock must be significantly shorter than the sender's.
  • Liquidity Lock-up: Funds are immobilized for the duration of the contract, which can be a capital efficiency concern for routing nodes.
06

Beyond Cryptocurrency: Interoperability

The HTLC pattern is a generic construct for conditional value transfer and is being explored beyond simple payments:

  • Cross-chain bridges for asset transfers.
  • Decentralized exchanges (DEXs) for order book matching.
  • Conditional escrow in supply chain or insurance smart contracts. It serves as a fundamental primitive for building interoperable, trust-minimized protocols.
security-considerations
HASH TIME-LOCKED CONTRACT (HTLC)

Security Considerations & Limitations

While HTLCs are a foundational primitive for secure cross-chain and off-chain transactions, their security model introduces specific risks and operational constraints that must be understood.

01

Preimage Secrecy & Collusion

The security of an HTLC hinges on the preimage (secret) remaining unknown until the intended recipient reveals it. Key risks include:

  • Sender Collusion: The original sender could reveal the preimage to a third party before the intended recipient, allowing them to claim the funds.
  • Malicious Intermediaries: In multi-hop payments (e.g., Lightning Network), a malicious node could learn the preimage and steal the payment.
  • Timing Attacks: Observers on-chain can monitor for preimage reveals and attempt to front-run transactions on the destination chain in cross-chain swaps.
02

Time-Lock Expiry Risks

The time-lock is a critical parameter that balances security and liveness. Setting it incorrectly creates vulnerabilities:

  • Too Short: The recipient may not have enough time to claim the funds, especially if blockchain congestion causes delays. The sender can reclaim funds after expiry, causing a failed transaction.
  • Too Long: Funds are locked and unusable for an extended period. If the sender's refund path is also time-locked, capital efficiency suffers.
  • Block Time Variance: On chains with variable block times (e.g., Proof-of-Work during high congestion), the actual expiry time is uncertain, requiring conservative buffer periods.
03

Implementation & Cryptographic Assumptions

HTLC security depends on correct implementation and robust cryptography:

  • Hash Function Security: The contract uses a cryptographic hash function (e.g., SHA-256). A cryptographic break of the hash function would allow anyone to generate the preimage, destroying the contract's security.
  • Smart Contract Bugs: On-chain HTLC implementations are vulnerable to reentrancy, logic errors, or incorrect condition checks in the smart contract code.
  • Oracle Reliance: Some cross-chain HTLC designs rely on oracles or relayers to verify proofs. This introduces trust in the oracle's availability and correctness.
04

Liquidity & Capital Lockup

HTLCs are not free; they impose real economic costs:

  • Locked Capital: The funds in the HTLC are immobilized for the duration of the time-lock, creating an opportunity cost and reducing liquidity.
  • Channel Capacity: In payment channels (Lightning Network), an HTLC locks a portion of a channel's capacity, preventing its use for other payments until it is resolved.
  • Cross-Chain Atomic Swaps: Requires both parties to have funds locked on two chains simultaneously, doubling the capital commitment and exposure to volatility during the swap period.
05

Privacy Limitations

On-chain HTLCs offer limited privacy, as their mechanics are publicly visible:

  • Transaction Graph Linkability: The hashlock and timelock conditions are visible on-chain. An observer can link the HTLC creation transaction with the subsequent reveal transaction, connecting sender and recipient addresses.
  • Amount Correlation: In atomic swaps, the locked amounts on both chains are public, making it possible to correlate the swap participants and values.
  • Preimage Surveillance: Services can watch for preimage reveals on public blockchains, compromising privacy for linked transactions.
06

Network & Consensus Dependencies

HTLC execution is ultimately subject to the underlying blockchain's properties:

  • Chain Reorganizations: A deep chain reorg could invalidate a preimage reveal transaction that appeared confirmed, potentially allowing a time-lock refund to proceed unfairly.
  • Censorship: Miners/validators could censor the transaction where the recipient reveals the preimage, causing the transaction to expire and the funds to revert to the sender.
  • Throughput Limits: During network congestion, high fees may be required to ensure the claim or refund transaction is included before the time-lock expires, increasing costs.
COMPARISON MATRIX

HTLCs vs. Other Cross-Chain Models

A feature and trade-off comparison of Hash Time-Locked Contracts against alternative cross-chain interoperability approaches.

Feature / MetricHTLCsAtomic Swaps (via HTLC)Bridges (Lock & Mint)Interoperability Hubs

Core Mechanism

Hash & Time Lock

HTLC Execution

Asset Custody & Wrapping

Cross-Chain Messaging

Trust Model

Trustless (Cryptographic)

Trustless (Peer-to-Peer)

Trusted / Federated

Trust-Minimized (Validator Set)

Native Asset Support

Cross-Chain Composability

Typical Latency

10 min - 1 hr

10 min - 1 hr

< 5 min

< 2 min

Capital Efficiency

High (No Lockup)

High (No Lockup)

Low (Vault Lockup)

Medium (Bonded Liquidity)

Security Assumptions

Blockchain Finality

Blockchain Finality

Bridge Operator Honesty

Validator Set Honesty

Primary Use Case

Peer-to-Peer Swaps

Direct Asset Exchange

Asset Portability

General Message Passing

examples
HTLC APPLICATIONS

Examples & Use Cases

Hash Time-Locked Contracts (HTLCs) are a fundamental cryptographic primitive enabling conditional, trust-minimized value transfer. Their primary use cases are in cross-chain and off-chain protocols.

04

Conditional Payments & Escrow

HTLCs can facilitate cryptographic escrow for goods, services, or oracle-based outcomes. Payment is locked until a specific, verifiable condition is met and the secret preimage is published.

  • Use Case: Paying for a digital file only upon successful delivery, where the decryption key is the preimage.
  • Oracle Integration: Locking funds contingent on a real-world event (e.g., "pay if team X wins"). A trusted oracle publishes the preimage (the outcome) to release funds to the correct party. The time lock ensures funds don't remain locked indefinitely if the oracle fails.
05

Privacy-Enhancing Protocols

Protocols like Atomic Multi-Path Payments (AMP) in Lightning use HTLC variants to improve privacy and reliability. By splitting a payment into multiple smaller HTLCs with different hashes and paths, it becomes harder for intermediaries to deduce the payment's sender, recipient, or total amount.

  • Benefit: Obscures the payment graph, enhancing transaction graph privacy.
  • Mechanism: The recipient can reconstruct the original preimage from shares provided across the different HTLCs, allowing them to claim all partial payments.
HASH TIME-LOCKED CONTRACT

Frequently Asked Questions (FAQ)

A Hash Time-Locked Contract (HTLC) is a specialized smart contract that enables conditional payments across blockchain networks, forming a critical component of trustless interoperability protocols like the Lightning Network and cross-chain bridges.

A Hash Time-Locked Contract (HTLC) is a smart contract that facilitates a conditional payment, requiring the recipient to acknowledge receipt by submitting a cryptographic proof within a specified timeframe or forfeit the ability to claim the funds. It works by locking funds with two conditions: the presentation of a preimage (a secret number) that hashes to a publicly known hash lock, and the fulfillment of this condition before a time lock expires. This creates a secure, trustless escrow. For example, in a cross-chain atomic swap, Party A locks BTC in an HTLC with hash H. Party B, seeing proof of this lock, locks ETH in a corresponding HTLC with the same hash H. Party A claims the ETH by revealing the preimage, which then allows Party B to claim the BTC using the same revealed secret, ensuring both transactions succeed or both fail.

ENQUIRY

Get In Touch
today.

Our experts will offer a free quote and a 30min call to discuss your project.

NDA Protected
24h Response
Directly to Engineering Team
10+
Protocols Shipped
$20M+
TVL Overall
NDA Protected Directly to Engineering Team
Hash Time-Locked Contract (HTLC) | Blockchain Glossary | ChainScore Glossary