Skip to content

fix: check bip 321 amounts against the bip - #225

Open
jvsena42 wants to merge 4 commits into
masterfrom
fix/bip321-amount-checks
Open

jvsena42 wants to merge 4 commits into
masterfrom
fix/bip321-amount-checks

Conversation

@jvsena42

@jvsena42 jvsena42 commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Description

This PR brings the scanner's BIP 321 amount handling in line with the BIP. Both issues came up in review of the NFC payments PRs (synonymdev/bitkit-android#1883, synonymdev/bitkit-ios#1302), where the apps currently guard them. Those guards are part of the app-side rewrite that goes away when the apps bump core (synonymdev/bitkit-android#1935, synonymdev/bitkit-ios#1344), so core needs the same checks first.

  • Amount grammar: parse_amount_as_satoshis parsed with f64. amount=1e3 read as 1,000 BTC and amount=inf as u64::MAX, while 0.0000799BTC and 0.0000799,50 were silently dropped. It now accepts only BIP 321's *digit [ "." *digit ] in decimal BTC, using integer arithmetic, and rejects digits below one satoshi. On-chain URIs behave as before when the amount is invalid: the amount is 0 and the user enters one.
  • Amount on a URI without an address: bitcoin:?amount=…&lightning=<BOLT11> returned only the invoice, so the URI's amount was lost. It now returns InvalidAmount when that amount is invalid, differs from the invoice's signed amount (compared in millisatoshis, before the decoded amount is rounded up to whole sats), or the invoice has no amount. A matching amount decodes as before (Numo's URI included).

Checked against BIP 321 and unchanged here: amount, label, message and pop must not repeat; any req- key is refused, which covers pop with req-pop; unknown keys may repeat; payment-instruction keys such as lightning may repeat.

Linked Issues/Tasks

Tests

  • utils::tests::bip321_amounts_parse_exactly, utils::tests::amounts_outside_the_bip321_grammar_are_invalid
  • bip321_tests::invalid_onchain_amount_is_not_read_as_another_amount
  • bip321_tests::uri_without_address_keeps_an_amount_its_invoice_states, uri_without_address_rejects_an_amount_its_invoice_does_not_state, uri_without_address_allows_repeated_unknown_parameters
  • utils::tests::every_satoshi_amount_round_trips: 20,000 values across the u64 range plus edge values, in both decimal forms, up to u64::MAX and one satoshi past it.
  • Each new test that checks a fix fails without it. cargo test --lib modules::scanner: 86 passed.
  • cargo clippy -- -D warnings fails on master with errors outside these lines; none are in the changed code.

🤖 Generated with Claude Code

jvsena42 and others added 2 commits November 23, 2026 06:16
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvsena42 jvsena42 self-assigned this Nov 23, 2026
@jvsena42
jvsena42 requested review from a team, ovitrif and piotr-iohk and removed request for a team November 23, 2026 09:17

@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. Replaces the f64 BIP 321 amount parser with an exact integer parser for the BIP's *digit [ "." *digit ] grammar, and makes an address-less bitcoin:?amount=…&lightning=<bolt11> URI fail with InvalidAmount unless the amount matches the invoice's. Reviewed 2688084, full tier, static.

What I checked, and 6 candidates I ruled out

Read in full: scanner/utils.rs, scanner/implementation.rs (decode, decode_bitcoin_uri, decode_lightning, decode_onchain), scanner/bip321_tests.rs
Call sites traced: parse_amount_as_satoshis has two callers, decode_onchain (amount falls back to 0 on error, same as before) and the new check in decode_bitcoin_uri
Spec: checked against the BIP 321 text (ABNF amountparam, the transfer amount section, the duplicate-key rules)
Run: talos-pr-run refused the build (could not check @jvsena42's permission on synonymdev/bitkit-core, a tooling issue on my side), so the crate was not built or tested here. I compiled parse_amount_as_satoshis alone in a scratch binary and probed it: 184467440737.09551615 gives u64::MAX, one sat more gives InvalidAmount, 0.0000799 gives 7990, 5. and .00042 parse, 1.000000000000000000000 (trailing zeros below a sat) gives 1 BTC, 0.000000015, "1 " and a full-width digit are rejected.
CI: no workflow runs the scanner tests (cargo test jobs cover paykit-route, usdt and wake only), so the new tests are not exercised in CI either. Bindgen and usdt jobs were still pending when I read them.

Ruled out

  • Empty amount= on an address-less URI: instructions drops empty values before the new check, and decode_onchain maps it to 0 as before.
  • AMOUNT= bypassing the check: keys are lowercased when instructions is built; the PR's own test covers it.
  • Invoice with a sub-satoshi amount: decode_lightning rounds msat up with div_ceil, so a URI amount has to equal the rounded-up sats. It needs a BOLT11 with sub-sat precision plus a URI amount floored to the sat. The decode fails safe (InvalidAmount), it never pays a different amount.
  • amount=0 with an amountless invoice: rejected with InvalidAmount. A merchant would not send that, and a URI amount of zero carries nothing to keep.
  • LNURL in an address-less URI ignoring amount: this is how it worked before, and the PR scopes the check to BOLT11.
  • On-chain URIs with an address that used to parse, such as 0.000000015 or 1e-5: the amount now drops to 0 and the user types one. The PR intends this, and it fails safe.

Merge confidence: 4/5, no findings, but neither CI nor I ran the scanner tests that cover the change.

@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: 💬 Comment. Compare the URI amount with the signed BOLT11 amount before rounding.

Findings:

  • MEDIUM src/modules/scanner/implementation.rs:204 compares the URI amount with the invoice amount rounded up to whole sats. A fractional-sat invoice can pass the equality check even though its signed amount differs from the request. The retiring mobile guards compare the decimal amounts exactly. Compare the signed amount in millisatoshis and keep the existing rounding for display. BOLT 11 amount encoding
  • LOW src/modules/scanner/utils.rs:40 has example coverage for the new decimal parser. Add a generated round-trip test over the u64 satoshi range, including the fractional boundary at u64::MAX.

QA for 2688084:

Run the Rust scanner regression suite ✅ All 85 scanner tests passed.

cargo test --locked --lib modules::scanner covered exact decimal conversion, malformed amounts, matching and rejected Lightning amounts, and repeated unknown query parameters.

Open an on-chain request with an exact or malformed amount ✅ App logs showed 7,990 sats for the exact decimal and an unspecified amount for the exponent form.
onchain2x.2x.mp4
Open an addressless Lightning request with a matching amount ✅ App logs showed 7,990 sats for the matching request and InvalidAmount for the mismatch, the URI amount paired with an amountless invoice, and the unit suffix.
lightning2x.2x.mp4

Reviewed the full scanner diff from e16cfc7 through 2688084. Android QA used regtest deep links on cf5cdd4 with the reviewed Rust library and Kotlin bindings, plus a test-only shim for the removed legacy Pubky auth APIs. The wallet was unfunded, so the amounts were verified from the app logs. This diff changes the shared scanner, so QR, NFC, Bluetooth, and the iOS interface were outside the device run. Earlier review and overflow-boundary probe


Reviewed by gpt-6.2-sol-high

Comment thread src/modules/scanner/implementation.rs Resolved
Comment thread src/modules/scanner/utils.rs Resolved

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

QA review

Scope: full PR at 2688084, re-reviewed after an explicit full review with no prior coverage baseline.

Code review completed with unverified earlier concerns.

No actionable code findings. parse_amount_as_satoshis accepts BIP 321 decimal BTC (*digit [ "." *digit ]), rejects a non-zero digit below one satoshi, and converts with integer arithmetic. An independent integer oracle matched the pinned implementation on the examples in the new tests, on 0.0000799 BTC = 7990 sats, on the u64 boundary (184467440737.09551615 is u64::MAX sats and one satoshi more is rejected), and on 200,000 random grammar strings. For those grammar-valid values the old f64 conversion disagreed only at 184467440737 BTC, where it dropped precision. An address-less bitcoin:?amount=…&lightning=<BOLT11> returns InvalidAmount when that amount is invalid, differs from the invoice's ceiled satoshi amount, or the invoice states no amount. An invalid on-chain amount still decodes as 0 sats.

Payment routing was checked only for that 0-sat contract, on iOS at f17214e: an on-chain amount below the dust limit opens amount entry. Android send routing was not traced. Neither app was reviewed.

Validation: the parser oracle was executed against the pinned function. cargo test --lib modules::scanner was not run here (Rust 1.83; the crate rust-version is 1.91, and the dependency cache is empty). The author reports that command passed at this revision; that run was not reproduced.

Suggested additional test cases:

  • iOS and Android, after taking this core: scan bitcoin:?amount=0.00042&lightning=<invoice for 42000 sats>. Expected: the payment screen shows 42000 sats.
  • Scan that URI with amount=0.00043, with an amountless invoice and amount=0.001, and with amount=1e3. Expected: decode fails and no payment amount is shown.
  • Scan bitcoin:<address>?amount=1e3 and bitcoin:<address>?amount=inf. Expected: the app asks for an amount.

Earlier findings reconciled at this revision: 2 not substantiated, 1 could not be verified.

Reconciliation incomplete: the following earlier concerns remain unverified. This report does not establish that they are fixed or invalid, or establish approval or device readiness.

Could not verify earlier concern — Scanner unit tests were not run at this head: This is an execution limit, not a product defect. rustc 1.83.0 cannot build the crate (rust-version 1.91), and cargo test --offline --lib modules::scanner stops because the pubky-swap git dependency is not in the local cache. The retained QA note reports an earlier crates.io CONNECT 403. talosmachina's review on this head also says the scanner tests were not run. ovi-reviewer attributes cargo test --locked --lib modules::scanner with 85 passed at 2688084; that run was not reproduced here.

Reviewer model: grok-4.7
Reasoning effort: xhigh

jvsena42 and others added 2 commits November 23, 2026 09:06
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvsena42
jvsena42 requested a review from piotr-iohk November 23, 2026 12:06

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

QA review

Scope: changes since 2688084, including affected code paths, at 5444cf3. Baseline: previous review. On-chain decoding of BIP 321 amounts (decode_onchain) is unchanged since the baseline, and its coverage is inherited from it. The merge base is still e16cfc7.

Code review completed with unverified earlier concerns.

The update changes how a bitcoin: URI with no address checks its amount against the BOLT11 invoice. The check now uses the invoice's signed millisatoshi amount. Before, it used amount_satoshis, which decode_lightning rounds up to whole sats. As a result, an amount that only matches after rounding (for example, a 42,000.5-sat invoice) now returns InvalidAmount instead of being accepted. An invoice without an amount still returns InvalidAmount, and so does a URI amount whose millisatoshi value overflows. The invoice is parsed again from the same lowercased string that decode_lightning has already accepted, so that parse cannot fail. InvalidAmount is an existing DecodingError variant and src/lib.rs is unchanged, so the generated bindings do not change. The new round-trip test is test-only and checks values up to u64::MAX sats, plus one satoshi past it.

Validation: I copied the changed parse_amount_as_satoshis and the millisatoshi comparison into a standalone Rust program (rustc 1.99.1) and ran it. An exact match passes. Fractional-sat mismatches, invoices without an amount and overflow all return InvalidAmount. Leading zeros, 1. and .1 parse as expected, and non-ASCII digits are rejected. I could not run cargo test --lib modules::scanner because the sandbox blocked crates.io, so the dependencies could not be fetched. The PR's own tests were read but not executed in this review. No runtime or downstream app check is needed for this library-only change beyond the app bumps the PR description already lists.

Earlier findings reconciled at this revision: 2 verified fixed, 1 could not be verified.

Reconciliation incomplete: the following earlier concerns remain unverified. This report does not establish that they are fixed or invalid, or establish approval or device readiness.

Could not verify earlier concern — Scanner unit tests were not run at this head: This is an execution limit, not a product defect. At 5444cf3 the scanner suite has still not been run by this reviewer or by CI. The local toolchain is now rustc/cargo 1.99.1, which satisfies rust-version 1.91, but cargo test --locked --lib modules::scanner could not fetch dependencies because the sandbox denied outbound access to index.crates.io. No CI workflow at this head runs the scanner tests. The author reports cargo test --lib modules::scanner with 86 passed at 5444cf3 (#225 (comment)); that run is attributed to the author and was not reproduced. Disclose this limitation in the report. It does not establish that the tests fail, and it is not a fix or a runtime pass.

Reviewer model: claude-opus-5-5
Reasoning effort: high

@jvsena42
jvsena42 enabled auto-merge November 24, 2026 09:35
@jvsena42
jvsena42 disabled auto-merge November 24, 2026 17:27
@jvsena42
jvsena42 enabled auto-merge November 24, 2026 18:03
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.

3 participants