@metamask-previews/eth-snap-keyring 20.0.0-ea8175e → 22.1.0-bc99865

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/CHANGELOG.md +83 -9
  2. package/README.md +0 -7
  3. package/dist/SnapIdMap.cjs +0 -2
  4. package/dist/SnapIdMap.cjs.map +1 -1
  5. package/dist/SnapIdMap.d.cts.map +1 -1
  6. package/dist/SnapIdMap.d.mts.map +1 -1
  7. package/dist/SnapIdMap.mjs +0 -2
  8. package/dist/SnapIdMap.mjs.map +1 -1
  9. package/dist/SnapKeyring.cjs +76 -28
  10. package/dist/SnapKeyring.cjs.map +1 -1
  11. package/dist/SnapKeyring.d.cts +1 -1
  12. package/dist/SnapKeyring.d.cts.map +1 -1
  13. package/dist/SnapKeyring.d.mts +1 -1
  14. package/dist/SnapKeyring.d.mts.map +1 -1
  15. package/dist/SnapKeyring.mjs +73 -25
  16. package/dist/SnapKeyring.mjs.map +1 -1
  17. package/dist/SnapKeyringV1.cjs +75 -21
  18. package/dist/SnapKeyringV1.cjs.map +1 -1
  19. package/dist/SnapKeyringV1.d.cts +45 -12
  20. package/dist/SnapKeyringV1.d.cts.map +1 -1
  21. package/dist/SnapKeyringV1.d.mts +45 -12
  22. package/dist/SnapKeyringV1.d.mts.map +1 -1
  23. package/dist/SnapKeyringV1.mjs +71 -17
  24. package/dist/SnapKeyringV1.mjs.map +1 -1
  25. package/dist/account.cjs +37 -3
  26. package/dist/account.cjs.map +1 -1
  27. package/dist/account.d.cts +21 -3
  28. package/dist/account.d.cts.map +1 -1
  29. package/dist/account.d.mts +21 -3
  30. package/dist/account.d.mts.map +1 -1
  31. package/dist/account.mjs +33 -1
  32. package/dist/account.mjs.map +1 -1
  33. package/dist/events.d.cts +12 -12
  34. package/dist/events.d.cts.map +1 -1
  35. package/dist/events.d.mts +12 -12
  36. package/dist/events.d.mts.map +1 -1
  37. package/dist/index.cjs +1 -0
  38. package/dist/index.cjs.map +1 -1
  39. package/dist/index.d.cts +1 -0
  40. package/dist/index.d.cts.map +1 -1
  41. package/dist/index.d.mts +1 -0
  42. package/dist/index.d.mts.map +1 -1
  43. package/dist/index.mjs +1 -0
  44. package/dist/index.mjs.map +1 -1
  45. package/dist/logger.d.cts +1 -0
  46. package/dist/logger.d.cts.map +1 -1
  47. package/dist/logger.d.mts +1 -0
  48. package/dist/logger.d.mts.map +1 -1
  49. package/dist/migrations/v1.cjs +9 -4
  50. package/dist/migrations/v1.cjs.map +1 -1
  51. package/dist/migrations/v1.d.cts +1 -1
  52. package/dist/migrations/v1.d.cts.map +1 -1
  53. package/dist/migrations/v1.d.mts +1 -1
  54. package/dist/migrations/v1.d.mts.map +1 -1
  55. package/dist/migrations/v1.mjs +5 -1
  56. package/dist/migrations/v1.mjs.map +1 -1
  57. package/dist/options.cjs +3 -2
  58. package/dist/options.cjs.map +1 -1
  59. package/dist/util.cjs +7 -22
  60. package/dist/util.cjs.map +1 -1
  61. package/dist/util.d.cts +0 -11
  62. package/dist/util.d.cts.map +1 -1
  63. package/dist/util.d.mts +0 -11
  64. package/dist/util.d.mts.map +1 -1
  65. package/dist/util.mjs +0 -15
  66. package/dist/util.mjs.map +1 -1
  67. package/dist/v2/SnapKeyring.cjs +16 -3
  68. package/dist/v2/SnapKeyring.cjs.map +1 -1
  69. package/dist/v2/SnapKeyring.d.cts +25 -8
  70. package/dist/v2/SnapKeyring.d.cts.map +1 -1
  71. package/dist/v2/SnapKeyring.d.mts +25 -8
  72. package/dist/v2/SnapKeyring.d.mts.map +1 -1
  73. package/dist/v2/SnapKeyring.mjs +16 -4
  74. package/dist/v2/SnapKeyring.mjs.map +1 -1
  75. package/dist/v2/SnapKeyringV1Adapter.cjs +23 -0
  76. package/dist/v2/SnapKeyringV1Adapter.cjs.map +1 -0
  77. package/dist/v2/SnapKeyringV1Adapter.d.cts +14 -0
  78. package/dist/v2/SnapKeyringV1Adapter.d.cts.map +1 -0
  79. package/dist/v2/SnapKeyringV1Adapter.d.mts +14 -0
  80. package/dist/v2/SnapKeyringV1Adapter.d.mts.map +1 -0
  81. package/dist/v2/SnapKeyringV1Adapter.mjs +19 -0
  82. package/dist/v2/SnapKeyringV1Adapter.mjs.map +1 -0
  83. package/dist/v2/index.cjs +1 -0
  84. package/dist/v2/index.cjs.map +1 -1
  85. package/dist/v2/index.d.cts +1 -0
  86. package/dist/v2/index.d.cts.map +1 -1
  87. package/dist/v2/index.d.mts +1 -0
  88. package/dist/v2/index.d.mts.map +1 -1
  89. package/dist/v2/index.mjs +1 -0
  90. package/dist/v2/index.mjs.map +1 -1
  91. package/package.json +49 -32
  92. package/v2.js +3 -0
package/CHANGELOG.md CHANGED
@@ -9,6 +9,70 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Added
11
11
 
12
+ - Add `SnapKeyringV1Adapter` to adapt Snap v2 keyrings to legacy v1 keyring operations ([#557](https://github.com/MetaMask/accounts/pull/557))
13
+ - This adapter mostly exposes EVM signing operations through `EthKeyringV1Adapter`.
14
+ - This adapter also implements `removeAccount` the way it was implemented by the legacy Snap keyring (compatible with the `KeyringController` account removal flow).
15
+
16
+ ### Changed
17
+
18
+ - Normalize `KeyringAccount`'s address with `:accountCreated` and `setAccounts` ([#556](https://github.com/MetaMask/accounts/pull/556))
19
+
20
+ ## [22.1.0]
21
+
22
+ ### Added
23
+
24
+ - Add `isSnapKeyring` (v2) helper ([#549](https://github.com/MetaMask/accounts/pull/549))
25
+
26
+ ### Changed
27
+
28
+ - Bump `@metamask/keyring-sdk` from `^2.0.2` to `^2.1.1` ([#544](https://github.com/MetaMask/accounts/pull/544), [#546](https://github.com/MetaMask/accounts/pull/546))
29
+ - Bump `@metamask/keyring-utils` from `^3.2.0` to `^3.3.1` ([#544](https://github.com/MetaMask/accounts/pull/544), [#546](https://github.com/MetaMask/accounts/pull/546))
30
+
31
+ ## [22.0.1]
32
+
33
+ ### Changed
34
+
35
+ - Bump `@metamask/keyring-internal-snap-client` from `^10.0.2` to `^10.0.3` ([#538](https://github.com/MetaMask/accounts/pull/538))
36
+ - Bump `@metamask/keyring-sdk` from `^2.0.1` to `^2.0.2` ([#538](https://github.com/MetaMask/accounts/pull/538))
37
+ - Bump `@metamask/keyring-internal-api` from `^11.0.0` to `^11.0.1` ([#538](https://github.com/MetaMask/accounts/pull/538))
38
+
39
+ ## [22.0.0]
40
+
41
+ ### Added
42
+
43
+ - **BREAKING:** Re-add `snapId` as part of the callbacks (`SnapKeyringCallbacks`) when building `SnapKeyring` v2 keyrings ([#520](https://github.com/MetaMask/accounts/pull/520))
44
+ - Since the `snapId` is now injected via `deserialize` we cannot auto-capture it at build-time.
45
+
46
+ ### Changed
47
+
48
+ - **BREAKING:** No longer use `snapId` as constructor parameter for `SnapKeyring` (v2) ([#519](https://github.com/MetaMask/accounts/pull/519))
49
+ - The `snapId` is now passed and bound to the keyring upon the first `deserialize` call, to better integrate with `KeyringController` keyrings lifecyle and keyring builders.
50
+ - Bump `@metamask/keyring-internal-api` from `^10.1.1` to `^11.0.0` ([#528](https://github.com/MetaMask/accounts/pull/528))
51
+ - Bump `@metamask/keyring-internal-snap-client` from `^10.0.1` to `^10.0.2` ([#528](https://github.com/MetaMask/accounts/pull/528))
52
+
53
+ ### Removed
54
+
55
+ - **BREAKING:** Removed `snap.{name,enabled}` from `InternalAccount` metadata produced by `SnapKeyring` ([#525](https://github.com/MetaMask/accounts/pull/525))
56
+ - Use `SnapController:getSnap` to get those information instead (in combination with `InternalAccount.metadata.snap.id`).
57
+
58
+ ## [21.0.1]
59
+
60
+ ### Changed
61
+
62
+ - Bump `@metamask/keyring-internal-api` from `^10.1.0` to `^10.1.1` ([#518](https://github.com/MetaMask/accounts/pull/518))
63
+ - Bump `@metamask/keyring-internal-snap-client` from `^10.0.0` to `^10.0.1` ([#518](https://github.com/MetaMask/accounts/pull/518))
64
+ - Bump `@metamask/keyring-sdk` from `^2.0.0` to `^2.0.1` ([#518](https://github.com/MetaMask/accounts/pull/518))
65
+ - Bump `@metamask/keyring-snap-sdk` from `^9.0.0` to `^9.0.1` ([#518](https://github.com/MetaMask/accounts/pull/518))
66
+
67
+ ### Fixed
68
+
69
+ - Workaround Browserify subpath export for `/v2` ([#516](https://github.com/MetaMask/accounts/pull/516))
70
+
71
+ ## [21.0.0]
72
+
73
+ ### Added
74
+
75
+ - Support Stellar (`xlm:account`) accounts in account assertions and v1 migrations ([#483](https://github.com/MetaMask/accounts/pull/483))
12
76
  - Add `SnapKeyringV1` class, a per-snap keyring wrapper that implements the old keyring interface and flows ([#507](https://github.com/MetaMask/accounts/pull/507))
13
77
  - This will allow us to re-use the existing logic (that we still use today) as part of the new V2 implementation until we completely them.
14
78
  - Add `SnapKeyringV2` class, a per-snap keyring wrapper that implements `KeyringV2` with `createAccount`, `createAccounts`, `deleteAccount`, `submitRequest`, `getAccount`, and `getAccounts` ([#501](https://github.com/MetaMask/accounts/pull/501)), ([#507](https://github.com/MetaMask/accounts/pull/507))
@@ -21,12 +85,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
21
85
 
22
86
  - **BREAKING:** Move and rename `SnapKeyringV2` to the new `./v2` subpath export ([#513](https://github.com/MetaMask/accounts/pull/513))
23
87
  - `SnapKeyringV2` is now `SnapKeyring` from `@metamask/keyring-snap-bridge/v2`.
88
+ - **BREAKING:** Bump `@metamask/keyring-api` from `^22.0.0` to `^23.0.0` ([#515](https://github.com/MetaMask/accounts/pull/515))
24
89
  - Use `DeferredPromise` from `@metamask/utils` ([#508](https://github.com/MetaMask/accounts/pull/508))
25
90
  - Refactor `SnapKeyring` to store accounts in per-snap `SnapKeyringV2` wrappers instead of a single flat map ([#501](https://github.com/MetaMask/accounts/pull/501))
26
- - Bump `@metamask/messenger` from `^0.3.0` to `^1.1.1` ([#489](https://github.com/MetaMask/accounts/pull/489), [#500](https://github.com/MetaMask/accounts/pull/500))
91
+ - Bump `@metamask/messenger` from `^0.3.0` to `^1.1.1` ([#489](https://github.com/MetaMask/accounts/pull/489)), ([#500](https://github.com/MetaMask/accounts/pull/500))
27
92
  - Bump `@metamask/snaps-controllers` from `^19.0.0` to `^19.0.1` ([#500](https://github.com/MetaMask/accounts/pull/500))
28
93
  - Bump `@metamask/snaps-utils` from `^12.1.2` to `^12.1.3` ([#500](https://github.com/MetaMask/accounts/pull/500))
29
- - Bump `@metamask/utils` from `^11.1.0` to `^11.10.0` ([#489](https://github.com/MetaMask/accounts/pull/489))
94
+ - Bump `@metamask/utils` from `^11.10.0` to `^11.11.0` ([#483](https://github.com/MetaMask/accounts/pull/483))
95
+ - Bump `@metamask/keyring-internal-api` from `^10.0.1` to `^10.1.0` ([#515](https://github.com/MetaMask/accounts/pull/515))
96
+ - Bump `@metamask/keyring-internal-snap-client` from `^9.0.1` to `^10.0.0` ([#515](https://github.com/MetaMask/accounts/pull/515))
97
+ - Bump `@metamask/keyring-sdk` from `^1.2.0` to `^2.0.0` ([#515](https://github.com/MetaMask/accounts/pull/515))
98
+ - Bump `@metamask/keyring-snap-sdk` from `^8.0.0` to `^9.0.0` ([#515](https://github.com/MetaMask/accounts/pull/515))
30
99
 
31
100
  ## [20.0.0]
32
101
 
@@ -336,7 +405,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
336
405
  ### Changed
337
406
 
338
407
  - Bump `sinon` and `@types/sinon` to latest versions ([#51](https://github.com/MetaMask/accounts/pull/51))
339
- - Add `syncpack` and sync dependencies ([#53](https://github.com/metamask/accounts/pull/53))
408
+ - Add `syncpack` and sync dependencies ([#53](https://github.com/MetaMask/accounts/pull/53))
340
409
 
341
410
  ## [4.3.5]
342
411
 
@@ -357,10 +426,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
357
426
 
358
427
  ### Changed
359
428
 
360
- - Bump `@metamask/snaps-controllers` from `^9.3.0` to `^9.6.0` ([#372](https://github.com/MetaMask/eth-snap-keyring/pull/372), [#389](https://github.com/MetaMask/eth-snap-keyring/pull/389))
429
+ - Bump `@metamask/snaps-controllers` from `^9.3.0` to `^9.6.0` ([#372](https://github.com/MetaMask/eth-snap-keyring/pull/372)), ([#389](https://github.com/MetaMask/eth-snap-keyring/pull/389))
361
430
  - Bump `@metamask/snaps-sdk` from `^6.1.0` to `^6.4.0` ([#389](https://github.com/MetaMask/eth-snap-keyring/pull/389))
362
431
  - Bump `@metamask/utils` from `^9.1.0` to `^9.2.1` ([#385](https://github.com/MetaMask/eth-snap-keyring/pull/385))
363
- - Bump `@metamask/keyring-api` from `^8.0.1` to `^8.1.0` ([#374](https://github.com/MetaMask/eth-snap-keyring/pull/374), [#376](https://github.com/MetaMask/eth-snap-keyring/pull/376))
432
+ - Bump `@metamask/keyring-api` from `^8.0.1` to `^8.1.0` ([#374](https://github.com/MetaMask/eth-snap-keyring/pull/374)), ([#376](https://github.com/MetaMask/eth-snap-keyring/pull/376))
364
433
 
365
434
  ## [4.3.2]
366
435
 
@@ -368,9 +437,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
368
437
 
369
438
  - Bump `@metamask/eth-sig-util` from `^7.0.1` to `^7.0.3` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311))
370
439
  - Bump `@metamask/keyring-api` from `^8.0.0` to `^8.0.1` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311))
371
- - Bump `@metamask/snaps-controllers` from `^8.1.1` to `^9.3.0` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311), [#363](https://github.com/MetaMask/eth-snap-keyring/pull/363))
372
- - Bump `@metamask/snaps-sdk` from `^4.2.0` to `^6.1.0` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311), [#363](https://github.com/MetaMask/eth-snap-keyring/pull/363))
373
- - Bump `@metamask/snaps-utils` from `^7.4.0` to `^7.8.0` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311), [#363](https://github.com/MetaMask/eth-snap-keyring/pull/363))
440
+ - Bump `@metamask/snaps-controllers` from `^8.1.1` to `^9.3.0` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311)), ([#363](https://github.com/MetaMask/eth-snap-keyring/pull/363))
441
+ - Bump `@metamask/snaps-sdk` from `^4.2.0` to `^6.1.0` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311)), ([#363](https://github.com/MetaMask/eth-snap-keyring/pull/363))
442
+ - Bump `@metamask/snaps-utils` from `^7.4.0` to `^7.8.0` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311)), ([#363](https://github.com/MetaMask/eth-snap-keyring/pull/363))
374
443
  - Bump `@metamask/utils` from `^8.4.0` to `^9.1.0` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311))
375
444
  - Set tsconfig options `module`, `moduleResolution` to `Node16` ([#311](https://github.com/MetaMask/eth-snap-keyring/pull/311))
376
445
 
@@ -666,7 +735,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
666
735
 
667
736
  - Initial release.
668
737
 
669
- [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@20.0.0...HEAD
738
+ [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@22.1.0...HEAD
739
+ [22.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@22.0.1...@metamask/eth-snap-keyring@22.1.0
740
+ [22.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@22.0.0...@metamask/eth-snap-keyring@22.0.1
741
+ [22.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@21.0.1...@metamask/eth-snap-keyring@22.0.0
742
+ [21.0.1]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@21.0.0...@metamask/eth-snap-keyring@21.0.1
743
+ [21.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@20.0.0...@metamask/eth-snap-keyring@21.0.0
670
744
  [20.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@19.0.0...@metamask/eth-snap-keyring@20.0.0
671
745
  [19.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@18.0.2...@metamask/eth-snap-keyring@19.0.0
672
746
  [18.0.2]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@18.0.1...@metamask/eth-snap-keyring@18.0.2
package/README.md CHANGED
@@ -37,35 +37,28 @@ Run `yarn lint` to run the linter, or run `yarn lint:fix` to run the linter and
37
37
  The project follows the same release process as the other libraries in the MetaMask organization. The GitHub Actions [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) are used to automate the release process; see those repositories for more information about how they work.
38
38
 
39
39
  1. Choose a release version.
40
-
41
40
  - The release version should be chosen according to SemVer. Analyze the changes to see whether they include any breaking changes, new features, or deprecations, then choose the appropriate SemVer version. See [the SemVer specification](https://semver.org/) for more information.
42
41
 
43
42
  2. If this release is backporting changes onto a previous release, then ensure there is a major version branch for that version (e.g. `1.x` for a `v1` backport release).
44
-
45
43
  - The major version branch should be set to the most recent release with that major version. For example, when backporting a `v1.0.2` release, you'd want to ensure there was a `1.x` branch that was set to the `v1.0.1` tag.
46
44
 
47
45
  3. Trigger the [`workflow_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch) event [manually](https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow) for the `Create Release Pull Request` action to create the release PR.
48
-
49
46
  - For a backport release, the base branch should be the major version branch that you ensured existed in step 2. For a normal release, the base branch should be the main branch for that repository (which should be the default value).
50
47
  - This should trigger the [`action-create-release-pr`](https://github.com/MetaMask/action-create-release-pr) workflow to create the release PR.
51
48
 
52
49
  4. Update the changelog to move each change entry into the appropriate change category ([See here](https://keepachangelog.com/en/1.0.0/#types) for the full list of change categories, and the correct ordering), and edit them to be more easily understood by users of the package.
53
-
54
50
  - Generally any changes that don't affect consumers of the package (e.g. lockfile changes or development environment changes) are omitted. Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).
55
51
  - Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).
56
52
  - Consolidate related changes into one change entry if it makes it easier to explain.
57
53
  - Run `yarn auto-changelog validate --rc` to check that the changelog is correctly formatted.
58
54
 
59
55
  5. Review and QA the release.
60
-
61
56
  - If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.
62
57
 
63
58
  6. Squash & Merge the release.
64
-
65
59
  - This should trigger the [`action-publish-release`](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.
66
60
 
67
61
  7. Publish the release on npm.
68
-
69
62
  - Wait for the `publish-release` GitHub Action workflow to finish. This should trigger a second job (`publish-npm`), which will wait for a run approval by the [`npm publishers`](https://github.com/orgs/MetaMask/teams/npm-publishers) team.
70
63
  - Approve the `publish-npm` job (or ask somebody on the npm publishers team to approve it for you).
71
64
  - Once the `publish-npm` job has finished, check npm to verify that it has been published.
@@ -220,7 +220,6 @@ class SnapIdMap {
220
220
  delete(snapId, key) {
221
221
  return this.has(snapId, key) && __classPrivateFieldGet(this, _SnapIdMap_map, "f").delete(key);
222
222
  }
223
- /* eslint-disable jsdoc/check-indentation */
224
223
  /**
225
224
  * Adds or updates a key-value pair in the map.
226
225
  *
@@ -239,7 +238,6 @@ class SnapIdMap {
239
238
  * @param value - Value of the element to add or update.
240
239
  * @returns The map itself.
241
240
  */
242
- /* eslint-enable jsdoc/check-indentation */
243
241
  set(key, value) {
244
242
  // If the key is present in the map but isn't associated with the given
245
243
  // Snap ID, it means that the item was added to the map by a different
@@ -1 +1 @@
1
- {"version":3,"file":"SnapIdMap.cjs","sourceRoot":"","sources":["../src/SnapIdMap.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAEA,iEAA0D;AAE1D;;GAEG;AACH,MAAa,kBAAmB,SAAQ,KAAK;IAW3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;;;;;;;;;;;OAmBG;IACH,MAAM;QACJ,OAAO,uBAAA,IAAI,sBAAK,CAAC,MAAM,EAAE,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,sBAAK,CAAC,IAAI,CAAC;IACxB,CAAC;CACF;AAhQD,8BAgQC","sourcesContent":["import type { SnapId } from '@metamask/snaps-sdk';\n\nimport { CaseInsensitiveMap } from './CaseInsensitiveMap';\n\n/**\n * Error thrown when an invalid Snap ID is encountered.\n */\nexport class InvalidSnapIdError extends Error {\n /**\n * The ID of the Snap that caused the error.\n */\n snapId: SnapId;\n\n /**\n * The key of the element that caused the error.\n */\n key: string;\n\n /**\n * Creates an instance of `InvalidSnapIdError`.\n *\n * @param snapId - The invalid Snap ID.\n * @param key - The key associated with the invalid Snap ID.\n */\n constructor(snapId: SnapId, key: string) {\n super(`Snap \"${snapId}\" is not allowed to set \"${key}\"`);\n this.name = 'InvalidSnapIdError';\n this.snapId = snapId;\n this.key = key;\n }\n}\n\n/**\n * A map that associates a string key with a value that has a `snapId`\n * property. Note that the key is case-insensitive.\n *\n * The `snapId` property is used to ensure that only the Snap that added an\n * item to the map can modify or delete it.\n */\nexport class SnapIdMap<Value extends { snapId: SnapId }> {\n readonly #map: CaseInsensitiveMap<Value>;\n\n /**\n * Creates a new `SnapIdMap` object.\n *\n * Example:\n *\n * ```ts\n * const items = [\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ];\n * const map = new SnapIdMap(items);\n * ```\n *\n * @param iterable - An iterable object whose elements are key-value pairs.\n * Each key-value pair will be added to the new map.\n */\n constructor(iterable?: Iterable<readonly [string, Value]>) {\n this.#map = new CaseInsensitiveMap(iterable);\n }\n\n /**\n * Returns a plain object with the same key-value pairs as this map.\n *\n * Example:\n *\n * ```ts\n * const items = [\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ];\n * const map = new SnapIdMap(items);\n * map.toObject();\n * // Returns\n * // {\n * // foo: { snapId: '1', name: 'foo' },\n * // bar: { snapId: '1', name: 'bar' },\n * // }\n * ```\n *\n * @returns A plain object with the same key-value pairs as this map.\n */\n toObject(): Record<string, Value> {\n return this.#map.toObject();\n }\n\n /**\n * Returns a new `SnapIdMap` object from an plain object.\n *\n * Example:\n *\n * ```ts\n * const obj = {\n * foo: { snapId: '1', name: 'foo' },\n * bar: { snapId: '1', name: 'bar' },\n * };\n * const map = SnapIdMap.fromObject(obj);\n * ```\n *\n * @param obj - A plain object whose elements will be added to the new map.\n * @returns A new `SnapIdMap` containing the elements of the given object.\n */\n static fromObject<Value extends { snapId: SnapId }>(\n obj: Record<string, Value>,\n ): SnapIdMap<Value> {\n return new SnapIdMap(Object.entries(obj));\n }\n\n /**\n * Gets a value from the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `undefined`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.get('1', 'foo'); // Returns { snapId: '1', name: 'foo' }\n * map.get('2', 'foo'); // Returns `undefined`\n * map.get('1', 'bar'); // Returns `undefined`\n * ```\n *\n * @param snapId - Snap ID present in the value to get.\n * @param key - Key of the element to get.\n * @returns The value associated with the given key and Snap ID.\n */\n get(snapId: SnapId, key: string): Value | undefined {\n const value = this.#map.get(key);\n return value?.snapId === snapId ? value : undefined;\n }\n\n /**\n * Gets a Snap ID that's associated with the given key from the map.\n *\n * @param key - Key of the element to get.\n * @returns The Snap ID associated with the given key.\n */\n getSnapId(key: string): SnapId | undefined {\n return this.#map.get(key)?.snapId;\n }\n\n /**\n * Gets and delete a value from the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `undefined`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.pop('1', 'foo'); // Returns { snapId: '1', name: 'foo' }\n * map.pop('1', 'foo'); // Returns `undefined` (already returned and deleted)\n * map.pop('1', 'bar'); // Returns `undefined`\n * ```\n *\n * @param snapId - Snap ID present in the value to get.\n * @param key - Key of the element to get.\n * @returns The value associated with the given key and Snap ID.\n */\n pop(snapId: SnapId, key: string): Value | undefined {\n const value = this.get(snapId, key);\n this.delete(snapId, key);\n return value;\n }\n\n /**\n * Checks if a key is present in the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `false`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.has('1', 'foo'); // Returns `true`\n * map.has('2', 'foo'); // Returns `false`\n * map.has('1', 'bar'); // Returns `false`\n * ```\n *\n * @param snapId - Snap ID present in the value to check.\n * @param key - Key of the element to check.\n * @returns `true` if the key is present in the map and the Snap ID of the\n * value is equal to the given Snap ID, `false` otherwise.\n */\n has(snapId: SnapId, key: string): boolean {\n return this.get(snapId, key) !== undefined;\n }\n\n /**\n * Checks if a snap ID exists in the map.\n *\n * @param snapId - Snap ID present in the value to check.\n * @returns `true` if the snap ID is present in the map, `false` otherwise.\n */\n hasSnapId(snapId: SnapId): boolean {\n // We could use a reverse-mapping to map Snap ID to their actual key too, but\n // for now, this will do the trick.\n return [...this.#map.values()].some((value) => value.snapId === snapId);\n }\n\n /**\n * Deletes a key from the map.\n *\n * If the given key is not present in the map or the Snap IDs don't match,\n * returns `false` and does nothing.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.delete('2', 'foo'); // Returns `false`\n * map.delete('1', 'bar'); // Returns `false`\n * map.delete('1', 'foo'); // Returns `true`\n * ```\n *\n * @param snapId - Snap ID present in the value to delete.\n * @param key - Key of the element to delete.\n * @returns `true` if the key was present in the map and the Snap ID of the\n * value was equal to the given Snap ID, `false` otherwise.\n */\n delete(snapId: SnapId, key: string): boolean {\n return this.has(snapId, key) && this.#map.delete(key);\n }\n\n /* eslint-disable jsdoc/check-indentation */\n /**\n * Adds or updates a key-value pair in the map.\n *\n * Note that this method has a different behavior from the `Map.set`.\n *\n * - If the given key is not already present in the map, this method adds the\n * key-value pair to the map.\n *\n * - If the given key is already present in the map and the Snap IDs match,\n * this method updates the value associated with the key.\n *\n * - However, if the given key is already present in the map but the Snap IDs\n * do not match, this method throws an error.\n *\n * @param key - Key of the element to add or update.\n * @param value - Value of the element to add or update.\n * @returns The map itself.\n */\n /* eslint-enable jsdoc/check-indentation */\n set(key: string, value: Value): this {\n // If the key is present in the map but isn't associated with the given\n // Snap ID, it means that the item was added to the map by a different\n // Snap. In this case, throw an error.\n if (this.#map.has(key) && !this.has(value.snapId, key)) {\n throw new InvalidSnapIdError(value.snapId, key);\n }\n this.#map.set(key, value);\n return this;\n }\n\n /**\n * Returns an iterable of the values in the map.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap([\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ]);\n * const values = [...map.values()];\n * // Returns\n * // [\n * // { snapId: '1', name: 'foo' },\n * // { snapId: '1', name: 'bar' },\n * // ]\n * ```\n *\n * @returns An iterable of the values in the map.\n */\n values(): IterableIterator<Value> {\n return this.#map.values();\n }\n\n /**\n * Returns the number of key-value pairs in the map.\n *\n * @returns The number of key-value pairs in the map.\n */\n get size(): number {\n return this.#map.size;\n }\n}\n"]}
1
+ {"version":3,"file":"SnapIdMap.cjs","sourceRoot":"","sources":["../src/SnapIdMap.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAEA,iEAA0D;AAE1D;;GAEG;AACH,MAAa,kBAAmB,SAAQ,KAAK;IAW3C;;;;;OAKG;IACH,YAAY,MAAc,EAAE,GAAW;QACrC,KAAK,CAAC,SAAS,MAAM,4BAA4B,GAAG,GAAG,CAAC,CAAC;QACzD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,C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CACF;AA9PD,8BA8PC","sourcesContent":["import type { SnapId } from '@metamask/snaps-sdk';\n\nimport { CaseInsensitiveMap } from './CaseInsensitiveMap';\n\n/**\n * Error thrown when an invalid Snap ID is encountered.\n */\nexport class InvalidSnapIdError extends Error {\n /**\n * The ID of the Snap that caused the error.\n */\n snapId: SnapId;\n\n /**\n * The key of the element that caused the error.\n */\n key: string;\n\n /**\n * Creates an instance of `InvalidSnapIdError`.\n *\n * @param snapId - The invalid Snap ID.\n * @param key - The key associated with the invalid Snap ID.\n */\n constructor(snapId: SnapId, key: string) {\n super(`Snap \"${snapId}\" is not allowed to set \"${key}\"`);\n this.name = 'InvalidSnapIdError';\n this.snapId = snapId;\n this.key = key;\n }\n}\n\n/**\n * A map that associates a string key with a value that has a `snapId`\n * property. Note that the key is case-insensitive.\n *\n * The `snapId` property is used to ensure that only the Snap that added an\n * item to the map can modify or delete it.\n */\nexport class SnapIdMap<Value extends { snapId: SnapId }> {\n readonly #map: CaseInsensitiveMap<Value>;\n\n /**\n * Creates a new `SnapIdMap` object.\n *\n * Example:\n *\n * ```ts\n * const items = [\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ];\n * const map = new SnapIdMap(items);\n * ```\n *\n * @param iterable - An iterable object whose elements are key-value pairs.\n * Each key-value pair will be added to the new map.\n */\n constructor(iterable?: Iterable<readonly [string, Value]>) {\n this.#map = new CaseInsensitiveMap(iterable);\n }\n\n /**\n * Returns a plain object with the same key-value pairs as this map.\n *\n * Example:\n *\n * ```ts\n * const items = [\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ];\n * const map = new SnapIdMap(items);\n * map.toObject();\n * // Returns\n * // {\n * // foo: { snapId: '1', name: 'foo' },\n * // bar: { snapId: '1', name: 'bar' },\n * // }\n * ```\n *\n * @returns A plain object with the same key-value pairs as this map.\n */\n toObject(): Record<string, Value> {\n return this.#map.toObject();\n }\n\n /**\n * Returns a new `SnapIdMap` object from an plain object.\n *\n * Example:\n *\n * ```ts\n * const obj = {\n * foo: { snapId: '1', name: 'foo' },\n * bar: { snapId: '1', name: 'bar' },\n * };\n * const map = SnapIdMap.fromObject(obj);\n * ```\n *\n * @param obj - A plain object whose elements will be added to the new map.\n * @returns A new `SnapIdMap` containing the elements of the given object.\n */\n static fromObject<Value extends { snapId: SnapId }>(\n obj: Record<string, Value>,\n ): SnapIdMap<Value> {\n return new SnapIdMap(Object.entries(obj));\n }\n\n /**\n * Gets a value from the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `undefined`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.get('1', 'foo'); // Returns { snapId: '1', name: 'foo' }\n * map.get('2', 'foo'); // Returns `undefined`\n * map.get('1', 'bar'); // Returns `undefined`\n * ```\n *\n * @param snapId - Snap ID present in the value to get.\n * @param key - Key of the element to get.\n * @returns The value associated with the given key and Snap ID.\n */\n get(snapId: SnapId, key: string): Value | undefined {\n const value = this.#map.get(key);\n return value?.snapId === snapId ? value : undefined;\n }\n\n /**\n * Gets a Snap ID that's associated with the given key from the map.\n *\n * @param key - Key of the element to get.\n * @returns The Snap ID associated with the given key.\n */\n getSnapId(key: string): SnapId | undefined {\n return this.#map.get(key)?.snapId;\n }\n\n /**\n * Gets and delete a value from the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `undefined`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.pop('1', 'foo'); // Returns { snapId: '1', name: 'foo' }\n * map.pop('1', 'foo'); // Returns `undefined` (already returned and deleted)\n * map.pop('1', 'bar'); // Returns `undefined`\n * ```\n *\n * @param snapId - Snap ID present in the value to get.\n * @param key - Key of the element to get.\n * @returns The value associated with the given key and Snap ID.\n */\n pop(snapId: SnapId, key: string): Value | undefined {\n const value = this.get(snapId, key);\n this.delete(snapId, key);\n return value;\n }\n\n /**\n * Checks if a key is present in the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `false`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.has('1', 'foo'); // Returns `true`\n * map.has('2', 'foo'); // Returns `false`\n * map.has('1', 'bar'); // Returns `false`\n * ```\n *\n * @param snapId - Snap ID present in the value to check.\n * @param key - Key of the element to check.\n * @returns `true` if the key is present in the map and the Snap ID of the\n * value is equal to the given Snap ID, `false` otherwise.\n */\n has(snapId: SnapId, key: string): boolean {\n return this.get(snapId, key) !== undefined;\n }\n\n /**\n * Checks if a snap ID exists in the map.\n *\n * @param snapId - Snap ID present in the value to check.\n * @returns `true` if the snap ID is present in the map, `false` otherwise.\n */\n hasSnapId(snapId: SnapId): boolean {\n // We could use a reverse-mapping to map Snap ID to their actual key too, but\n // for now, this will do the trick.\n return [...this.#map.values()].some((value) => value.snapId === snapId);\n }\n\n /**\n * Deletes a key from the map.\n *\n * If the given key is not present in the map or the Snap IDs don't match,\n * returns `false` and does nothing.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.delete('2', 'foo'); // Returns `false`\n * map.delete('1', 'bar'); // Returns `false`\n * map.delete('1', 'foo'); // Returns `true`\n * ```\n *\n * @param snapId - Snap ID present in the value to delete.\n * @param key - Key of the element to delete.\n * @returns `true` if the key was present in the map and the Snap ID of the\n * value was equal to the given Snap ID, `false` otherwise.\n */\n delete(snapId: SnapId, key: string): boolean {\n return this.has(snapId, key) && this.#map.delete(key);\n }\n\n /**\n * Adds or updates a key-value pair in the map.\n *\n * Note that this method has a different behavior from the `Map.set`.\n *\n * - If the given key is not already present in the map, this method adds the\n * key-value pair to the map.\n *\n * - If the given key is already present in the map and the Snap IDs match,\n * this method updates the value associated with the key.\n *\n * - However, if the given key is already present in the map but the Snap IDs\n * do not match, this method throws an error.\n *\n * @param key - Key of the element to add or update.\n * @param value - Value of the element to add or update.\n * @returns The map itself.\n */\n set(key: string, value: Value): this {\n // If the key is present in the map but isn't associated with the given\n // Snap ID, it means that the item was added to the map by a different\n // Snap. In this case, throw an error.\n if (this.#map.has(key) && !this.has(value.snapId, key)) {\n throw new InvalidSnapIdError(value.snapId, key);\n }\n this.#map.set(key, value);\n return this;\n }\n\n /**\n * Returns an iterable of the values in the map.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap([\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ]);\n * const values = [...map.values()];\n * // Returns\n * // [\n * // { snapId: '1', name: 'foo' },\n * // { snapId: '1', name: 'bar' },\n * // ]\n * ```\n *\n * @returns An iterable of the values in the map.\n */\n values(): IterableIterator<Value> {\n return this.#map.values();\n }\n\n /**\n * Returns the number of key-value pairs in the map.\n *\n * @returns The number of key-value pairs in the map.\n */\n get size(): number {\n return this.#map.size;\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"SnapIdMap.d.cts","sourceRoot":"","sources":["../src/SnapIdMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAIlD;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;;;;OAKG;gBACS,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;CAMxC;AAED;;;;;;GAMG;AACH,qBAAa,SAAS,CAAC,KAAK,SAAS;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE;;IAGrD;;;;;;;;;;;;;;;OAeG;gBACS,QAAQ,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAIzD;;;;;;;;;;;;;;;;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et(this, _SnapIdMap_map, "f").delete(key);
218
218
  }
219
- /* eslint-disable jsdoc/check-indentation */
220
219
  /**
221
220
  * Adds or updates a key-value pair in the map.
222
221
  *
@@ -235,7 +234,6 @@ export class SnapIdMap {
235
234
  * @param value - Value of the element to add or update.
236
235
  * @returns The map itself.
237
236
  */
238
- /* eslint-enable jsdoc/check-indentation */
239
237
  set(key, value) {
240
238
  // If the key is present in the map but isn't associated with the given
241
239
  // Snap ID, it means that the item was added to the map by a different
@@ -1 +1 @@
1
- {"version":3,"file":"SnapIdMap.mjs","sourceRoot":"","sources":["../src/SnapIdMap.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAE1D;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAW3C;;;;;OAKG;IACH,YAAY,MAAc,EAAE,GAAW;QACrC,KAAK,CAAC,SAAS,MAAM,4BAA4B,GAAG,GAAG,CAAC,CAAC;QACzD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,OAAO,SAAS;IAGpB;;;;;;;;;;;;;;;OAeG;IACH,YAAY,QAA6C;QAlBhD,iCAAgC;QAmBvC,uBAAA,IAAI,kBAAQ,IAAI,kBAAkB,CAAC,QAAQ,CAAC,MAAA,CAAC;IAC/C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ;QACN,OAAO,uBAAA,IAAI,sBAAK,CAAC,QAAQ,EAAE,CAAC;IAC9B,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,UAAU,CACf,GAA0B;QAE1B,OAAO,IAAI,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;;;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,4CAA4C;IAC5C;;;;;;;;;;;;;;;;;OAiBG;IACH,2CAA2C;IAC3C,GAAG,CAAC,GAAW,EAAE,KAAY;QAC3B,uEAAuE;QACvE,sEAAsE;QACtE,sCAAsC;QACtC,IAAI,uBAAA,IAAI,sBAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;YACvD,MAAM,IAAI,kBAAkB,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAClD,CAAC;QACD,uBAAA,IAAI,sBAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM;QACJ,OAAO,uBAAA,IAAI,sBAAK,CAAC,MAAM,EAAE,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,sBAAK,CAAC,IAAI,CAAC;IACxB,CAAC;CACF","sourcesContent":["import type { SnapId } from '@metamask/snaps-sdk';\n\nimport { CaseInsensitiveMap } from './CaseInsensitiveMap';\n\n/**\n * Error thrown when an invalid Snap ID is encountered.\n */\nexport class InvalidSnapIdError extends Error {\n /**\n * The ID of the Snap that caused the error.\n */\n snapId: SnapId;\n\n /**\n * The key of the element that caused the error.\n */\n key: string;\n\n /**\n * Creates an instance of `InvalidSnapIdError`.\n *\n * @param snapId - The invalid Snap ID.\n * @param key - The key associated with the invalid Snap ID.\n */\n constructor(snapId: SnapId, key: string) {\n super(`Snap \"${snapId}\" is not allowed to set \"${key}\"`);\n this.name = 'InvalidSnapIdError';\n this.snapId = snapId;\n this.key = key;\n }\n}\n\n/**\n * A map that associates a string key with a value that has a `snapId`\n * property. Note that the key is case-insensitive.\n *\n * The `snapId` property is used to ensure that only the Snap that added an\n * item to the map can modify or delete it.\n */\nexport class SnapIdMap<Value extends { snapId: SnapId }> {\n readonly #map: CaseInsensitiveMap<Value>;\n\n /**\n * Creates a new `SnapIdMap` object.\n *\n * Example:\n *\n * ```ts\n * const items = [\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ];\n * const map = new SnapIdMap(items);\n * ```\n *\n * @param iterable - An iterable object whose elements are key-value pairs.\n * Each key-value pair will be added to the new map.\n */\n constructor(iterable?: Iterable<readonly [string, Value]>) {\n this.#map = new CaseInsensitiveMap(iterable);\n }\n\n /**\n * Returns a plain object with the same key-value pairs as this map.\n *\n * Example:\n *\n * ```ts\n * const items = [\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ];\n * const map = new SnapIdMap(items);\n * map.toObject();\n * // Returns\n * // {\n * // foo: { snapId: '1', name: 'foo' },\n * // bar: { snapId: '1', name: 'bar' },\n * // }\n * ```\n *\n * @returns A plain object with the same key-value pairs as this map.\n */\n toObject(): Record<string, Value> {\n return this.#map.toObject();\n }\n\n /**\n * Returns a new `SnapIdMap` object from an plain object.\n *\n * Example:\n *\n * ```ts\n * const obj = {\n * foo: { snapId: '1', name: 'foo' },\n * bar: { snapId: '1', name: 'bar' },\n * };\n * const map = SnapIdMap.fromObject(obj);\n * ```\n *\n * @param obj - A plain object whose elements will be added to the new map.\n * @returns A new `SnapIdMap` containing the elements of the given object.\n */\n static fromObject<Value extends { snapId: SnapId }>(\n obj: Record<string, Value>,\n ): SnapIdMap<Value> {\n return new SnapIdMap(Object.entries(obj));\n }\n\n /**\n * Gets a value from the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `undefined`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.get('1', 'foo'); // Returns { snapId: '1', name: 'foo' }\n * map.get('2', 'foo'); // Returns `undefined`\n * map.get('1', 'bar'); // Returns `undefined`\n * ```\n *\n * @param snapId - Snap ID present in the value to get.\n * @param key - Key of the element to get.\n * @returns The value associated with the given key and Snap ID.\n */\n get(snapId: SnapId, key: string): Value | undefined {\n const value = this.#map.get(key);\n return value?.snapId === snapId ? value : undefined;\n }\n\n /**\n * Gets a Snap ID that's associated with the given key from the map.\n *\n * @param key - Key of the element to get.\n * @returns The Snap ID associated with the given key.\n */\n getSnapId(key: string): SnapId | undefined {\n return this.#map.get(key)?.snapId;\n }\n\n /**\n * Gets and delete a value from the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `undefined`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.pop('1', 'foo'); // Returns { snapId: '1', name: 'foo' }\n * map.pop('1', 'foo'); // Returns `undefined` (already returned and deleted)\n * map.pop('1', 'bar'); // Returns `undefined`\n * ```\n *\n * @param snapId - Snap ID present in the value to get.\n * @param key - Key of the element to get.\n * @returns The value associated with the given key and Snap ID.\n */\n pop(snapId: SnapId, key: string): Value | undefined {\n const value = this.get(snapId, key);\n this.delete(snapId, key);\n return value;\n }\n\n /**\n * Checks if a key is present in the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `false`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.has('1', 'foo'); // Returns `true`\n * map.has('2', 'foo'); // Returns `false`\n * map.has('1', 'bar'); // Returns `false`\n * ```\n *\n * @param snapId - Snap ID present in the value to check.\n * @param key - Key of the element to check.\n * @returns `true` if the key is present in the map and the Snap ID of the\n * value is equal to the given Snap ID, `false` otherwise.\n */\n has(snapId: SnapId, key: string): boolean {\n return this.get(snapId, key) !== undefined;\n }\n\n /**\n * Checks if a snap ID exists in the map.\n *\n * @param snapId - Snap ID present in the value to check.\n * @returns `true` if the snap ID is present in the map, `false` otherwise.\n */\n hasSnapId(snapId: SnapId): boolean {\n // We could use a reverse-mapping to map Snap ID to their actual key too, but\n // for now, this will do the trick.\n return [...this.#map.values()].some((value) => value.snapId === snapId);\n }\n\n /**\n * Deletes a key from the map.\n *\n * If the given key is not present in the map or the Snap IDs don't match,\n * returns `false` and does nothing.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.delete('2', 'foo'); // Returns `false`\n * map.delete('1', 'bar'); // Returns `false`\n * map.delete('1', 'foo'); // Returns `true`\n * ```\n *\n * @param snapId - Snap ID present in the value to delete.\n * @param key - Key of the element to delete.\n * @returns `true` if the key was present in the map and the Snap ID of the\n * value was equal to the given Snap ID, `false` otherwise.\n */\n delete(snapId: SnapId, key: string): boolean {\n return this.has(snapId, key) && this.#map.delete(key);\n }\n\n /* eslint-disable jsdoc/check-indentation */\n /**\n * Adds or updates a key-value pair in the map.\n *\n * Note that this method has a different behavior from the `Map.set`.\n *\n * - If the given key is not already present in the map, this method adds the\n * key-value pair to the map.\n *\n * - If the given key is already present in the map and the Snap IDs match,\n * this method updates the value associated with the key.\n *\n * - However, if the given key is already present in the map but the Snap IDs\n * do not match, this method throws an error.\n *\n * @param key - Key of the element to add or update.\n * @param value - Value of the element to add or update.\n * @returns The map itself.\n */\n /* eslint-enable jsdoc/check-indentation */\n set(key: string, value: Value): this {\n // If the key is present in the map but isn't associated with the given\n // Snap ID, it means that the item was added to the map by a different\n // Snap. In this case, throw an error.\n if (this.#map.has(key) && !this.has(value.snapId, key)) {\n throw new InvalidSnapIdError(value.snapId, key);\n }\n this.#map.set(key, value);\n return this;\n }\n\n /**\n * Returns an iterable of the values in the map.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap([\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ]);\n * const values = [...map.values()];\n * // Returns\n * // [\n * // { snapId: '1', name: 'foo' },\n * // { snapId: '1', name: 'bar' },\n * // ]\n * ```\n *\n * @returns An iterable of the values in the map.\n */\n values(): IterableIterator<Value> {\n return this.#map.values();\n }\n\n /**\n * Returns the number of key-value pairs in the map.\n *\n * @returns The number of key-value pairs in the map.\n */\n get size(): number {\n return this.#map.size;\n }\n}\n"]}
1
+ {"version":3,"file":"SnapIdMap.mjs","sourceRoot":"","sources":["../src/SnapIdMap.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,OAAO,EAAE,kBAAkB,EAAE,iCAA6B;AAE1D;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAW3C;;;;;OAKG;IACH,YAAY,MAAc,EAAE,GAAW;QACrC,KAAK,CAAC,SAAS,MAAM,4BAA4B,GAAG,GAAG,CAAC,CAAC;QACzD,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,OAAO,SAAS;IAGpB;;;;;;;;;;;;;;;OAeG;IACH,YAAY,QAA6C;QAlBhD,iCAAgC;QAmBvC,uBAAA,IAAI,kBAAQ,IAAI,kBAAkB,CAAC,QAAQ,CAAC,MAAA,CAAC;IAC/C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ;QACN,OAAO,uBAAA,IAAI,sBAAK,CAAC,QAAQ,EAAE,CAAC;IAC9B,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,UAAU,CACf,GAA0B;QAE1B,OAAO,IAAI,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CAAC,MAAc,EAAE,GAAW;QAC7B,MAAM,KAAK,GAAG,uBAAA,IAAI,sBAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACjC,OAAO,KAAK,EA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AAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;YACvD,MAAM,IAAI,kBAAkB,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAClD,CAAC;QACD,uBAAA,IAAI,sBAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM;QACJ,OAAO,uBAAA,IAAI,sBAAK,CAAC,MAAM,EAAE,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,sBAAK,CAAC,IAAI,CAAC;IACxB,CAAC;CACF","sourcesContent":["import type { SnapId } from '@metamask/snaps-sdk';\n\nimport { CaseInsensitiveMap } from './CaseInsensitiveMap';\n\n/**\n * Error thrown when an invalid Snap ID is encountered.\n */\nexport class InvalidSnapIdError extends Error {\n /**\n * The ID of the Snap that caused the error.\n */\n snapId: SnapId;\n\n /**\n * The key of the element that caused the error.\n */\n key: string;\n\n /**\n * Creates an instance of `InvalidSnapIdError`.\n *\n * @param snapId - The invalid Snap ID.\n * @param key - The key associated with the invalid Snap ID.\n */\n constructor(snapId: SnapId, key: string) {\n super(`Snap \"${snapId}\" is not allowed to set \"${key}\"`);\n this.name = 'InvalidSnapIdError';\n this.snapId = snapId;\n this.key = key;\n }\n}\n\n/**\n * A map that associates a string key with a value that has a `snapId`\n * property. Note that the key is case-insensitive.\n *\n * The `snapId` property is used to ensure that only the Snap that added an\n * item to the map can modify or delete it.\n */\nexport class SnapIdMap<Value extends { snapId: SnapId }> {\n readonly #map: CaseInsensitiveMap<Value>;\n\n /**\n * Creates a new `SnapIdMap` object.\n *\n * Example:\n *\n * ```ts\n * const items = [\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ];\n * const map = new SnapIdMap(items);\n * ```\n *\n * @param iterable - An iterable object whose elements are key-value pairs.\n * Each key-value pair will be added to the new map.\n */\n constructor(iterable?: Iterable<readonly [string, Value]>) {\n this.#map = new CaseInsensitiveMap(iterable);\n }\n\n /**\n * Returns a plain object with the same key-value pairs as this map.\n *\n * Example:\n *\n * ```ts\n * const items = [\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ];\n * const map = new SnapIdMap(items);\n * map.toObject();\n * // Returns\n * // {\n * // foo: { snapId: '1', name: 'foo' },\n * // bar: { snapId: '1', name: 'bar' },\n * // }\n * ```\n *\n * @returns A plain object with the same key-value pairs as this map.\n */\n toObject(): Record<string, Value> {\n return this.#map.toObject();\n }\n\n /**\n * Returns a new `SnapIdMap` object from an plain object.\n *\n * Example:\n *\n * ```ts\n * const obj = {\n * foo: { snapId: '1', name: 'foo' },\n * bar: { snapId: '1', name: 'bar' },\n * };\n * const map = SnapIdMap.fromObject(obj);\n * ```\n *\n * @param obj - A plain object whose elements will be added to the new map.\n * @returns A new `SnapIdMap` containing the elements of the given object.\n */\n static fromObject<Value extends { snapId: SnapId }>(\n obj: Record<string, Value>,\n ): SnapIdMap<Value> {\n return new SnapIdMap(Object.entries(obj));\n }\n\n /**\n * Gets a value from the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `undefined`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.get('1', 'foo'); // Returns { snapId: '1', name: 'foo' }\n * map.get('2', 'foo'); // Returns `undefined`\n * map.get('1', 'bar'); // Returns `undefined`\n * ```\n *\n * @param snapId - Snap ID present in the value to get.\n * @param key - Key of the element to get.\n * @returns The value associated with the given key and Snap ID.\n */\n get(snapId: SnapId, key: string): Value | undefined {\n const value = this.#map.get(key);\n return value?.snapId === snapId ? value : undefined;\n }\n\n /**\n * Gets a Snap ID that's associated with the given key from the map.\n *\n * @param key - Key of the element to get.\n * @returns The Snap ID associated with the given key.\n */\n getSnapId(key: string): SnapId | undefined {\n return this.#map.get(key)?.snapId;\n }\n\n /**\n * Gets and delete a value from the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `undefined`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.pop('1', 'foo'); // Returns { snapId: '1', name: 'foo' }\n * map.pop('1', 'foo'); // Returns `undefined` (already returned and deleted)\n * map.pop('1', 'bar'); // Returns `undefined`\n * ```\n *\n * @param snapId - Snap ID present in the value to get.\n * @param key - Key of the element to get.\n * @returns The value associated with the given key and Snap ID.\n */\n pop(snapId: SnapId, key: string): Value | undefined {\n const value = this.get(snapId, key);\n this.delete(snapId, key);\n return value;\n }\n\n /**\n * Checks if a key is present in the map.\n *\n * If the given key is not present in the map or the Snap ID of the value is\n * different from the given Snap ID, returns `false`.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.has('1', 'foo'); // Returns `true`\n * map.has('2', 'foo'); // Returns `false`\n * map.has('1', 'bar'); // Returns `false`\n * ```\n *\n * @param snapId - Snap ID present in the value to check.\n * @param key - Key of the element to check.\n * @returns `true` if the key is present in the map and the Snap ID of the\n * value is equal to the given Snap ID, `false` otherwise.\n */\n has(snapId: SnapId, key: string): boolean {\n return this.get(snapId, key) !== undefined;\n }\n\n /**\n * Checks if a snap ID exists in the map.\n *\n * @param snapId - Snap ID present in the value to check.\n * @returns `true` if the snap ID is present in the map, `false` otherwise.\n */\n hasSnapId(snapId: SnapId): boolean {\n // We could use a reverse-mapping to map Snap ID to their actual key too, but\n // for now, this will do the trick.\n return [...this.#map.values()].some((value) => value.snapId === snapId);\n }\n\n /**\n * Deletes a key from the map.\n *\n * If the given key is not present in the map or the Snap IDs don't match,\n * returns `false` and does nothing.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap();\n * map.set('foo', { snapId: '1', name: 'foo' });\n * map.delete('2', 'foo'); // Returns `false`\n * map.delete('1', 'bar'); // Returns `false`\n * map.delete('1', 'foo'); // Returns `true`\n * ```\n *\n * @param snapId - Snap ID present in the value to delete.\n * @param key - Key of the element to delete.\n * @returns `true` if the key was present in the map and the Snap ID of the\n * value was equal to the given Snap ID, `false` otherwise.\n */\n delete(snapId: SnapId, key: string): boolean {\n return this.has(snapId, key) && this.#map.delete(key);\n }\n\n /**\n * Adds or updates a key-value pair in the map.\n *\n * Note that this method has a different behavior from the `Map.set`.\n *\n * - If the given key is not already present in the map, this method adds the\n * key-value pair to the map.\n *\n * - If the given key is already present in the map and the Snap IDs match,\n * this method updates the value associated with the key.\n *\n * - However, if the given key is already present in the map but the Snap IDs\n * do not match, this method throws an error.\n *\n * @param key - Key of the element to add or update.\n * @param value - Value of the element to add or update.\n * @returns The map itself.\n */\n set(key: string, value: Value): this {\n // If the key is present in the map but isn't associated with the given\n // Snap ID, it means that the item was added to the map by a different\n // Snap. In this case, throw an error.\n if (this.#map.has(key) && !this.has(value.snapId, key)) {\n throw new InvalidSnapIdError(value.snapId, key);\n }\n this.#map.set(key, value);\n return this;\n }\n\n /**\n * Returns an iterable of the values in the map.\n *\n * Example:\n *\n * ```ts\n * const map = new SnapIdMap([\n * ['foo', { snapId: '1', name: 'foo' }],\n * ['bar', { snapId: '1', name: 'bar' }],\n * ]);\n * const values = [...map.values()];\n * // Returns\n * // [\n * // { snapId: '1', name: 'foo' },\n * // { snapId: '1', name: 'bar' },\n * // ]\n * ```\n *\n * @returns An iterable of the values in the map.\n */\n values(): IterableIterator<Value> {\n return this.#map.values();\n }\n\n /**\n * Returns the number of key-value pairs in the map.\n *\n * @returns The number of key-value pairs in the map.\n */\n get size(): number {\n return this.#map.size;\n }\n}\n"]}
@@ -10,12 +10,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _SnapKeyring_instances, _SnapKeyring_messenger, _SnapKeyring_snapKeyrings, _SnapKeyring_accountIndex, _SnapKeyring_callbacks, _SnapKeyring_isAnyAccountTypeAllowed, _SnapKeyring_lock, _SnapKeyring_getOrCreateKeyring, _SnapKeyring_removeSnapKeyringIfEmpty, _SnapKeyring_assertAccountCanBeUsed, _SnapKeyring_getAccount, _SnapKeyring_resolveAddress, _SnapKeyring_getSnap, _SnapKeyring_getSnapMetadata, _SnapKeyring_transformToInternalAccount;
13
+ var _SnapKeyring_instances, _SnapKeyring_messenger, _SnapKeyring_snapKeyrings, _SnapKeyring_accountIndex, _SnapKeyring_callbacks, _SnapKeyring_isAnyAccountTypeAllowed, _SnapKeyring_lock, _SnapKeyring_getOrCreateKeyringLock, _SnapKeyring_getOrCreateKeyring, _SnapKeyring_removeSnapKeyringIfEmpty, _SnapKeyring_assertAccountCanBeUsed, _SnapKeyring_getAccount, _SnapKeyring_resolveAddress, _SnapKeyring_getSnap, _SnapKeyring_getSnapMetadata, _SnapKeyring_transformToInternalAccount;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SnapKeyring = exports.SNAP_KEYRING_TYPE = void 0;
16
16
  const eth_sig_util_1 = require("@metamask/eth-sig-util");
17
17
  const keyring_api_1 = require("@metamask/keyring-api");
18
18
  const async_mutex_1 = require("async-mutex");
19
+ const account_1 = require("./account.cjs");
19
20
  const SnapKeyringMessenger_1 = require("./SnapKeyringMessenger.cjs");
20
21
  const util_1 = require("./util.cjs");
21
22
  const SnapKeyring_1 = require("./v2/SnapKeyring.cjs");
@@ -77,6 +78,15 @@ class SnapKeyring {
77
78
  * `withLock` callback.
78
79
  */
79
80
  _SnapKeyring_lock.set(this, void 0);
81
+ /**
82
+ * Serializes lazy keyring initialization in {@link SnapKeyring.#getOrCreateKeyring}.
83
+ *
84
+ * Kept separate from {@link SnapKeyring.#lock} (which guards `createAccounts`
85
+ * uniqueness checks) to avoid mixing concerns. The lock is held only for the
86
+ * duration of a single keyring's initialization - released before the caller
87
+ * proceeds with its operation.
88
+ */
89
+ _SnapKeyring_getOrCreateKeyringLock.set(this, void 0);
80
90
  this.type = SnapKeyring.type;
81
91
  __classPrivateFieldSet(this, _SnapKeyring_messenger, messenger, "f");
82
92
  __classPrivateFieldSet(this, _SnapKeyring_snapKeyrings, new Map(), "f");
@@ -84,6 +94,7 @@ class SnapKeyring {
84
94
  __classPrivateFieldSet(this, _SnapKeyring_callbacks, callbacks, "f");
85
95
  __classPrivateFieldSet(this, _SnapKeyring_isAnyAccountTypeAllowed, isAnyAccountTypeAllowed, "f");
86
96
  __classPrivateFieldSet(this, _SnapKeyring_lock, new async_mutex_1.Mutex(), "f");
97
+ __classPrivateFieldSet(this, _SnapKeyring_getOrCreateKeyringLock, new async_mutex_1.Mutex(), "f");
87
98
  }
88
99
  /**
89
100
  * Handle a message from a Snap.
@@ -103,7 +114,7 @@ class SnapKeyring {
103
114
  // We can create a new keyring if the message is an AccountCreated event.
104
115
  const isAccountCreated = message.method === `${keyring_api_1.KeyringEvent.AccountCreated}`;
105
116
  if (!keyring && isAccountCreated) {
106
- keyring = __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId);
117
+ keyring = await __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId);
107
118
  }
108
119
  if (!keyring) {
109
120
  throw new Error(`SnapKeyring - Received a message for an unknown snap keyring '${snapId}'`);
@@ -166,10 +177,11 @@ class SnapKeyring {
166
177
  // Clear both indexes before rebuilding — they must always be consistent.
167
178
  __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").clear();
168
179
  __classPrivateFieldGet(this, _SnapKeyring_accountIndex, "f").clear();
169
- // Rebuild per-snap keyrings. Each keyrings handles its own validation
170
- // and migration internally.
180
+ // Rebuild per-snap keyrings. Each keyring handles its own validation
181
+ // and migration internally. #getOrCreateKeyring initializes the keyring
182
+ // with an empty state; the second deserialize call loads the real accounts.
171
183
  for (const [snapId, accounts] of bySnap) {
172
- const keyring = __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId);
184
+ const keyring = await __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId);
173
185
  await keyring.deserialize({ snapId, accounts });
174
186
  // onRegister callbacks fired above have repopulated #accountIndex.
175
187
  await __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_removeSnapKeyringIfEmpty).call(this, snapId);
@@ -184,7 +196,7 @@ class SnapKeyring {
184
196
  const addresses = [];
185
197
  for (const keyring of __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").values()) {
186
198
  for (const account of keyring.accounts()) {
187
- addresses.push((0, util_1.normalizeAccountAddress)(account));
199
+ addresses.push((0, account_1.normalizeAccountAddress)(account));
188
200
  }
189
201
  }
190
202
  return (0, util_1.unique)(addresses);
@@ -196,7 +208,7 @@ class SnapKeyring {
196
208
  * @returns The addresses of the accounts associated with the given Snap.
197
209
  */
198
210
  async getAccountsBySnapId(snapId) {
199
- return (0, util_1.unique)((__classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").get(snapId)?.accounts() ?? []).map(util_1.normalizeAccountAddress));
211
+ return (0, util_1.unique)((__classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").get(snapId)?.accounts() ?? []).map(account_1.normalizeAccountAddress));
200
212
  }
201
213
  /**
202
214
  * Create an account (v1 event-driven flow).
@@ -209,7 +221,8 @@ class SnapKeyring {
209
221
  * @returns The account object.
210
222
  */
211
223
  async createAccount(snapId, options, internalOptions) {
212
- return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId).createAccount(options, internalOptions);
224
+ const keyring = await __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId);
225
+ return keyring.createAccount(options, internalOptions);
213
226
  }
214
227
  /**
215
228
  * Creates one or more new accounts according to the provided options.
@@ -222,7 +235,8 @@ class SnapKeyring {
222
235
  * @returns An array of the created account objects.
223
236
  */
224
237
  async createAccounts(snapId, options) {
225
- return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId).createAccounts(options);
238
+ const keyring = await __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId);
239
+ return keyring.createAccounts(options);
226
240
  }
227
241
  /**
228
242
  * Checks if a Snap ID is known from the keyring.
@@ -248,7 +262,8 @@ class SnapKeyring {
248
262
  if (!this.hasSnapId(snapId)) {
249
263
  throw new Error(`Unable to resolve account address: unknown Snap ID: ${snapId}`);
250
264
  }
251
- return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId).resolveAccountAddress(scope, request);
265
+ const keyring = await __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getOrCreateKeyring).call(this, snapId);
266
+ return keyring.resolveAccountAddress(scope, request);
252
267
  }
253
268
  /**
254
269
  * Submit a request to a Snap from an account ID.
@@ -412,7 +427,6 @@ class SnapKeyring {
412
427
  bySnap.set(snapId, snapAccounts);
413
428
  }
414
429
  await Promise.all([...__classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").entries()].map(async ([snapId, keyring]) => keyring.setSelectedAccounts(
415
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
416
430
  /* istanbul ignore next */
417
431
  bySnap.get(snapId) ?? [])));
418
432
  }
@@ -449,31 +463,67 @@ class SnapKeyring {
449
463
  }
450
464
  }
451
465
  exports.SnapKeyring = SnapKeyring;
452
- _SnapKeyring_messenger = new WeakMap(), _SnapKeyring_snapKeyrings = new WeakMap(), _SnapKeyring_accountIndex = new WeakMap(), _SnapKeyring_callbacks = new WeakMap(), _SnapKeyring_isAnyAccountTypeAllowed = new WeakMap(), _SnapKeyring_lock = new WeakMap(), _SnapKeyring_instances = new WeakSet(), _SnapKeyring_getOrCreateKeyring = function _SnapKeyring_getOrCreateKeyring(snapId) {
453
- let keyring = __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").get(snapId);
454
- if (!keyring) {
455
- keyring = new SnapKeyring_1.SnapKeyring({
456
- snapId,
466
+ _SnapKeyring_messenger = new WeakMap(), _SnapKeyring_snapKeyrings = new WeakMap(), _SnapKeyring_accountIndex = new WeakMap(), _SnapKeyring_callbacks = new WeakMap(), _SnapKeyring_isAnyAccountTypeAllowed = new WeakMap(), _SnapKeyring_lock = new WeakMap(), _SnapKeyring_getOrCreateKeyringLock = new WeakMap(), _SnapKeyring_instances = new WeakSet(), _SnapKeyring_getOrCreateKeyring =
467
+ /**
468
+ * Get the SnapKeyringEntry for a Snap, creating and initializing it if it
469
+ * does not exist yet.
470
+ *
471
+ * When a new keyring is created it is immediately initialized by calling
472
+ * `deserialize({ snapId, accounts: {} })` so that `snapId` and the internal
473
+ * snap client are available before any event handler or method runs.
474
+ *
475
+ * Both v1 and v2 share the same KeyringAccountRegistry instance. The
476
+ * onRegister / onUnregister callbacks keep #accountIndex in sync regardless
477
+ * of which class mutates the registry.
478
+ *
479
+ * @param snapId - Snap ID.
480
+ * @returns The SnapKeyringEntry for the given Snap.
481
+ */
482
+ async function _SnapKeyring_getOrCreateKeyring(snapId) {
483
+ // Fast path: keyring already exists, no lock needed.
484
+ const existing = __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").get(snapId);
485
+ if (existing) {
486
+ return existing;
487
+ }
488
+ return __classPrivateFieldGet(this, _SnapKeyring_getOrCreateKeyringLock, "f").runExclusive(async () => {
489
+ // Double-check: a concurrent caller may have created the keyring while
490
+ // we were waiting for the lock.
491
+ const keyring = __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").get(snapId);
492
+ if (keyring) {
493
+ return keyring;
494
+ }
495
+ const newKeyring = new SnapKeyring_1.SnapKeyring({
457
496
  messenger: __classPrivateFieldGet(this, _SnapKeyring_messenger, "f"),
458
497
  isAnyAccountTypeAllowed: __classPrivateFieldGet(this, _SnapKeyring_isAnyAccountTypeAllowed, "f"),
459
498
  callbacks: {
499
+ // Optional callbacks that mutate accounts are responsible for keeping
500
+ // #accountIndex in sync via these callbacks.
460
501
  onRegister: (id) => {
502
+ // We can safely use `snapId` here because we create the keyring
503
+ // instance right away.
461
504
  __classPrivateFieldGet(this, _SnapKeyring_accountIndex, "f").set(id, snapId);
462
505
  },
463
506
  onUnregister: (id) => {
464
507
  __classPrivateFieldGet(this, _SnapKeyring_accountIndex, "f").delete(id);
465
508
  },
466
- addAccount: async (address, handleUserInput, onceSaved, accountNameSuggestion, internalOptions) => __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").addAccount(address, snapId, handleUserInput, onceSaved, accountNameSuggestion, internalOptions),
467
- removeAccount: async (address, handleUserInput) => __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").removeAccount(address, snapId, handleUserInput),
509
+ // Required callbacks:
510
+ // NOTE: `keyringSnapId` and `snapId` are the same value. The `keyringSnapId` is coming from
511
+ // the `deserialize` call right after.
512
+ addAccount: async (address, keyringSnapId, handleUserInput, onceSaved, accountNameSuggestion, internalOptions) => __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").addAccount(address, keyringSnapId, handleUserInput, onceSaved, accountNameSuggestion, internalOptions),
513
+ removeAccount: async (address, keyringSnapId, handleUserInput) => __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").removeAccount(address, keyringSnapId, handleUserInput),
468
514
  saveState: async () => __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").saveState(),
469
- redirectUser: async (url, message) => __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").redirectUser(snapId, url, message),
515
+ redirectUser: async (id, url, message) => __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").redirectUser(id, url, message),
470
516
  assertAccountCanBeUsed: async (account) => __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_assertAccountCanBeUsed).call(this, account),
471
517
  withLock: async (callback) => __classPrivateFieldGet(this, _SnapKeyring_lock, "f").runExclusive(callback),
472
518
  },
473
519
  });
474
- __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").set(snapId, keyring);
475
- }
476
- return keyring;
520
+ // Initialize the keyring with its snap ID (no accounts yet).
521
+ await newKeyring.deserialize({ snapId, accounts: {} });
522
+ // Keyring is fully initialized; register it before releasing the lock so
523
+ // that no concurrent caller can create a duplicate.
524
+ __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").set(snapId, newKeyring);
525
+ return newKeyring;
526
+ });
477
527
  }, _SnapKeyring_removeSnapKeyringIfEmpty =
478
528
  /**
479
529
  * Drop a per-snap keyring from {@link #snapKeyrings} when it has no accounts.
@@ -486,7 +536,7 @@ _SnapKeyring_messenger = new WeakMap(), _SnapKeyring_snapKeyrings = new WeakMap(
486
536
  */
487
537
  async function _SnapKeyring_removeSnapKeyringIfEmpty(snapId) {
488
538
  const keyring = __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").get(snapId);
489
- if (keyring !== undefined && keyring.accounts().length === 0) {
539
+ if (keyring?.accounts().length === 0) {
490
540
  await keyring.destroy();
491
541
  __classPrivateFieldGet(this, _SnapKeyring_snapKeyrings, "f").delete(snapId);
492
542
  }
@@ -499,7 +549,7 @@ async function _SnapKeyring_removeSnapKeyringIfEmpty(snapId) {
499
549
  * @throws If the account cannot be used.
500
550
  */
501
551
  async function _SnapKeyring_assertAccountCanBeUsed(account) {
502
- const address = (0, util_1.normalizeAccountAddress)(account);
552
+ const address = (0, account_1.normalizeAccountAddress)(account);
503
553
  // The `AnyAccountType.Account` generic account type is allowed only during
504
554
  // development, so we check whether it's allowed before continuing.
505
555
  if (!__classPrivateFieldGet(this, _SnapKeyring_isAnyAccountTypeAllowed, "f") &&
@@ -539,9 +589,7 @@ async function _SnapKeyring_assertAccountCanBeUsed(account) {
539
589
  return __classPrivateFieldGet(this, _SnapKeyring_messenger, "f").call('SnapController:getSnap', snapId);
540
590
  }, _SnapKeyring_getSnapMetadata = function _SnapKeyring_getSnapMetadata(snapId) {
541
591
  const snap = __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getSnap).call(this, snapId);
542
- return snap
543
- ? { id: snapId, name: snap.manifest.proposedName, enabled: snap.enabled }
544
- : undefined;
592
+ return snap ? { id: snapId } : undefined;
545
593
  }, _SnapKeyring_transformToInternalAccount = function _SnapKeyring_transformToInternalAccount(account, snapId) {
546
594
  const snap = __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getSnapMetadata).call(this, snapId);
547
595
  return {
@@ -555,7 +603,7 @@ async function _SnapKeyring_assertAccountCanBeUsed(account) {
555
603
  //
556
604
  // NOTE: We convert the address only for EVM accounts, see
557
605
  // `normalizeAccountAddress`.
558
- address: (0, util_1.normalizeAccountAddress)(account),
606
+ address: (0, account_1.normalizeAccountAddress)(account),
559
607
  metadata: {
560
608
  name: '',
561
609
  importTime: 0,