Skip to content

fix: protect BitBox pairing against stale hash on re-pair#308

Merged
TaprootFreak merged 3 commits into
developfrom
fix/bitbox-stale-hash-on-repair
May 8, 2026
Merged

fix: protect BitBox pairing against stale hash on re-pair#308
TaprootFreak merged 3 commits into
developfrom
fix/bitbox-stale-hash-on-repair

Conversation

@TaprootFreak
Copy link
Copy Markdown
Contributor

Summary

  • Snapshot getChannelHash() before starting init() and require the polled hash to differ from it
  • Drop the _pendingInit ?? Future.value(true) fallback in confirmPairing and use the bang assertion the state guard already guarantees

Why

Stale hash on re-pair

BitboxService is registered as a DI singleton (lib/setup/di.dart:121), and the BitboxManager instance inside it persists across pairings. The connect modal is reachable from at least two entry points (welcome_page.dart, kyc_registration_page.dart), and any second pairing attempt within the same app session goes through that same SDK instance.

Before this PR, the polling loop accepted any non-empty hash. If bitboxManager.getChannelHash() still holds the previous session's hash when polling starts, the app shows the old hash while the BitBox displays the new one — codes don't match, user is stuck.

The 500 ms first-iteration delay introduced in #305 (...so the SDK can finish setting up its Go-side device pointer...) confirms there is a real timing gap during which the SDK is not yet up-to-date for the new session, which is precisely the window where stale hashes surface.

The fix records the hash once before init() runs and ignores any polled hash that matches the snapshot.

?? fallback in confirmPairing

_pendingInit ?? Future.value(true) silently substitutes "init succeeded" if the future is missing. The is! BitboxCheckHash state guard at the top of confirmPairing already guarantees that connectToBitbox set _pendingInit. The fallback only triggers in an impossible state, and if that state ever became reachable, returning true would let channelHashVerify() run on an unverified channel — exactly the crash #301 set out to prevent.

Replacing it with the bang assertion makes the precondition explicit and matches the project's bang usage in similar state-guarded code paths.

Changes

  • lib/screens/hardware_connect_bitbox/bloc/connect_bitbox_cubit.dart: add priorHash snapshot + hash != priorHash check in polling; replace ?? fallback with bang on _pendingInit

Test plan

  • Happy path on iOS BitBox02 Plus: open Welcome → pair → wallet creates (regression check, no behavioural change expected on first pair)
  • Re-pair in same session: pair successfully → trigger a second pairing without restarting the app (e.g. via KYC retry flow, or by opening the connect modal again). Verify the code shown in app matches the code on the BitBox — not the previous session's code
  • Cancel during BitboxConnecting (swipe modal down before code appears): no StateError, no orphaned timer (regression check for fix: prevent orphaned timer leak on BitBox modal dismiss #303)

`BitboxService` is a DI singleton, so a second pairing attempt in the
same app session (re-open from welcome, KYC retry flow, or after an
in-flight cancel) hits the same `BitboxManager` instance. Its
`getChannelHash()` can still return the previous session's hash before
the new `pair()` handshake lands the new one, which would then be shown
to the user and not match the code on the device.

Snapshot the hash before starting init() and require the polled value
to differ from it. While in confirmPairing, drop the
`?? Future.value(true)` fallback on `_pendingInit`: the state guard
above guarantees it is non-null on this path, and silently substituting
"init succeeded" hides any real state inconsistency.
@TaprootFreak TaprootFreak marked this pull request as ready for review May 8, 2026 10:51
@TaprootFreak TaprootFreak merged commit 951ac75 into develop May 8, 2026
1 check passed
@TaprootFreak TaprootFreak deleted the fix/bitbox-stale-hash-on-repair branch May 8, 2026 10:52
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.

1 participant