Commit 0c1f4aa
authored
fix: make SnapKeyring :accountCreated idempotent (#399)
Make `notify:accountCreated` idempotent if the same account ID and
account address (owned by a given Snap) is sent multiple times.
This is used mostly by the Bitcoin Snap which can create
`KeyringAccount` in its state during discovery and start to notify
MetaMask when we call `keyring_createAccount`. This avoid having a state
in the Snap side to check if the event has been emitted once or not.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Makes `AccountCreated` handling idempotent for same account ID and
address from the same snap, with accompanying tests and minor refactors.
>
> - **Core (`packages/keyring-snap-bridge/src/SnapKeyring.ts`)**:
> - Make `AccountCreated` idempotent: if an account with the same `id`
and normalized `address` already exists for the snap, no-op and return
`null`.
> - Normalize address once early during account creation; remove
duplicate later declaration.
> - Keep duplicate-address and duplicate-ID guards intact.
> - **Tests (`packages/keyring-snap-bridge/src/SnapKeyring.test.ts`)**:
> - Add test verifying idempotent behavior for repeated `AccountCreated`
events.
> - Adjust duplicate-address test to bypass idempotency by using a new
ID.
> - Minor updates (import `Json`, mock adjustments) to support new flow.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
4e2fdc2. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 8185566 commit 0c1f4aa
2 files changed
Lines changed: 49 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
430 | 431 | | |
431 | 432 | | |
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 | + | |
| 465 | + | |
433 | 466 | | |
434 | 467 | | |
435 | 468 | | |
| |||
438 | 471 | | |
439 | 472 | | |
440 | 473 | | |
441 | | - | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
442 | 477 | | |
443 | 478 | | |
444 | 479 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| 332 | + | |
332 | 333 | | |
333 | 334 | | |
334 | 335 | | |
| |||
339 | 340 | | |
340 | 341 | | |
341 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
342 | 355 | | |
343 | 356 | | |
344 | 357 | | |
345 | | - | |
346 | 358 | | |
347 | 359 | | |
348 | 360 | | |
| |||
0 commit comments