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
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.bcmainnet,tbtestnet,tbssignet,bcrtregtest.- An optional amount with a multiplier:
mmilli (10⁻³ BTC),umicro (10⁻⁶),nnano (10⁻⁹),ppico (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.
- Why is a BOLT11 invoice meant to be paid only once?
- What would a shop gain from a reusable offer instead of a new invoice per sale?
Check yourself
Further reading
Adapted from Mastering the Lightning Network. Andreas M. Antonopoulos, Olaoluwa Osuntokun and René Pickhardt. Licence: CC BY-SA 4.0.