@metamask-previews/eth-snap-keyring 12.0.0-cfe31db → 12.1.0-24dfaca
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.
- package/CHANGELOG.md +10 -1
- package/dist/SnapIdMap.cjs +25 -0
- package/dist/SnapIdMap.cjs.map +1 -1
- package/dist/SnapIdMap.d.cts +21 -0
- package/dist/SnapIdMap.d.cts.map +1 -1
- package/dist/SnapIdMap.d.mts +21 -0
- package/dist/SnapIdMap.d.mts.map +1 -1
- package/dist/SnapIdMap.mjs +25 -0
- package/dist/SnapIdMap.mjs.map +1 -1
- package/dist/SnapKeyring.cjs +115 -38
- package/dist/SnapKeyring.cjs.map +1 -1
- package/dist/SnapKeyring.d.cts +11 -1
- package/dist/SnapKeyring.d.cts.map +1 -1
- package/dist/SnapKeyring.d.mts +11 -1
- package/dist/SnapKeyring.d.mts.map +1 -1
- package/dist/SnapKeyring.mjs +116 -39
- package/dist/SnapKeyring.mjs.map +1 -1
- package/dist/events.d.cts +11 -0
- package/dist/events.d.cts.map +1 -1
- package/dist/events.d.mts +11 -0
- package/dist/events.d.mts.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/options.cjs +47 -0
- package/dist/options.cjs.map +1 -0
- package/dist/options.d.cts +46 -0
- package/dist/options.d.cts.map +1 -0
- package/dist/options.d.mts +46 -0
- package/dist/options.d.mts.map +1 -0
- package/dist/options.mjs +43 -0
- package/dist/options.mjs.map +1 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [12.1.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add new concept of "internal options" and add a new `SnapKeyring.createAccount` method ([#252](https://github.com/MetaMask/accounts/pull/252)), ([#261](https://github.com/MetaMask/accounts/pull/261))
|
|
15
|
+
- Those internal options can be used by the caller of that method to customize the account creation flow.
|
|
16
|
+
- Add `setSelectedAccount` internal option ([#257](https://github.com/MetaMask/accounts/pull/257))
|
|
17
|
+
|
|
10
18
|
## [12.0.0]
|
|
11
19
|
|
|
12
20
|
### Changed
|
|
@@ -490,7 +498,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
490
498
|
|
|
491
499
|
- Initial release.
|
|
492
500
|
|
|
493
|
-
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@12.
|
|
501
|
+
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@12.1.0...HEAD
|
|
502
|
+
[12.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@12.0.0...@metamask/eth-snap-keyring@12.1.0
|
|
494
503
|
[12.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@11.1.0...@metamask/eth-snap-keyring@12.0.0
|
|
495
504
|
[11.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@11.0.0...@metamask/eth-snap-keyring@11.1.0
|
|
496
505
|
[11.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-snap-keyring@10.0.0...@metamask/eth-snap-keyring@11.0.0
|
package/dist/SnapIdMap.cjs
CHANGED
|
@@ -127,6 +127,31 @@ class SnapIdMap {
|
|
|
127
127
|
const value = __classPrivateFieldGet(this, _SnapIdMap_map, "f").get(key);
|
|
128
128
|
return value?.snapId === snapId ? value : undefined;
|
|
129
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Gets and delete a value from the map.
|
|
132
|
+
*
|
|
133
|
+
* If the given key is not present in the map or the Snap ID of the value is
|
|
134
|
+
* different from the given Snap ID, returns `undefined`.
|
|
135
|
+
*
|
|
136
|
+
* Example:
|
|
137
|
+
*
|
|
138
|
+
* ```ts
|
|
139
|
+
* const map = new SnapIdMap();
|
|
140
|
+
* map.set('foo', { snapId: '1', name: 'foo' });
|
|
141
|
+
* map.pop('1', 'foo'); // Returns { snapId: '1', name: 'foo' }
|
|
142
|
+
* map.pop('1', 'foo'); // Returns `undefined` (already returned and deleted)
|
|
143
|
+
* map.pop('1', 'bar'); // Returns `undefined`
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @param snapId - Snap ID present in the value to get.
|
|
147
|
+
* @param key - Key of the element to get.
|
|
148
|
+
* @returns The value associated with the given key and Snap ID.
|
|
149
|
+
*/
|
|
150
|
+
pop(snapId, key) {
|
|
151
|
+
const value = this.get(snapId, key);
|
|
152
|
+
this.delete(snapId, key);
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
130
155
|
/**
|
|
131
156
|
* Checks if a key is present in the map.
|
|
132
157
|
*
|
package/dist/SnapIdMap.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;CACF;AAvBD,gDAuBC;AAED;;;;;;GAMG;AACH,MAAa,SAAS;IAGpB;;;;;;;;;;;;;;;OAeG;IACH,YAAY,QAA6C;QAlBhD,i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C;IAED,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;AA5ND,8BA4NC","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 * 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,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,GAAG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7E,mCAAmC;QACnC,OAAO,CAAC,GAAG,uBAAA,IAAI,sBAAK,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC;IAC1E,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,MAAM,CAAC,MAAc,EAAE,GAAW;QAChC,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,uBAAA,IAAI,sBAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACxD,CAAC;IAED,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;AAtPD,8BAsPC","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 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"]}
|
package/dist/SnapIdMap.d.cts
CHANGED
|
@@ -109,6 +109,27 @@ export declare class SnapIdMap<Value extends {
|
|
|
109
109
|
* @returns The value associated with the given key and Snap ID.
|
|
110
110
|
*/
|
|
111
111
|
get(snapId: SnapId, key: string): Value | undefined;
|
|
112
|
+
/**
|
|
113
|
+
* Gets and delete a value from the map.
|
|
114
|
+
*
|
|
115
|
+
* If the given key is not present in the map or the Snap ID of the value is
|
|
116
|
+
* different from the given Snap ID, returns `undefined`.
|
|
117
|
+
*
|
|
118
|
+
* Example:
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* const map = new SnapIdMap();
|
|
122
|
+
* map.set('foo', { snapId: '1', name: 'foo' });
|
|
123
|
+
* map.pop('1', 'foo'); // Returns { snapId: '1', name: 'foo' }
|
|
124
|
+
* map.pop('1', 'foo'); // Returns `undefined` (already returned and deleted)
|
|
125
|
+
* map.pop('1', 'bar'); // Returns `undefined`
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @param snapId - Snap ID present in the value to get.
|
|
129
|
+
* @param key - Key of the element to get.
|
|
130
|
+
* @returns The value associated with the given key and Snap ID.
|
|
131
|
+
*/
|
|
132
|
+
pop(snapId: SnapId, key: string): Value | undefined;
|
|
112
133
|
/**
|
|
113
134
|
* Checks if a key is present in the map.
|
|
114
135
|
*
|
package/dist/SnapIdMap.d.cts.map
CHANGED
|
@@ -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;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ,IAAI,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;IAIjC;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,UAAU,CAAC,KAAK,SAAS;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,EAChD,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GACzB,SAAS,CAAC,KAAK,CAAC;IAInB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS;IAKnD;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzC;;;;;OAKG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAMlC;;;;;;;;;;;;;;;;;;;;OAoB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M,GAAG,KAAK,GAAG,SAAS;IAKnD;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS;IAMnD;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzC;;;;;OAKG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAMlC;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAK5C;;;;;;;;;;;;;;;;;OAiBG;IAEH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI;IAWpC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM,IAAI,gBAAgB,CAAC,KAAK,CAAC;IAIjC;;;;OAIG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF"}
|
package/dist/SnapIdMap.d.mts
CHANGED
|
@@ -109,6 +109,27 @@ export declare class SnapIdMap<Value extends {
|
|
|
109
109
|
* @returns The value associated with the given key and Snap ID.
|
|
110
110
|
*/
|
|
111
111
|
get(snapId: SnapId, key: string): Value | undefined;
|
|
112
|
+
/**
|
|
113
|
+
* Gets and delete a value from the map.
|
|
114
|
+
*
|
|
115
|
+
* If the given key is not present in the map or the Snap ID of the value is
|
|
116
|
+
* different from the given Snap ID, returns `undefined`.
|
|
117
|
+
*
|
|
118
|
+
* Example:
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* const map = new SnapIdMap();
|
|
122
|
+
* map.set('foo', { snapId: '1', name: 'foo' });
|
|
123
|
+
* map.pop('1', 'foo'); // Returns { snapId: '1', name: 'foo' }
|
|
124
|
+
* map.pop('1', 'foo'); // Returns `undefined` (already returned and deleted)
|
|
125
|
+
* map.pop('1', 'bar'); // Returns `undefined`
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @param snapId - Snap ID present in the value to get.
|
|
129
|
+
* @param key - Key of the element to get.
|
|
130
|
+
* @returns The value associated with the given key and Snap ID.
|
|
131
|
+
*/
|
|
132
|
+
pop(snapId: SnapId, key: string): Value | undefined;
|
|
112
133
|
/**
|
|
113
134
|
* Checks if a key is present in the map.
|
|
114
135
|
*
|
package/dist/SnapIdMap.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapIdMap.d.mts","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;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ,IAAI,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;IAIjC;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,UAAU,CAAC,KAAK,SAAS;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,EAChD,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GACzB,SAAS,CAAC,KAAK,CAAC;IAInB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,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M,CAAC,UAAU,CAAC,KAAK,SAAS;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,EAChD,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GACzB,SAAS,CAAC,KAAK,CAAC;IAInB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS;IAKnD;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS;IAMnD;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzC;;;;;OAKG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAMlC;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAK5C;;;;;;;;;;;;;;;;;OAiBG;IAEH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI;IAWpC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM,IAAI,gBAAgB,CAAC,KAAK,CAAC;IAIjC;;;;OAIG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF"}
|
package/dist/SnapIdMap.mjs
CHANGED
|
@@ -123,6 +123,31 @@ export class SnapIdMap {
|
|
|
123
123
|
const value = __classPrivateFieldGet(this, _SnapIdMap_map, "f").get(key);
|
|
124
124
|
return value?.snapId === snapId ? value : undefined;
|
|
125
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Gets and delete a value from the map.
|
|
128
|
+
*
|
|
129
|
+
* If the given key is not present in the map or the Snap ID of the value is
|
|
130
|
+
* different from the given Snap ID, returns `undefined`.
|
|
131
|
+
*
|
|
132
|
+
* Example:
|
|
133
|
+
*
|
|
134
|
+
* ```ts
|
|
135
|
+
* const map = new SnapIdMap();
|
|
136
|
+
* map.set('foo', { snapId: '1', name: 'foo' });
|
|
137
|
+
* map.pop('1', 'foo'); // Returns { snapId: '1', name: 'foo' }
|
|
138
|
+
* map.pop('1', 'foo'); // Returns `undefined` (already returned and deleted)
|
|
139
|
+
* map.pop('1', 'bar'); // Returns `undefined`
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @param snapId - Snap ID present in the value to get.
|
|
143
|
+
* @param key - Key of the element to get.
|
|
144
|
+
* @returns The value associated with the given key and Snap ID.
|
|
145
|
+
*/
|
|
146
|
+
pop(snapId, key) {
|
|
147
|
+
const value = this.get(snapId, key);
|
|
148
|
+
this.delete(snapId, key);
|
|
149
|
+
return value;
|
|
150
|
+
}
|
|
126
151
|
/**
|
|
127
152
|
* Checks if a key is present in the map.
|
|
128
153
|
*
|
package/dist/SnapIdMap.mjs.map
CHANGED
|
@@ -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,IA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;;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 * 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,C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uBAAA,IAAI,sBAAK,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC;IAC1E,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,MAAM,CAAC,MAAc,EAAE,GAAW;QAChC,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,uBAAA,IAAI,sBAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACxD,CAAC;IAED,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 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"]}
|
package/dist/SnapKeyring.cjs
CHANGED
|
@@ -13,7 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
13
13
|
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");
|
|
14
14
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
15
15
|
};
|
|
16
|
-
var _SnapKeyring_instances, _SnapKeyring_messenger, _SnapKeyring_snapClient, _SnapKeyring_accounts, _SnapKeyring_requests, _SnapKeyring_callbacks, _SnapKeyring_handleAccountCreated, _SnapKeyring_handleAccountUpdated, _SnapKeyring_handleAccountDeleted, _SnapKeyring_handleRequestApproved, _SnapKeyring_handleRequestRejected, _SnapKeyring_rePublishAccountEvent,
|
|
16
|
+
var _SnapKeyring_instances, _SnapKeyring_messenger, _SnapKeyring_snapClient, _SnapKeyring_accounts, _SnapKeyring_requests, _SnapKeyring_options, _SnapKeyring_callbacks, _SnapKeyring_getInternalOptions, _SnapKeyring_handleAccountCreated, _SnapKeyring_handleAccountUpdated, _SnapKeyring_handleAccountDeleted, _SnapKeyring_handleRequestApproved, _SnapKeyring_handleRequestRejected, _SnapKeyring_rePublishAccountEvent, _SnapKeyring_handleAccountBalancesUpdated, _SnapKeyring_handleAccountAssetListUpdated, _SnapKeyring_handleAccountTransactionsUpdated, _SnapKeyring_getAccount, _SnapKeyring_submitRequest, _SnapKeyring_submitSnapRequest, _SnapKeyring_hasMethod, _SnapKeyring_createRequestPromise, _SnapKeyring_clearRequestPromise, _SnapKeyring_handleSyncResponse, _SnapKeyring_handleAsyncResponse, _SnapKeyring_sanitizeRedirectUrl, _SnapKeyring_validateRedirectUrl, _SnapKeyring_deleteAccount, _SnapKeyring_resolveAddress, _SnapKeyring_getSnap, _SnapKeyring_getSnapMetadata, _SnapKeyring_getSnapAllowedOrigins;
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.SnapKeyring = exports.SNAP_KEYRING_TYPE = void 0;
|
|
19
19
|
const tx_1 = require("@ethereumjs/tx");
|
|
@@ -30,6 +30,7 @@ const DeferredPromise_1 = require("./DeferredPromise.cjs");
|
|
|
30
30
|
const events_2 = require("./events.cjs");
|
|
31
31
|
const logger_1 = require("./logger.cjs");
|
|
32
32
|
const migrations_1 = require("./migrations/index.cjs");
|
|
33
|
+
const options_1 = require("./options.cjs");
|
|
33
34
|
const SnapIdMap_1 = require("./SnapIdMap.cjs");
|
|
34
35
|
const types_1 = require("./types.cjs");
|
|
35
36
|
const util_1 = require("./util.cjs");
|
|
@@ -79,6 +80,10 @@ class SnapKeyring extends events_1.EventEmitter {
|
|
|
79
80
|
* Mapping between request IDs and their deferred promises.
|
|
80
81
|
*/
|
|
81
82
|
_SnapKeyring_requests.set(this, void 0);
|
|
83
|
+
/**
|
|
84
|
+
* Mapping between internal options, a correlation ID and a Snap ID.
|
|
85
|
+
*/
|
|
86
|
+
_SnapKeyring_options.set(this, void 0);
|
|
82
87
|
/**
|
|
83
88
|
* Callbacks used to interact with other components.
|
|
84
89
|
*/
|
|
@@ -88,6 +93,7 @@ class SnapKeyring extends events_1.EventEmitter {
|
|
|
88
93
|
__classPrivateFieldSet(this, _SnapKeyring_snapClient, new keyring_internal_snap_client_1.KeyringInternalSnapClient({ messenger }), "f");
|
|
89
94
|
__classPrivateFieldSet(this, _SnapKeyring_requests, new SnapIdMap_1.SnapIdMap(), "f");
|
|
90
95
|
__classPrivateFieldSet(this, _SnapKeyring_accounts, new SnapIdMap_1.SnapIdMap(), "f");
|
|
96
|
+
__classPrivateFieldSet(this, _SnapKeyring_options, new SnapIdMap_1.SnapIdMap(), "f");
|
|
91
97
|
__classPrivateFieldSet(this, _SnapKeyring_callbacks, callbacks, "f");
|
|
92
98
|
}
|
|
93
99
|
/**
|
|
@@ -189,6 +195,51 @@ class SnapKeyring extends events_1.EventEmitter {
|
|
|
189
195
|
.filter(({ snapId: accountSnapId }) => accountSnapId === snapId)
|
|
190
196
|
.map(({ account }) => normalizeAccountAddress(account)));
|
|
191
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Create an account.
|
|
200
|
+
*
|
|
201
|
+
* @param snapId - Snap ID to create the account for.
|
|
202
|
+
* @param options - Account creation options. Differs between keyrings.
|
|
203
|
+
* @param internalOptions - Internal Snap keyring options.
|
|
204
|
+
* @returns The account object.
|
|
205
|
+
*/
|
|
206
|
+
async createAccount(snapId, options, internalOptions) {
|
|
207
|
+
const client = new keyring_internal_snap_client_1.KeyringInternalSnapClient({
|
|
208
|
+
messenger: __classPrivateFieldGet(this, _SnapKeyring_messenger, "f"),
|
|
209
|
+
snapId,
|
|
210
|
+
});
|
|
211
|
+
// The 'metamask' field is reserved, so we have to prevent use of it on
|
|
212
|
+
// the "normal options".
|
|
213
|
+
const reserved = 'metamask';
|
|
214
|
+
if ((0, utils_1.hasProperty)(options, reserved)) {
|
|
215
|
+
throw new Error(`The '${reserved}' property is reserved for internal use`);
|
|
216
|
+
}
|
|
217
|
+
// Those internal options are optional. If not set, we avoid registering anything
|
|
218
|
+
// to internal map (to avoid holding resources for nothing). In this case, it's
|
|
219
|
+
// just a normal `keyring_createAccount`.
|
|
220
|
+
if (!internalOptions) {
|
|
221
|
+
return await client.createAccount(options);
|
|
222
|
+
}
|
|
223
|
+
// A unique ID to identify this execution flow which allows to associate the
|
|
224
|
+
// internal options and the current `keyring_createAccount` flow for that Snap.
|
|
225
|
+
const correlationId = (0, uuid_1.v4)();
|
|
226
|
+
// Register those internal options to use them during the `keyring_createAccount`
|
|
227
|
+
// flow.
|
|
228
|
+
__classPrivateFieldGet(this, _SnapKeyring_options, "f").set(correlationId, {
|
|
229
|
+
snapId,
|
|
230
|
+
options: internalOptions,
|
|
231
|
+
});
|
|
232
|
+
return await client.createAccount({
|
|
233
|
+
...options,
|
|
234
|
+
// Create internal options context.
|
|
235
|
+
// NOTE: Those options HAVE TO be re-emitted during the `notify:accountCreated` event.
|
|
236
|
+
...{
|
|
237
|
+
metamask: {
|
|
238
|
+
correlationId,
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
}
|
|
192
243
|
/**
|
|
193
244
|
* Checks if a Snap ID is known from the keyring.
|
|
194
245
|
*
|
|
@@ -459,7 +510,23 @@ class SnapKeyring extends events_1.EventEmitter {
|
|
|
459
510
|
}
|
|
460
511
|
}
|
|
461
512
|
exports.SnapKeyring = SnapKeyring;
|
|
462
|
-
_SnapKeyring_messenger = new WeakMap(), _SnapKeyring_snapClient = new WeakMap(), _SnapKeyring_accounts = new WeakMap(), _SnapKeyring_requests = new WeakMap(), _SnapKeyring_callbacks = new WeakMap(), _SnapKeyring_instances = new WeakSet(),
|
|
513
|
+
_SnapKeyring_messenger = new WeakMap(), _SnapKeyring_snapClient = new WeakMap(), _SnapKeyring_accounts = new WeakMap(), _SnapKeyring_requests = new WeakMap(), _SnapKeyring_options = new WeakMap(), _SnapKeyring_callbacks = new WeakMap(), _SnapKeyring_instances = new WeakSet(), _SnapKeyring_getInternalOptions = function _SnapKeyring_getInternalOptions(snapId, correlationId) {
|
|
514
|
+
if (correlationId) {
|
|
515
|
+
// We still need to check if the correlation ID is valid and associated to
|
|
516
|
+
// some internal options.
|
|
517
|
+
//
|
|
518
|
+
// NOTE: `found` will be `undefined` if a Snap tried to use a correlation ID that
|
|
519
|
+
// belongs to another Snap ID. However, if a Snap starts 2 parallel flow (which
|
|
520
|
+
// will results in 2 different correlation IDs), we won't be able to prevent
|
|
521
|
+
// the Snap from swapping/mixing up those correlation IDs he owns.
|
|
522
|
+
const found = __classPrivateFieldGet(this, _SnapKeyring_options, "f").pop(snapId, correlationId);
|
|
523
|
+
if (found) {
|
|
524
|
+
return found.options;
|
|
525
|
+
}
|
|
526
|
+
console.warn(`SnapKeyring - Received unmapped correlation ID: "${correlationId}"`);
|
|
527
|
+
}
|
|
528
|
+
return undefined;
|
|
529
|
+
}, _SnapKeyring_handleAccountCreated =
|
|
463
530
|
/**
|
|
464
531
|
* Handle an Account Created event from a Snap.
|
|
465
532
|
*
|
|
@@ -469,7 +536,8 @@ _SnapKeyring_messenger = new WeakMap(), _SnapKeyring_snapClient = new WeakMap(),
|
|
|
469
536
|
*/
|
|
470
537
|
async function _SnapKeyring_handleAccountCreated(snapId, message) {
|
|
471
538
|
(0, superstruct_1.assert)(message, events_2.AccountCreatedEventStruct);
|
|
472
|
-
const {
|
|
539
|
+
const { metamask, // Used for internal options.
|
|
540
|
+
account: newAccountFromEvent, accountNameSuggestion, displayAccountNameSuggestion, displayConfirmation, } = message.params;
|
|
473
541
|
// READ THIS CAREFULLY:
|
|
474
542
|
// ------------------------------------------------------------------------
|
|
475
543
|
// The account creation flow is now asynchronous. We expect the Snap to
|
|
@@ -536,7 +604,15 @@ async function _SnapKeyring_handleAccountCreated(snapId, message) {
|
|
|
536
604
|
onceSaved.reject(error);
|
|
537
605
|
});
|
|
538
606
|
}
|
|
539
|
-
}, onceSaved.promise, accountNameSuggestion,
|
|
607
|
+
}, onceSaved.promise, accountNameSuggestion, (0, options_1.getInternalOptionsOf)([
|
|
608
|
+
// 1. We use the internal options from the Snap keyring.
|
|
609
|
+
__classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getInternalOptions).call(this, snapId, metamask?.correlationId) ?? {},
|
|
610
|
+
// 2. We use the ones coming from the Snap.
|
|
611
|
+
{
|
|
612
|
+
displayConfirmation,
|
|
613
|
+
displayAccountNameSuggestion,
|
|
614
|
+
},
|
|
615
|
+
]));
|
|
540
616
|
return null;
|
|
541
617
|
}, _SnapKeyring_handleAccountUpdated =
|
|
542
618
|
/**
|
|
@@ -625,27 +701,31 @@ async function _SnapKeyring_handleRequestRejected(snapId, message) {
|
|
|
625
701
|
/**
|
|
626
702
|
* Re-publish an account event.
|
|
627
703
|
*
|
|
704
|
+
* @param snapId - Snap ID.
|
|
628
705
|
* @param event - The event type. This is a unique identifier for this event.
|
|
629
|
-
* @param
|
|
630
|
-
*
|
|
706
|
+
* @param filteredEventCallback - A callback that returns the event to re-publish. This callback takes a filtering
|
|
707
|
+
* function as parameter that can be used to filter out account ID that do not belong to this Snap ID.
|
|
631
708
|
* @template EventType - A Snap keyring event type.
|
|
632
709
|
* @returns `null`.
|
|
633
710
|
*/
|
|
634
|
-
async function _SnapKeyring_rePublishAccountEvent(event,
|
|
635
|
-
|
|
711
|
+
async function _SnapKeyring_rePublishAccountEvent(snapId, event, filteredEventCallback) {
|
|
712
|
+
// This callback can be used to filter out the accounts that no longer exists on the Snap (fail-safe) or to
|
|
713
|
+
// prevent other Snaps from updating accounts they do not own.
|
|
714
|
+
const filter = (accountMapping) => {
|
|
715
|
+
return Object.entries(accountMapping).reduce((filtered, [accountId, entry]) => {
|
|
716
|
+
if (__classPrivateFieldGet(this, _SnapKeyring_accounts, "f").has(snapId, accountId)) {
|
|
717
|
+
// If the Snap owns this account, we can use it.
|
|
718
|
+
filtered[accountId] = entry;
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
// Otherwise, we just filter it out and log it (for debugging/tracking purposes).
|
|
722
|
+
console.warn(`SnapKeyring - ${event} - Found an unknown account ID "${accountId}" for Snap ID "${snapId}". Skipping.`);
|
|
723
|
+
}
|
|
724
|
+
return filtered;
|
|
725
|
+
}, {});
|
|
726
|
+
};
|
|
727
|
+
__classPrivateFieldGet(this, _SnapKeyring_messenger, "f").publish(event, ...filteredEventCallback(filter));
|
|
636
728
|
return null;
|
|
637
|
-
}, _SnapKeyring_filterAccountEventEntries = function _SnapKeyring_filterAccountEventEntries(snapId, eventName, eventEntries) {
|
|
638
|
-
const entries = {};
|
|
639
|
-
for (const [accountId, entry] of Object.entries(eventEntries)) {
|
|
640
|
-
const account = __classPrivateFieldGet(this, _SnapKeyring_accounts, "f").get(snapId, accountId);
|
|
641
|
-
if (account) {
|
|
642
|
-
entries[accountId] = entry;
|
|
643
|
-
}
|
|
644
|
-
else {
|
|
645
|
-
console.warn(`SnapKeyring - ${eventName} - Found an unknown account ID "${accountId}" for Snap ID "${snapId}". Skipping.`);
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
return entries;
|
|
649
729
|
}, _SnapKeyring_handleAccountBalancesUpdated =
|
|
650
730
|
/**
|
|
651
731
|
* Handle a balances updated event from a Snap.
|
|
@@ -656,12 +736,11 @@ async function _SnapKeyring_rePublishAccountEvent(event, ...payload) {
|
|
|
656
736
|
*/
|
|
657
737
|
async function _SnapKeyring_handleAccountBalancesUpdated(snapId, message) {
|
|
658
738
|
(0, superstruct_1.assert)(message, keyring_api_1.AccountBalancesUpdatedEventStruct);
|
|
659
|
-
const
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
};
|
|
664
|
-
return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_rePublishAccountEvent).call(this, 'SnapKeyring:accountBalancesUpdated', event);
|
|
739
|
+
const event = message.params;
|
|
740
|
+
return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_rePublishAccountEvent).call(this, snapId, 'SnapKeyring:accountBalancesUpdated', (filter) => {
|
|
741
|
+
event.balances = filter(event.balances);
|
|
742
|
+
return [event];
|
|
743
|
+
});
|
|
665
744
|
}, _SnapKeyring_handleAccountAssetListUpdated =
|
|
666
745
|
/**
|
|
667
746
|
* Handle a asset list updated event from a Snap.
|
|
@@ -672,12 +751,11 @@ async function _SnapKeyring_handleAccountBalancesUpdated(snapId, message) {
|
|
|
672
751
|
*/
|
|
673
752
|
async function _SnapKeyring_handleAccountAssetListUpdated(snapId, message) {
|
|
674
753
|
(0, superstruct_1.assert)(message, keyring_api_1.AccountAssetListUpdatedEventStruct);
|
|
675
|
-
const
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
};
|
|
680
|
-
return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_rePublishAccountEvent).call(this, 'SnapKeyring:accountAssetListUpdated', event);
|
|
754
|
+
const event = message.params;
|
|
755
|
+
return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_rePublishAccountEvent).call(this, snapId, 'SnapKeyring:accountAssetListUpdated', (filter) => {
|
|
756
|
+
event.assets = filter(event.assets);
|
|
757
|
+
return [event];
|
|
758
|
+
});
|
|
681
759
|
}, _SnapKeyring_handleAccountTransactionsUpdated =
|
|
682
760
|
/**
|
|
683
761
|
* Handle a transactions updated event from a Snap.
|
|
@@ -688,12 +766,11 @@ async function _SnapKeyring_handleAccountAssetListUpdated(snapId, message) {
|
|
|
688
766
|
*/
|
|
689
767
|
async function _SnapKeyring_handleAccountTransactionsUpdated(snapId, message) {
|
|
690
768
|
(0, superstruct_1.assert)(message, keyring_api_1.AccountTransactionsUpdatedEventStruct);
|
|
691
|
-
const
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
};
|
|
696
|
-
return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_rePublishAccountEvent).call(this, 'SnapKeyring:accountTransactionsUpdated', event);
|
|
769
|
+
const event = message.params;
|
|
770
|
+
return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_rePublishAccountEvent).call(this, snapId, 'SnapKeyring:accountTransactionsUpdated', (filter) => {
|
|
771
|
+
event.transactions = filter(event.transactions);
|
|
772
|
+
return [event];
|
|
773
|
+
});
|
|
697
774
|
}, _SnapKeyring_getAccount = function _SnapKeyring_getAccount(id) {
|
|
698
775
|
const found = [...__classPrivateFieldGet(this, _SnapKeyring_accounts, "f").values()].find((entry) => entry.account.id === id);
|
|
699
776
|
if (!found) {
|