diff --git a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt index 8ceb509432..b183375a7f 100644 --- a/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt +++ b/app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt @@ -5470,8 +5470,20 @@ class AppViewModel @Inject constructor( return true } // Savings cannot cover it but a hardware wallet can, so pay from that instead of opening Amount. - if (!validateAmount(amount) && _sendUiState.value.hardwareWalletId == null) { - selectHardwareFundingSourceForAmount(amount, walletGeneration) + // The balance check above counts raw hardware balances, so no wallet may cover it once fees are taken. + if ( + !validateAmount(amount) && + _sendUiState.value.hardwareWalletId == null && + !selectHardwareFundingSourceForAmount(amount, walletGeneration) + ) { + toast( + type = Toast.ToastType.ERROR, + title = context.getString(R.string.other__pay_insufficient_savings), + description = context.getString(R.string.other__pay_insufficient_savings_description), + testTag = "InsufficientSavingsToast", + ) + clearActiveContactPaymentContext() + return true } if (!validateAmount(amount)) return false requireCurrentPaymentPreparation(walletGeneration) diff --git a/app/src/test/java/to/bitkit/viewmodels/AppViewModelSendFlowTest.kt b/app/src/test/java/to/bitkit/viewmodels/AppViewModelSendFlowTest.kt index d2701e3b08..f19870423c 100644 --- a/app/src/test/java/to/bitkit/viewmodels/AppViewModelSendFlowTest.kt +++ b/app/src/test/java/to/bitkit/viewmodels/AppViewModelSendFlowTest.kt @@ -9531,6 +9531,32 @@ class AppViewModelSendFlowTest : BaseUnitTest() { assertEquals(20_000uL, sut.sendUiState.value.amount) } + @Test + fun `main scanner onchain scan the hardware balance but not its spendable covers shows insufficient funds`() = + test { + val input = stubOnchainAmountScan(amountSats = 9_000u) + val onchainInfo = mock() + val btInfo = mock() + whenever(onchainInfo.feeRates).thenReturn(FeeRates(fast = 5u, mid = 3u, slow = 1u)) + whenever(btInfo.onchain).thenReturn(onchainInfo) + whenever(blocktankRepo.blocktankState).thenReturn(MutableStateFlow(BlocktankState(info = btInfo))) + balanceState.value = BalanceState(maxSendOnchainSats = 0u) + hwWallets.value = persistentListOf(hardwareWallet(fundingBalanceSats = 10_000uL)) + whenever(hwWalletRepo.maxSpendableFunding(any(), any(), any())).thenReturn(Result.success(8_000uL)) + whenever(hwWalletRepo.estimateFundingMiningFee(any(), any(), any(), any())) + .thenReturn(Result.failure(Exception("insufficient funds for amount and fee"))) + + sut.showScannerSheet() + sut.onScannerSheetResult(input) + advanceUntilIdle() + + assertFalse(sut.currentSheet.value is Sheet.Send) + verify(hwWalletRepo, atLeast(1)).estimateFundingMiningFee(any(), any(), eq(9_000uL), any()) + val toasts = argumentCaptor() + verify(toastManager, atLeast(1)).enqueue(toasts.capture()) + assertTrue(toasts.allValues.any { it.testTag == "InsufficientSavingsToast" }) + } + @Test fun `speed change keeps a scanned hardware amount that equals the hardware maximum`() = test { val input = stubOnchainAmountScan(amountSats = 48_000u) diff --git a/changelog.d/next/1790.fixed.md b/changelog.d/next/1790.fixed.md new file mode 100644 index 0000000000..a8b35187ec --- /dev/null +++ b/changelog.d/next/1790.fixed.md @@ -0,0 +1 @@ +Show an insufficient funds message when a scanned Bitcoin request is more than a hardware wallet can pay after fees, instead of opening an amount screen that cannot continue. diff --git a/journeys/README.md b/journeys/README.md index 5770c36ac8..217bca25e7 100644 --- a/journeys/README.md +++ b/journeys/README.md @@ -151,6 +151,7 @@ Known differences in the corpus, as of the iOS port (synonymdev/bitkit-ios#691): | `hardware-wallet/usb-reconnect.xml` | `reconnect.xml` — over Bridge, since iOS cannot do WebUSB | | `cjit-notifications/non-cjit-channel-no-payment-notification.xml` | not ported — iOS dropped its CJIT notification suite with the Blocktank sunset; this one checks Android's channel-ready notification on a Manual Setup channel | | `hardware-wallet/receive-onchain.xml`, `hardware-wallet/send-onchain.xml` | not ported | +| `hardware-wallet/scan-onchain-amount-over-spendable.xml` | ported with synonymdev/bitkit-ios#1228 — iOS opens Confirm and switches to the hardware wallet there, then shows Insufficient Savings and closes the sheet when its fee preview fails | | `activity/date-range-rapid-month-taps.xml` | not ported — iOS has no activity journey suite, and the rapid month tap behaviour was not checked there | | `coin-selection/manual-coin-selection.xml` | not ported — iOS has the screen (`SendUtxoSelectionView`) but no accessibility identifiers on it yet | | `coin-selection/manual-wallet-switch.xml` | same file, journey name and actions; iOS can open the picker on the wallet switch, Android on the next swipe. The iOS picker has no accessibility identifiers, so assert its visible "Coin Selection" title. | diff --git a/journeys/hardware-wallet/README.md b/journeys/hardware-wallet/README.md index ae12fd1e29..f5147f2c90 100644 --- a/journeys/hardware-wallet/README.md +++ b/journeys/hardware-wallet/README.md @@ -77,6 +77,7 @@ the other three rely on, and `passphrase-settings-remove.xml` removes it again. | `settings-hardware-wallets.xml` | Payments count row, Hardware Wallets screen list, rename sheet, Add button sheet/back dismiss, per-row delete confirm + re-pair | | `detail-overview.xml` | Detail screen overview, Transfer placeholder when funded, activity, Remove confirm + forget | | `send-onchain.xml` | Normal Send flow funded by Trezor: source selection, guarded preparation, device signing, broadcast, success, and activity | +| `scan-onchain-amount-over-spendable.xml` | Scanned on-chain amount the Trezor balance covers but not after fees: Insufficient Savings toast instead of a locked Amount screen | | `receive-onchain.xml` | Trezor Receive tab: current address and QR display plus exact on-device address verification | | `passphrase-pairing.xml` | Passphrase button on Paired → Enter Passphrase → Passphrase Funds Found; second home tile, own label, no passphrase in logs | | `passphrase-duplicate.xml` | Re-entering a watched passphrase reports "already added" and adds no tile | diff --git a/journeys/hardware-wallet/scan-onchain-amount-over-spendable.xml b/journeys/hardware-wallet/scan-onchain-amount-over-spendable.xml new file mode 100644 index 0000000000..a617825d1f --- /dev/null +++ b/journeys/hardware-wallet/scan-onchain-amount-over-spendable.xml @@ -0,0 +1,22 @@ + + + Verifies that an on-chain BIP21 request whose amount a paired hardware wallet's balance covers, + but not once the network fee is taken, shows the Insufficient Savings toast instead of opening + the Send Amount screen with a fixed amount that cannot be changed or continued. Nothing is + broadcast. + Precondition: an onboarded dev wallet with 0 sats in Savings and a paired Bridge emulator (see + README.md) whose native-segwit account holds a confirmed regtest balance B of at least 10 000 + sats, read from its home tile. Use an amount between B minus 100 sats and B, which the balance + covers but the balance minus any non-zero fee does not, and a regtest address the wallet does + not own. Start on the wallet home screen. + The Send Amount screen is identified by its number field (testTag "SendNumberField"), the + Confirm screen by its amount (testTag "ReviewAmount"). + + + Read the paired Trezor's balance B from its home tile and pick an amount A with B - 100 < A < B, written in BTC + Run adb shell am start -a android.intent.action.VIEW -d "bitcoin:<regtest address>?amount=<A>" to.bitkit.dev + Verify the Insufficient Savings toast (testTag "InsufficientSavingsToast") appears (screenshot), and neither the Send Amount screen (testTag "SendNumberField") nor the Confirm screen (testTag "ReviewAmount") opens + Pick an amount below B by at least 2 000 sats and run the same deep link with it + Verify the Confirm screen opens with the Trezor as the funding source and the amount shown (testTag "ReviewAmount"); close the sheet without sending + +