Repository navigation
bolt12handler+invoices: carry the invoice envelope in payment paths - #11532
bitromortac wants to merge 7 commits into
Conversation
🔴 PR Severity: CRITICAL
🔴 Critical (10 files)
🟠 High (15 files)
🟡 Medium (18 files)
🟢 Low (30 files)
AnalysisThis PR implements BOLT12 offer/invoice support end-to-end, touching core server wiring ( To override, add a |
A node that makes a blinded path for an offer or an invoice request must later recognize which message the path belongs to. The binding must exist before the paths, and the arriving message must let the node compute it again. OfferHashWithoutPaths is the offer hash with offer_paths left out. An invoice_request mirrors the offer fields, so the issuer gets the same value from a request. InvoiceRequestHash covers the invoice_request ranges with invreq_paths left out. An invoice mirrors these ranges of its request, so the payer gets the same value from an invoice.
BOLT 12 asks the issuer to ignore an invoice request for an offer without offer_paths when it arrived on a blinded path, and an invoice request for an offer with offer_paths when it did not arrive on one of them. The payer must ignore an invoice that did not arrive on the reply path of its request. A node can only tell its own blinded paths from others through the path_id it put into the final hop. A path id is a nonce and a tag. The nonce is an HMAC of the message the path belongs to, and the tag is an HMAC of the nonce. The tag alone shows that this node made the path, and the nonce shows which message the path belongs to. The node stores nothing for either check. A context separates offer paths from reply paths. The key is a tagged hash of the ECDH of the node key and a fixed point with no known private key. The ECDH needs only the minimal ECDHer interface, so it works with keychain.SingleKeyECDH and a remote signer. A payment path id has a random nonce instead. Its tag covers the nonce and a 32-byte binding, the hash of the invoice envelope, so a changed envelope byte fails the check. The same key with a separate label over the nonce and the binding yields the preimage, so the onion carries none. The random nonce keeps each preimage unique, even for two equal invoice requests.
A BOLT 12 receiver that stores nothing at invoice-request time still has to recover the invoice when the HTLC arrives. Add an optional invoice envelope beside the path_id in the final hop's encrypted data, so the payer carries the state back without being able to read it. The envelope uses a custom TLV type to stay clear of future spec assignments in encrypted_recipient_data, and is absent for BOLT 11 blinded paths. A path with an envelope must leave room in the payer's onion for the payer's own hops.
The final hop of each blinded payment path carries an invoice envelope: the optional offer hash, the payer id, the creation time, the amount, the optional quantity and the optional payer note. The receiver stores nothing when it answers an invoice request, so the envelope holds the invoice data. The codec lives in the package bolt12handler/envelope, which imports only tlv and fn, so the invoice store can decode an envelope although bolt12handler imports invoices. The envelope is node policy, so it stays out of the spec package bolt12. The envelope uses truncated integers. Padding repeats it on every blinded hop, so its size costs the payer onion bytes. The payer note is capped at 64 bytes, and the largest envelope is 151 bytes.
The handler now puts an invoice envelope and a payment path id into the final hop of each blinded payment path. The server derives the path id key from the node key. The path id is also the payment address. All MPP shards of a payment carry the same path id, so later shards find the invoice row without the envelope.
53e9761 to
959ec66
Compare
A received BOLT 12 invoice needs facts that a BOLT 11 invoice does not have: the offer it answers and the order data of its invoice request. Add the bolt12_invoices development table, keyed by the invoice id. It stores the offer id and the invoice envelope that the payment carried. The envelope is the authoritative source of the order data, as the encoded offer is for an offer. The shared invoice queries then stay as they are on master, so a release build that does not apply the development migrations runs them unchanged.
The SQL invoice store writes and reads the bolt12_invoices table only when it is created with WithBolt12, and refuses a BOLT 12 invoice otherwise. It writes the invoice envelope and decodes it on every read into the typed BOLT 12 fields, including the offer hash and the payer note. lnd sets the option only when BOLT 12 offers are enabled, because the table comes from a development migration.
959ec66 to
0a528c3
Compare
GeorgeTsagk
left a comment
There was a problem hiding this comment.
The settle side is not in here. Nothing checks a payment path id or writes a BOLT 12 row yet, and paying an issued invoice over its path fails with invoice not found. The description reads as if the node already rebuilds the invoice from the HTLC.
I'll do another pass soon.
| // TODO: Answer with an invoice_error once the handler sends invoice | ||
| // errors. | ||
| payerNote := ir.InvreqPayerNote.ValOpt().UnwrapOr(nil) | ||
| if len(payerNote) > envelope.MaxPayerNoteLen { |
There was a problem hiding this comment.
non-blocking: a request with a payer note over 64 bytes now gets no invoice and no error, the payer just times out. BOLT 12 has no cap on invreq_payer_note, and before this PR such a request was answered. The envelope only holds our copy of the note, so truncate it to 64 bytes there instead of dropping the request. The invoice still mirrors the full note.
| @@ -0,0 +1,458 @@ | |||
| //go:build test_db_sqlite | |||
There was a problem hiding this comment.
nit: these only build with test_db_sqlite, so the side table has no postgres coverage. I ran them against a postgres fixture and they pass. Worth running on both, like the other store tests.
ViktorT-11
left a comment
There was a problem hiding this comment.
Generally I think this looks good 🔥! I only have some minor not really blocking feedback. I think @GeorgeTsagk first comment is worth addressing though :)
| func DerivePathIDKey(ecdh ECDHer) (PathIDKey, error) { | ||
| // signrpc.DeriveSharedKey returns the same value for this point, so a | ||
| // caller with signer:generate can derive the key. That permission can | ||
| // already sign invoices with the node key, so the key adds no power. |
There was a problem hiding this comment.
Not sure I fully agree with the "adds no power" comment here.
I think it's fair to say that the signer:generate permission is one of the most powerful ones, and can generate signatures for node transactions, but that permission has previously not been able to generate the node's preimages. Therefore, I think it makes sense to correct this comment, and also add in the release notes that the signer:generate permission can now derive every BOLT 12 preimage this node issues. Unless you'd want to opt for another permission to generate such preimages, hence change the key used.
| InvoiceID: invoiceID, | ||
| Envelope: invoice.Bolt12Envelope, | ||
| } | ||
|
|
There was a problem hiding this comment.
Should we validate that the invoice.OfferID matches the one in the envelope before inserting it? Just so that we can't end up in a scenario where new code in the future could somehow make them diverge.
| -- The offer that created the invoice. NULL for an invoice that answers | ||
| -- no offer. | ||
| offer_id BIGINT REFERENCES offers(id), |
There was a problem hiding this comment.
Maybe we should mention that we intentionally designed this to not make it possible to delete an offer if there's invoices linked to it? (Given that it was actually intended).
GeorgeTsagk
left a comment
There was a problem hiding this comment.
ACK
Did another pass, one more nit. Other than that, what remains is addressing Viktor's comments.
| // | ||
| // A settled BOLT 12 invoice also has no payment request, because the | ||
| // signed lni1... string is not kept after it is sent to the payer. | ||
| return len(i.PaymentRequest) == 0 && !i.IsAMP() && !i.IsBolt12 |
There was a problem hiding this comment.
nit: with the flag off, a stored BOLT 12 invoice reads back as keysend, since the side table is not read and the payment request is empty. Worth a note, or derive it from the stored is_keysend column.
The node stores nothing when it answers an invoice request. It puts the invoice data into an envelope in its blinded payment paths, and the invoice store keeps that envelope as the record of the BOLT 12 invoice.
Depends on #11374. Review starts at "bolt12: add hashes that bind blinded paths to a message".
Why stateless: an invoice request is free to send, so a row per request would let anyone with an offer string fill the invoices table. The payer carries the envelope back in the encrypted data of the final hop, and the node rebuilds the invoice when the HTLC arrives.
Why HMAC path ids: the node must recognize its own paths and trust the envelope without stored state. The payment path id tags the envelope hash, and the same key derives the preimage. A changed envelope fails the check, and the onion carries no preimage. The key comes from an ECDH with the node key, so a remote signer works.
Why a small envelope: padding repeats it on every blinded hop, which costs the payer onion bytes. So it uses truncated integers, and the payer note is capped at 64 bytes.
Why a side table: the bolt12_invoices table is a development migration. The store uses it only when BOLT 12 offers are enabled, and a release build runs the shared invoice queries as on master.