Skip to content

fix: compose a hardware-funded request once - #1221

Merged
jvsena42 merged 4 commits into
masterfrom
fix/hw-request-single-compose
Nov 5, 2026
Merged

jvsena42 merged 4 commits into
masterfrom
fix/hw-request-single-compose

Conversation

@jvsena42

@jvsena42 jvsena42 commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Fixes #1187
Twin: synonymdev/bitkit-android#1785

This PR makes a payment request funded from a hardware wallet run one compose instead of two, so the confirmation is ready about one compose (about 7 s on the Docker regtest setup) sooner.

Description

  • SendSheet: when Savings cannot cover a request, the hardware wallet is now chosen by composing the exact payment (largest balance first) instead of a send-max compose. A successful compose proves the wallet can pay and gives the fee. If it fails, the old max-spendable compose still runs so the insufficient-funds check is unchanged.
  • HwSendCoordinator: seedPreview stores that fee, and preparePreview reuses a stored fee while the wallet, address, amount, fee rate and wallet balance (standing in for the UTXO set) all match. Its own compose result is stored the same way, so a repeated preview for the same payment does not compose again. Changing any input composes again.
  • With several hardware wallets, the source is now the largest balance that can pay, where it was the largest max-spendable amount. The initial available amount on this path is balance minus this payment's fee, used only by the "over half your balance" warning.

Review round 1

  • The stored fee preview is keyed on the funding account's balance plus its transaction ids (HwWalletManager.fundingStateFingerprint), not the balance alone, so different coins at the same total compose again (1f8643f).
  • SendSheet.swift guard continuation lines are back to master's indentation; CI's SwiftFormat 0.64.0 rejected the 0.64.4 re-indent (c002e75).

Out of Scope

  • bitkit-core composeTransaction: each call opens a new Electrum client and fully syncs a fresh wallet (compose.rs:69-70). Reusing a synced wallet is a separate follow-up in bitkit-core.
  • Android: fixed in the twin PR.
  • SendFeeRate: opening the speed picker composes a fee for every speed, and its selection refreshes the max-spendable amount, so a speed change on a hardware send still runs several composes (about 10 in the device run). That path is unchanged here; reusing the stored fee there is a separate change.

Design

N/A — no UI changes.

Preview

N/A

QA Notes

Setup

  • Regtest against bitkit-docker, Trezor emulator paired through Trezor Bridge as in hardware-wallet.
  • A contact that can send payment requests; Savings smaller than the request, the Trezor wallet larger.

Journeys

N/A — not drivable; see Manual Tests.

Manual Tests

  • 1. Open a BTC payment request (1,000,000 sats) from a contact → the log shows one composeTransaction before the confirmation is ready, and the swipe is accepted about one compose after Confirm shows — compose timing from the app log is not in Capabilities
  • 2. Same as 1 with a USD-quoted request → one composeTransaction
  • 3. On the confirmation of 1, change the fee speed → the fee updates to the new speed — compose count from the app log is not in Capabilities
  • 4. Pay the request from 1 → the device signs and the payment broadcasts — regression check

Automated Checks

  • added HwWalletManagerFundingTests.swift — the funding state changes when the transactions change at the same balance, and ignores other address types
  • added HwFundingSignerTests.swift — a changed funding state composes the preview again
  • added HwFundingSignerTests.swift — a seeded fee is reused for the same payment; a changed fee rate composes again; repeated previews compose once; switching wallets drops the stored fee

Evidence

  • HwFundingSignerTests and SendConfirmationLifecycleTests on the iPhone 17 simulator (iOS 26.5) at 4cb45d0: 78 tests, 0 failures.

  • swiftformat --lint on the changed files: 0 files require formatting.

  • Device run at 0092df0, Debug E2E_BUILD, E2E_BACKEND=staging, regtest, iPhone 17e simulator (iOS 26.5), Trezor emulator over Bridge (Savings 0, Trezor 24,242,671 sats), requests from tools/paykit-fixture-sender (synonymdev/bitkit-e2e-tests):

    • 1. BTC 1,000,000 sats: request preparation finished 11:31:59.1, one composeTransaction (7.41 s) finished 11:32:06.9, and the confirmation showed the enabled Swipe To Pay arrow; no further compose in the next 20 s. A first request the same way also ran exactly one compose (8.32 s). The issue measured about 15 s with two composes.
    • 2. USD 600 at a fixed rate (150,000 sats): one composeTransaction (8.11 s), then ready to swipe.
    • 3. Normal (141 sats) → Fast: the fee updated to 423 sats at 3 sat/vB. The speed picker ran about 10 composes; see Out of Scope.
    • 4. Swiped the request from 1, approved on the emulator: the signing compose (7.22 s) ran, the device signed, and the sheet showed Bitcoin Sent for 1,000,000 sats to the requested address.
  • Round 1 at 1f8643f: HwFundingSignerTests, HwWalletManagerFundingTests, SendConfirmationLifecycleTests on the iPhone 17 simulator, 84 cases passed (xcresult).

Gaps

  • Runs used staging Electrum (where a compose takes about 7.4 s); not repeated on the local Docker Electrum.
  • Master was not re-run in the same session; the before figure is the issue's.

Models used

  • Planning/scoping: claude-opus-5-5 (reasoning: Not exposed)
  • Implementation: claude-opus-5-5 (reasoning: Not exposed)
  • Review: Not performed

🤖 Generated with Claude Code

jvsena42 and others added 2 commits November 5, 2026 07:07
Choosing the hardware wallet for a payment request now composes the exact
payment instead of a send-max, and hands its fee to the confirmation, which
reuses it while the wallet, address, amount, fee rate and balance match.
The confirmation no longer runs a second compose for the same payment.

Fixes #1187

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 5, 2026
@jvsena42
jvsena42 marked this pull request as ready for review November 5, 2026 12:44
@jvsena42
jvsena42 requested review from a team, ben-kaufman and pwltr and removed request for a team November 5, 2026 12:44
@greptile-apps

greptile-apps Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable new issues were found.

Summary

Hardware-funded requests reuse the fee from the compose that selected the wallet, avoiding a second compose before confirmation.

  • Hardware-funded requests reuse one compose for confirmation.

Reviews (2) · Last reviewed commit: "fix: key the hardware fee preview on the..." · Reviewed by Greptile

@ovitrif

ovitrif commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Placed in 3.2.0 with its twin: #1187 was already in the milestone, and the twin synonymdev/bitkit-android#1785 closes synonymdev/bitkit-android#1786.

@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. Composes a hardware-funded payment request once: selectHardwareFundingSourceIfNeeded now composes the exact payment per wallet, largest balance first, and seeds that fee into HwSendCoordinator. preparePreview then reuses it while the wallet, payment and balance are unchanged. Reviewed 0092df0, full tier. Static review: iOS does not build on this machine, so this is reasoned from the code and from CI. Twin: synonymdev/bitkit-android#1785.

CI is red because of this diff. SwiftFormat fails the indent rule on Bitkit/Views/Wallets/Send/SendSheet.swift:604-608, which are the guard let amount = try? request.payment(...) continuation lines this PR re-indented. The same lint passes on master, so restoring the previous indentation (or running swiftformat on the file) clears it. build-local failed earlier, at "Verify current PR merge checkout" ("Checked-out merge does not match the PR's current base and head"), so that run checked nothing in the code. Unit and integration tests were still running.

What I checked, and 4 candidates I ruled out

Read: the HwFundingSigner.swift diff with selectWallet, seedPreview, preparePreview and ComposedPreview; the whole selectHardwareFundingSourceIfNeeded loop in SendSheet.swift; every reader of hwSend.availableSats (SendAmountView, SendConfirmationView warnings, SendFeeRate, SendFeeCustom).

Ruled out

  • seedPreview racing selectWallet: seedPreview returns early unless selectWallet actually switched to that wallet. When selectWallet refuses (an operation is in flight), nothing is seeded and the confirmation composes as before.
  • Stale reuse: preparePreview compares wallet, request (address, sats, fee rate) and a balance read before composing. selectWallet clears the cache, so a switch or a fee-rate change composes again.
  • initialAvailableSats set to balance minus this payment's fee: the compose succeeding proves it covers amountSats. SendFeeRate and SendFeeCustom call refreshAvailable before the max-send path reads it.
  • A failed exact compose skipping a payable wallet: the error falls through to the existing maxSpendableFunding estimate for the same wallet, so the old selection still applies.

Merge confidence: 1/5, because CI fails on the change itself (the SwiftFormat lint on SendSheet.swift:604-608). Once that is fixed and the test jobs pass, nothing else here would hold it back.

Reviewer model: claude-opus-5-5
Reasoning effort: Unknown (the runner does not report the setting to the review session)

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

Reviewed commit 0092df0.

Potential fee-preview mismatch: preview reuse is keyed by payment details and total hardware-wallet balance, which does not identify the spending inputs. If the inputs change without changing the total, returning to confirmation for the same payment may retain an old fee preview while signing composes a different fee. This could make the app confirmation and fee warnings stale. Device approval still occurs. This remains a source-review candidate; it has not been reproduced at runtime.

Review details

General review: GPT-6.1 Sol / high.

@jvsena42
jvsena42 marked this pull request as draft November 5, 2026 14:11
jvsena42 and others added 2 commits November 5, 2026 11:11
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvsena42

jvsena42 commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

Review round 1, pushed at 1f8643f:

  • @talosmachina, SwiftFormat failure on SendSheet.swift:604-608: fixed in c002e75. Those guard continuation lines are back to master's indentation, so the PR diff no longer touches them. My local SwiftFormat is 0.64.4, and it re-indented them; CI pins 0.64.0. That commit was made with --no-verify, because the local 0.64.4 hook also rejects master's own indentation there.
  • @pwltr, fee preview keyed by balance: fixed in 1f8643f. The stored preview is now keyed on HwWalletManager.fundingStateFingerprint, which combines the funding balance with the sorted txids of the native-segwit watcher's transactions. Different coins at the same total change the key and compose again. Tests: HwWalletManagerFundingTests.testFundingStateChangesWithTransactionsAtTheSameBalance (same coins keep the state; a spend and top-up back to the same total change it; other address types are ignored) and HwFundingSignerTests.testCoordinatorPreviewComposesAgainWhenFundingStateChanges. Both are new tests against the new API, so there is no failing-before run. HwFundingSignerTests, HwWalletManagerFundingTests and SendConfirmationLifecycleTests: 84 cases passed (checked in the xcresult). Ported to fix: compose a hardware-funded request once bitkit-android#1785 in 2f09e7443.

@jvsena42
jvsena42 marked this pull request as ready for review November 5, 2026 15:41
@jvsena42
jvsena42 requested review from a team, pwltr and talosmachina November 5, 2026 15:41

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

Re-review of 1f8643f after 0092df0. 0 earlier findings, 0 new. The fee preview is now keyed on HwWalletManager.fundingStateFingerprint (the funding account's balance plus its sorted transaction ids) instead of the balance alone, and the SendSheet.swift guard is back to the indentation master has. Full tier, static: iOS does not build on this machine, so this is reasoned from the code and from CI.

CI: the SwiftFormat failure from my last review is fixed (SwiftFormat passes at 1f8643f). Run Tests, Run Integration Tests and build-local were still running when this was written.

What I checked, and 3 candidates I ruled out

Read: git diff 0092df0..1f8643f, the whole of fundingStateFingerprint next to fundingBalance, handleWatcherEvent (it replaces the watcher's transactionDetails on every event, so the fingerprint reads the current list), and both call sites. SendSheet.selectHardwareFundingSourceIfNeeded reads it before its compose, and so does HwSendCoordinator.preparePreview, so a change during the compose invalidates the result in both places.

The point @pwltr raised (same total, different coins): covered now. A spend plus a top-up back to the same total adds txids, so the fingerprint changes and preparePreview composes again. testFundingStateChangesWithTransactionsAtTheSameBalance and testCoordinatorPreviewComposesAgainWhenFundingStateChanges check this.

Ruled out

  • Hasher is seeded per process: the fingerprint lives only in memory, in ComposedPreview, and is compared within one run, never persisted.
  • A watcher event carrying a partial transactionDetails list: that changes the fingerprint and leads to an extra compose, never a stale fee. It fails safe.
  • A transaction confirming leaves txids and balance unchanged: the coin set is the same, and signing composes again before the device approves, so a stale preview could not reach the signed fee anyway.

Merge confidence: 4/5, no findings and the new tests cover the change, but the unit and integration runs at 1f8643f had not finished and nothing was built here.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the runtime does not report a setting for this session)

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

Reviewed 1f8643f against f7ecae9.

Looks good. The guard indentation is restored and SwiftFormat passes, which resolves my earlier Low finding. The reused hardware fee preview is now keyed on the funding account's balance and transactions, read before composing, so a different coin set recomposes. No new findings.

Review details

Claude Opus 5.5, effort high

jvsena42 added a commit to synonymdev/bitkit-android that referenced this pull request Nov 5, 2026
Choosing the hardware wallet for a payment request now composes the exact
payment instead of a send-max, and HwWalletRepo shares exact-fee composes
for the same payment and balance, so the confirm fee reuses it. Hardware
sends estimate only the selected speed; the other speeds are estimated
when the speed picker opens. The source selection no longer starts a fee
refresh that the confirm navigation cancels.

Twin of synonymdev/bitkit-ios#1221

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvsena42
jvsena42 merged commit 0e4783c into master Nov 5, 2026
42 checks passed
@jvsena42
jvsena42 deleted the fix/hw-request-single-compose branch November 5, 2026 18:24
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.

perf: hardware funded requests run two compose calls

4 participants