Skip to content

fix: hold hardware sends before broadcasting - #1772

Draft
ben-kaufman wants to merge 14 commits into
masterfrom
fix/held-hardware-broadcasts
Draft

ben-kaufman wants to merge 14 commits into
masterfrom
fix/held-hardware-broadcasts

Conversation

@ben-kaufman

Copy link
Copy Markdown
Contributor

Twin: synonymdev/bitkit-ios#1210

This PR keeps a signed hardware-wallet send whose broadcast may have gone out, so the same payment is never signed or sent a second time.

Description

  • Stops waiting for a hardware broadcast after 60 s without cancelling it. The send then shows on Send Pending with its txid and unknown-outcome copy, and its late answer still lands: an accepted broadcast stays pending until the wallet sees the transaction, and a late answer proving nothing was sent releases the send with a "Payment not sent" toast and closes the sheet. A Shop payment keeps its existing guarded sheet, and its late answer is delivered as before.

  • Holds a plain hardware send whose broadcast failed after dispatch without proof that nothing was sent, instead of letting it be cancelled and signed again. A first broadcast that core could not decode or connect for, or that the only server to receive it refused, still proves nothing went out and releases it: an Electrum server and its Bitcoin Core node keep nothing they refused (min relay fee, mempool min fee, non-final, txn-mempool-conflict), and a missing coin there is one already spent.

  • Writes every plain hardware send to its own store before broadcasting, and does not broadcast when that write fails. A held send survives closing the sheet and restarting the app.

  • Scopes the hold to its payment (wallet, address and amount, address case ignored). Other payments from any wallet can still be signed, and removing a wallet drops its in-memory hold, while the stored hold guards the payment again once the wallet is back.

  • Adds Broadcast again on Send Pending, which re-writes the hold and broadcasts the identical stored bytes, never signing again. Making the same payment again from any sheet does the same. An accepted broadcast shows an info toast and stays pending.

    • Only one dispatch of the same bytes runs at a time across sheets; while one is in flight the button is disabled and a toast explains why.
    • A broadcast again whose transaction the wallet has seen meanwhile is completed as sent instead of dispatched, and a proof that nothing was sent never releases a send the wallet has seen.
    • A dispatch that has not answered after 2 minutes counts as possibly sent, which re-enables Broadcast again; a possibly sent dispatch never releases a hold, and resending identical bytes is harmless.
  • Releases a held send only on proof it can never confirm, and only while the wallet has not seen its txid:

    • a consensus or script refusal (mandatory- or block-script-verify-flag-failed and the bad-txns consensus checks);
    • bytes core cannot decode;
    • a missing-coin refusal when a fresh Electrum read shows the coin spent by another transaction with 6 or more confirmations.

    Fee, mempool, dust, standardness, conflict and unreadable refusals keep the hold, with a toast saying the payment may still confirm. A late proof that nothing was sent releases a hold only while no other dispatch of the same bytes may have sent it.

  • Records a held send as sent once the wallet sees its transaction (a replaced transaction, which no longer exists, does not count): the activity and its tags first, then the hold is released, then the success screen shows if its pending screen is open. Stored holds the wallet has seen are also released as watcher activity arrives and at startup, keeping their tags, so a later deliberate repeat of the same payment is a new one. A completion while the sheet is closed is never replayed later as a stale success.

  • Asks "This payment was already sent on . Send it again?" when the same payment is made while a seen hold has not been released yet. Send again signs a new payment; View payment shows the one already sent.

  • Fails closed on unreadable hold records. A record that cannot be read is kept as it is, read again by every later read so a newer app version can recover it, and refuses sends from its wallet; a record whose wallet cannot be read, or a file that cannot be read at all, refuses every hardware send. The sign sheet shows what the record still says (amount, address, txid, date), then offers Clear record, or Clear for all wallets when the whole file is concerned, in destructive styling with a second confirmation that says exactly what is deleted (this wallet's unreadable records, those whose wallet cannot be read, or every record of all wallets when the file cannot be read) and that ignores taps for its first second. Nothing is ever reset silently.

  • Clears held sends with the rest of the wallet on a wipe; a clear that fails fails the wipe.

Out of Scope

  • Hardware send: a held send refused only for relay policy (for example dust, or a fee too low for every mempool) stays pending until the wallet sees it or its coins are spent elsewhere. The way out is a user-confirmed Replace payment that signs a conflicting transaction reusing one of its coins; that is a separate follow-up. Until it lands, such a payment cannot be made again from that wallet.
  • Hardware transfer funding: the held-funding guard for transfer orders is not ported, because transfers to spending were removed from master.
  • Send-time guards (such as signing without a known fee rate) and the stuck on-chain cancel flow are covered by separate PRs.
  • Shop (Paykit) hardware payments keep their existing retained-candidate guard and guarded sheet after the wait; only their late answer is now delivered.
  • Hardware device call timeouts during connect, compose and sign are unchanged.

Design

N/A — no design available. Send Pending maps to Send (Edge cases) › Transaction pending Lightning in docs/screens-map.md; the hardware unknown-outcome copy, the Broadcast again button and the unreadable-record dialog reuse existing layouts and have no frame of their own.

Preview

N/A

QA Notes

Setup

  • Dev (regtest) debug build of this branch on an emulator, with the Trezor emulator from bitkit-docker paired over Bridge as described in journeys/hardware-wallet/README.md, and on-chain funds sent to the hardware wallet with ./lsp.
  • The cases below need the broadcast to stall or fail after it reached Electrum (for example by pausing or dropping the Electrum connection while the broadcast is in flight), which the journey environment cannot inject.

Journeys

N/A — not drivable; see Manual Tests.

Manual Tests

  • 1. Send on-chain from the hardware wallet, sign on the device, then stall Electrum so the broadcast does not answer for 60 s → Send Pending shows the txid, the hardware copy and Broadcast again; Broadcast again is disabled until the first broadcast answers — broadcast fault injection not in Capabilities.
  • 2. After 1, restore Electrum and tap Broadcast again → an info toast says the network accepted the payment; once the wallet sees the transaction, the success screen shows and the activity is recorded once — broadcast fault injection not in Capabilities.
  • 3. Break the Electrum connection right after a broadcast is written, then start the same payment (wallet, address, amount) again from a new send sheet → it goes to Send Pending and broadcasts the same bytes, without asking the device to sign — broadcast fault injection not in Capabilities.
  • 4. After 3, kill and relaunch the app and repeat the same payment → it is broadcast again as the same bytes, not signed; once the wallet has seen it, repeating the payment shows it as sent instead of signing a new one — broadcast fault injection not in Capabilities.
  • 5. With a send held, make a different payment from the same wallet → it signs and sends normally — broadcast fault injection not in Capabilities.

Automated Checks

  • added HwSendViewModelTest.kt — a broadcast unanswered for 60 s lets the sheet go to Send Pending, keeps its hold, and completes only once the wallet sees it, recording the activity before the release
  • added HwSendViewModelTest.kt — a late answer proving nothing was sent releases the send, unless another dispatch of the same bytes may have sent it; a late answer that may have gone out keeps it
  • added HwSendViewModelTest.kt — a lost or failed broadcast keeps the signed transaction, and the same payment is broadcast again as the same bytes, never signed again, while other payments and other wallets can still be signed
  • added HwSendViewModelTest.kt — Broadcast again re-writes the hold first, runs one dispatch at a time across sheets, stays pending when accepted, keeps the hold on fee, dust or unreadable refusals, and releases only on a consensus refusal or a final spend of a coin by another transaction
  • added HwSendViewModelTest.kt — a send is held before its broadcast, an unwritable or unreadable hold store blocks signing and broadcasting, and the unreadable-record dialog clears only on request
  • added HwSendViewModelTest.kt — a held payment the wallet has seen is recorded, then shown as sent or, on Send again, signed anew; a replaced transaction is not seen; removing a wallet forgets its in-memory hold; a completion while no sheet listens is not replayed
  • added HwSendViewModelTest.kt — a broadcast that never answers stops blocking Broadcast again after 2 minutes, a refused Broadcast again says why, and a Broadcast again the wallet saw meanwhile completes as sent without a dispatch
  • added HwSendViewModelTest.kt — an unreadable record shows what it still says and is cleared only after a second confirmation, for all wallets when the whole file is concerned
  • added HwHeldDispatchTrackerTest.kt — one dispatch of the same bytes at a time, expiry counts as possibly sent, and finished dispatches are pruned
  • added HwSendViewModelTest.kt — a Shop broadcast unanswered for 60 s keeps its sheet guarded and its late answer still lands
  • added HwHeldSendStoreTest.kt — an unreadable file fails every read and write until cleared; an unreadable record is kept through writes, read again later, reports what it still says, and refuses only its wallet, or every wallet when its wallet cannot be read
  • added HwWalletRepoTest.kt — a held send is found only for its own payment, unreadable records refuse sends, a seen hold is tagged and released unless its transaction was replaced, a wipe clears held sends or fails, and only a 6-confirmation spend of a coin by another transaction proves a held send can never confirm
  • added BroadcastExceptionExtTest.kt — only undecodable bytes or a failed connect prove nothing was sent; only undecodable bytes or a consensus or script refusal in the server's JSON prove a transaction can never confirm
  • added SignedTransactionIdTest.kt — reads the coins a signed transaction spends
  • updated HwSendViewModelTest.kt and PaykitPaymentProofRepoTest.kt — connectivity failures use core's real Electrum connect prefix, and the view model takes the shared dispatch tracker

Evidence

  • On 416466178: just compile, just test (4904 tests, 0 failures), just lint and ./gradlew compileDevReleaseKotlin compileDevDebugAndroidTestKotlin passed.
  • The first broadcast-timeout test and the three broadcast-wait tests failed against the code before their fixes. The unwritable-hold test fails when the hold write's failure is ignored. The dispatch-race tests were written before the shared dispatch tracker, which they need in order to compile. The test for a broadcast again the wallet saw meanwhile failed before its fix.

Gaps

  • Manual tests 1–5 not run: no hardware wallet broadcast fault injection in this environment.
  • Device checks not run.

Models used

  • Planning/scoping: Opus 5.5 (reasoning: Not exposed)
  • Implementation: Opus 5.5 (reasoning: Not exposed)
  • Review: Opus 5.5 (reasoning: Not exposed)

Metadata note: the session does not expose a reasoning setting.

ben-kaufman and others added 13 commits November 4, 2026 08:51
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 7faf634 (run).

Download bitkit-dev-debug universal APK (expires in 30 days).

@jvsena42

Copy link
Copy Markdown
Member

@ben-kaufman #1952 and synonymdev/bitkit-ios#1350 (for #1657 / #1079) make an on-chain send compare the fee of the transaction prepared after the swipe with the fee shown on Confirm. If it is higher, nothing is broadcast and the user re-approves that same transaction.

Those PRs leave hardware-wallet sends out because this PR reworks that path. Could this one and its twin be parked for now? Then I would extend #1952 / #1350 to cover hardware sends too. #1771 / #1773 and #1209 / #1211 can stay as they are; whichever lands second rebases.

@ovitrif

ovitrif commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@ben-kaufman #1770 (keep signed hardware sends on retry) is now merged, like its iOS twin. This draft also keeps a signed hardware send whose broadcast may have gone out, so please reconcile it with what landed on master before it comes out of draft.

This branch has not been deployed

No deployments
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