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, forming the basis for atomic swaps.
Chainscore © 2026
definition
BLOCKCHAIN PROTOCOL

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

A Hash Time-Locked Contract (HTLC) is a specialized smart contract that enables conditional payments across a blockchain network, forming the cryptographic backbone for trustless, cross-chain, and off-chain transactions.

A Hash Time-Locked Contract (HTLC) is a type of smart contract that facilitates conditional payments by requiring the recipient to acknowledge receipt cryptographically within a set timeframe. It uses two core cryptographic primitives: a hashlock and a timelock. The hashlock is a condition that requires the presentation of a cryptographic proof, known as a preimage, to unlock funds. The timelock sets a strict deadline, after which the transaction can be refunded to the original sender if the condition is not met. This creates a secure, trustless escrow mechanism.

The primary use case for HTLCs is enabling atomic swaps and payment channels, which are fundamental to Layer 2 scaling solutions like the Lightning Network. In an atomic swap, two parties can exchange cryptocurrencies across different blockchains without a centralized intermediary. The HTLC ensures that either the entire exchange completes successfully or all funds are returned, eliminating counterparty risk. This mechanism is also crucial for cross-chain bridges and decentralized finance (DeFi) protocols that require interoperable, conditional logic.

From a technical perspective, creating an HTLC involves generating a secret preimage and its cryptographic hash. The hash is embedded into the contract's spending condition. To claim the funds, the recipient must reveal the preimage, which proves they can fulfill the contract's terms. This revealed secret can then be used to trigger subsequent linked contracts, enabling complex, multi-hop payment routes. The deterministic nature of the hash function guarantees that only the holder of the correct preimage can proceed.

While highly secure, HTLCs have limitations. The fixed timelock requires careful coordination of block times across networks and can expose participants to liquidity risks if a party delays revealing the preimage. Furthermore, they are susceptible to hashlock griefing attacks, where a malicious actor can lock up a counterparty's capital without intending to complete the trade. Newer protocols, such as Point Time-Locked Contracts (PTLCs), are being developed to address these inefficiencies using more advanced cryptographic signatures.

how-it-works
MECHANISM

How Does an HTLC Work?

A Hash Time-Locked Contract (HTLC) is a conditional payment mechanism enabling secure, trust-minimized value transfer across blockchain networks or payment channels.

An HTLC is a smart contract that locks funds with two cryptographic conditions: a hash lock and a time lock. The hash lock requires the recipient to present the cryptographic preimage—the original data that generates a specific hash—to claim the funds. The time lock sets a deadline; if the funds are not claimed by the recipient before this deadline, they can be refunded to the original sender. This creates a conditional escrow, ensuring the transaction is atomic: it either completes fully for both parties or fails safely.

The protocol typically involves two interconnected HTLCs on different ledgers or channels. In a cross-chain atomic swap, Party A locks funds in an HTLC on Chain 1, revealing the hash of a secret. Party B, seeing this proof on Chain 1, can lock corresponding funds in an HTLC on Chain 2, using the same hash. To claim the funds on Chain 2, Party A must reveal the secret preimage, which automatically unlocks the funds for them. Crucially, this revelation also reveals the secret to Party B, who can then use it to claim the original funds locked on Chain 1, completing the swap.

Time locks are critical for security and liveness. They are implemented using absolute block heights (e.g., cltv in Bitcoin) or relative time delays (e.g., in Lightning Network channels). The refund path protects the sender if the counterparty becomes unresponsive. The timing must be carefully calibrated: the timeout on the first chain must be sufficiently longer than on the second to allow the entire sequence of claims to settle, preventing a scenario where one party can claim funds on one chain while refunding on the other.

HTLCs are the foundational primitive for interoperability and layer-2 scaling. Beyond simple atomic swaps, they power the routing of payments in the Lightning Network and other payment channel networks. Each hop in a multi-hop payment is secured by a separate HTLC, creating a chain of conditional payments that settles only when the final recipient reveals the preimage. This enables fast, low-cost transactions without requiring global blockchain consensus for each step.

While revolutionary, basic HTLCs have limitations, including routing complexity and capital inefficiency in payment channels. They are also vulnerable to hash collision attacks if weak hash functions are used, and to griefing attacks where a node can lock up a counterparty's capital without intending to settle. Advanced constructions like Point Time-Locked Contracts (PTLCs), which use cryptographic signatures instead of hash preimages, are being developed to address these issues, offering greater privacy and efficiency.

key-features
MECHANISM BREAKDOWN

Key Features of HTLCs

A Hash Time-Locked Contract (HTLC) is a conditional payment mechanism that enforces atomicity in cross-chain or off-chain transactions using cryptographic proofs and time constraints.

01

Atomic Swap

An HTLC enables trustless peer-to-peer cryptocurrency trades across different blockchains without a centralized intermediary. The swap is atomic, meaning it either completes entirely for both parties or fails entirely, preventing one side from stealing funds.

  • Process: Alice locks BTC in an HTLC with a hash puzzle. Bob, seeing the lock, locks ETH in a corresponding HTLC with the same puzzle. Alice reveals the secret to claim Bob's ETH, which then allows Bob to use that secret to claim Alice's BTC.
  • Use Case: Direct exchange of Bitcoin for Litecoin or other cross-chain assets.
02

Payment Hash & Secret

The core cryptographic primitive of an HTLC is a hashlock, a condition that requires the presentation of a secret preimage to spend the locked funds.

  • Hash (Hashlock): A public cryptographic hash (e.g., H = SHA256(secret)). This is placed into the smart contract or script as the spending condition.
  • Secret (Preimage): The private data that, when hashed, produces the public hash. Revealing this secret unlocks the funds.
  • Property: It is cryptographically infeasible to derive the secret from the hash, ensuring security until the rightful party chooses to reveal it.
03

Timelock (CLTV/CSV)

An HTLC includes a timelock, a second condition that allows the original fund sender to reclaim their money if the payment isn't completed within a set period.

  • Purpose: Acts as a safety mechanism, preventing funds from being locked indefinitely if the counterparty disappears.
  • Implementation:
    • CheckLockTimeVerify (CLTV): Absolute timelock (e.g., "after block 850,000").
    • CheckSequenceVerify (CSV): Relative timelock (e.g., "1000 blocks after confirmation").
  • Refund Path: If the secret is not revealed before the timelock expires, the sender can execute a refund transaction.
04

Lightning Network Payments

HTLCs are the fundamental building block for off-chain payment channels like the Bitcoin Lightning Network and Ethereum's Lightning-analogous systems.

  • Function: They enable secure, multi-hop routing. Each node in the payment path creates an HTLC with the next node, all using the same hash.
  • Process: The secret is revealed upon payment to the final recipient, then propagates back through the chain, allowing each intermediary to claim funds from the preceding party.
  • Result: Enables instant, low-fee, scalable transactions that are ultimately settled on the base blockchain.
05

Cross-Chain Bridges

HTLCs are used in simple atomic cross-chain bridges to facilitate the movement of assets between two independent blockchains.

  • Model: A user locks Asset A on Chain 1 in an HTLC. A bridge validator or watcher observes this lock and mints a wrapped representation of Asset A on Chain 2, sending it to the user.
  • Claim: The user reveals the secret to claim the wrapped assets, which also allows the bridge to finalize the lock on the original chain.
  • Consideration: While trust-minimized for the user, these models often involve trust in the bridge operators or federations.
06

Conditional Escrow

HTLCs can function as a form of cryptographic escrow for contingent payments, where release of funds depends on proving receipt of a good, service, or piece of data.

  • Mechanism: The payment is locked with a hash whose secret is known only to the payer. The secret is shared with the payee only upon satisfactory fulfillment of the off-chain condition.
  • Example: In a data marketplace, a hash is generated from a specific data report. Payment is locked in an HTLC. Upon delivery and verification of the correct report (the preimage), the seller can claim the funds.
  • Advantage: Reduces need for trusted third-party escrow agents.
visual-explainer
PROTOCOL MECHANICS

Visualizing an HTLC Atomic Swap

A step-by-step visualization of the cryptographic dance that enables trustless cross-chain asset exchange using Hash Time-Locked Contracts.

An HTLC atomic swap is a multi-step cryptographic protocol that enables two parties to exchange assets on different blockchains without a trusted intermediary, ensuring the swap either completes entirely or fails without loss. The process is governed by a Hash Time-Locked Contract (HTLC) on each chain, which uses a cryptographic hash and a timelock to enforce the atomicity—the "all-or-nothing" property—of the transaction. This visualization breaks down the sequence from initiation to final settlement.

The process begins when Party A, who holds Bitcoin, and Party B, who holds Litecoin, agree to swap. Party A generates a secret random number (a preimage) and computes its cryptographic hash. Party A then creates an HTLC on the Bitcoin blockchain, locking their BTC with two conditions: it can be claimed by anyone who reveals the preimage matching the published hash, or it can be refunded to Party A after a specified timelock (e.g., 48 hours) expires. This hash is the critical piece of information that links the two contracts.

Upon seeing the hash on the Bitcoin blockchain, Party B creates a corresponding HTLC on the Litecoin blockchain, locking their LTC with identical conditions: it pays to the party who reveals the preimage, with a shorter refund timelock (e.g., 24 hours). The shorter timelock on the second chain is a crucial security measure; it ensures Party B can claim the Bitcoin and then Party A has ample time to claim the Litecoin before either contract can be refunded. This timelock asymmetry protects both parties from being stranded with an unclaimable asset.

To complete the swap, Party B claims the BTC by submitting the preimage to the Bitcoin HTLC, which proves they know the secret and releases the funds. This action, however, publicly reveals the preimage on the Bitcoin blockchain. Party A then uses this now-public preimage to claim the LTC from the Litecoin HTLC before the shorter timelock expires. If any step fails—for instance, if Party B never creates the second contract—the initial timelocks will eventually expire, and all funds are automatically refunded to their original owners, resulting in a failed but safe state.

This protocol demonstrates core blockchain principles: trust minimization through cryptographic proofs, interoperability without centralized bridges, and atomic settlement. While primarily used for cross-chain swaps of UTXO-based assets like Bitcoin and Litecoin, the HTLC pattern is a foundational primitive for more complex systems such as the Lightning Network, which uses HTLCs for routed payments. Visualizing the flow of hash, preimage, and timelocks reveals the elegant security model that makes decentralized finance possible.

examples
HASH TIME-LOCKED CONTRACT (HTLC)

Primary Use Cases & Examples

HTLCs are a foundational cryptographic primitive enabling conditional, time-bound value transfers across blockchains and payment channels. Their primary applications are in atomic swaps, cross-chain bridges, and the Lightning Network.

01

Atomic Swaps

An atomic swap is a peer-to-peer cryptocurrency exchange between two different blockchains without a trusted third party. An HTLC makes this possible by creating a conditional payment where:

  • Party A locks funds in an HTLC on Chain A, with a cryptographic secret as the condition.
  • Party B, upon seeing the lock, creates a corresponding HTLC on Chain B.
  • When Party A reveals the secret to claim the funds on Chain B, Party B learns the secret and can claim the original funds on Chain A. This ensures the swap either completes entirely for both parties or is refunded, preventing partial execution.
02

Lightning Network Payments

The Lightning Network relies on HTLCs to enable fast, cheap Bitcoin (or Litecoin) payments through a network of bidirectional payment channels. When routing a payment:

  • Each hop in the payment path creates an HTLC, with the same payment hash but sequentially shorter timelocks.
  • The final recipient reveals the payment preimage to claim the funds, which propagates back through the chain, allowing each intermediary to claim their inbound HTLC.
  • The decrementing timelocks ensure that if a node fails to forward the secret, the previous hop can safely refund their funds before their lock expires, securing the network against theft.
03

Cross-Chain Bridges

Many cross-chain bridges use HTLCs for simple asset transfers. In this model:

  • A user locks asset X on the source chain in an HTLC with a specific secret hash and timelock.
  • A bridge validator or relayer observes this event and creates (mints or unlocks) the equivalent wrapped asset Y on the destination chain, sending it to the user.
  • The user reveals the secret to claim asset Y, which also allows the bridge to finalize the transaction on the source chain. This provides a cryptographic guarantee that the wrapped assets are fully backed, though most production bridges use more complex, federated, or optimistic models for scalability.
04

Conditional Escrow & Payments

Beyond interoperability, HTLCs can function as a simple form of cryptographic escrow for contingent payments within a single blockchain. Examples include:

  • Betting or Oracles: Payment is locked until a trusted oracle publishes data (like a sports score), where the data itself or a derivative acts as the secret.
  • Multi-Signature Fallback: An HTLC can be set up alongside a multi-signature wallet, providing a timelocked refund path if the other parties become unresponsive.
  • Proof-of-Payment: Revealing the preimage serves as cryptographic proof that a specific payment was completed, useful for receipt generation.
05

Key Mechanism: The Timelock

The timelock is the critical security parameter in an HTLC. It defines the absolute block height or timestamp after which the locked funds can be refunded to the original sender. Key considerations:

  • Relative vs. Absolute: Timelocks can be relative (e.g., 1000 blocks after the HTLC is confirmed) or absolute (e.g., block #1,500,000).
  • Security Implications: A too-short timelock risks the recipient not having enough time to claim. A too-long timelock unnecessarily locks the sender's capital.
  • Decrementing Timelocks: In routed payments (like Lightning), each hop has a successively shorter timelock, creating a pressure chain that ensures timely settlement.
06

The Cryptographic Secret (Preimage)

The preimage is the secret data that unlocks an HTLC. Its hash, the payment hash, is used to create the condition. Mechanics:

  • Generation: The payment hash H is computed as H = Hash(preimage). The payer knows H and the payee will eventually learn the preimage.
  • Revelation: The preimage is only revealed on-chain when the payee claims the funds, making it publicly visible.
  • Security: The cryptographic strength of the hash function (typically SHA-256) ensures it is infeasible to find the preimage given only the hash H. This guarantees that only the intended recipient, who receives the preimage off-chain, can claim the payment.
ecosystem-usage
CORE MECHANISM

HTLCs in the Ecosystem

A Hash Time-Locked Contract (HTLC) is a conditional smart contract that enables trust-minimized, cross-chain or cross-network value transfer. It uses a cryptographic hash and a timelock to secure the transaction.

01

The Core Mechanism

An HTLC enforces a payment condition with two possible outcomes:

  • Payment Execution: The recipient must present the cryptographic preimage (the secret input that generates the known hash) before a deadline to claim the funds.
  • Refund: If the deadline expires, the funds are automatically refunded to the original sender. This creates a secure, atomic conditional transfer without requiring mutual trust between parties.
02

Enabling Atomic Swaps

HTLCs are the foundational protocol for atomic swaps, allowing direct peer-to-peer cryptocurrency trades across different blockchains (e.g., Bitcoin for Ethereum). The swap is atomic—it either completes entirely for both parties or fails entirely, eliminating counterparty risk. Both legs of the trade are secured by interlocking HTLCs with the same hash preimage.

03

Lightning Network & Payment Channels

The Lightning Network for Bitcoin and similar Layer 2 networks use HTLCs as their core routing mechanism. They enable secure, multi-hop payments where no single intermediary needs to be trusted. Each hop in the payment path is secured by an HTLC, ensuring a node only forwards funds if it is guaranteed to receive funds from the next node.

04

Inter-Blockchain Bridges

Many cross-chain bridges utilize HTLC-like constructs for asset transfers. A user locks assets on Chain A, generating a cryptographic proof. A relayer or validator observes this and mints a representative asset on Chain B. While not always a pure HTLC, the principle of conditional locking based on cryptographic proof is directly derived from it.

05

Limitations & Considerations

While powerful, HTLCs have specific constraints:

  • Timelock Sensitivity: The refund and claim deadlines must be carefully calibrated across networks to account for block time variances.
  • Capital Efficiency: Funds are locked for the duration of the contract, which can be inefficient for high-frequency use.
  • Hash Oracle Reliance: The security model depends on the secrecy and eventual reveal of the preimage.
security-considerations
HASH TIME-LOCKED CONTRACT (HTLC)

Security Considerations & Limitations

While HTLCs enable trustless cross-chain and off-chain transactions, their security model introduces specific risks and operational constraints that participants must understand.

01

Hash Preimage Exposure

The core security of an HTLC depends on the secrecy of the preimage (the secret data). If a malicious party discovers the preimage before the intended recipient, they can claim the funds. This risk is mitigated by using a cryptographically secure hash function (like SHA-256) and ensuring the preimage is generated and transmitted securely off-chain.

02

Timelock Expiry Risks

HTLCs enforce strict deadlines via absolute or relative timelocks. This creates two primary risks:

  • Sender Risk: If the recipient fails to claim before expiry, funds are refunded, but the transaction fails.
  • Recipient Risk: If network congestion delays transaction confirmation, a valid claim may expire, causing irreversible loss of funds. Participants must set timelocks with ample buffer for worst-case block times.
03

Liquidity Lockup & Capital Efficiency

Funds are immobilized for the duration of the HTLC's timelock. In routing scenarios (e.g., the Lightning Network), this creates liquidity locks across multiple channels, reducing capital efficiency for node operators. Large or long-duration HTLCs can tie up significant capital, making systems vulnerable to liquidity-based attacks or reducing network throughput.

04

Griefing & Denial-of-Service Attacks

A malicious participant can initiate an HTLC with no intention of completing it, griefing their counterparty by locking their funds until the timelock expires. In payment channel networks, an attacker can propose many small, unclaimable HTLCs to exhaust a node's channel capacity, performing a Denial-of-Service (DoS) attack on its liquidity.

05

Implementation & Smart Contract Risk

On programmable blockchains, HTLCs are implemented as smart contracts. Bugs in this code can lead to loss of funds. Common vulnerabilities include:

  • Incorrect timelock logic.
  • Flawed preimage verification.
  • Reentrancy attacks on the claim/refund functions. Audits and formal verification are critical for custom implementations.
06

Cross-Chain Atomic Swap Limitations

When used for atomic swaps between disparate blockchains, HTLCs face additional constraints:

  • Block Time Mismatch: Timelocks must account for different block confirmation times, increasing complexity.
  • Chain Reorganizations: A reorg on one chain can invalidate a claimed or refunded state, breaking atomicity.
  • Limited Asset Support: Primarily suited for native assets or simple token standards, not complex DeFi positions.
ARCHITECTURAL COMPARISON

HTLCs vs. Other Bridge Mechanisms

A comparison of key technical and operational characteristics between HTLC-based bridges and alternative cross-chain solutions.

Feature / MetricHTLC-Based BridgesLiquidity Network BridgesTrusted (Federated) Bridges

Trust Model

Trust-minimized (cryptographic)

Trust-minimized (cryptographic)

Trusted (multi-party committee)

Native Asset Support

Arbitrary Message Passing

Finality Time

Source chain finality + timeout

Near-instant (< 1 sec)

Source chain finality

Capital Efficiency

Low (locked per transfer)

High (pooled liquidity)

Low (locked per transfer)

Typical Fee Structure

Network fee + service fee

Liquidity provider fee (< 0.1%)

Protocol fee + validator fee

Primary Security Risk

Liquidity attack (griefing)

Liquidity pool insolvency

Validator collusion

Example Protocols

Lightning Network, Composable

Hop, Connext, Stargate

Multichain, Wormhole (Guardian set)

evolution
CROSS-CHAIN MECHANISMS

Evolution and Context

This section explores the foundational protocols that enable secure, trust-minimized value transfer between disparate blockchain networks, a critical evolution from isolated ledgers to an interconnected ecosystem.

A Hash Time-Locked Contract (HTLC) is a type of smart contract that enables conditional payments by requiring the recipient to acknowledge receipt cryptographically within a set timeframe or forfeit the ability to claim the funds. This mechanism creates a cryptographic escrow that enforces the atomicity of a transaction—meaning it either completes entirely for all parties or fails completely, preventing partial settlements. The core components are a hashlock, which requires presenting the preimage (secret data) that generates a known cryptographic hash, and a timelock, which sets a deadline for this proof to be provided.

The primary evolutionary context for HTLCs is the Interledger Protocol and early cross-chain atomic swaps, which sought to facilitate transactions between different blockchains without a trusted third party. Before sophisticated bridges, HTLCs provided the first viable blueprint for trust-minimized interoperability. They are the foundational technology behind Lightning Network payment channels, where they secure multi-hop payments across a network of bidirectional channels, ensuring intermediaries cannot steal funds while routing payments.

In practice, an HTLC-based atomic swap between Bitcoin and Litecoin works as follows: Alice generates a secret and its hash. She creates an HTLC on Bitcoin locked to that hash, payable to Bob if he reveals the secret within 48 hours. Bob, seeing the hash, creates a corresponding HTLC on Litecoin locked to the same hash, payable to Alice. To claim the Litecoin, Alice must reveal the secret, which automatically discloses it to Bob on the public blockchain, allowing him to claim the Bitcoin. If either party fails to act, the timelock expires and funds are refunded.

While revolutionary, classic HTLCs have limitations that spurred further evolution. They are susceptible to hashlock griefing attacks, where a malicious party can lock up a counterparty's capital without intending to complete the swap. Their discreet log contracts (DLCs) and more complex adaptor signature-based constructions address these issues by offering greater privacy and flexibility. Furthermore, modern cross-chain bridges often use HTLC-like principles in their validator or federated security models, but encapsulate the logic off-chain, using the hash-time-lock pattern as a core settlement guarantee.

The legacy of the HTLC is its proof that cryptographic primitives—hashes and time—could orchestrate complex, multi-party financial agreements across trust boundaries. It established the critical concept of atomicity as a non-negotiable requirement for interoperable systems, directly influencing the design of subsequent layer-2 protocols and serving as a fundamental teaching tool for understanding conditional blockchain payments.

HASH TIME-LOCKED CONTRACT

Frequently Asked Questions (FAQ)

A Hash Time-Locked Contract (HTLC) is a fundamental smart contract primitive enabling conditional, trust-minimized payments across blockchain networks. These FAQs address its core mechanics, applications, and security considerations.

A Hash Time-Locked Contract (HTLC) is a specialized smart contract that facilitates a conditional payment, requiring the recipient to provide a cryptographic proof of payment (preimage) within a specified time limit (timelock) to claim the funds. It works through a two-phase cryptographic commitment: the sender locks funds in the contract using a cryptographic hash of a secret. The intended recipient can only claim the funds by submitting the correct secret that generates that hash. If they fail to do so before the timelock expires, the funds are automatically refunded to the sender. This mechanism creates a secure, atomic conditional transfer without requiring trust between parties.

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