Skip to content

fix: show insufficient savings when the hardware fallback cannot pay the fee - #1228

Merged
jvsena42 merged 6 commits into
masterfrom
fix/scan-unaffordable-hardware-amount
Nov 6, 2026
Merged

jvsena42 merged 6 commits into
masterfrom
fix/scan-unaffordable-hardware-amount

Conversation

@jvsena42

@jvsena42 jvsena42 commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Closes #1229
Twin: synonymdev/bitkit-android#1790

Scanning an on-chain request with Savings unable to pay, for an amount a paired hardware wallet's balance covers but not with the network fee, left the Confirm screen on the hardware wallet. Its fee preview failed with "Please try again. Failed to compose hardware transfer", and the swipe stayed disabled. Android's twin fixes the same check.

Description

  • SendConfirmationView: when the hardware wallet Confirm switched to because Savings could not pay fails its fee preview, and the compose reports the shortfall or the amount leaves less than the fee reserve, show the Insufficient Savings toast and close the sheet. This is the same result as a scan with no wallet that can pay. Any other preview failure (Electrum unreachable, a missing account) keeps master's Try again handling, and so does a hardware wallet the user picks. The check reuses the preview Confirm already runs, so the path where the hardware wallet can pay gets no extra compose.
  • SendConfirmationView.hardwareWalletCovering: fall back to the hardware wallet with the largest balance, as Android does, instead of the first one whose balance covers the amount.
  • New journey scan-onchain-amount-over-spendable.xml, ported from Android.

Out of Scope

  • AppViewModel.validateOnchainBalance: the scan-time check still compares against the raw hardware balance. A pre-check there would add a 12–15 s hardware compose to every scan the hardware wallet can pay.
  • A fallback preview that fails for a reason other than funds still closes the sheet after Try again, as on master (verified with Electrum unreachable).
  • With several hardware wallets, only the largest is tried; a smaller wallet that could pay a fee the largest cannot is not tried (declined as marginal in review).

Design

N/A — no UI changes; reuses the existing Insufficient Savings toast.

Preview

See Evidence.

QA Notes

Setup

  • Dev Debug build with TREZOR_BRIDGE=true TREZOR_BRIDGE_URL=http://127.0.0.1:21325, staging regtest.
  • Trezor User Env emulator from bitkit-docker, paired as in journeys/hardware-wallet/README.md.
  • Wallet with 0 sats in Savings. The Trezor's native-segwit account holds a confirmed balance B; here B was 19,928,006 sats.

Journeys

  • J1: new scan-onchain-amount-over-spendable.xml — an amount the hardware balance covers but not with the fee shows Insufficient Savings and closes the sheet; an amount it can pay opens Confirm from the Trezor with a fee

Manual Tests

N/A

Automated Checks

  • updated SendConfirmationSwipeTests.swift — a failed preview of the Savings fallback wallet counts as Insufficient Savings only when the compose reports the shortfall or the amount leaves less than the fee reserve; a wallet the user picks keeps Try again; the fallback is the largest covering wallet
  • ran SendConfirmationSwipeTests.swift — 24 tests, 0 failures

Evidence

J1 at 0a6e338, iPhone 17 simulator (iOS 26.5), Bridge Trezor with B = 19,928,006 sats, Savings 0:

Build Scan Result
master 2d5744b B − 30 Confirm switches to the Trezor; "Please try again. Failed to compose hardware transfer" after about 16 s; stays on Confirm with the swipe disabled and no fee
this PR B − 30 Insufficient Savings toast; sheet closes 13.7 s after the scan (the Trezor compose takes 13.2 s)
this PR B − 20,000 Confirm from the Trezor, 209-sat fee, swipe enabled, 17 s after the scan; closed without sending

Round 2, at 1df0d9a (d1578e1 changes only the multi-wallet pick): B − 30 shows Insufficient Savings and the sheet closes in 15 s. B − 20,000 reaches Confirm with a 209-sat fee in 18 s. With the app's Electrum unreachable, B − 20,000 shows "Please try again. Failed to compose hardware transfer", as master's code does in the same setup (#1228 (comment)).

Gaps

  • Not run on a physical device or over BLE; the change is transport-independent.
  • The largest-wallet pick (d1578e1) is covered by a unit test only: the Bridge setup has one emulator.
  • The compose-shortfall check (9eafe12), for a fee above the reserve, is covered by a unit test only. The shared-seed Bridge Trezor was being spent by another session, which swept the small UTXOs the device case needs.

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 19:07
…the fee

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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvsena42

jvsena42 commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

J1 evidence (iPhone 17 simulator, Bridge Trezor B = 19,928,006 sats, Savings 0).

master 2d5744b, scan B − 30: Confirm switches to the Trezor, then shows "Please try again. Failed to compose hardware transfer" and stays with the swipe disabled (last frame).

This PR, scan B − 30: Insufficient Savings toast, and the sheet closes 13.7 s after the scan.

This PR, scan B − 20,000: Confirm from the Trezor, 209-sat fee, swipe enabled.

Recordings: master pr_master_over.mp4 · this PR pr_fix_over.mp4

@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, with no new actionable issues found.

Summary

The PR shows Insufficient Savings and leaves Confirm when the hardware wallet chosen after Savings fails cannot cover the fee.

  • Scanned payments that exceed Savings but not hardware spendable funds now close with an Insufficient Savings message.

Reviews (3) · Last reviewed commit: "fix: treat a compose shortfall above the..." · Reviewed by Greptile

@ovitrif

ovitrif commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Placed in 3.2.0 with its twin: a non-blocking fix found after the 3.1 cut. I filed #1229 to track it under the milestone and added Closes #1229 to this PR. The twin synonymdev/bitkit-android#1790 closes synonymdev/bitkit-android#1793.

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

1 finding (1 P1). When Savings cannot pay a scanned fixed amount and Confirm falls back to a hardware wallet whose fee preview then fails, this shows Insufficient Savings and closes the sheet instead of Try again. Reviewed 95af2fd, full tier, static: per the repo's Bitkit track rules an auto review does not build bitkit-ios, and iOS does not build on Linux.

What I checked, and 4 candidates I ruled out

Read in full: SendConfirmationView.swift (fallback selection, selectFundingSource, calculateTransactionFee, leaveAfterPreparationFailure, the hwSend.walletId onChange), HwFundingSigner.preparePreview, HwWalletManager.estimateOfflineFundingMiningFee and composeFundingTransactionInternal, bitkit-core onchain/compose.rs, the twin synonymdev/bitkit-android#1790.
CI: SwiftFormat, validate, build-local green; Run Tests, integration and e2e still pending at review time.

Ruled out

  • Stale fallback id after a manual switch: selectFundingSource clears it first, and the fallback path sets it after the call, so a user-picked source never carries it.
  • Superseded or cancelled preview hitting the new branch: the catch returns early on !isCurrentSend() and CancellationError, and a successful preview clears the id.
  • Toast copy and identifier: other__pay_insufficient_savings and its description exist, and InsufficientSavingsToast matches the Android tag the shared journey asserts.
  • leaveAfterPreparationFailure with a fixed amount: canEditAmount is false for a BIP21 amount, so it pops Confirm or hides the sheet, which matches J1's expected result.
  • Cold reader: the finding stood; it rated it P2. I kept P1 because the wrong outcome is a user told they lack funds and the sheet closed on a transient network failure.

Merge confidence: 3/5, one P1 on the error path of the fallback wallet's preview.

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

@jvsena42
jvsena42 requested review from a team, ben-kaufman and pwltr and removed request for a team November 5, 2026 23:09

@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 95af2fd against 2d5744b.

When the automatic Savings-to-hardware fallback cannot cover the fee, Confirm now shows Insufficient Savings and leaves instead of a generic error with a disabled swipe; a manual switch or a successful preview clears the fallback marker. One Low finding:

Low: hardwareWalletCovering still returns the first hardware wallet whose balance covers the amount, in stored order. With several devices, a smaller one can be chosen, fail on the fee, and end the send as Insufficient Savings although a larger wallet could pay. Consider choosing the wallet with the largest funding balance (as Android does) or trying the next covering wallet first.

Review details

Claude Opus 5.5, effort high

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

Potential Medium: establish insufficient funds before choosing the fallback error. The new catch branch treats every preview failure for the automatically selected hardware wallet as insufficient Savings. The marker only checks wallet identity, while preview preparation can also fail for account, backend or transaction-composition errors. A funded wallet can therefore receive a misleading “more bitcoin is needed” message and leave confirmation without its actionable error. Use a verified affordability failure before applying that toast/navigation policy; preserve ordinary error handling when affordability is unknown. This is a candidate from static source tracing, not a reproduced failure.

Review details

General review
Model: GPT-6.1 Sol
Reasoning effort: high

@jvsena42
jvsena42 marked this pull request as draft November 6, 2026 08:58
jvsena42 and others added 2 commits November 6, 2026 06:05
…each

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvsena42

jvsena42 commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Round 2 evidence. iPhone 17 simulator, Bridge Trezor B = 19,928,006 sats, Savings 0. The app at 1df0d9a (d1578e1 adds only the multi-wallet pick, which can't be driven with one emulator).

Electrum down, scan B − 20,000 (the Trezor can pay). The app's Electrum server pointed at a local proxy, which I killed after the balance synced. The preview fails with Electrum connection failed: … Connection refused.

  • This PR: "Please try again. Failed to compose hardware transfer", not Insufficient Savings.
  • master's SendConfirmationView, same setup: the same toast, and the sheet also closes. So the closing in this case is pre-existing, and this PR now matches master here.

Scan B − 30: Insufficient Savings toast; the sheet closes 15 s after the scan.

Scan B − 20,000, Electrum up: Confirm from the Trezor, 209-sat fee, swipe enabled, 18 s after the scan.

Recording of the Electrum-down run: r2_fix_electrum_down.mp4

The simulator's Electrum setting was reset to default afterwards.

@jvsena42

jvsena42 commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Replies to pwltr's reviews:

Potential Medium (insufficient funds before the fallback error): confirmed, the same finding as the thread above. Fixed in 1df0d9a: the toast is gated on the amount exceeding the balance minus the fee reserve, and any other preview failure keeps Try again. Device proof with Electrum unreachable: #1228 (comment)

Low (first covering wallet, not the largest): confirmed, and in scope. On master a failed fallback left Confirm open for a manual switch; this PR ends the send, so picking a smaller wallet first is now worse. Fixed in d1578e1: Confirm falls back to the wallet with the largest balance, as Android's sortedByDescending { it.fundingBalanceSats } does. A smaller wallet can't pay what the largest can't, so only one preview runs. testAFixedAmountSavingsCannotPayMovesToTheLargestHardwareWallet fails on the first-match code and passes now. Not driven on a device: the Bridge setup has a single emulator.

@jvsena42
jvsena42 marked this pull request as ready for review November 6, 2026 09:21
@jvsena42
jvsena42 marked this pull request as draft November 6, 2026 09:26

@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 d1578e1 after 95af2fd. 1 earlier finding: fixed. 0 new findings. When Savings cannot pay a scanned fixed amount, Confirm now falls back to the paired hardware wallet with the largest balance. A failed fee preview of that wallet shows Insufficient Savings and closes the sheet only when the amount leaves less than HwFundingSigner.feeReserve of its balance. Full tier, static: iOS does not build on Linux, so Run Tests is the gate for SendConfirmationSwipeTests.

What I checked, and 3 candidates I ruled out

Earlier finding

  • Insufficient Savings for any preview failure: fixed in 1df0d9ad0. isSavingsFallbackShortOfFee now also requires amountSats > balanceSats - feeReserveSats, and balanceSats < feeReserveSats is checked first so the subtraction cannot underflow. An Electrum or account failure on an amount with room for the fee takes the Try again branch, which the author showed on a simulator with Electrum unreachable. Electrum failing on an amount that is within the reserve of the balance (1,200 vB at the confirmation rate) still reads as Insufficient Savings. That window is narrow, and in it the funds really are near the limit.

Read in full: SendConfirmationView.swift at d1578e1 (selectFundingSource, the UTXO-selection fallback, calculateTransactionFee and its catch, the hwSend.walletId onChange), HwFundingSigner.feeReserve, HwWalletManager.fundingBalance, SendConfirmationSwipeTests.swift.
CI at d1578e1: SwiftFormat, validate, Greptile green; Run Tests, Run Integration Tests and build-local pending at review time.

Ruled out

  • Largest wallet fails where a smaller one could pay (Greptile): this needs two paired hardware wallets, the amount inside the larger one's fee reserve but payable by the smaller one, and the larger one's coins fragmented enough to lose its fee lead. Outside that, a failed preview of the largest wallet keeps Try again with Confirm open (afterFundingSwitch: true), and the user can switch source. The pick matches the Android twin's sortedByDescending { it.fundingBalanceSats }.
  • Reserve smaller than a many-input fee (Greptile): this happens on a fallback wallet whose fee needs more than 1,200 vB. There it gives master's behaviour (Try again, Confirm stays open). So the gap is a limit on how far this fix reaches, not a regression it adds.
  • Fee rate mismatch between the switch and the catch: the catch runs only after confirmationFeeRate is non-nil, so feeReserve takes its rate-times-vbytes branch, which is the same reserve the funding switch uses for initialAvailableSats.

Merge confidence: 4/5, no open findings, but the money path is covered by unit tests that Run Tests has not finished yet and the largest-wallet pick was not driven on a device.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the run metadata available to this reviewer does not report it)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvsena42

jvsena42 commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Correction to my round-2 evidence comment (#1228 (comment)): the simulator's Electrum setting was not reset then. The reset was not saved. I reset it now and confirmed it in the app log: Saved Electrum server config: ssl://electrs.bitkit.stag0.blocktank.to:9999.

@jvsena42
jvsena42 marked this pull request as ready for review November 6, 2026 10:20
@jvsena42
jvsena42 requested a review from talosmachina November 6, 2026 10:20

@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 9eafe12 after d1578e1. Earlier findings: none open. 0 new findings. A failed fee preview of the Savings fallback hardware wallet now also counts as Insufficient Savings when the compose itself reports the shortfall, which covers a fee above the 1,200 vB reserve that Greptile raised. Full tier, static: iOS does not build on Linux, so Run Tests is the gate for SendConfirmationSwipeTests.

What I checked, and 3 candidates I ruled out

Earlier points

  • Insufficient Savings for any preview failure (my P1 at 95af2fd): still fixed. The new text match sits after the fallback-id guard, so a wallet the user picked keeps Try again whatever the error says, and the test asserts that with the shortfall text (fallback: nil).
  • Reserve smaller than a many-input fee (Greptile P2, which I had ruled out as master's behaviour rather than a regression): now covered. Traced the text end to end: bitkit-core 0.8.0-rc11 (the Package.resolved pin) builds the PSBT with bdk 0.30.2, finish_psbt maps builder.finish() errors with format!("{}", e), and bdk's Error::InsufficientFunds displays as Insufficient funds: {} sat available of {} sat needed. composeFundingTransactionInternal puts the first ComposeResult.error into AppError.debugMessage, and neither HwFundingSigner.estimateOfflineFundingMiningFee nor preparePreview rewraps it, so (error as? AppError)?.debugMessage reaches the check intact.

Read in full: the diff d1578e1..9eafe12, calculateTransactionFee and its catch, isSavingsFallbackShortOfFee, HwWalletManager.composeFundingTransactionInternal and estimateOfflineFundingMiningFee, HwFundingSigner.preparePreview, AppError, bitkit-core onchain/compose.rs at the pinned revision, bdk 0.30.2 error.rs.
CI at 9eafe12: SwiftFormat, validate, Greptile green; Run Tests, Run Integration Tests and build-local pending at review time. At d1578e1 Run Tests was cancelled by the next push, so no head since 95af2fd has a finished unit run yet.

Ruled out

  • Text match breaks on a core bump: the string is bdk's, not bitkit-core's, and bitkit-core's own test_compose_insufficient_funds asserts only the Error variant. A bump that changes the text falls back to the reserve check, which is the d1578e1 behaviour, not a regression.
  • A non-funds failure carrying the phrase: Electrum, account and fee-rate failures come out of compose_inner as AccountInfoError text or the Fee rate must be... message; none contains insufficient funds.
  • Compose shortfall on coins the balance counts but the compose cannot spend: the result is the honest one, since this wallet cannot pay the amount, and before this commit the same case read as Try again with Confirm left open.

Merge confidence: 4/5, no open findings; the change is small and the error text is traced to its source, but Run Tests has not finished on any head since 95af2fd and the compose-shortfall case was not driven on a device.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the run metadata available to this reviewer does not report it)

@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 9eafe12. Two Medium candidates from static source traces; neither has runtime reproduction or completed follow-up verification.

Medium candidate — Check spendability before choosing the largest wallet. The automatic fallback selects the highest gross balance, which can be fragmented enough to fail payment-plus-fee selection while a smaller wallet can pay. A failed preview dismisses confirmation without trying the payable alternative. See SendConfirmationView.swift:1217–1220 and 2200–2216.

Medium candidate — Preserve network failures when affordability is unknown. The new insufficient-funds predicate can classify an Electrum connection failure as a funds shortfall when headroom is below the conservative 1,200-vbyte reserve, even though the actual transaction fee could fit. See SendConfirmationView.swift:1238 and 2200–2216.

Review details

General review
Model: GPT-6.1 Sol
Reasoning effort: high

@jvsena42

jvsena42 commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

Replies to pwltr's review of 9eafe12 (#1228 (review)). No code change for either.

Medium: check spendability before choosing the largest wallet. Declined as marginal, the same case as Greptile's thread (#1228 (comment)).

  • It needs two paired hardware wallets, a scanned amount the smaller one can pay, and a largest wallet whose extra fee from fragmented coins exceeds its balance lead over the smaller one.
  • Trying each covering wallet in turn adds a 12–15 s compose per wallet before Confirm settles.
  • If this case comes up, the fix is to move to the next covering wallet when the compose reports a shortfall.
  • Android twin: no change needed. selectHardwareFundingSourceForAmount already composes each wallet in descending balance order and takes the first that pays.

Medium: an Electrum failure inside the reserve reads as a shortfall. The case can happen. Declined, because the fix removes the guard that the main fix relies on.

  • The case: the fallback wallet's preview fails for a non-funds reason and the amount is within 1,200 vB × fee rate of its balance. That is 2,400 sats at 2 sat/vB.
  • The result: the "Insufficient Savings" toast instead of "Please try again". The sheet closes in both cases. The Electrum-down device run shows the Try again path closing the sheet for the fallback wallet, as on master (fix: show insufficient savings when the hardware fallback cannot pay the fee #1228 (comment)). So the only difference is the toast text, and only for an amount near the wallet's limit.
  • Every real shortfall in this preview already comes from the compose as bdk's Insufficient funds: X sat available of Y sat needed. preparePreview → estimateOfflineFundingMiningFee → composeFundingTransactionInternal has no check before the compose. So the reserve branch only takes effect if a bitkit-core bump changes that text. Without it, that bump would bring back this PR's bug: Confirm locked on a failing preview with the swipe disabled. A wrong toast text inside a few-thousand-sat window costs less than losing that guard.
  • Android twin: same behavior. When both the exact compose and max-spendable fail, hardwareMaxSpendable falls back to hardwareEstimatedAvailable, which is balance minus HW_SEND_FALLBACK_TX_VBYTES (1,200) × rate. An amount inside that reserve also shows Insufficient Savings. The two platforms match.

Leaving both for you to decide.

@jvsena42
jvsena42 requested a review from pwltr November 6, 2026 11:57

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

The earlier preview-error and fee-shortfall issues are fixed. The remaining multiple-wallet and near-balance error cases are accepted tradeoffs.

I did not rerun local app tests or J1. Physical-device and BLE flows remain unverified. Both independent security passes are incomplete after provider authentication failures.

Reviewer model: claude-opus-5-5, gpt-6.1-sol
Reasoning effort: high for both

@jvsena42
jvsena42 merged commit 90949e4 into master Nov 6, 2026
42 checks passed
@jvsena42
jvsena42 deleted the fix/scan-unaffordable-hardware-amount branch November 6, 2026 12:41
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.

bug: scan opens an unpayable hardware confirm when no wallet covers the fee

5 participants