Skip to content

fix: keep held hardware sends until resolved - #1210

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

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

Conversation

@ben-kaufman

@ben-kaufman ben-kaufman commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Twin: synonymdev/bitkit-android#1772

This PR keeps a signed hardware-wallet send whose broadcast outcome is unknown, so the same payment is never signed a second time, and gives such a send a safe way out through broadcasting its own signed bytes again.

Description

  • Removes the 120 s deadline that dropped the hardware broadcast's answer, because the broadcast keeps running on the core queue and a late answer for a payment that went out was lost. A 60 s screen deadline now stops the spinner without stopping the broadcast. A plain send moves to the pending screen, and its late answer still lands. A Shop (Paykit) payment goes to its existing uncertain state, and its late answer goes where an earlier one would: a success to the proof flow, after which the sign screen completes it as an in-time success, and a refusal to the retained candidate, which is marked refused. Any other answer leaves the candidate retained. The same bytes are not broadcast again until the running broadcast answers.
  • Holds a plain hardware send whose broadcast did not prove that nothing was sent. Proof that nothing was sent means core could not decode the transaction, the Electrum connect failed, or the node refused it as min relay fee not met, mempool min fee not met, bad-txns-inputs-missingorspent, txn-mempool-conflict, non-final, or Bitcoin Core's replacement-fee refusal. On a first broadcast that refusal comes from the only server the bytes were sent to, so the transaction never left. Every other failure keeps the signed transaction, which is never signed again.
  • Writes every plain hardware send to a persisted store before each broadcast of its bytes, so another sheet, or the app after it was killed mid-broadcast, refuses that payment (same wallet, address in any case, and amount) and shows it as pending instead of signing it again.
  • Counts every broadcast of a held send's bytes across the app. A "not sent" answer releases the send only when no other broadcast of the same bytes began since, is still running, or was taken. Broadcast Again and a retry of the same payment from any sheet are refused while a broadcast of those bytes is running, with a message saying so. Electrum has no read timeout, so a broadcast still running about 2 minutes after it started stops blocking another and counts as possibly sent, which never releases the payment.
  • Adds Broadcast Again on the unknown-outcome pending screen, and does the same on a retry of the held payment. It sends the identical signed bytes, which can never pay twice, and never signs.
    • Accepted, or found already recorded because the wallet saw it meanwhile: the send stays pending until the wallet sees it, then moves to success.
    • Refused for a reason that proves the transaction can never confirm, while the wallet has not seen it: the hold is released and the user can make the payment again. Those reasons are bytes core cannot decode, consensus script failure (mandatory-script-verify-flag-failed, block-script-verify-flag-failed), a few consensus bad-txns codes (in-belowout, inputs-duplicate, vout-negative, vout-toolarge, txouttotal-toolarge, oversize), or bad-txns-inputs-missingorspent once a fresh read of the account shows a coin spent by another transaction with 6 or more confirmations. That read covers at most 25 transactions and proves nothing beyond them.
    • Any other refusal (relay policy such as dust, non-standard outputs or mempool-script-verify-flag-failed, fees, mempool full, too-long-mempool-chain, max-fee-exceeded, conflicts, unreadable or unknown errors): the hold is kept, with copy that the payment may still confirm and stays pending until the wallet sees it or its coins are spent elsewhere.
  • Records and releases a held send once the wallet has seen it, at launch and whenever wallet activity arrives, with the tags and contact it was sent with. A send counts as seen only while the wallet still has that transaction, not one that replaced it.
  • Asks "This payment was already sent on . Send it again?" when the user pays a held send the wallet has already seen: Send Again signs a new payment, Show Payment opens the existing one. The date is left out when it cannot be read.
  • Completes an unresolved send on its pending screen once the wallet sees the transaction, recording it with its tags and contact and letting the sheet be dismissed. A successful send is released only after its activity is recorded; a seen one is released just before, as the wallet already has it.
  • Reads held sends one entry at a time, so an entry from a newer version still holds. An entry that cannot be read is kept as written and refuses new hardware sends of its own wallet, or of every wallet when its wallet cannot be read either, with a specific error instead of a generic decoding toast.

Out of Scope

  • Replace payment: a user-confirmed replacement that signs a conflicting transaction spending at least one of the held send's coins. Until it exists, a held send refused for a reason that leaves a valid transaction stays pending for that wallet, address and amount until the wallet sees it or its coins are spent elsewhere. A fixed-amount invoice to that address cannot be paid again meanwhile.
  • Bitkit/Services/HwHeldBroadcastStore.swift: a held send is not released in the background when its coins are spent elsewhere. That needs the hardware spend watcher from the stuck hardware payment work; here the check runs only when Broadcast Again gets a missing-coin refusal.
  • Hardware transfer funding: the held-funding guard for transfer orders is not ported, because hardware transfer funding is disabled on master (Transfer Unavailable).
  • Send-time guards and the stuck on-chain cancel flow are covered by separate PRs.
  • Shop (Paykit) hardware payments keep their existing retained-candidate guard; this PR adds only the screen deadline and late-answer routing.

Design

N/A — no design available.

Preview

N/A

QA Notes

Setup

  • Debug build of this branch on an iOS simulator, regtest with a local or staging Electrum backend.
  • A paired hardware wallet (Trezor or Jade) holding regtest coins.

Journeys

N/A — not drivable; see Manual Tests.

Manual Tests

  • 1. Send on-chain from the hardware wallet, then drop the network after signing and before the broadcast answers → after at most 60 s the send shows Pending with its txid and the unknown-outcome copy; retrying the same send from a new send sheet shows Pending again without asking the device to sign — needs a paired hardware wallet and network fault injection during broadcast, not in Capabilities.
  • 2. After 1, restore the network and tap Broadcast Again → a broadcast toast shows; once the wallet sees the transaction the screen moves to success, the activity shows as sent with its tags, and the sheet can be swiped away — same capability gap as 1.
  • 3. Kill the app while a hardware send is broadcasting, relaunch and repeat the same send → it broadcasts the same bytes again and shows Pending without signing — same capability gap as 1.
  • 4. After 3, let the transaction confirm and relaunch → the payment shows as sent with its tags; paying the same amount to the same address again asks whether to send it again — same capability gap as 1.

Automated Checks

  • added HwFundingSignerTests.swift — a plain send with an unknown broadcast outcome is held, cannot be left, and a retry only broadcasts the same bytes
  • added HwFundingSignerTests.swift — a held send is refused from another sheet during and after its broadcast, matches its address in any case, does not hold back other payments, and is released once the wallet sees it
  • added HwFundingSignerTests.swift — a rebroadcast refused as never confirmable releases the send, which can then be signed afresh; refusals that leave a valid transaction keep the hold
  • added HwFundingSignerTests.swift — a missing-coin refusal releases only once a fresh read shows the coin spent elsewhere, never on a failed read or once the wallet has seen the transaction
  • added HwFundingSignerTests.swift — a broadcast past its deadline moves to pending, and its late answer keeps or releases the hold
  • added HwFundingSignerTests.swift — a late or concurrent "not sent" answer keeps the hold once the same bytes were broadcast again, and the bytes are not broadcast again from any sheet while one broadcast runs
  • added HwFundingSignerTests.swift — a retry of a send the wallet has seen asks before signing again and records the earlier one with its tags; seen sends are recorded and released once; a replaced or missing transaction is not seen
  • added HwFundingSignerTests.swift — a broadcast that never answers stops blocking after its bound without ever releasing the payment; a wallet wipe clears the held sends
  • added HwFundingSignerTests.swift — a Shop broadcast past its deadline stops the spinner, refuses a retry while it runs, and its late success reaches the proof flow and the sign screen, or its late refusal marks the retained candidate
  • added HwFundingSignerTests.swift — Broadcast Again on a send the app just recorded as seen moves to success instead of reporting it not sent
  • added HwFundingSignerTests.swift — a successful send stays held until it is recorded; a retry proven not dispatched leaves no hold; an observed unresolved send is recorded once, then released and leavable
  • added HwFundingSignerTests.swift — held sends survive a new store, entries from a newer version still hold, an unreadable entry refuses only its own wallet and is kept, and an unreadable store refuses before signing
  • added BroadcastConnectivityTests.swift — refusal parsing through core and server envelopes, and that only consensus refusals prove a transaction can never confirm
  • updated SendConfirmationLifecycleTests.swift — signer built without a broadcast timeout and with its own held store

Evidence

  • Tested revision: ef4a14053 (branch head), iPhone 17e simulator on iOS 26.5, Xcode command line.
  • Debug simulator build: succeeded. Release-configuration simulator build: succeeded. Build-for-testing: succeeded.
  • HwFundingSignerTests, SendConfirmationLifecycleTests, BroadcastConnectivityTests, TransferServiceActivityTests: 128 tests, 0 failures.
  • Unit suite with the CI selection (-only-testing:BitkitTests, skipping UtxoSelectionTests, BlocktankTests, AddressTypeIntegrationTests and RNMigrationCleanupTests, no parallel testing): 3010 tests, 1 skipped, 0 failures.
  • The broadcast-race tests and the just-seen Broadcast Again test failed against the code before their fixes and pass now.
  • SwiftFormat 0.64.0 --lint .: 0 files require formatting.
  • node scripts/validate-translations.js: 0 errors; the new strings are filled in all 15 locales.

Gaps

  • Manual tests 1–4 not run: no paired hardware wallet or 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 7 commits November 4, 2026 09:03
The hardware broadcast has no deadline, so a late answer is never
dropped. A plain send whose broadcast failed without proof that nothing
was sent keeps its signed transaction, shows as pending until the wallet
sees its txid, and is never sent again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A hardware send is written to a persisted store before its broadcast
and kept until the broadcast succeeds or proves nothing was sent, or
the wallet sees its transaction. Until then the same send is refused
from any sheet or launch and shows as pending, and a store that cannot
be read refuses the send before signing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A held send whose outcome is unknown can be broadcast again as signed,
from its pending screen or by retrying the same payment; nothing is
ever signed again. It is released only when the refusal proves the
transaction can never confirm (it breaks consensus, an output can never
relay, or a fresh read shows a coin spent by another final transaction)
and the wallet has not seen it. A refusal that leaves a valid
transaction keeps it pending.

A broadcast that outlives its deadline moves to the pending screen
while its late answer still lands. Held sends are read one by one, so
an unreadable entry only refuses its own wallet and is never dropped. A
send is released only after its payment is recorded, and an observed
send completes on its pending screen, which can then be dismissed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every hand-off of a held send's signed bytes is counted app-wide and
persisted first. A "not sent" answer releases the send only when no
other broadcast of the same bytes began since, is still running, or was
taken, and the bytes are not broadcast again from any sheet while one
broadcast of them is running.

A retry of a send the wallet has already seen shows it as pending, where
it is recorded and completed, instead of signing it again. Shop payments
wait for their broadcast's answer. Only consensus failures release a
held send; relay-policy refusals such as dust keep it, and newer
Bitcoin Core consensus codes are read. The spend check reads a bounded
number of transactions and proves nothing beyond that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A Shop hardware payment also stops waiting on its broadcast after the
screen deadline and shows as uncertain, while the broadcast finishes.
Its late answer goes where an earlier one would: a success to the proof
flow, a refusal to the retained candidate. The same bytes are not
broadcast again from the sheet until that answer lands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A held hardware send the wallet has seen is recorded, with the tags and
contact it was sent with, and released at launch and as wallet activity
arrives. Retrying a payment the wallet has already seen asks whether to
send it again instead of showing it as sent, and only counts it seen
while the wallet still has that transaction rather than one that
replaced it.

A broadcast of a held send's bytes that never answers stops blocking
another after two minutes and counts as possibly sent, so it never
releases the payment. A refused rebroadcast says when another is still
running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A Broadcast Again that finds its send released in the meantime checks
again whether the wallet has seen it, and moves to success if so,
rather than saying the payment was not sent. It reports the payment can
be made again only when it was proven never to have gone out.

A Shop payment whose broadcast succeeds after the screen deadline now
completes on its sign screen as an in-time success does. The
already-sent prompt drops the date when it cannot be read, a new amount
clears a stale unresolved send, and a broadcast that never answers
stops blocking another about two minutes after it started.

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

Copy link
Copy Markdown
Member

@ben-kaufman synonymdev/bitkit-android#1952 and #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 and its twin rework 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 #1208 (keep signed hardware sends on retry) is now merged. 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.

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