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

Hashed Timelock Contract (HTLC)

A Hashed Timelock Contract (HTLC) is a specialized smart contract that facilitates trustless, conditional value transfers by requiring the recipient to reveal a cryptographic secret within a specified time limit.
Chainscore © 2026
definition
GLOSSARY

What is a Hashed Timelock Contract (HTLC)?

A technical deep dive into the conditional payment mechanism enabling trust-minimized cross-chain and off-chain transactions.

A Hashed Timelock Contract (HTLC) is a specialized smart contract that facilitates conditional payments by requiring the recipient to provide a cryptographic proof of payment (hash preimage) within a specified time window, or else the funds are returned to the sender. This creates a cryptographic condition enforced by the blockchain, enabling trustless interactions between parties who do not need to rely on a central intermediary. HTLCs are the foundational building block for atomic swaps and payment channels in networks like the Lightning Network.

The core mechanism relies on two timelocks: a hashlock and a timelock. The hashlock is a condition where payment is only released upon the presentation of the secret data (the preimage) that generates a known cryptographic hash. The timelock, typically implemented via OP_CHECKLOCKTIMEVERIFY in Bitcoin or block.timestamp in Ethereum, sets a deadline. If the preimage is not revealed before this deadline expires, the contract logic allows the locked funds to be refunded to the original sender, preventing funds from being locked indefinitely.

HTLCs are most famously applied in cross-chain atomic swaps, allowing for the direct peer-to-peer exchange of cryptocurrencies across different blockchains without centralized exchanges. In this process, Party A locks funds in an HTLC on Chain A. To claim them, Party B must use the preimage, an action which simultaneously reveals it, allowing Party A to claim the funds locked in a corresponding HTLC on Chain B. This ensures the swap either completes entirely for both parties or not at all, guaranteeing atomicity.

Beyond swaps, HTLCs are the critical component for routing payments in Layer 2 networks like the Lightning Network. Here, a payment is routed through a path of connected payment channels. Each hop in the path is secured by an HTLC, where each intermediary node can only claim funds from the upstream node if they can produce the preimage, which they learn by paying the next node downstream. This creates a secure, trustless multi-hop payment system where no single participant can steal funds.

While powerful, HTLCs have limitations. They introduce liquidity locking along the payment route for the duration of the timelock and are susceptible to hash collision attacks if a weak hash function is used (though SHA-256 is considered secure). Furthermore, the requirement for a global timelock can lead to capital inefficiency in routing networks. Newer constructions, such as Point Time-Locked Contracts (PTLCs), aim to improve upon HTLCs by using cryptographic adaptor signatures, which offer greater privacy and efficiency.

key-features
MECHANICAL BUILDING BLOCKS

Key Features of HTLCs

A Hashed Timelock Contract (HTLC) is a conditional payment mechanism that enforces atomicity in cross-chain or off-chain transactions. Its core features are defined by cryptographic proofs and time-based refunds.

01

Cryptographic Hashlock

The hashlock is a cryptographic condition that locks funds. The payer generates a secret preimage and publishes its cryptographic hash (e.g., SHA-256). Funds can only be claimed by the recipient who reveals the correct preimage, proving knowledge of the secret. This creates a secure, verifiable link between two transactions.

  • Example: Hash 0x3f7... is set. To claim, the recipient must provide the secret s where hash(s) = 0x3f7....
02

Time-Based Refund (Timelock)

The timelock is a safety mechanism that allows the original sender to reclaim their funds after a predefined block height or timestamp passes. This prevents funds from being locked indefinitely if the recipient fails to claim them by providing the preimage. Timelocks are typically implemented using OP_CHECKLOCKTIMEVERIFY (CLTV) in Bitcoin or block.timestamp in Ethereum.

  • Function: Ensures transaction finality and return of capital.
03

Atomicity Guarantee

HTLCs enable atomic swaps—transactions that either complete entirely or fail entirely, with no intermediate state. This is achieved by linking two separate HTLCs on different chains or payment channels with the same hashlock. The revelation of the preimage to claim funds on one side automatically provides the secret needed to claim funds on the other, making the swap trustless.

  • Result: Eliminates counterparty risk in decentralized exchanges.
04

Core Use Case: Lightning Network

HTLCs are the fundamental building block of the Lightning Network and other payment channel networks. They enable secure, multi-hop payments where intermediaries can forward payments without having to trust the sender or receiver. Each hop in the route is secured by a unique HTLC, with successively shorter timelocks to protect intermediaries from capital lockup.

  • Mechanism: Enables fast, low-cost microtransactions off-chain.
05

Interoperability & Cross-Chain Swaps

HTLCs facilitate trustless asset exchange between different blockchains (e.g., swapping BTC for ETH). Two HTLCs are created, one on each chain, with identical hashlocks but chain-specific timelocks. The first party to reveal the preimage claims the asset on the target chain, simultaneously revealing the secret for the counterparty to claim the original asset. This does not require a centralized custodian or bridge.

  • Prerequisite: Both chains must support compatible hash functions and timelock opcodes.
06

Limitations & Considerations

While powerful, HTLCs have specific constraints:

  • Liquidity Requirements: Both sides must lock funds for the duration, requiring capital efficiency.
  • Timelock Coordination: Timelocks must be carefully calibrated to account for block time variances and prevent race conditions.
  • Privacy Leakage: The hash is public, potentially allowing network observers to link transactions.
  • Smart Contract Dependency: On chains like Ethereum, HTLC logic is implemented within a smart contract, subject to gas costs and potential bugs.
how-it-works
MECHANISM EXPLAINER

How a Hashed Timelock Contract Works

A technical breakdown of the cryptographic and time-based mechanisms that enable secure, trust-minimized conditional payments across blockchain networks.

A Hashed Timelock Contract (HTLC) is a specialized smart contract that facilitates conditional payments by requiring the recipient to provide a cryptographic proof of payment (preimage) within a specified time window, or else the funds are returned to the sender. This mechanism creates a secure, self-enforcing agreement that does not rely on a trusted intermediary. The contract's logic is governed by two primary constraints: a hashlock, which locks funds until the correct preimage is revealed, and a timelock, which defines an expiration deadline for the transaction.

The process begins when the sender creates a cryptographic hash (the hashlock) from a secret preimage and embeds it into the HTLC. Funds are locked in this contract with the condition that to claim them, the recipient must present the secret that produces the agreed-upon hash. This creates a cryptographic proof of payment that is verifiable on-chain. Concurrently, the contract includes a CLTV (CheckLockTimeVerify) or CSV (CheckSequenceVerify) timelock, setting a block height or time after which the sender can reclaim the funds if the secret is not revealed.

HTLCs are the foundational building block for cross-chain atomic swaps and payment channel networks like the Lightning Network. In an atomic swap between two blockchains, Party A locks funds in an HTLC on Chain A. To claim them, Party B must reveal the secret, which then allows Party A to claim funds from an HTLC on Chain B using the same secret, ensuring the swap either completes entirely for both parties or not at all. This eliminates counterparty risk without a central custodian.

Within a Lightning Network payment channel, HTLCs enable multi-hop routing. A payment is broken into a chain of HTLCs across multiple nodes. Each intermediary node forwards the payment by creating a new HTLC for the next hop, with a slightly earlier timelock. This creates a hashed timelock contract chain where the revelation of the final recipient's preimage unlocks funds backwards along the route, ensuring intermediaries get paid only if the payment is successful.

The security of an HTLC hinges on the one-way property of the hash function and the deterministic finality of the timelock. The hash function (commonly SHA-256) makes it computationally infeasible to derive the preimage from the hashlock. The timelock, enforced by blockchain consensus, guarantees the sender a guaranteed refund path. However, risks include timelock griefing, where a participant delays revelation to pressure others, and the necessity for precise time synchronization across network hops to avoid fund loss.

primary-use-cases
HASHLOCKED APPLICATIONS

Primary Use Cases

A Hashed Timelock Contract (HTLC) is a conditional smart contract that enforces atomic swaps and trustless payments by requiring the recipient to provide a cryptographic proof of payment (preimage) within a set time limit.

01

Atomic Cross-Chain Swaps

Enables trustless exchange of assets across different blockchains without a centralized intermediary. The process is atomic: either both parties complete the swap, or the funds are returned.

  • Mechanism: Party A locks funds in an HTLC on Chain A using a hash. Party B, seeing the proof, locks funds in a corresponding HTLC on Chain B. Revealing the preimage to claim funds on Chain B allows Party A to claim funds on Chain A.
  • Example: Swapping Bitcoin for Ethereum directly between users.
02

Lightning Network Payments

Forms the core cryptographic primitive of Layer 2 payment channels like the Lightning Network. HTLCs enable secure, multi-hop routing where intermediaries can forward payments without custodial risk.

  • How it works: Each hop in the payment path is secured by an HTLC. The final recipient reveals the preimage to claim funds, which then propagates backwards, allowing each intermediary to claim payment from the previous hop.
  • Key Benefit: Enables instant, high-throughput micropayments off-chain.
03

Cross-Chain Bridges & Interoperability

Serves as a fundamental building block for cross-chain communication protocols. HTLCs facilitate the locked-mint or burn-mint models used by many bridges to move assets between chains.

  • Lock-and-Mint: User locks asset A in an HTLC on the source chain. A relayer provides proof, triggering the minting of a wrapped asset A on the destination chain.
  • Security: The timelock ensures the bridge operation has a finite window, after which locked funds can be reclaimed, mitigating some bridge risk.
04

Trustless Escrow & Conditional Payments

Acts as a programmable escrow for scenarios requiring a cryptographic proof of completion. Payment is contingent on the recipient providing specific data (the preimage).

  • Use Cases:
    • Oracle-Triggered Payments: Pay for a service once an oracle attests to its completion by publishing the preimage.
    • Data Publication Bounties: Reward the first entity to publish a specific piece of verifiable data on-chain.
  • Security Property: The payer is always guaranteed a refund via the timelock expiry if the condition is not met.
05

Privacy-Enhancing Transactions

Can be used to obscure payment paths and relationships in certain protocol designs. The hash commitment reveals no details about the recipient until the preimage is disclosed.

  • Principle: Observers see a hash-locked output but cannot determine the intended recipient or payment condition until settlement.
  • Application: In complex, multi-party coinjoin or payment channel constructions, HTLCs can help break the direct on-chain link between payer and payee.
visual-explainer
MECHANISM EXPLAINER

Visualizing an HTLC Atomic Swap

A step-by-step walkthrough of how a Hashed Timelock Contract (HTLC) enables a trustless cryptocurrency exchange between two parties on potentially different blockchains.

An HTLC atomic swap is a multi-step cryptographic protocol that allows two parties to exchange different cryptocurrencies without a trusted intermediary, ensuring the trade either completes entirely for both sides or fails entirely for both. The process is secured by two core components: a cryptographic hash lock and a timelock. The hash lock ensures only the party who knows the secret preimage can claim the funds, while the timelock guarantees funds can be refunded to their original owner if the swap is not completed within a specified period. This creates the atomicity—the all-or-nothing property—that defines the swap.

The swap begins when Party A, wanting to trade Bitcoin for Party B's Litecoin, creates an HTLC on the Bitcoin blockchain. This contract locks A's Bitcoin with two conditions: it can be claimed by anyone who presents the correct cryptographic secret (the preimage of a published hash), or it can be refunded back to A after a set time window expires. Party A then sends the hash of the secret to Party B. Crucially, at this point, Party B cannot claim the Bitcoin because they do not yet know the secret preimage, only its hash.

Upon seeing the hash on the Bitcoin chain, Party B creates a corresponding HTLC on the Litecoin blockchain, locking their Litecoin with the same hash and a shorter timelock. This timelock must be shorter than the one on A's initial contract to prevent a race condition. When Party A sees B's Litecoin HTLC, they can claim the Litecoin by revealing the secret preimage on the Litecoin chain. This act of claiming automatically publishes the secret to the public blockchain.

Finally, with the secret now publicly visible on the Litecoin ledger, Party B can use it to claim the Bitcoin from the original HTLC on the Bitcoin chain. The swap is complete. If at any point a party fails to act—for instance, if B never creates the Litecoin HTLC—the timelocks will eventually expire, and all locked funds will be refunded to their original owners. This elegant sequence ensures trustlessness; neither party can steal funds or be left in a state where they have paid but not received.

Visualizing this flow highlights key requirements for cross-chain atomic swaps: both blockchains must support compatible hash functions (like SHA-256) and a scripting language capable of enforcing HTLC logic (e.g., Bitcoin Script). While powerful for direct peer-to-peer trading, HTLCs have limitations, including timelock coordination complexity and capital being locked during the process. They remain a foundational cryptographic primitive enabling more complex decentralized finance (DeFi) constructs like the Lightning Network for off-chain payments.

ecosystem-usage
HASHLOCKED PAYMENTS

Ecosystem Usage & Protocols

Hashed Timelock Contracts (HTLCs) are cryptographic smart contracts that enable trust-minimized, conditional payments across blockchain networks, forming the backbone of interoperability protocols.

01

Core Mechanism

An HTLC enforces a payment with two cryptographic conditions: a hashlock and a timelock. The recipient must present the cryptographic preimage (secret) that hashes to a known value to claim funds before a specified block height or timestamp, after which the sender can refund.

02

Atomic Swaps

HTLCs enable peer-to-peer cross-chain trading without centralized intermediaries. Two parties can atomically swap assets on different blockchains (e.g., BTC for ETH) by linking two HTLCs with the same hashlock. The swap either completes entirely for both parties or fails entirely, eliminating counterparty risk.

03

Payment Channels & Lightning Network

HTLCs are the fundamental building block for off-chain payment channels. In the Lightning Network, HTLCs allow funds to be routed across a network of channels. Each hop in the payment path uses an HTLC, enabling fast, low-cost micropayments that settle on the base blockchain only when channels close.

04

Cross-Chain Bridges

Many interoperability bridges use HTLCs for asset transfers. To move an asset from Chain A to Chain B, the user locks funds in an HTLC on Chain A. A relayer or validator provides the preimage on Chain B to mint a wrapped asset, ensuring the locked funds can only be released upon proof of the minting event.

05

Key Properties & Security

  • Atomicity: Transactions are all-or-nothing.
  • Timelock Security: Protects the sender via a refund clause.
  • Hashlock Secrecy: The preimage acts as a one-time secret for claim authorization.
  • Trust Minimization: Relies on cryptographic proofs, not third-party honesty.
06

Limitations & Considerations

  • Liquidity Requirements: Requires locked capital in channels or bridges.
  • Timelock Attacks: Malicious participants may attempt to delay or front-run transactions.
  • Blockchain Compatibility: Both chains must support compatible hash functions and timelock opcodes (e.g., Bitcoin Script, Ethereum smart contracts).
security-considerations
HASHED TIMELOCK CONTRACT (HTLC)

Security Considerations & Risks

While HTLCs are a fundamental building block for secure cross-chain and off-chain transactions, they introduce specific risks that must be managed by developers and users.

01

Hash Preimage Exposure

The core security of an HTLC relies on the secrecy of the preimage (the input that generates the hash). If the preimage is leaked before the contract is funded, an attacker can claim the funds immediately. This risk is managed by:

  • Using a cryptographically secure hash function (e.g., SHA-256).
  • Securely generating and transmitting the preimage only to the intended recipient.
02

Timelock Expiry & Liquidity Risk

HTLCs have a strict timelock (e.g., cltv in Bitcoin, block.timestamp in Ethereum). If the recipient fails to provide the preimage before the deadline, the funds return to the sender. This creates risks:

  • Liquidity Lock-up: Funds are immobilized for the duration.
  • Price Volatility: The locked asset's value may change significantly.
  • Chain Congestion: Slow block times or high fees can prevent timely claim/refund transactions, causing funds to be stuck or lost.
03

Griefing & Denial-of-Service Attacks

A malicious participant can initiate an HTLC with no intention to complete it, causing harm to the counterparty.

  • Sender Griefing: A sender can lock funds with a preimage they already know but never reveal, forcing the recipient to wait for the timelock to expire and waste resources monitoring the chain.
  • Recipient Griefing: A recipient can receive the preimage off-chain but never submit the on-chain claim, forcing the sender to wait for the refund.
04

Implementation & Smart Contract Risks

On smart contract platforms like Ethereum, HTLC logic must be correctly implemented. Common vulnerabilities include:

  • Reentrancy: Improper state handling during the claim function.
  • Timestamp Manipulation: Relying on manipulatable block.timestamp for short durations.
  • Front-running: A network observer seeing the preimage in a public claim transaction and racing to submit their own. Audited, widely-used libraries are essential to mitigate these risks.
05

Cross-Chain Bridge Vulnerabilities

HTLCs are often used in cross-chain bridges. Here, the security of the entire system depends on the weakest chain's security and the honesty of external validators or oracles.

  • If one chain suffers a 51% attack or reorg, HTLCs can be invalidated, leading to double-spends or lost funds.
  • Bridge operators acting as intermediaries for HTLCs become centralized points of failure and trust.
06

Privacy Leakage

HTLCs are transparent on-chain. This can leak sensitive information:

  • The hash and timelock values reveal that a conditional payment is in progress.
  • Network analysis can link the sender and recipient addresses.
  • The transaction amount and timing are public. Techniques like Scriptless Scripts or zero-knowledge proofs are being explored to enhance HTLC privacy.
CONTRACT COMPARISON

HTLC vs. Related Concepts

A comparison of Hashed Timelock Contracts with other conditional payment and state management mechanisms in blockchain.

Feature / MechanismHashed Timelock Contract (HTLC)Atomic SwapPayment ChannelSmart Contract

Primary Purpose

Conditional, time-bound payment escrow

Trustless cross-chain asset exchange

Off-chain payment scaling with final settlement

General-purpose programmable agreement

Trust Model

Trustless (cryptographic enforcement)

Trustless (cryptographic enforcement)

Requires initial on-chain deposit; trust in counterparty's online presence

Trustless (code is law)

Typical Use Case

Cross-chain swaps, Lightning Network payments

Direct token exchange between blockchains

High-frequency, low-latency micropayments

Decentralized applications (DeFi, NFTs, DAOs)

State Finality

Time-locked refund or hash-preimage completion

Successful swap or time-locked refund

Can be settled on-chain at any time by either party

Immediate upon blockchain confirmation

On-Chain Footprint

One or two transactions (create/claim or refund)

Two transactions per chain (initiate/claim or refund)

Two transactions (channel open/close); many off-chain updates

Variable; code deployment and execution calls

Condition Logic

Single secret preimage reveal OR timeout

Cross-chain secret preimage reveal

Mutually signed balance updates

Turing-complete, arbitrary logic

Interoperability Focus

Cross-chain and intra-chain conditional payments

Specifically for cross-chain asset transfer

Primarily intra-chain (e.g., Bitcoin Lightning, Ethereum state channels)

Chain-specific, though bridges can connect them

Complexity / Flexibility

Low (fixed, simple logic)

Low (fixed, two-party swap pattern)

Medium (requires off-chain protocol and dispute handling)

High (unlimited custom logic and interactions)

HASHED TIMELOCK CONTRACT

Frequently Asked Questions (FAQ)

A Hashed Timelock Contract (HTLC) is a specialized smart contract that enables conditional, time-bound payments across blockchains. These FAQs cover its core mechanics, use cases, and security considerations.

A Hashed Timelock Contract (HTLC) is a type of smart contract that enforces a conditional payment, requiring the recipient to provide a cryptographic proof of payment (a secret) before a deadline, or else the funds are refunded to the sender. It works using two primary conditions: a hashlock and a timelock. The sender creates a cryptographic hash of a secret and embeds it into the contract. The recipient can only claim the funds by submitting the pre-image (the original data that generates that hash) before the timelock expires. If they fail, the sender can reclaim the funds after the timeout period. This creates a trust-minimized, 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