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 specialized smart contract that enables trustless, conditional payments by requiring the recipient to provide a cryptographic proof of payment (a secret) within a specified time window.
Chainscore © 2026
definition
BLOCKCHAIN SMART CONTRACT

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, requiring the recipient to acknowledge receipt by providing a cryptographic proof within a set time limit.

A Hash Time-Locked Contract (HTLC) is a type of smart contract that facilitates trustless, conditional value transfer. It enforces two core conditions: the recipient must produce a cryptographic proof, known as a preimage, of a specified hash to claim the funds, and this must be done before a predefined timelock expires. If the recipient fails to provide the proof in time, the funds are automatically refunded to the sender. This mechanism creates a secure, atomic "proof-of-payment" system without requiring a trusted intermediary.

The primary technical components are the hashlock and the timelock. The hashlock is created by the sender, who picks a secret (the preimage) and publishes its cryptographic hash. The contract is funded and locked with this hash. To unlock it, the recipient must present the exact preimage that generates the published hash. Simultaneously, the timelock, often implemented via OP_CHECKLOCKTIMEVERIFY in Bitcoin or block numbers in Ethereum, sets a strict deadline. This dual-condition structure is fundamental to cross-chain swaps and payment channel networks like the Lightning Network.

HTLCs are the foundational building block for atomic swaps, allowing for the peer-to-peer exchange of cryptocurrencies across different blockchains without centralized exchanges. In a typical atomic swap, two parties create mirrored HTLCs on two separate chains, linked by the same hash. When one party reveals the preimage to claim funds from one contract, they inadvertently disclose it, allowing the counterparty to claim funds from the second contract. This ensures the swap either completes entirely for both parties or not at all, eliminating counterparty risk.

Beyond atomic swaps, HTLCs are critical for off-chain scaling solutions. In the Lightning Network, payments are routed through a series of connected payment channels, each hop secured by an HTLC. As the payment propagates, each intermediate node sets up an HTLC with the next, using the same hash but a successively shorter timelock. This creates an incentive-aligned path where each participant is motivated to reveal the preimage quickly to claim their inbound funds before their outbound timelock expires, enabling fast and cheap micropayments.

While highly secure, HTLCs introduce complexities such as liquidity locking and timelock management. Funds are immobilized for the duration of the contract, and careful coordination of decreasing timelocks along a payment route is required to prevent funds from being stuck. Furthermore, the public revelation of the preimage on-chain can have privacy implications. Despite these considerations, the HTLC remains a seminal innovation in cryptographic commerce, enabling a new class of decentralized, interoperable financial applications built on conditional logic and self-enforcing contracts.

how-it-works
MECHANISM EXPLAINED

How a Hash Time-Locked Contract (HTLC) Works

A Hash Time-Locked Contract (HTLC) is a specialized smart contract that enables conditional payments across blockchain networks, primarily serving as the foundational mechanism for trustless cross-chain swaps and payment channels.

A Hash Time-Locked Contract (HTLC) is a type of smart contract that facilitates a conditional payment, which is only finalized upon the cryptographic proof of a secret or after a predefined time period expires. It is a core cryptographic primitive enabling atomic swaps and the secure operation of Lightning Network payment channels. The contract locks funds with two possible unlocking conditions: the presentation of the correct cryptographic preimage (a secret number) that hashes to a publicly known value, or the expiration of a timelock, which allows the original sender to reclaim their funds.

The workflow involves two cryptographic components: a hashlock and a timelock. The initiating party creates a cryptographic hash (e.g., SHA-256) of a secret preimage and embeds this hash into the HTLC. The contract is funded and deployed, stating that the recipient can claim the funds by revealing the preimage that produces the specified hash. Concurrently, a timelock clause is set, creating a refund pathway. If the recipient does not produce the secret before the timelock expires, the funds are automatically returned to the sender, eliminating the risk of funds being permanently locked.

This mechanism enables trustless cross-chain atomic swaps. For example, Alice can lock Bitcoin in an HTLC on the Bitcoin blockchain, while Bob locks Ethereum in a corresponding HTLC on the Ethereum blockchain, both contracts using the same hash. When Alice reveals her secret to claim Bob's Ethereum, she inadvertently publishes the preimage on-chain, allowing Bob to use it to claim Alice's Bitcoin. The timelocks are carefully sequenced to ensure that if one party fails to act, the other can always execute a refund, making the swap atomic—it either completes entirely for both parties or not at all.

Beyond atomic swaps, HTLCs are the building block for hash time-locked contracts within Layer 2 scaling solutions like the Lightning Network. In a payment channel, HTLCs construct the multi-hop payment routes. Each intermediary node in the route sets up an HTLC with the next node, all secured by the same hashlock. As the preimage travels back along the route upon payment completion, each contract is sequentially settled, enabling fast, low-cost transactions without broadcasting each one to the main blockchain.

The security and reliability of an HTLC depend on the precise configuration of its timelocks, often implemented via OP_CHECKLOCKTIMEVERIFY (CLTV) or OP_CHECKSEQUENCEVERIFY (CSV) in Bitcoin Script. Network latency and block time differences between chains must be accounted for when setting these deadlines in cross-chain scenarios. Properly implemented, HTLCs provide a powerful tool for interoperability and off-chain scalability, though they introduce complexity in timeout management and require careful protocol design to prevent griefing attacks.

key-features
MECHANICAL BREAKDOWN

Key Features of HTLCs

Hash Time-Locked Contracts (HTLCs) are smart contract primitives that enable conditional, trust-minimized value transfers by combining cryptographic proof with a time-based fail-safe.

01

Cryptographic Condition (Hashlock)

The hashlock is the cryptographic puzzle that secures the payment. The sender creates a cryptographic hash of a secret preimage. To claim the funds, the recipient must reveal this preimage, which produces a matching hash, proving they have the secret. This mechanism enables atomic swaps and cross-chain bridges without a trusted intermediary.

  • Example: Alice sends BTC to a contract with hashlock H = SHA256(secret). Bob can only claim it by providing the secret that hashes to H.
02

Time-Based Expiry (Timelock)

The timelock is the fail-safe mechanism that prevents funds from being locked indefinitely. It defines a deadline (e.g., 48 hours) by which the cryptographic condition must be met. If the recipient fails to provide the secret preimage before the timelock expires, the sender can refund the locked funds. This creates a strict, self-enforcing timeline for the transaction.

  • Types: Can be absolute (block height) or relative (blocks after an event).
03

Trust-Minimized Atomic Swaps

HTLCs enable atomic cross-chain swaps, allowing two parties to exchange different cryptocurrencies without a centralized exchange. The process is atomic: either both parties complete the swap, or neither does. The hashlock ensures the secret is revealed upon the first claim, allowing the counterparty to immediately claim their side of the trade.

  • Process: 1) Alice locks BTC with hashlock H. 2) Bob locks ETH with the same hashlock H. 3) Alice reveals secret to claim ETH, exposing it to Bob. 4) Bob uses secret to claim BTC.
04

Core to Payment Channels & Lightning Network

HTLCs are the fundamental building block for payment channels and Layer 2 networks like the Lightning Network. They enable secure, off-chain routing of payments through a network of intermediaries. Each hop in the payment path is secured by its own HTLC with a sequentially shorter timelock, ensuring the payment either completes fully or can be rolled back at each step.

  • Function: Allows multi-hop payments where no single node needs to be fully trusted.
05

Refund Pathway & Security

The refund pathway is a guaranteed, built-in security feature. If the intended recipient is unresponsive or the transaction fails, the original sender can always reclaim their funds after the timelock expires. This eliminates the risk of permanent loss due to a counterparty's failure to act. The contract's logic autonomously enforces this, requiring no third-party intervention.

06

Limitations & Considerations

While powerful, HTLCs have specific constraints that developers must account for:

  • Liquidity Lockup: Funds are immobilized for the duration of the timelock.
  • Timelock Precision: Requires careful coordination of block times across different chains for cross-chain use.
  • Hashlock Security: Relies on the cryptographic strength of the hash function (e.g., SHA-256) and the secrecy of the preimage.
  • No Partial Execution: The contract state is binary—either fully completed or fully refunded.
visual-explainer
PROTOCOL MECHANICS

HTLC Atomic Swap Flow

The step-by-step process by which two parties exchange cryptocurrencies across different blockchains without a trusted intermediary, secured by cryptographic proofs and time constraints.

An HTLC atomic swap begins when the initiating party, Alice, creates a Hash Time-Locked Contract (HTLC) on her blockchain (e.g., Bitcoin). She deposits her funds into this smart contract, which is locked with a cryptographic puzzle: a secret preimage that produces a specific hash. Alice then sends this hash to the counterparty, Bob. The contract stipulates that Bob can claim the funds only if he presents the correct preimage within a defined time window. If he fails, the funds are automatically refunded to Alice. This setup creates the conditional offer for the swap.

To accept the swap, Bob must create a corresponding HTLC on his blockchain (e.g., Litecoin). He deposits his side of the trade into a contract locked with the identical hash received from Alice. Crucially, Bob's contract also has a shorter time lock. This asymmetry is vital: Bob must claim Alice's funds first, which forces him to reveal the secret preimage on the public ledger. Once revealed, Alice can use that same preimage to claim the funds from Bob's contract on the second chain. The process is atomic: either both transactions complete successfully, or neither does, preventing one party from stealing funds.

The final phase involves the secret revelation and fund claiming. Bob, incentivized by his shorter time lock, submits the correct preimage to Alice's HTLC to claim the Bitcoin. This action publishes the secret on the Bitcoin blockchain. Alice now monitors this transaction, extracts the revealed preimage, and uses it to claim the Litecoin from Bob's contract before his longer time lock expires. If any party becomes uncooperative, the time locks ensure automatic refunds. This elegant flow enables trustless cross-chain exchange, forming the basis for decentralized exchanges and payment channel networks like the Lightning Network.

examples
HASH TIME-LOCKED CONTRACT (HTLC)

Primary Use Cases & Examples

HTLCs are a foundational cryptographic primitive enabling conditional, trust-minimized value transfers. Their primary applications are in cross-chain and off-chain protocols.

05

Limitations & Considerations

While powerful, HTLCs have specific constraints:

  • Timing Attacks: The refund time lock must be carefully calibrated across a payment route to prevent theft.
  • Liquidity Lockup: Funds are immobilized for the duration of the time lock.
  • On-Chain Cost: Disputes or settlements requiring on-chain execution incur transaction fees.
  • Privacy: The hash is public, potentially allowing network observers to link transactions. Newer constructions like Point Time-Locked Contracts (PTLCs) using Schnorr signatures aim to improve efficiency and privacy.
ecosystem-usage
HASH TIME-LOCKED CONTRACT (HTLC)

Ecosystem Usage

Hash Time-Locked Contracts (HTLCs) are a fundamental cryptographic primitive enabling trustless, conditional payments across blockchain networks. They are the core mechanism powering cross-chain swaps and payment channels.

04

Conditional Payments & Escrow

HTLCs can facilitate cryptographic escrow for real-world agreements. Funds are locked until a specific condition, proven by revealing a secret, is met.

  • Use Cases:
    • Betting Oracles: Two parties lock funds, and the outcome (provided by an oracle) determines the secret.
    • Data Feeds: Payment for a private data set, where the decryption key is the secret.
    • Adversarial Collaboration: Ensuring all parties in a multi-step process fulfill their obligations.
06

Limitations & Considerations

While powerful, HTLCs have specific constraints and risks that dictate their practical use.

  • Liquidity Requirements: For routing, each channel must have sufficient locked liquidity.
  • Timing Attacks: Careful time-lock scheduling is critical to prevent theft via griefing or race conditions.
  • On-Chain Cost: Settling or disputing an HTLC on-chain requires a transaction, which can be expensive.
  • Privacy: The hash can be observed on-chain, potentially linking transactions in a payment route.
security-considerations
HASH TIME-LOCKED CONTRACT (HTLC)

Security Considerations & Limitations

While HTLCs are a foundational tool for enabling trustless, conditional payments, they introduce specific security parameters and operational constraints that must be carefully managed.

01

Timelock Expiry Risk

The timelock is the contract's primary security mechanism, but its expiry creates a critical failure mode. If the payment recipient fails to provide the preimage before the timelock expires, the funds are refunded to the sender. This introduces liquidity lockup and potential price volatility risk for the duration of the lock. Mismatched timelocks across different blockchains in a cross-chain swap can lead to one party being able to claim funds while the other cannot.

02

Preimage Secrecy & Hash Collisions

The security of an HTLC hinges on the cryptographic strength of the hash function (e.g., SHA-256) and the secrecy of the preimage. If the preimage is discovered by any party before the conditional payment is initiated, the security fails. While theoretically secure, the use of weak hash functions or flawed random number generation for the preimage could make it vulnerable to brute-force attacks. The protocol assumes hash collisions are computationally infeasible.

03

Blockchain Congestion & Fee Market Risk

HTLC execution is subject to underlying blockchain performance. To claim funds before the timelock expires, a transaction revealing the preimage must be confirmed on-chain. During periods of high network congestion, rising transaction fees or slow block times could prevent timely settlement, causing the claim transaction to fail and the funds to be refunded. This makes HTLCs unreliable in volatile fee environments without careful fee estimation.

04

Trust Assumptions in Routing

In multi-hop payment networks like the Lightning Network, HTLCs are chained together. This introduces an intermediary risk. While each HTLC is individually trustless, the entire path relies on each intermediary node to forward the preimage honestly and in a timely manner. A malicious or offline intermediary can cause the payment to fail, though it cannot steal funds. This requires robust node reputation systems and pathfinding algorithms.

05

Limited Expressiveness & State Complexity

HTLCs are designed for a single, binary condition: "reveal the preimage." They cannot encode complex logic (e.g., multi-signature approvals, oracle data). Managing many concurrent HTLCs, especially with varying timelocks, increases state management complexity for nodes and wallets. This can lead to resource exhaustion attacks if an attacker initiates many HTLCs that a node must track until expiry.

06

Cross-Chain Atomic Swap Limitations

When used for atomic swaps between different blockchains, HTLCs require careful coordination of timelocks to account for differing block times and finality rules. The slower chain dictates the minimum safe timelock for the faster chain. This results in long lockup periods (often hours or days) to ensure security, reducing capital efficiency and increasing exposure to market risk during the swap period.

COMPARISON

HTLC vs. Other Cross-Chain Mechanisms

A technical comparison of Hash Time-Locked Contracts against other major interoperability solutions.

Feature / MetricHTLCAtomic Swaps (via HTLC)Lock & Mint BridgesGeneralized Message Passing

Core Mechanism

Hash-locked conditional payment

Peer-to-peer HTLC execution

Centralized or MPC-controlled custodian

Arbitrary state verification and relay

Trust Model

Trustless (cryptographic)

Trustless (cryptographic)

Trusted custodian or committee

Trusted relayers or optimistic/zk-verifiers

Asset Movement

Native asset swap

Native asset swap

Wrapped synthetic asset

Native or wrapped asset transfer

Finality Speed

Block confirmations + timeout

Block confirmations + timeout

Near-instant (custodian decision)

Varies by proof system (minutes to hours)

Capital Efficiency

Low (funds locked for duration)

Low (funds locked for duration)

High (liquidity pool based)

Medium (bonded relayers)

Programmability

Single conditional payment

Single conditional swap

Limited to mint/burn

High (arbitrary cross-chain logic)

Security Risks

Timeout griefing, price volatility

Timeout griefing, counterparty discovery

Custodial risk, bridge exploit

Verifier failure, message delay attack

Example Protocols

Lightning Network payments

Cross-chain DEX trades

Wrapped BTC (WBTC), Multichain

LayerZero, Axelar, Chainlink CCIP

DEBUNKED

Common Misconceptions About HTLCs

Hash Time-Locked Contracts (HTLCs) are a fundamental building block for atomic swaps and cross-chain interoperability, but several persistent myths obscure their true function and limitations.

No, HTLCs are a protocol-agnostic smart contract pattern implemented across numerous blockchains. While the concept was popularized by Bitcoin's Lightning Network, the core logic of a hash lock and a time lock is now a standard feature in many smart contract platforms like Ethereum, Solana, and Cosmos. They are the foundational mechanism for atomic swaps between different cryptocurrencies and for conditional payments in layer-2 networks. The specific implementation details—such as the scripting language (Bitcoin Script vs. Solidity) or the opcodes used—vary, but the cryptographic and temporal conditional logic remains universally applicable.

HASH TIME-LOCKED CONTRACT

Frequently Asked Questions (FAQ)

A Hash Time-Locked Contract (HTLC) is a specialized smart contract that enables conditional, trust-minimized payments across blockchains or payment channels. These questions address its core mechanics, use cases, and security considerations.

A Hash Time-Locked Contract (HTLC) is a type of smart contract that enforces a conditional payment using a cryptographic hash and a time constraint. It works by requiring the recipient to provide a cryptographic proof, called a preimage, that matches a publicly known hash value within a specified time window; if they fail, the funds are returned to the sender. This mechanism creates a conditional escrow, enabling atomic swaps and secure cross-chain transactions without a trusted third party. HTLCs are fundamental to Layer 2 payment channels like the Lightning Network and interoperability protocols.

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