Lightning for Builders Protocol

Lightning payment requests

Take an invoice apart: the human-readable part that holds the amount, the tagged fields inside, and the signature. Write a decoder for the amount.

35 minIntermediate15 / 17

Draft: this lesson is still waiting for review.

What you'll learn

  • Name the parts of a BOLT11 invoice
  • Convert an invoice's amount and multiplier into sats
  • Explain what BOLT 12 offers add

Key terms

BOLT11
The standard for Lightning invoices that start with lnbc (or lnbcrt on regtest).
invoice (ankara)
A Lightning payment request, often shown as a QR code, with an amount and an expiry time.
preimage
The secret whose hash is the payment hash. Revealing it proves the payment arrived.

You have pasted many invoices by now. A BOLT11 invoice is a bech32 string with three parts:

lnbcrt500u 1 pj9x…(data)…  (signature)
└── hrp ─┘ ↑ └──── tagged fields ────┘
       separator

The human-readable part (hrp)

  • ln: Lightning.
  • bc mainnet, tb testnet, tbs signet, bcrt regtest.
  • An optional amount with a multiplier: m milli (10⁻³ BTC), u micro (10⁻⁶), n nano (10⁻⁹), p pico (10⁻¹²).

So lnbcrt500u means 500 × 10⁻⁶ BTC = 0.0005 BTC = 50,000 sats. The separator is the last 1 in the string; bech32 data never contains a 1.

Tagged fields

After a timestamp, the data holds tagged fields: p the payment hash (the hash of the preimage), d a description, x expiry, c the final time lock, s a payment secret, r routing hints for private channels, and 9 feature bits. The invoice ends with Dina's signature, which also reveals her public key.

Decode the amount

Read the amount inside an invoice

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

The check tries your amount_sats on several kinds of invoices: micro, milli and nano amounts on mainnet and regtest.

Beyond BOLT11: offers

BOLT 11 invoices are single-use and carry everything in one string. BOLT 12 offers are reusable: a shop prints one offer, and each payer's wallet fetches a fresh invoice over Lightning itself. That allows refunds, recurring payments and more privacy for the receiver.

Reflect

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

  1. Why is a BOLT11 invoice meant to be paid only once?
  2. What would a shop gain from a reusable offer instead of a new invoice per sale?

Check yourself

Question 1 of 4How many sats is lnbc2500u?

Further reading

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