Lightning for Builders Inside Lightning

Routing on a network of payment channels

How Alice pays Dina through two strangers without trusting either: hash time-locked contracts, step by step, then a real two-hop payment.

45 minIntermediate8 / 17

Draft: this lesson is still waiting for review.

What you'll learn

  • Explain how an HTLC makes a payment conditional on a secret
  • Explain why time locks decrease along the route
  • Pay across two intermediate nodes and verify the preimage

Key terms

HTLC
Hash time-locked contract: a payment that unlocks only with the right secret before a deadline.
preimage
The secret whose hash is the payment hash. Revealing it proves the payment arrived.
invoice (ankara)
A Lightning payment request, often shown as a QR code, with an amount and an expiry time.

Alice wants to pay Dina 21,000 sats. They have no channel, but Alice → Bob → Chan → Dina is a path. Why would Bob and Chan, strangers to Alice, pass the money on honestly?

The promise: an HTLC

  1. Dina picks a random secret, the preimage, and puts its hash in her invoice.
  2. Alice offers Bob an HTLC: "21,002 sats are yours if you show me the secret behind this hash before block height H."
  3. Bob offers Chan the same deal for 21,001 sats, with an earlier deadline. Chan offers Dina 21,000 sats, earlier still.
  4. Dina knows the secret, so she reveals it to Chan and claims her 21,000. Chan now knows it and claims from Bob; Bob claims from Alice.

Each hop keeps 1 sat as its fee. Nobody can claim without the secret, and the secret only travels backwards once Dina has been paid. If anything fails, the deadlines pass and every HTLC is cancelled: the whole payment happens, or none of it does.

Why the deadlines shrink

Bob must have time to claim from Alice after Chan claims from him. So each hop's time lock is shorter than the one before it, by that node's time_lock_delta (you saw 80 blocks for LND in lesson 5).

Step 1: Pay through two hops

Pay through Bob and Chan

These exercises need a laptop with the lab running. You can read the lesson on your phone.

The route table lists Bob and Chan, each taking a small fee.

Step 2: The preimage is the receipt

Alice now holds the preimage. Hash it, and you get exactly the payment hash in Dina's invoice. That is proof she was paid, which Dina can't deny.

Check the preimage

These exercises need a laptop with the lab running. You can read the lesson on your phone.

Reflect

Write your answer down, or talk it over with a friend or your chama.

  1. Why can't Bob take Alice's payment and keep it?
  2. What would go wrong if every hop had the same time lock?

Check yourself

Question 1 of 4What unlocks an HTLC?

Further reading

Adapted from Mastering the Lightning Network. Andreas M. Antonopoulos, Olaoluwa Osuntokun and René Pickhardt. Licence: CC BY-SA 4.0.