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 enables trustless, atomic swaps by locking funds with a cryptographic hash and a timelock.
Chainscore © 2026
definition
BLOCKCHAIN PROTOCOL

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

A cryptographic conditional payment mechanism enabling 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 with a cryptographic proof within a specified time window. It is a fundamental building block for trustless interactions, as it eliminates the need for a trusted intermediary by using two cryptographic primitives: a hashlock and a timelock. The hashlock requires the presentation of a secret (a preimage) that generates a known hash, while the timelock enforces a deadline by which the transaction must be completed or refunded.

The core mechanism involves two parties: a payer and a payee. The payer creates a payment locked to a cryptographic hash. To claim the funds, the payee must produce the secret preimage that generates that hash before the timelock expires. If the payee fails to do so, the funds are automatically refunded to the payer. This creates a secure, atomic conditional transfer. HTLCs are the essential protocol enabling atomic swaps between different cryptocurrencies and are the backbone of payment channels in the Lightning Network for Bitcoin and similar Layer 2 scaling solutions.

In practice, HTLCs enable complex, multi-hop payment routes. In the Lightning Network, a payment can travel through a path of interconnected nodes via a chain of HTLCs. Each intermediate node forwards the payment by creating a new HTLC for the next hop, with a slightly shorter timelock. This creates an incentive-compatible system where each participant is motivated to reveal the secret to claim their funds from the upstream contract before their downstream timelock expires, ensuring the payment propagates reliably to the final recipient.

how-it-works
MECHANISM

How Does an HTLC Work?

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

An HTLC is a smart contract that enforces a payment conditional on the cryptographic proof of a secret. It requires two core components: a hash lock and a time lock. The hash lock is a cryptographic condition: the recipient must produce the preimage (the original data) that hashes to a specified hash digest to claim the funds. The time lock is a deadline; if the preimage is not revealed before this deadline, the funds can be refunded to the original sender. This creates a secure, self-executing escrow system without a trusted third party.

The canonical workflow involves two parties, Alice and Bob, on a payment channel or separate blockchains. First, Alice generates a random secret and computes its hash. She then creates an HTLC on her side, locking funds with this hash and a refund timelock. Bob, upon seeing this hash, creates a corresponding HTLC on his side, locking his funds with the same hash but a shorter timelock. To claim Alice's funds, Bob must reveal the secret, which simultaneously allows Alice to claim Bob's funds. This atomicity ensures the swap either completes entirely or fails, preventing one party from stealing funds.

This mechanism is the foundation for cross-chain atomic swaps and Lightning Network payments. In a cross-chain swap between Bitcoin and Litecoin, HTLCs on each chain are linked by the same hash, forcing the secret's revelation to be public and enabling the counterparty to claim their funds. Within the Lightning Network, HTLCs enable multi-hop payments; each node along the route creates a conditional payment to the next, with sequentially decreasing timelocks, ensuring the secret propagates backward to settle all obligations upon successful payment to the final recipient.

Critical security parameters are the timelock durations and hash function security. Timelocks must be carefully calibrated: the refund timelock on the initiating chain must be significantly longer than the claim timelock on the destination chain to account for block time variance and potential congestion. If timelocks are misconfigured, a participant could claim funds on one side and delay revealing the secret, causing the counterparty's refund window to expire. The cryptographic hash function (typically SHA-256) must be preimage-resistant, ensuring the secret cannot be derived from the published hash digest.

key-features
MECHANICAL BREAKDOWN

Key Features of HTLCs

A Hash Time-Locked Contract (HTLC) is a conditional smart contract that enables trust-minimized, cross-chain or off-chain value transfer by requiring the revelation of a cryptographic secret within a specific timeframe.

01

Cryptographic Hashlock

The hashlock is the primary conditional lock. A payment is locked by requiring the presentation of a preimage (secret data) that hashes to a known, public value (the hash digest). This creates a cryptographic proof-of-payment condition that is verifiable by all parties without revealing the secret until the final step.

02

Time-Based Expiry

The timelock is the secondary, safety condition. It defines a deadline (e.g., a specific block height or timestamp) by which the transaction must be completed. If the secret is not revealed in time, the locked funds are refunded to the original sender. This prevents funds from being locked indefinitely and mitigates denial-of-service attacks.

03

Atomic Swap Mechanism

HTLCs enable atomic swaps, allowing two parties to exchange assets across different blockchains without a trusted intermediary. The process is atomic: either the entire swap completes successfully when the secret is revealed, or all funds are refunded. This is a foundational primitive for decentralized exchanges (DEXs) and cross-chain interoperability.

04

Payment Channels & Lightning Network

HTLCs are the core building block for payment channel networks like the Lightning Network. They allow secure, multi-hop payments where intermediaries can forward payments by creating a chain of linked HTLCs, each with successively shorter timelocks. This enables fast, low-cost, off-chain transactions secured by the underlying blockchain.

05

Trust-Minimized Escrow

An HTLC acts as a programmable, trust-minimized escrow. Funds are held in a neutral contract state, not by a third party. Release is governed solely by the deterministic logic of the hashlock and timelock. This reduces counterparty risk and enables complex conditional payments (e.g., for oracle-based outcomes or cross-chain bridges).

06

Preimage & Hash Verification

The security model relies on the one-way property of cryptographic hash functions (like SHA-256).

  • Preimage: A random secret generated by the payment receiver.
  • Hash Digest: The hash(preimage) published to lock the funds.
  • Verification: The contract logic checks that hash(submitted_secret) == stored_hash_digest. The first party to submit the correct preimage claims the funds.
visual-explainer
MECHANISM EXPLAINER

Visualizing an Atomic Swap with HTLCs

An atomic swap is a peer-to-peer cryptocurrency exchange between two different blockchains, executed trustlessly using Hash Time-Locked Contracts (HTLCs). This process ensures that either the entire trade completes successfully or no funds are transferred, eliminating counterparty risk.

The core mechanism enabling an atomic swap is the Hash Time-Locked Contract (HTLC), a conditional smart contract present on both blockchains involved in the trade. An HTLC requires the recipient of funds to acknowledge a payment by submitting a cryptographic proof—a preimage that generates a known hash—within a specified time limit. If the proof is not provided in time, the funds are refunded to the sender. This creates the "atomic" property: the swap either completes fully for both parties or fails completely, with no intermediate state.

To visualize the swap, imagine two parties, Alice (with Bitcoin) and Bob (with Litecoin). First, Alice generates a secret preimage and calculates its hash. She then creates an HTLC on the Bitcoin blockchain, locking her BTC with the condition: "Pay this to Bob if he provides the preimage for hash H within 48 hours, otherwise refund to me." Bob, seeing this transaction, creates a corresponding HTLC on the Litecoin chain, locking his LTC with the condition: "Pay this to Alice if she provides the preimage for hash H within 24 hours." The shorter time lock on Bob's contract is critical to protect him.

The execution phase begins when Alice, to claim the LTC, is forced to reveal the preimage by submitting it to Bob's Litecoin HTLC. This action publicly broadcasts the secret. Bob can now use that revealed preimage to claim the BTC from Alice's original Bitcoin HTLC before its longer time lock expires. If Alice never claims the LTC, Bob's contract expires and he gets his LTC back. If Bob fails to claim the BTC after learning the secret, Alice's contract eventually expires, refunding her BTC. This elegant sequence ensures trustless cross-chain interoperability without centralized intermediaries.

primary-use-cases
HASH TIME-LOCKED CONTRACT

Primary Use Cases

HTLCs are cryptographic smart contracts that enable conditional payments across blockchains or payment channels, acting as the fundamental building block for trustless interoperability.

01

Atomic Cross-Chain Swaps

Enables the trustless exchange of assets across different blockchains without a centralized intermediary. The swap is atomic: it either completes entirely for both parties or fails, returning funds. This is the core mechanism behind decentralized exchanges (DEXs) for cross-chain trading.

  • Process: Party A locks funds in an HTLC on Chain A with a secret. Party B, upon seeing the proof, locks funds on Chain B. Party A reveals the secret to claim B's funds, which allows B to claim A's original funds.
02

Lightning Network Payments

Forms the backbone of off-chain payment channels in Layer 2 networks like Bitcoin's Lightning Network. HTLCs allow payments to be routed across a network of channels without settling every transaction on the base layer.

  • How it works: Each hop in a payment route is secured by an HTLC. The payment secret is passed along the route, allowing each intermediary to claim funds from the upstream node and pay the downstream node, enabling fast, low-cost micropayments.
03

Cross-Chain Bridges & Interoperability

Serves as a critical component in many cross-chain bridge designs to facilitate asset transfers. An HTLC can lock assets on the source chain, which are then minted or released on the destination chain upon proof of the secret.

  • Example: A user locks 1 BTC in an HTLC. A bridge validator observes this, mints 1 wrapped BTC on Ethereum, and provides the secret to the user to claim it. This creates a cryptographically secured, time-bound commitment between chains.
04

Decentralized Escrow & Conditional Payments

Acts as a programmable escrow service for scenarios requiring a specific condition to be met before payment is released. The time-lock ensures funds are not locked indefinitely.

  • Use Cases:
    • Oracles: Pay for data only if a specific off-chain event (e.g., sports score) is reported.
    • Services: Pay for a freelance job upon delivery of a verifiable proof of work.
    • Betting: Release winnings only upon verification of a game's outcome.
05

The Hash & Time-Lock Mechanism

The security of an HTLC relies on two cryptographic primitives:

  • Hashlock: A payment is locked with the cryptographic hash of a secret. Only the party who knows the pre-image (the original secret) can claim the funds.
  • Timelock: A CLTV (CheckLockTimeVerify) or CSV (CheckSequenceVerify) script that defines an absolute or relative time window. If the secret is not revealed before the timelock expires, the funds can be refunded to the original sender, preventing funds from being locked forever.
06

Limitations & Considerations

While powerful, HTLCs have inherent constraints that shape their application:

  • Liquidity Requirements: For routing payments, each channel must have sufficient locked capital.
  • Timing Attacks: The refund timelock must be carefully set across hops to prevent a malicious party from claiming funds and then delaying the secret.
  • On-Chain Cost: Settling a disputed or expired HTLC requires an on-chain transaction, incurring base-layer fees.
  • Privacy: The hash can be observed on-chain, potentially allowing network analysis of linked transactions.
ecosystem-usage
CROSS-CHAIN & PAYMENT INFRASTRUCTURE

Ecosystem Usage

Hash Time-Locked Contracts (HTLCs) are a foundational cryptographic primitive enabling trust-minimized, conditional value transfers across decentralized networks. Their primary use cases are in cross-chain interoperability and payment channel networks.

01

Atomic Swaps

HTLCs enable atomic swaps, allowing users to exchange cryptocurrencies across different blockchains without a centralized intermediary. The swap is atomic, meaning it either completes entirely or fails and refunds both parties, eliminating counterparty risk.

  • Mechanism: Both parties lock funds into HTLCs on their respective chains using the same hash preimage as the secret.
  • Example: Swapping Bitcoin for Litecoin directly between user wallets.
  • Key Property: The time-lock ensures funds are not locked indefinitely if one party abandons the swap.
02

Lightning Network Payments

HTLCs are the core mechanism for routing payments through the Lightning Network and other payment channel networks. They enable secure, multi-hop transfers where intermediaries can forward payments without taking custody of funds.

  • How it works: Each hop in the payment path is secured by an HTLC. The recipient reveals the preimage to claim the final payment, which then propagates backwards, allowing each intermediary to claim their funds from the previous hop.
  • Function: This creates a trustless network for fast, low-cost micropayments atop a base layer like Bitcoin.
03

Cross-Chain Bridges

Many cross-chain bridges utilize HTLCs for simple asset transfers between chains. They act as a trust-minimized escrow, locking an asset on the source chain and minting or releasing a representation on the destination chain.

  • Process: 1. User locks Asset A in an HTLC on Chain A. 2. The bridge observes the lock. 3. The bridge mints wrapped Asset A on Chain B for the user. 4. To redeem, the user burns the wrapped asset, revealing the preimage to unlock the original on Chain A.
  • Consideration: This model is often used in simpler, non-custodial bridge designs, though many modern bridges use more complex validator sets.
04

Conditional Payments & Escrow

Beyond interoperability, HTLCs can facilitate various conditional payment scenarios where the release of funds depends on proving knowledge of a secret.

  • Use Cases:
    • Cryptographic Escrow: Paying for a digital good (like a decryption key) where payment and delivery are atomic.
    • Oracle-Triggered Settlements: The secret preimage is revealed by an oracle upon a real-world event, triggering settlement of a bet or contract.
    • Dead Man's Switch: Funds are time-locked to a beneficiary unless the original owner provides a periodic proof-of-life (the secret).
05

Interledger Protocol (ILP)

The Interledger Protocol uses a variant of HTLCs, called Interledger Protocol Conditional Transfers, to facilitate payments across any type of ledger (blockchain or traditional). It's designed for universal payment connectivity.

  • Adaptation: ILP's condition is a SHA-256 hash preimage, identical to an HTLC.
  • Network Role: ILP connectors (routers) use these conditional transfers to forward packets of value, creating a global network for money similar to the internet for data.
  • Standardization: This demonstrates HTLCs' role as a standard for conditional value transfer adopted beyond single blockchain ecosystems.
06

Limitations & Considerations

While powerful, HTLCs have inherent limitations that shape their usage.

  • Liquidity Requirements: Routing payments (e.g., in Lightning) requires intermediaries to have sufficient locked liquidity across channels.
  • Timing Attacks: The time-lock values must be carefully set per hop to prevent theft; a malicious party could delay preimage revelation to expire earlier hops.
  • Blockchain Congestion: On-chain HTLC settlement (for closing channels or cross-chain swaps) is vulnerable to base-layer network delays and high fees, which can affect time-lock safety.
  • Privacy: Basic HTLCs can leak payment correlation data via the publicly visible hash.
security-considerations
HASH TIME-LOCKED CONTRACT (HTLC)

Security Considerations & Risks

While HTLCs are a foundational security primitive for atomic swaps and payment channels, their implementation introduces specific risks that must be managed.

01

Timelock Expiry & Liquidity Risk

A core risk in HTLCs is the timelock expiry. If the recipient fails to provide the cryptographic proof before the timelock expires, the funds are refunded to the sender. This creates liquidity risk where capital is locked and unusable for the duration, and exposes the sender to price volatility. In cross-chain swaps, mismatched timelocks can leave one party's funds locked longer than the other's.

02

Hash Preimage Secrecy & Front-Running

The security of an HTLC depends on the hash preimage (the secret) being unknown until the recipient is ready to claim. Risks include:

  • Preimage Leakage: If the secret is discovered by a third party before the swap, they can intercept the payment.
  • Mempool Front-Running: On transparent blockchains, a network participant can see the claim transaction containing the preimage in the mempool, copy it, and attempt to claim the funds on the other chain first in a race condition.
03

Griefing Attacks

A malicious participant can launch a griefing attack by initiating an HTLC with no intention of completing the swap. They lock funds, forcing the counterparty to also lock funds, only to let the timelock expire. This denies the honest party the use of their capital (a Denial-of-Service on liquidity) and incurs opportunity cost, all at minimal expense to the attacker beyond transaction fees.

04

Implementation Flaws & Oracle Reliance

HTLC security is only as strong as its implementation. Common flaws include:

  • Incorrect Timelock Logic: Bugs in refund or claim conditions.
  • Weak Hash Functions: Use of a cryptographically broken hash function could allow preimage forgery.
  • Oracle Dependency: Some HTLC variants for conditional payments rely on external oracles. This introduces oracle risk, where manipulation or failure of the oracle data source determines the contract outcome.
05

Cross-Chain Protocol Risks

In cross-chain atomic swaps, HTLCs compound the risks of each individual chain. Key concerns are chain reorganizations (reorgs) and congestion. A reorg on one chain after a preimage is revealed could invalidate a transaction, breaking atomicity. Network congestion can delay transaction inclusion, causing a party to miss their timelock deadline through no fault of their own.

06

Mitigation Strategies

Standard practices to mitigate HTLC risks include:

  • Careful Timelock Scheduling: Setting appropriate, staggered timelocks that account for block times and confirmation safety.
  • Hash Commitment Schemes: Using a commit-reveal pattern to publish the hash without revealing the preimage link prematurely.
  • Fee Bumping: Using Replace-By-Fee (RBF) or similar mechanisms to ensure critical claim/refund transactions are mined.
  • Audited Libraries: Using well-tested, audited code from established frameworks like the Lightning Network's BOLT specifications.
COMPARISON MATRIX

HTLCs vs. Other Cross-Chain Methods

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

Feature / MetricHTLCsAtomic SwapsBridges (Lock & Mint)Relay Networks

Primary Mechanism

Hash & Time-Lock

Script-based HTLC

Custodial or Trusted

Light Client Verification

Trust Model

Trustless

Trustless

Trusted (Validator Set)

Minimally Trusted

Native Asset Transfer

Wrapped Asset Support

Typical Latency

10 min - 1 hr

10 min - 1 hr

< 5 min

< 2 min

Protocol Complexity

Low

Low

High

High

Capital Efficiency

Low (Locked)

Low (Locked)

High

High

Security Assumption

Cryptographic

Cryptographic

Economic/Social

Cryptographic

HTLC

Frequently Asked Questions (FAQ)

Essential questions and answers about Hash Time-Locked Contracts (HTLCs), the cryptographic primitive enabling trust-minimized, conditional payments across blockchains and payment channels.

A Hash Time-Locked Contract (HTLC) is a smart contract that facilitates a conditional payment, requiring the recipient to provide a cryptographic proof of payment (a preimage) within a set timeframe or forfeit the ability to claim the funds. It works by locking funds with two conditions: a hashlock and a timelock. The sender creates a cryptographic hash of a secret (the preimage) and embeds it into the contract. The recipient can only claim the funds by submitting the correct preimage that generates the known hash. If they fail to do so before the timelock expires, the funds are refunded to the sender. This creates a secure, atomic conditional transfer.

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