Skip to content

bolt12handler+invoices: carry the invoice envelope in payment paths - #11532

Open
bitromortac wants to merge 7 commits into
lightningnetwork:masterfrom
bitromortac:20261104-2742-dispatch
Open

bitromortac wants to merge 7 commits into
lightningnetwork:masterfrom
bitromortac:20261104-2742-dispatch

Conversation

@bitromortac

@bitromortac bitromortac commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

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.

@github-actions github-actions Bot added ci-hold CI is held; remove this label to run CI severity-critical Requires expert review - security/consensus critical labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

🔴 PR Severity: CRITICAL

gh pr view | 73 files | 5952 lines changed

🔴 Critical (10 files)
  • lnwire/blinded_path_sphinx.go - lnwire/* (Lightning wire protocol messages)
  • rpcserver.go - explicitly listed core server file
  • server.go - explicitly listed core server file
  • sqldb/migrations_dev.go - sqldb/* (database migration registration — always critical)
  • sqldb/sqlc/bolt12.sql.go - sqldb/* (generated query code)
  • sqldb/sqlc/migrations/000017_bolt12_invoices.up.sql - new DB migration — always critical
  • sqldb/sqlc/migrations/000017_bolt12_invoices.down.sql - new DB migration — always critical
  • sqldb/sqlc/models.go - sqldb/* models
  • sqldb/sqlc/querier.go - sqldb/* querier interface
  • sqldb/sqlc/queries/bolt12.sql - sqldb/* query definitions
🟠 High (15 files)
  • invoices/errors.go, invoices/invoices.go, invoices/sql_store.go, invoices/sql_migration.go - invoices/* (invoice settlement/storage)
  • lnrpc/devrpc/dev.proto, dev.pb.go, dev.pb.gw.go, dev.pb.json.go, dev.swagger.json, dev.yaml, dev_grpc.pb.go, dev_server.go, offer_creator.go, config_active.go - lnrpc/* (RPC/API surface)
  • routing/blindedpath/blinded_path.go - routing/* (pathfinding)
🟡 Medium (18 files)
  • bolt12/offer_hash.go, bolt12handler/*.go (dispatcher, envelope, generate, handler, intro_resolver, node_secret, path_id) - uncategorized Go packages
  • cmd/commands/cmd_offer.go, cmd/commands/devrpc_active.go - cmd/* (CLI client, does not inherit RPC severity)
  • config_builder.go, subrpcserver_config.go - uncategorized root Go files
  • onionmessage/*.go (actor, hop, hop_peeler, onion_endpoint) - uncategorized Go package
  • record/blinded_data.go - record/*
🟢 Low (30 files)
  • docs/release-notes/unreleased/11374.md - release notes
  • All *_test.go files, plus itest/* and lntest/* changes (test-only)

Analysis

This PR implements BOLT12 offer/invoice support end-to-end, touching core server wiring (server.go, rpcserver.go), new onion-message/blinded-path plumbing (lnwire/blinded_path_sphinx.go), and — most notably — adds a new SQL migration and generated query layer under sqldb/* for persisting BOLT12 invoices. Per the rules, database migrations are always CRITICAL, and server.go/rpcserver.go are explicitly CRITICAL files, so the PR is classified CRITICAL regardless of the bump rules (which would also trigger here: 39 non-test/non-generated files and >500 lines changed). Expert review should focus on the new migration's correctness/reversibility and the invoice persistence changes in invoices/sql_store.go.


To override, add a severity-override-{critical,high,medium,low} label.

@bitromortac bitromortac added full-ci Run the merge queue CI tier on this PR and removed ci-hold CI is held; remove this label to run CI labels Nov 9, 2026
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.
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.
@bitromortac
bitromortac force-pushed the 20261104-2742-dispatch branch from 959ec66 to 0a528c3 Compare November 11, 2026 15:25

@GeorgeTsagk GeorgeTsagk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread bolt12handler/generate.go
// TODO: Answer with an invoice_error once the handler sends invoice
// errors.
payerNote := ir.InvreqPayerNote.ValOpt().UnwrapOr(nil)
if len(payerNote) > envelope.MaxPayerNoteLen {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@saubyk saubyk added this to lnd v0.22 Nov 12, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in lnd v0.22 Nov 12, 2026
@saubyk saubyk moved this from Backlog to In review in lnd v0.22 Nov 12, 2026

@ViktorT-11 ViktorT-11 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I think this looks good 🔥! I only have some minor not really blocking feedback. I think @GeorgeTsagk first comment is worth addressing though :)

Comment thread bolt12handler/path_id.go
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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread invoices/sql_store.go
InvoiceID: invoiceID,
Envelope: invoice.Bolt12Envelope,
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +10 to +12
-- The offer that created the invoice. NULL for an invoice that answers
-- no offer.
offer_id BIGINT REFERENCES offers(id),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GeorgeTsagk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

Did another pass, one more nit. Other than that, what remains is addressing Viktor's comments.

Comment thread invoices/invoices.go
//
// 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the merge queue CI tier on this PR severity-critical Requires expert review - security/consensus critical

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants