Skip to content

fix: route to lightning only on a leading lightning: scheme - #228

Open
jvsena42 wants to merge 3 commits into
masterfrom
fix/scanner-lightning-prefix
Open

jvsena42 wants to merge 3 commits into
masterfrom
fix/scanner-lightning-prefix

Conversation

@jvsena42

@jvsena42 jvsena42 commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Fixes #227

Cause

Scanner::decode took the Lightning branch when the lowercased input contained lightning:. A bitcoin: URI with lightning: inside a parameter value, such as bitcoin:<address>?label=pay%20lightning:x or the same in message, went to decode_lightning and failed with InvalidFormat.

The leading lightning: strip at the top of decode was also case-sensitive. LIGHTNING: followed by an LNURL, a node id (pubkey@host:port) or a Lightning address skipped that strip, matched the contains check, and failed in the BOLT11 decoder.

Change

  • The Lightning branch is taken only when the trimmed input (after the lightning: strip and bitkit:// handling) starts with lightning: in any case, or is a bare lnbc/lntb invoice (lnbcrt and lntbs are covered by those prefixes). A bitcoin: URI goes to decode_bitcoin_uri, which already reads its lightning parameter.
  • The leading lightning: strip is now case-insensitive, the same way decode_onchain strips bitcoin:. LIGHTNING:LNURL1…, LIGHTNING:<pubkey>@host:port and LIGHTNING:user@domain now reach the LNURL, node-id and Lightning address paths. LIGHTNING:<BOLT11> decodes as before.
  • Checked the other contains(...) calls in decode. The node-id @ check only fires when the part before the single : contains @, so a bitcoin: URI never matches; contains("onion") only runs inside that branch. Neither has the bug.
  • CHANGELOG entry under Unreleased. No version bump or binding changes; the public API is unchanged.

Known, pre-existing, out of scope

  • Simnet invoices (lnsb) are not recognised as bare invoices.
  • lightning:// (with slashes) is not stripped.
  • BOLT12 offers (lno…) are not decoded.

Tests

New scanner tests:

  • label containing lightning: (percent-encoded, plain, uppercase)
  • message containing lightning: (percent-encoded and plain)
  • uppercase QR form BITCOIN:<ADDR>?LABEL=LIGHTNING:X decodes as on-chain
  • LIGHTNING: prefix with a lowercase invoice still decodes as Lightning
  • LIGHTNING:LNURL1… and Lightning:lnurl1… (LNURL-auth, no network request) decode as LnurlAuth
  • LIGHTNING:<pubkey>@host:port decodes as NodeId
  • bitcoin:?lightning=<bolt11> still decodes as Lightning
  • bitcoin:<address>?...&lightning=<bolt11> decodes as on-chain with the invoice in params

Run locally (CI does not run modules::scanner):

  • cargo test --locked --lib modules::scanner: 87 passed, 0 failed.
  • Same tests against the old implementation.rs: master fails 6 (label, message, address plus lightning, uppercase QR label, uppercase LNURL, uppercase node id); 53e5b18 fails 2 (uppercase LNURL, uppercase node id).
  • cargo fmt --check: clean.
  • cargo clippy --locked --lib --tests: same warning set as before this change (compared with the previous head, 53e5b18, which added none over master).

🤖 Generated with Claude Code

Scanner::decode sent any input containing `lightning:` to the Lightning
decoder, so a bitcoin: URI with `lightning:` in a label or message
failed to decode. Match the scheme at the start only.

Fixes #227

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jvsena42 jvsena42 self-assigned this Nov 25, 2026
The leading `lightning:` strip was case-sensitive, so `LIGHTNING:` with
an LNURL, node id or Lightning address reached the BOLT11 decoder and
failed. Also test an uppercase bitcoin: URI with `LIGHTNING:` in its
label.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jvsena42
jvsena42 marked this pull request as ready for review November 25, 2026 16:06
@jvsena42
jvsena42 requested a review from a team November 25, 2026 16:06

@talosmachina talosmachina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No findings. Routes Scanner::decode to Lightning only on a leading lightning: scheme (now matched in any case) or a bare lnbc/lntb invoice, so a bitcoin: URI with lightning: inside label or message decodes as that URI. Reviewed 3166f06, full tier, static.

What I checked, and 5 candidates I ruled out

Read in full: src/modules/scanner/implementation.rs (decode, decode_bitcoin_uri, decode_onchain), the new tests in src/modules/scanner/tests.rs
Build/tests: build skipped: talos-pr-run could not check the author's permission on this repo (HTTP 403) and fails closed, so the 87 scanner tests in the PR body were not rerun here
CI: green, but no workflow runs modules::scanner (only paykit_route, wake, usdt and the bindgen jobs), so CI does not exercise this change

Ruled out

  • Panic on the new strip: invoice_str.get(..LIGHTNING_SCHEME.len()) returns None off a char boundary, and when it returns Some the following byte slice starts on that same boundary.
  • Inputs that relied on the old contains match: LIGHTNING:<BOLT11> is now stripped up front and still reaches decode_lightning through the lnbc/lntb check, lowercased as before; bitkit://lightning:... recurses into decode, which strips the scheme.
  • Leading lightning: check after the strip: it only matches a doubled lightning:lightning: prefix; harmless, and not a behaviour change worth a fix.
  • Node-id @ branch catching a bitcoin: URI with @ in a parameter: parts[0] is bitcoin, which never contains @, and the branch is unchanged by this diff.
  • Uppercase LABEL= landing in params instead of label: pre-existing key matching in decode_onchain; the new test asserts the current behaviour.

Merge confidence: 4/5, no findings, but the scanner tests run in neither CI nor this review.

@ovi-reviewer ovi-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: ✅ Approve

Findings:

  • LOW — src/modules/scanner/tests.rs:730: Add a generated-input property test for the scheme-routing invariant; the maintained regressions use fixed examples only.

QA for 3166f06:

Native scanner tests and a review-only Swift app linked to the rebuilt library, on an iPhone 17 Pro simulator with iOS 26.5. The simulator cases call the public UniFFI decode binding directly.

Scanner regression tests ✅ All scanner regressions passed.

cargo test --locked --lib modules::scanner: 87 passed, none ignored. Formatting and diff whitespace checks also passed.

Bitcoin labels and messages containing lightning: ✅ Swift assertions preserved the on-chain result and metadata.

Checked plain and percent-encoded labels/messages, uppercase LIGHTNING: in a value, and uppercase Bitcoin QR input. Assertions checked the address, amount and retained metadata.

metadata.2x.mp4

Case-insensitive Lightning wrappers ✅ Swift assertions passed for invoices, LNURL-auth, node connections and Unicode boundaries.

Checked uppercase/mixed-case wrappers, a bitkit:// wrapper, and 39 malformed strings around UTF-8 byte boundaries. LNURL-auth decoded locally without a network request.

prefixes.2x.mp4

Bitcoin requests with Lightning fallbacks ✅ Addressless requests returned Lightning; addressed requests retained their on-chain destination and fallback.

Swift assertions checked the invoice, amount, address, label and retained lightning parameter.

fallback.2x.mp4

Not driven: consumer-app QR, paste, NFC and Bluetooth journeys, or network-backed Lightning-address lookups; this run exercised the shared decoder directly with offline inputs. Clippy was not rerun.

Full review: diff, all 3 changed files, plus the affected scanner and exported decode path. The prior static review had no runtime test evidence; the results above were run for this review.


Reviewed by gpt-6.1-sol-high

Comment thread src/modules/scanner/tests.rs Resolved
Loop over bitcoin: prefix cases, label/message/both, and values built
from mixed-case lightning:, plain text, @ and : fragments, plain and
percent-encoded, and check each decodes as on-chain with the same
address, amount and params. Check that lightning: in any case before
the same invoice decodes identically.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jvsena42
jvsena42 requested a review from ovitrif November 26, 2026 10:57

@ovi-reviewer ovi-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: ✅ Approve

Findings: None. The new generated-input tests address the earlier LOW coverage request.

QA for 3db335d:

Check: Generated Bitcoin metadata stays on-chain ✅ Native assertions passed for 648 constructed URI inputs.

The test combines three Bitcoin scheme variants, label/message/both, and plain or percent-encoded pairs of fragments including mixed-case lightning:, spaces, @ and :. Assertions check on-chain routing, address, the 12,345-satoshi amount, label, message and retained parameter entries.

Check: Lightning prefix case variants decode identically ✅ Native assertions passed for 24 wrapper/body combinations.

Across three invoice amounts, four Lightning scheme variants and lower/uppercase invoice bodies, assertions compare BOLT11, payment hash, amount, payee and description with the decoded bare invoice.

Ran cargo test --offline --locked --lib modules::scanner::tests::tests::test_generated -- --nocapture: both tests passed, none ignored. cargo fmt --check and delta whitespace checks passed.

No new device run: this delta changes tests only. The production decoder, dependencies and exported bindings are unchanged from the passing native and Swift-binding review at 3166f06. That report's scanner regressions and simulator cases for metadata, case-insensitive wrappers and Lightning fallbacks remain applicable; they were not rerun. Consumer-app QR, paste, NFC/Bluetooth journeys and network-backed Lightning-address lookups remain outside that evidence.

Reviewed the complete 3166f06..3db335d delta: one file, two test additions and their helper, plus the scanner paths they call and the prior open coverage request. The added test code was driven by native tests; no production change required a new device case.


Reviewed by gpt-6.1-sol-high

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

utAck

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scanner decodes a bitcoin: URI as Lightning when a value contains lightning:

3 participants