Commit c08ec7b
fix: detect KeepKey hot-swap + drop stale cache on device change (#49)
* fix: detect KeepKey hot-swap + drop stale cache on device change
The extension treated any reachable vault as a reachable device, with no
validation that the device currently paired with the vault is the same
one whose pubkeys we cached. That made three failure modes silent:
1. Hot-swap on same vault — checkKeepKey kept state.deviceConnected=true
and never re-probed, so addresses/balances pinned to the old device
indefinitely.
2. Extension reload with a new device — init() loaded the old pubkey
cache without comparing deviceId against the freshly-probed one, so
cached addresses survived even a full BEX reload.
3. Vault auth rejection (stale apiKey after re-pair) — fetchPubkeys
caught the error and silently fell back to cached pubkeys from the
previous device, masking the re-pair requirement.
Four narrow changes:
- wallet.init() now compares cached deviceInfo.deviceId to the freshly-
probed deviceId when a device is reachable. Mismatch → clear storage
before fetchPubkeys runs. (Covers case 2.)
- wallet.handleDeviceSwitch() primitive clears in-memory pubkeys/paths,
resets the deviceConnected flag, and wipes pubkeyStorage so the next
fetch rebuilds from the new device. Keeps SDK alive — only the
device-specific state is dropped.
- background/index.ts checkKeepKey now runs a periodic (30s, throttled)
getFeatures re-probe while deviceConnected. Compares before/after
deviceId; on mismatch calls handleDeviceSwitch which clears balance
cache, per-chain address caches (Solana/Tron/TON), and triggers a
fresh pubkey+balance refresh. (Covers case 1.)
- fetchPubkeys no longer silently falls back to cache on auth errors.
Detects 401 / "unauthorized" / "not paired" shapes, clears the stored
apiKey, and throws a user-facing message for the sidebar to surface.
Non-auth failures (network blip, device busy) still fall back to cache
as view-only — that path is correct. (Covers case 3.)
No user-visible UX change on the happy path. Recovery from device swap
is now automatic within ~30s on steady state, immediate on extension
reload.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix: rebuild default paths on device switch so refreshPubkeys has a batch
handleDeviceSwitch cleared state.paths to [], but the caller
(background checkKeepKey) then calls wallet.refreshPubkeys() which
only probes + fetches — it does not repopulate paths. fetchPubkeys
maps state.paths into the batch sent to the device, so an empty
paths array meant an empty batch, meaning the hot-swap recovery
finished with zero pubkeys and zero balances instead of the new
device's view.
Reset to getDefaultPaths() on switch so refreshPubkeys has something
to query against.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix: re-prefetch Solana/Tron/TON pubkeys on device switch
refreshPubkeys only re-runs the default-path batch via
wallet.getPublicKeys — that batch covers BTC / LTC / ETH / Cosmos /
etc., but *not* SOL / TRX / TON. Those three are derived dynamically
at onStart via prefetchSolanaPubkey / prefetchTronPubkey /
prefetchTonAddress (each calls its chain's `*GetAddress` method and
stashes the result in a per-chain cache + adds a pubkey to state).
After a hot-swap:
- handleDeviceSwitch clears the per-chain caches (resetXState)
- refreshPubkeys repopulates the default batch
- but nothing re-runs the three prefetches
→ SOL/TRX/TON vanish from the network dropdown / asset list / balance
fetch until the user reloads the extension or manually navigates to
those chains, which racy-triggers the lazy getAddress in each handler.
Fan out all three prefetches in parallel via Promise.allSettled after
refreshPubkeys. Non-throwing by design, so if one chain's derivation
fails (minFirmware mismatch, etc.) the other two still recover.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 0bac405 commit c08ec7b
2 files changed
Lines changed: 179 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
113 | 173 | | |
114 | 174 | | |
115 | 175 | | |
| |||
139 | 199 | | |
140 | 200 | | |
141 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
142 | 224 | | |
143 | 225 | | |
144 | 226 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
101 | 117 | | |
102 | 118 | | |
103 | 119 | | |
| |||
205 | 221 | | |
206 | 222 | | |
207 | 223 | | |
208 | | - | |
209 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
210 | 246 | | |
211 | 247 | | |
212 | 248 | | |
| |||
216 | 252 | | |
217 | 253 | | |
218 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
219 | 275 | | |
220 | 276 | | |
221 | 277 | | |
| |||
366 | 422 | | |
367 | 423 | | |
368 | 424 | | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
369 | 465 | | |
370 | 466 | | |
371 | 467 | | |
| |||
0 commit comments