Skip to content

bolt12handler+devrpc: dispatch invoice requests and add CreateOffer - #11374

Merged
bitromortac merged 16 commits into
lightningnetwork:masterfrom
bitromortac:20261016-c7e5-dispatch
Nov 9, 2026
Merged

bitromortac merged 16 commits into
lightningnetwork:masterfrom
bitromortac:20261016-c7e5-dispatch

Conversation

@bitromortac

Copy link
Copy Markdown
Collaborator

This is part 3 of the BOLT 12 receiver work for #10736. It is stacked on #11340, so review only the commits after multi: gate BOLT 12 offers behind a protocol flag.

#11340 adds a handler that answers an invoice request, but the node does not give it any requests. This PR connects the handler to the onion message layer and adds the API to create offers, so a node can now receive an invoice request for its offer and reply with a signed invoice. All of it stays behind the dev-only protocol.bolt12-offers flag and needs native SQL.

The handler answers only the onion messages that are delivered to this node, because a forwarding node must ignore the payload of its hop. Each request runs on its own goroutine with a fixed number of slots, so a slow reply path does not stall the other requests. A failed request gets no invoice_error reply yet.

CreateOffer is on the devrpc sub-server with lncli createoffer, so a release build has no BOLT 12 API while the feature is experimental. The offer includes the active chain hash, so that other implementations can validate it off mainnet.

The new itests (create offer and bolt12 invoice request) skip unless they run with the test_native_sql tag.

@github-actions github-actions Bot added the severity-critical Requires expert review - security/consensus critical label Oct 20, 2026
@github-actions

Copy link
Copy Markdown

🔴 PR Severity: CRITICAL

gh pr view | 49 files | 3517 lines changed

🔴 Critical (3 files)
  • server.go - core server coordination
  • rpcserver.go - core server coordination
  • lnwire/blinded_path_sphinx.go - Lightning wire protocol message (blinded path / sphinx handling)
🟠 High (9 files)
  • invoices/invoiceregistry.go - invoice management and settlement
  • invoices/invoices.go - invoice management and settlement
  • invoices/invoiceregistry_test.go - invoices package (test)
  • lnrpc/devrpc/* (config_active.go, dev.proto, dev.swagger.json, dev.yaml, dev_server.go, dev_server_offer_test.go, offer_creator.go, dev.pb.go, dev.pb.gw.go, dev.pb.json.go, dev_grpc.pb.go) - RPC/API definitions
  • lnrpc/invoicesrpc/utils.go, lnrpc/invoicesrpc/utils_test.go - RPC/API definitions
🟡 Medium (13 files)
  • bolt12/validate.go, bolt12handler/* (dispatcher.go, handler.go, replier.go, validate.go) - new BOLT12 offer-handling package
  • cmd/commands/cmd_offer.go, cmd/commands/devrpc_active.go - CLI client commands
  • config.go, config_builder.go, config_test.go, subrpcserver_config.go - server configuration wiring
  • lncfg/protocol_experimental_off.go, lncfg/protocol_experimental_on.go - feature flag config
  • onionmessage/actor.go, onionmessage/onion_endpoint.go - onion messaging
🟢 Low (24 files)
  • docs/release-notes/release-notes-0.22.0.md - release notes
  • itest/*, lntest/*, and various *_test.go files - test-only changes

Analysis

This PR adds BOLT12 offer support end-to-end: a new bolt12handler package, CLI offer command, devrpc offer-creation RPC, and supporting wire/invoice changes. It directly modifies server.go and rpcserver.go (core server coordination) and adds new wire-protocol logic in lnwire/blinded_path_sphinx.go, each independently CRITICAL per policy. The change also touches invoices/* and lnrpc/* (HIGH), spans 49 files and ~3,500 lines changed (well above the 20-file/500-line bump thresholds), and crosses multiple distinct critical packages — reinforcing CRITICAL as the appropriate severity. Expert review of the server/RPC wiring and wire-format handling is recommended, in addition to review of the new offer/invoice business logic.


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

@saubyk saubyk added the bolt12 label Oct 20, 2026
@saubyk saubyk added this to lnd v0.22 Oct 20, 2026
@saubyk saubyk moved this from Backlog to In progress in lnd v0.22 Oct 20, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in lnd v0.22 Oct 20, 2026
@saubyk saubyk moved this from In progress to In review in lnd v0.22 Oct 20, 2026
@saubyk saubyk added this to the v0.22.0 milestone Oct 20, 2026
@litbot-9000

Copy link
Copy Markdown
Collaborator

@ViktorT-11: review reminder
@GeorgeTsagk: review reminder

@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.

Since the PR this PR is based on hasn't been merged yet, I've mainly focused this review on spec compliance/interoperability issues introduced by this PR, as well as potential deadlocks introduces by the new invoice request handler server. Those areas seems unlikely to change due to any of the dependent PR's changes.

Once that initial PR is merged & this has been update, I'll focus my next review on testing the actual cmd & focus on the UX there.

Great work 🔥!!

Comment thread server.go Outdated
Comment on lines +4599 to +4603
// Include the active chain hash so other implementations can
// validate the offer on non-mainnet networks. The spec defaults
// to Bitcoin mainnet when offer_chains is absent.
genesisHash := *s.cfg.ActiveNetParams.GenesisHash
chains := [][32]byte{genesisHash}

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.

I think that potentially always setting this value actually violates the spec, and it should be omitted when the offer is for bitcoin mainnet?

https://github.com/lightning/bolts/blob/191462825ae341f0a27098765c075f01ca9bc6c2/12-offer-encoding.md?plain=1#L238-L241

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right, I think let's follow the spec's should 👍

Comment on lines +83 to +84
case SciddirIntro:
return nil, fmt.Errorf("sciddir intro is not supported")

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.

Just pointing out here a interoperability issue my LLM review found here:

LDK’s DefaultMessageRouter compacts the introduction node to a sciddir specifically for the invoice_request reply path. As we'll drop the entire request due to this error, this will lead to interoperability issues with LDK when LDK's DefaultMessageRouter does that.

Both CLN plugins/offers.c:276-285 & Eclair Postman.scala:154-172 resolves this, so I think we should do the same in lnd.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it was kind of meant as something we'd do later, but this PR adds a commit to resolve the scid, though it comes at the cost of a db lookup.

Comment thread server.go Outdated
Comment on lines +2947 to +2949
if s.bolt12Handler != nil {
s.bolt12Handler.Stop()
}

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.

This should be moved below the s.invoices.Stop() clause below.

Posting an LLM deadlock finding here, which is basically due to that the runInvoiceRequest in dispatcher.go can block on waiting for s.invoices to stop, which isn't possible as the shutdown is triggered after the s.bolt12Handler, and hence needs to wait for s.bolt12Handler to finish before it is stopped.

Posting the full finding below:


  1. Stop() → h.cg.WgWait() (dispatcher.go:49) waits for every in-flight runInvoiceRequest.
  2. runInvoiceRequest → HandleInvoiceRequest → notifyInvoice → NotifyNewBolt12Invoice → notifyClients (invoices/invoiceregistry.go:1573-1576):
    select {
    case i.invoiceEvents <- event:
    case <-i.quit:
    }
    No context. h.cg.Quit() cannot interrupt it. Only i.quit can.
  3. i.quit is closed by s.invoices.Stop() — which runs after, at line 2950.
  4. And the registry's own event loop can be blocked meanwhile: dispatchToSingleClients (invoiceregistry.go:374-380) parks on <-client.backlogDelivered, again breakable only by i.quit.

So: any SubscribeSingleInvoice client with an undelivered backlog at shutdown + one in-flight invoice request past SendInvoiceReply ⇒ WgWait never returns ⇒ lnd never shuts down. There is no watchdog on server.Stop().

The cleanup path gets this right by accident: cleaner.run() is LIFO (server.go:2277-2283) and s.invoices.Stop is registered later (:2608) than the handler's (:2407), so it runs first. Only the explicit Stop() has the order inverted.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the detailed trace and good catch, have updated that.

Comment thread bolt12handler/dispatcher.go Outdated
// Stop cancels the running requests and waits until they return.
func (h *Handler) Stop() {
h.cg.Quit()
h.cg.WgWait()

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.

Just adding here that this h.cg.WgWait can be executed prior to the h.cg.WgAdd(1) call in the Start function, which will then cause a panic. This is due to that server.Stop (which calls this function) can be executed in a race with the server.Start function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Nice, I have switched to use fn.GoroutineManager. Its Go refuses to start a goroutine once Stop has begun, so no add can race the wait. Because GoroutineManager needs each request to exit when its context ends, #11340 now passes the context to NotifyNewBolt12Invoice.

@bitromortac
bitromortac force-pushed the 20261016-c7e5-dispatch branch from 969f60b to 395ce73 Compare October 29, 2026 13:24
Comment thread itest/lnd_bolt12_test.go Outdated
alicePath := onionmessage.BuildBlindedPath(ht.T, aliceHops)

// Reply path to Bob (single hop: Bob is the final dest).
bobHops := []*sphinx.HopInfo{

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: the reply path starts at Bob, so the new self-processing in server.go never runs in a test. An unannounced LDK or CLN payer connected only to Alice sends a reply path that starts at Alice. I ran this test with a two hop reply path, Alice with next_node_id Bob and then Bob, and it passes through the self actor. Worth adding as a case here, or switching this one to that shape.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done, thanks for checking. I added a second case where we originate at the source node.

Comment thread server.go Outdated

// SendOnionMessage sends a custom message to the peer with the specified
// pubkey.
// pubkey. If the pubkey is our own, the node processes the message itself.

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: this also changes the lnrpc SendOnionMessage RPC. A call with our own key used to fail on the peer lookup and now runs the onion through the self actor, without the ingress limiter. Fine for a local caller, but it is not in the PR text or the proto doc. Either note it there or keep the self path on the replier.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I think that the change wasn't too great putting more methods onto the server and changing an RPC. The self actor is gone now. The new commit onionmessage: peel the own hops off a blinded path adds HopPeeler, which removes the hops of this node at the start of a blinded path. The handler uses it on the reply path before it answers, so the reply goes to the next node, which is a peer. Let me know what you think about those changes. I think it's cleaner to encapsulate this in the onionmessage package.

// node answers. Each answer costs a signature and puts an event into
// the queue of every invoice subscriber, and any peer can send a
// request. A payer needs one invoice per payment.
requestRate = 10

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: this limiter and the slots are global, not per peer. The default per peer ingress limit of 512 kbps lets one peer push well over 10 small invoice requests a second, so one flooding peer makes the node drop everyone else's requests. The update carries the peer key, so a per peer limiter in front of this one would be cheap. Can be a follow-up.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, agreed. I keep this as a follow-up, as you suggest. The global limit protects the node, and a fair split between peers needs a bucket per peer in a bounded cache, but there may be other interactions with the ingress side to consider, which also has rate limits.

@bitromortac bitromortac left a comment •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Rebased on master now that #11340 is merged.

Edit: the branch was pushed, but github didn't update the PR here yet, as there are problems with PRs right now (https://www.githubstatus.com/).

The linter flags a missing blank line after a wrapped function signature
and a blank line before a closing brace in invoiceregistry_test.go. Fix
both in a separate commit, so that the lint check of this PR passes.
The linter flags a return without a blank line before it in the stream
loop of SubscribeChannelEvents. The PR changes this file, so the lint
diff covers the line. Fix it in a separate commit before the change.
ToSphinx converts a blinded path to the sphinx form that the onion
message layer uses to send a reply. It accepts only a pubkey-form
introduction node, because the sciddir form needs a channel-graph
lookup.
Add the Delivered field to OnionMessageUpdate. The actor sets it when it
delivers the message to this node, and leaves it false when it forwards
the message.

For a forwarded message, the custom records and the reply path come from
the payload of this hop, and BOLT 4 tells a forwarding node to ignore
them. The flag lets a subscriber skip these updates.
The final hop of a blinded path carries a path_id that the creator of
the path set. The actor decrypts the recipient data of each hop, but it
dropped the path_id. A subscriber could not tell whether a message
arrived on a path it made.

A delivered message now carries the path_id in OnionMessageUpdate. Only
the creator of the path can check it, so the actor passes it on and
does not interpret it.
A payer can name the introduction node of its reply path by a channel and
a direction, the sciddir form, to keep the path short. To send the reply,
the node needs the key of that introduction node.

GraphIntroResolver finds the key in the channel graph. Direction 0 names
node_1, the node with the lesser key, and direction 1 names node_2.
resolveIntroNode replaces a sciddir introduction node with that key and
returns a path with a pubkey introduction node as it is. The next commit
uses it for the reply path of an invoice request.
Add Start and Stop to the Handler. Start subscribes to the incoming onion
messages and gives each invoice request to HandleInvoiceRequest, which
replies over the reply path of the request.

The handler answers only messages that are delivered to this node,
because a forwarding node must ignore the payload of its hop. Each
request runs on its own goroutine with a fixed number of slots, so that a
slow peer on one reply path does not stall the other requests.

The dispatch ends with Stop, with the context that Start gets, or when
the subscribe server ends the subscription. The subscribe server signals
this with Quit and does not close the updates, so the dispatch waits on
Quit. The end of the dispatch cancels the running requests.
Create the BOLT 12 handler when the offer store is set, which needs
native SQL. Start the handler after the onion message server, so that
the handler answers the invoice requests in incoming onion messages.
A blinded path can start at this node. A payer whose only peer is this
node names it as the introduction node of its reply path, to hide its
own node id. An onion message on such a path has this node as its first
hop, and no peer connection leads there.

HopPeeler removes the hops of this node at the start of a path. It
decrypts the own hop with the node key, finds the next node by its id or
by a channel, and derives the next blinding point, or takes the override
from the hop data. The result starts at the next node and keeps the
remaining hops, so the sender can build the onion for a real peer. The
actor and the peeler share the lookup of the next node, which moves into
its own function.
A payer whose only peer is this node names this node as the
introduction node of its reply path, so that its own node id stays
hidden. The replier sends the invoice to the introduction node, and no peer
connection leads to the own node, so such a payer got no invoice.

Start takes a HopPeeler, and the handler removes the hops of
this node at the start of the reply path before it answers. The reply
then goes to the next node, which is a peer. A reply path that ends at
this node gets no answer. The server passes an onionmessage.HopPeeler
with the onion message router and the graph. The SendOnionMessage RPC
does not change.
Add the CreateOffer method and its messages to the Dev service, with the
POST /v2/dev/createoffer REST route. The Dev service exists only in a
dev build, so a release build has no BOLT 12 API while the feature is
experimental. The server does not implement the method yet.
CreateOffer checks the request and passes it to the OfferCreator of the
config, which is nil when the node runs without BOLT 12 offers. It maps
the offer errors to gRPC codes, so that a client can tell a bad request
from a failure of the node.
addDeps is close to the funlen limit of 200 lines. A later commit adds
the offer creator of the dev RPC to it. Move the code that adds the
macaroon permissions to the interceptor chain into addPermissions. The
behaviour does not change.
Add CreateOffer to the server. It builds the offer from the node's
identity, includes the active chain hash so other implementations can
validate it off mainnet, persists the offer, and returns the lno1 string
with its content-addressed offer hash.

The server gives itself to devrpc as the offer creator only when the
offer store is set.
Add createoffer, which calls the CreateOffer RPC of devrpc and prints the
encoded offer and its offer hash. Like the other devrpc commands, it
exists only in a dev build.

The expiry can be a duration from now, such as --expiry 1h, which lncli
converts to the absolute expiry of the offer. --absolute_expiry still
takes a timestamp, and the two flags cannot be used together.
Cover offer creation and decoding, and the receiver-side negotiation
loop: an invoice request arrives over an onion message and the node
replies with a signed BOLT 12 invoice carrying a blinded payment path.

@bitromortac bitromortac left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Rebased on upstream/master. The only conflict was in server_test.go, where both sides added tests. The PR diff is unchanged. (M2) A new prep commit, invoices: fix whitespace in the registry tests, fixes the two lint issues that master has in invoiceregistry_test.go. (M3)

@bitromortac
bitromortac force-pushed the 20261016-c7e5-dispatch branch from b0acbd9 to f658650 Compare November 5, 2026 16:59

@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.

This looks really good to me. I just have one feedback/question that may be worth considering. Other than that, this looks ready to go from my end.

Comment thread bolt12handler/handler.go
activeChain: activeChain,
finalCLTVDelta: finalCLTVDelta,
maxInflight: maxInflightRequests,
requestLimiter: rate.NewLimiter(requestRate, requestBurst),

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 make this rate limit per peer instead of being global? Else we potentially open up for a griefing vector for single peers, which will then be able to block other payers from creating invoice requests.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Was discussed in #11374 (comment), I'd defer that as it needs more wholistic approach/analysis.

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.

Ah sorry my bad! Makes sense.

@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.

LGTM 🎉

@ViktorT-11

Copy link
Copy Markdown
Collaborator

Note: This can be rebased on master to remove the initial commit

@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.

LGTM

@bitromortac bitromortac added the full-ci Run the merge queue CI tier on this PR label Nov 9, 2026
@bitromortac
bitromortac enabled auto-merge November 9, 2026 13:04
@bitromortac
bitromortac added this pull request to the merge queue Nov 9, 2026
Merged via the queue into lightningnetwork:master with commit a9d4ca3 Nov 9, 2026
79 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in lnd v0.22 Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants