MSTR Cat

Blockchain/Level IV · Protocol depth/4.1 Script/Lesson 5

Hashlocks and HTLCs

The secret that unlocks everything, and the timer that saves it from being lost forever

MaterialsAny text editor or command line with SHA-256 supportA block explorer (e.g., mempool.space)The Lightning Network whitepaper by Poon & Dryja

Wonder

  1. How can a payment be locked to a secret only one person knows, without that person having to trust anyone else?
  2. If revealing the secret to claim your own money exposes it to the entire network, what stops others from stealing it first?
  3. Why does every hop in a multi-node payment path need a slightly different deadline?

Explore

  1. A hashlock is a Bitcoin Script condition that restricts spending until specific data, the preimage, is revealed. Coins are locked behind a hash, usually supplied by the person who holds the secret. The critical property here is that if any participant reveals the preimage to claim their funds, they reveal it to whoever sees the claim: on-chain, to everyone; inside a Lightning channel, to the counterparty.
  2. This public revelation allows other parties holding outputs locked by the same hash to also spend them. In the Lightning Network, this means when the final recipient (Charlie) reveals the secret to claim payment from Bob, that same secret unlocks Alice’s output locked for Bob. This mechanism enables atomicity: either everyone gets paid, or no one does.
  3. However, a pure hashlock has a flaw: if Charlie disappears and never reveals the preimage, the funds are stuck forever. Adding a timelock creates a hash time-locked contract (HTLC). The script now states, for the last hop offered by Bob: ‘Charlie can claim these coins by revealing the preimage before deadline T, OR Bob can reclaim them after T.’
  4. This dual-path logic ensures trustless operation. If the payment succeeds, Charlie reveals the secret within the timeout window, triggering a cascade of claims from Bob back to Alice. If the payment fails or Charlie goes offline, each HTLC times out and its offerer (Bob from Charlie, Alice from Bob) reclaims the funds through the script’s timeout branch.
  5. This fairness protocol is what allows multi-hop routing. Each HTLC in the path has a slightly longer timeout than the HTLC its holder offers to the next node downstream. The gap protects each intermediate node: if its downstream peer claims with the preimage at the last moment, the intermediate node still has time to use that preimage upstream before its own incoming HTLC can be refunded.
  6. While Script puzzles can be complex, real-world contracts like HTLCs always include signature checks alongside hashlocks and timelocks. This binds the solution to specific parties and prevents miner front-running, where a miner might see a valid preimage in the mempool and redirect the payout to themselves before it is confirmed.

Key facts

  • A hashlock restricts spending until a specified piece of data (the preimage) is publicly revealed.
  • Once any hashlock using a specific hash is opened, the revealed preimage lets all other hashlocks using that same hash be spent.
  • An HTLC combines hashlocks and timelocks to require the recipient to spend before a deadline or allow the sender a refund.
  • HTLCs are used in Lightning Network commitment transactions to enable trustless multi-hop routing with atomicity.
  • Script provides OP_SHA256 and OP_HASH160 for hashlocks; Lightning uses SHA-256 payment hashes.
  • Tier Nolan proposed cross-chain atomic swaps using this hashlock mechanism in 2013.

Exercise nine, six, three, one

9CollectList 9 real-world or cryptographic scenarios where one action reveals a secret that unlocks multiple other actions (e.g., opening a safe, publishing a proof-of-work).
6ClusterGroup the 9 into 6 by how the secret is protected: single-owner, multi-owner, time-bound, space-bound, public but expensive, or irreversible.
3DistillReduce the 6 to 3 core mechanisms that allow trustless coordination between parties who do not know each other.
1CrystallizeWrite 1 sentence on why a hash is a commitment and a preimage is a key, and how a timer makes them safe.

Fact check

Dates, figures and claims checked against the published sources listed below.

Open in the interactive academy →

PT ES ZH JA KO RU DE FR MS