@metamask-previews/eth-snap-keyring 4.3.2-preview-8d6e44e → 4.3.2-preview-d458961

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.
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CaseInsensitiveMap = void 0;
4
+ const util_1 = require("./util");
5
+ /**
6
+ * A case-insensitive map that stores key-value pairs.
7
+ */
8
+ class CaseInsensitiveMap extends Map {
9
+ /**
10
+ * Create a new case-insensitive map from a plain object.
11
+ *
12
+ * @param obj - An object with entries to initialize the map with.
13
+ * @returns A new case-insensitive map with all entries from `obj`.
14
+ */
15
+ static fromObject(obj) {
16
+ return new CaseInsensitiveMap(Object.entries(obj));
17
+ }
18
+ /**
19
+ * Return a plain object with all entries from this map.
20
+ *
21
+ * @returns A plain object with all entries from this map.
22
+ */
23
+ toObject() {
24
+ return Object.fromEntries(this.entries());
25
+ }
26
+ /**
27
+ * Return the value associated to the given key, or `undefined` if the key is
28
+ * not found.
29
+ *
30
+ * @param key - The key to get the value for.
31
+ * @returns The value associated to the given key, or `undefined` if the key
32
+ * is not found.
33
+ */
34
+ get(key) {
35
+ return super.get(key.toLowerCase());
36
+ }
37
+ /**
38
+ * Return the value associated with the given key, or throw an error if the
39
+ * key is not found.
40
+ *
41
+ * @param key - The key to look up in the map.
42
+ * @param name - Optional name of the key to include in the error message.
43
+ * @returns The value associated with the given key.
44
+ */
45
+ getOrThrow(key, name = 'Key') {
46
+ return this.get(key) ?? (0, util_1.throwError)(`${name} '${key}' not found`);
47
+ }
48
+ /**
49
+ * Check whether the given key is present in the map.
50
+ *
51
+ * @param key - The key to check for.
52
+ * @returns `true` if the key is present in the map, `false` otherwise.
53
+ */
54
+ has(key) {
55
+ return super.has(key.toLowerCase());
56
+ }
57
+ /**
58
+ * Set the value for the given key. If the key already exists in the map, its
59
+ * value will be updated.
60
+ *
61
+ * The key is converted to lowercase before being stored in the map to ensure
62
+ * case-insensitivity.
63
+ *
64
+ * @param key - The key to set the value for.
65
+ * @param value - The value to set.
66
+ * @returns The map instance.
67
+ */
68
+ set(key, value) {
69
+ return super.set(key.toLowerCase(), value);
70
+ }
71
+ /**
72
+ * Delete the entry for the given key.
73
+ *
74
+ * @param key - The key to delete the entry for.
75
+ * @returns `true` if the entry was present in the map, `false` otherwise.
76
+ */
77
+ delete(key) {
78
+ return super.delete(key.toLowerCase());
79
+ }
80
+ }
81
+ exports.CaseInsensitiveMap = CaseInsensitiveMap;
82
+ //# sourceMappingURL=CaseInsensitiveMap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CaseInsensitiveMap.js","sourceRoot":"","sources":["../src/CaseInsensitiveMap.ts"],"names":[],"mappings":";;;AAAA,iCAAoC;AAEpC;;GAEG;AACH,MAAa,kBAA0B,SAAQ,GAAkB;IAC/D;;;;;OAKG;IACH,MAAM,CAAC,UAAU,CACf,GAA0B;QAE1B,OAAO,IAAI,kBAAkB,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;IACrD,CAAC;IAED;;;;OAIG;IACH,QAAQ;QACN,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;;;OAOG;IACH,GAAG,CAAC,GAAW;QACb,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;;OAOG;IACH,UAAU,CAAC,GAAW,EAAE,IAAI,GAAG,KAAK;QAClC,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,IAAA,iBAAU,EAAC,GAAG,IAAI,KAAK,GAAG,aAAa,CAAC,CAAC;IACnE,CAAC;IAED;;;;;OAKG;IACH,GAAG,CAAC,GAAW;QACb,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;;;;;OAUG;IACH,GAAG,CAAC,GAAW,EAAE,KAAY;QAC3B,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,KAAK,CAAC,CAAC;IAC7C,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,GAAW;QAChB,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC;IACzC,CAAC;CACF;AAhFD,gDAgFC","sourcesContent":["import { throwError } from './util';\n\n/**\n * A case-insensitive map that stores key-value pairs.\n */\nexport class CaseInsensitiveMap<Value> extends Map<string, Value> {\n /**\n * Create a new case-insensitive map from a plain object.\n *\n * @param obj - An object with entries to initialize the map with.\n * @returns A new case-insensitive map with all entries from `obj`.\n */\n static fromObject<Value>(\n obj: Record<string, Value>,\n ): CaseInsensitiveMap<Value> {\n return new CaseInsensitiveMap(Object.entries(obj));\n }\n\n /**\n * Return a plain object with all entries from this map.\n *\n * @returns A plain object with all entries from this map.\n */\n toObject(): Record<string, Value> {\n return Object.fromEntries(this.entries());\n }\n\n /**\n * Return the value associated to the given key, or `undefined` if the key is\n * not found.\n *\n * @param key - The key to get the value for.\n * @returns The value associated to the given key, or `undefined` if the key\n * is not found.\n */\n get(key: string): Value | undefined {\n return super.get(key.toLowerCase());\n }\n\n /**\n * Return the value associated with the given key, or throw an error if the\n * key is not found.\n *\n * @param key - The key to look up in the map.\n * @param name - Optional name of the key to include in the error message.\n * @returns The value associated with the given key.\n */\n getOrThrow(key: string, name = 'Key'): Value {\n return this.get(key) ?? throwError(`${name} '${key}' not found`);\n }\n\n /**\n * Check whether the given key is present in the map.\n *\n * @param key - The key to check for.\n * @returns `true` if the key is present in the map, `false` otherwise.\n */\n has(key: string): boolean {\n return super.has(key.toLowerCase());\n }\n\n /**\n * Set the value for the given key. If the key already exists in the map, its\n * value will be updated.\n *\n * The key is converted to lowercase before being stored in the map to ensure\n * case-insensitivity.\n *\n * @param key - The key to set the value for.\n * @param value - The value to set.\n * @returns The map instance.\n */\n set(key: string, value: Value): this {\n return super.set(key.toLowerCase(), value);\n }\n\n /**\n * Delete the entry for the given key.\n *\n * @param key - The key to delete the entry for.\n * @returns `true` if the entry was present in the map, `false` otherwise.\n */\n delete(key: string): boolean {\n return super.delete(key.toLowerCase());\n }\n}\n"]}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeferredPromise = void 0;
4
+ /**
5
+ * A deferred promise can be resolved by a caller different from the one who
6
+ * created it.
7
+ *
8
+ * Example:
9
+ * - "A" creates a deferred promise "P", adds it to a list, and awaits it
10
+ * - "B" gets "P" from the list and resolves it
11
+ * - "A" gets the resolved value
12
+ */
13
+ class DeferredPromise {
14
+ constructor() {
15
+ this.resolve = undefined;
16
+ this.reject = undefined;
17
+ this.promise = new Promise((resolve, reject) => {
18
+ this.resolve = resolve;
19
+ this.reject = reject;
20
+ });
21
+ // This is a sanity check to make sure that the promise constructor
22
+ // actually set the `resolve` and `reject` functions.
23
+ /* istanbul ignore next */
24
+ if (!this.resolve || !this.reject) {
25
+ throw new Error('Promise constructor failed');
26
+ }
27
+ }
28
+ }
29
+ exports.DeferredPromise = DeferredPromise;
30
+ //# sourceMappingURL=DeferredPromise.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeferredPromise.js","sourceRoot":"","sources":["../src/DeferredPromise.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;GAQG;AACH,MAAa,eAAe;IAO1B;QAJA,YAAO,GAA8C,SAAgB,CAAC;QAEtE,WAAM,GAA2B,SAAgB,CAAC;QAGhD,IAAI,CAAC,OAAO,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC7C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;YACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACvB,CAAC,CAAC,CAAC;QAEH,mEAAmE;QACnE,qDAAqD;QACrD,0BAA0B;QAC1B,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YACjC,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;SAC/C;IACH,CAAC;CACF;AApBD,0CAoBC","sourcesContent":["/**\n * A deferred promise can be resolved by a caller different from the one who\n * created it.\n *\n * Example:\n * - \"A\" creates a deferred promise \"P\", adds it to a list, and awaits it\n * - \"B\" gets \"P\" from the list and resolves it\n * - \"A\" gets the resolved value\n */\nexport class DeferredPromise<Type> {\n promise: Promise<Type>;\n\n resolve: (value: Type | PromiseLike<Type>) => void = undefined as any;\n\n reject: (reason?: any) => void = undefined as any;\n\n constructor() {\n this.promise = new Promise((resolve, reject) => {\n this.resolve = resolve;\n this.reject = reject;\n });\n\n // This is a sanity check to make sure that the promise constructor\n // actually set the `resolve` and `reject` functions.\n /* istanbul ignore next */\n if (!this.resolve || !this.reject) {\n throw new Error('Promise constructor failed');\n }\n }\n}\n"]}
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _SnapControllerSender_snapId, _SnapControllerSender_origin, _SnapControllerSender_controller, _SnapControllerSender_handler, _KeyringSnapControllerClient_controller;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.KeyringSnapControllerClient = void 0;
16
+ const keyring_api_1 = require("@metamask/keyring-api");
17
+ /**
18
+ * Implementation of the `Sender` interface that can be used to send requests
19
+ * to a snap through a `SnapController`.
20
+ */
21
+ class SnapControllerSender {
22
+ /**
23
+ * Create a new instance of `SnapControllerSender`.
24
+ *
25
+ * @param controller - The `SnapController` instance to send requests to.
26
+ * @param snapId - The ID of the snap to use.
27
+ * @param origin - The sender's origin.
28
+ * @param handler - The handler type.
29
+ */
30
+ constructor(controller, snapId, origin, handler) {
31
+ _SnapControllerSender_snapId.set(this, void 0);
32
+ _SnapControllerSender_origin.set(this, void 0);
33
+ _SnapControllerSender_controller.set(this, void 0);
34
+ _SnapControllerSender_handler.set(this, void 0);
35
+ __classPrivateFieldSet(this, _SnapControllerSender_controller, controller, "f");
36
+ __classPrivateFieldSet(this, _SnapControllerSender_snapId, snapId, "f");
37
+ __classPrivateFieldSet(this, _SnapControllerSender_origin, origin, "f");
38
+ __classPrivateFieldSet(this, _SnapControllerSender_handler, handler, "f");
39
+ }
40
+ /**
41
+ * Send a request to the snap and return the response.
42
+ *
43
+ * @param request - JSON-RPC request to send to the snap.
44
+ * @returns A promise that resolves to the response of the request.
45
+ */
46
+ async send(request) {
47
+ return __classPrivateFieldGet(this, _SnapControllerSender_controller, "f").handleRequest({
48
+ snapId: __classPrivateFieldGet(this, _SnapControllerSender_snapId, "f"),
49
+ origin: __classPrivateFieldGet(this, _SnapControllerSender_origin, "f"),
50
+ handler: __classPrivateFieldGet(this, _SnapControllerSender_handler, "f"),
51
+ request,
52
+ });
53
+ }
54
+ }
55
+ _SnapControllerSender_snapId = new WeakMap(), _SnapControllerSender_origin = new WeakMap(), _SnapControllerSender_controller = new WeakMap(), _SnapControllerSender_handler = new WeakMap();
56
+ /**
57
+ * A `KeyringClient` that allows the communication with a snap through the
58
+ * `SnapController`.
59
+ */
60
+ class KeyringSnapControllerClient extends keyring_api_1.KeyringClient {
61
+ /**
62
+ * Create a new instance of `KeyringSnapControllerClient`.
63
+ *
64
+ * The `handlerType` argument has a hard-coded default `string` value instead
65
+ * of a `HandlerType` value to prevent the `@metamask/snaps-utils` module
66
+ * from being required at runtime.
67
+ *
68
+ * @param args - Constructor arguments.
69
+ * @param args.controller - The `SnapController` instance to use.
70
+ * @param args.snapId - The ID of the snap to use (default: `'undefined'`).
71
+ * @param args.origin - The sender's origin (default: `'metamask'`).
72
+ * @param args.handler - The handler type (default: `'onKeyringRequest'`).
73
+ */
74
+ constructor({ controller, snapId = 'undefined', origin = 'metamask', handler = 'onKeyringRequest', }) {
75
+ super(new SnapControllerSender(controller, snapId, origin, handler));
76
+ _KeyringSnapControllerClient_controller.set(this, void 0);
77
+ __classPrivateFieldSet(this, _KeyringSnapControllerClient_controller, controller, "f");
78
+ }
79
+ /**
80
+ * Create a new instance of `KeyringSnapControllerClient` with the specified
81
+ * `snapId`.
82
+ *
83
+ * @param snapId - The ID of the snap to use in the new instance.
84
+ * @returns A new instance of `KeyringSnapControllerClient` with the
85
+ * specified snap ID.
86
+ */
87
+ withSnapId(snapId) {
88
+ return new KeyringSnapControllerClient({
89
+ controller: __classPrivateFieldGet(this, _KeyringSnapControllerClient_controller, "f"),
90
+ snapId,
91
+ });
92
+ }
93
+ /**
94
+ * Get the `SnapController` instance used by this client.
95
+ *
96
+ * @returns The `SnapController` instance used by this client.
97
+ */
98
+ getController() {
99
+ return __classPrivateFieldGet(this, _KeyringSnapControllerClient_controller, "f");
100
+ }
101
+ }
102
+ exports.KeyringSnapControllerClient = KeyringSnapControllerClient;
103
+ _KeyringSnapControllerClient_controller = new WeakMap();
104
+ //# sourceMappingURL=KeyringSnapControllerClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyringSnapControllerClient.js","sourceRoot":"","sources":["../src/KeyringSnapControllerClient.ts"],"names":[],"mappings":";;;;;;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C;CACF;AAtDD,kEAsDC","sourcesContent":["import { KeyringClient, type Sender } from '@metamask/keyring-api';\nimport type { JsonRpcRequest } from '@metamask/keyring-api/dist/JsonRpcRequest';\nimport type { SnapController } from '@metamask/snaps-controllers';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport type { HandlerType } from '@metamask/snaps-utils';\nimport type { Json } from '@metamask/utils';\n\n/**\n * Implementation of the `Sender` interface that can be used to send requests\n * to a snap through a `SnapController`.\n */\nclass SnapControllerSender implements Sender {\n #snapId: SnapId;\n\n #origin: string;\n\n #controller: SnapController;\n\n #handler: HandlerType;\n\n /**\n * Create a new instance of `SnapControllerSender`.\n *\n * @param controller - The `SnapController` instance to send requests to.\n * @param snapId - The ID of the snap to use.\n * @param origin - The sender's origin.\n * @param handler - The handler type.\n */\n constructor(\n controller: any,\n snapId: SnapId,\n origin: string,\n handler: HandlerType,\n ) {\n this.#controller = controller;\n this.#snapId = snapId;\n this.#origin = origin;\n this.#handler = handler;\n }\n\n /**\n * Send a request to the snap and return the response.\n *\n * @param request - JSON-RPC request to send to the snap.\n * @returns A promise that resolves to the response of the request.\n */\n async send(request: JsonRpcRequest): Promise<Json> {\n return this.#controller.handleRequest({\n snapId: this.#snapId,\n origin: this.#origin,\n handler: this.#handler,\n request,\n }) as Promise<Json>;\n }\n}\n\n/**\n * A `KeyringClient` that allows the communication with a snap through the\n * `SnapController`.\n */\nexport class KeyringSnapControllerClient extends KeyringClient {\n #controller: SnapController;\n\n /**\n * Create a new instance of `KeyringSnapControllerClient`.\n *\n * The `handlerType` argument has a hard-coded default `string` value instead\n * of a `HandlerType` value to prevent the `@metamask/snaps-utils` module\n * from being required at runtime.\n *\n * @param args - Constructor arguments.\n * @param args.controller - The `SnapController` instance to use.\n * @param args.snapId - The ID of the snap to use (default: `'undefined'`).\n * @param args.origin - The sender's origin (default: `'metamask'`).\n * @param args.handler - The handler type (default: `'onKeyringRequest'`).\n */\n constructor({\n controller,\n snapId = 'undefined' as SnapId,\n origin = 'metamask',\n handler = 'onKeyringRequest' as HandlerType,\n }: {\n controller: SnapController;\n snapId?: SnapId;\n origin?: string;\n handler?: HandlerType;\n }) {\n super(new SnapControllerSender(controller, snapId, origin, handler));\n this.#controller = controller;\n }\n\n /**\n * Create a new instance of `KeyringSnapControllerClient` with the specified\n * `snapId`.\n *\n * @param snapId - The ID of the snap to use in the new instance.\n * @returns A new instance of `KeyringSnapControllerClient` with the\n * specified snap ID.\n */\n withSnapId(snapId: SnapId): KeyringSnapControllerClient {\n return new KeyringSnapControllerClient({\n controller: this.#controller,\n snapId,\n });\n }\n\n /**\n * Get the `SnapController` instance used by this client.\n *\n * @returns The `SnapController` instance used by this client.\n */\n getController(): SnapController {\n return this.#controller;\n }\n}\n"]}
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _SnapIdMap_map;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.SnapIdMap = exports.InvalidSnapIdError = void 0;
16
+ const CaseInsensitiveMap_1 = require("./CaseInsensitiveMap");
17
+ /**
18
+ * Error thrown when an invalid Snap ID is encountered.
19
+ */
20
+ class InvalidSnapIdError extends Error {
21
+ /**
22
+ * Creates an instance of `InvalidSnapIdError`.
23
+ *
24
+ * @param snapId - The invalid Snap ID.
25
+ * @param key - The key associated with the invalid Snap ID.
26
+ */
27
+ constructor(snapId, key) {
28
+ super(`Snap "${snapId}" is not allowed to set "${key}"`);
29
+ this.name = 'InvalidSnapIdError';
30
+ this.snapId = snapId;
31
+ this.key = key;
32
+ }
33
+ }
34
+ exports.InvalidSnapIdError = InvalidSnapIdError;
35
+ /**
36
+ * A map that associates a string key with a value that has a `snapId`
37
+ * property. Note that the key is case-insensitive.
38
+ *
39
+ * The `snapId` property is used to ensure that only the Snap that added an
40
+ * item to the map can modify or delete it.
41
+ */
42
+ class SnapIdMap {
43
+ /**
44
+ * Creates a new `SnapIdMap` object.
45
+ *
46
+ * Example:
47
+ *
48
+ * ```ts
49
+ * const items = [
50
+ * ['foo', { snapId: '1', name: 'foo' }],
51
+ * ['bar', { snapId: '1', name: 'bar' }],
52
+ * ];
53
+ * const map = new SnapIdMap(items);
54
+ * ```
55
+ *
56
+ * @param iterable - An iterable object whose elements are key-value pairs.
57
+ * Each key-value pair will be added to the new map.
58
+ */
59
+ constructor(iterable) {
60
+ _SnapIdMap_map.set(this, void 0);
61
+ __classPrivateFieldSet(this, _SnapIdMap_map, new CaseInsensitiveMap_1.CaseInsensitiveMap(iterable), "f");
62
+ }
63
+ /**
64
+ * Returns a plain object with the same key-value pairs as this map.
65
+ *
66
+ * Example:
67
+ *
68
+ * ```ts
69
+ * const items = [
70
+ * ['foo', { snapId: '1', name: 'foo' }],
71
+ * ['bar', { snapId: '1', name: 'bar' }],
72
+ * ];
73
+ * const map = new SnapIdMap(items);
74
+ * map.toObject();
75
+ * // Returns
76
+ * // {
77
+ * // foo: { snapId: '1', name: 'foo' },
78
+ * // bar: { snapId: '1', name: 'bar' },
79
+ * // }
80
+ * ```
81
+ *
82
+ * @returns A plain object with the same key-value pairs as this map.
83
+ */
84
+ toObject() {
85
+ return __classPrivateFieldGet(this, _SnapIdMap_map, "f").toObject();
86
+ }
87
+ /**
88
+ * Returns a new `SnapIdMap` object from an plain object.
89
+ *
90
+ * Example:
91
+ *
92
+ * ```ts
93
+ * const obj = {
94
+ * foo: { snapId: '1', name: 'foo' },
95
+ * bar: { snapId: '1', name: 'bar' },
96
+ * };
97
+ * const map = SnapIdMap.fromObject(obj);
98
+ * ```
99
+ *
100
+ * @param obj - A plain object whose elements will be added to the new map.
101
+ * @returns A new `SnapIdMap` containing the elements of the given object.
102
+ */
103
+ static fromObject(obj) {
104
+ return new SnapIdMap(Object.entries(obj));
105
+ }
106
+ /**
107
+ * Gets a value from the map.
108
+ *
109
+ * If the given key is not present in the map or the Snap ID of the value is
110
+ * different from the given Snap ID, returns `undefined`.
111
+ *
112
+ * Example:
113
+ *
114
+ * ```ts
115
+ * const map = new SnapIdMap();
116
+ * map.set('foo', { snapId: '1', name: 'foo' });
117
+ * map.get('1', 'foo'); // Returns { snapId: '1', name: 'foo' }
118
+ * map.get('2', 'foo'); // Returns `undefined`
119
+ * map.get('1', 'bar'); // Returns `undefined`
120
+ * ```
121
+ *
122
+ * @param snapId - Snap ID present in the value to get.
123
+ * @param key - Key of the element to get.
124
+ * @returns The value associated with the given key and Snap ID.
125
+ */
126
+ get(snapId, key) {
127
+ const value = __classPrivateFieldGet(this, _SnapIdMap_map, "f").get(key);
128
+ return value?.snapId === snapId ? value : undefined;
129
+ }
130
+ /**
131
+ * Checks if a key is present in 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 `false`.
135
+ *
136
+ * Example:
137
+ *
138
+ * ```ts
139
+ * const map = new SnapIdMap();
140
+ * map.set('foo', { snapId: '1', name: 'foo' });
141
+ * map.has('1', 'foo'); // Returns `true`
142
+ * map.has('2', 'foo'); // Returns `false`
143
+ * map.has('1', 'bar'); // Returns `false`
144
+ * ```
145
+ *
146
+ * @param snapId - Snap ID present in the value to check.
147
+ * @param key - Key of the element to check.
148
+ * @returns `true` if the key is present in the map and the Snap ID of the
149
+ * value is equal to the given Snap ID, `false` otherwise.
150
+ */
151
+ has(snapId, key) {
152
+ return this.get(snapId, key) !== undefined;
153
+ }
154
+ /**
155
+ * Deletes a key from the map.
156
+ *
157
+ * If the given key is not present in the map or the Snap IDs don't match,
158
+ * returns `false` and does nothing.
159
+ *
160
+ * Example:
161
+ *
162
+ * ```ts
163
+ * const map = new SnapIdMap();
164
+ * map.set('foo', { snapId: '1', name: 'foo' });
165
+ * map.delete('2', 'foo'); // Returns `false`
166
+ * map.delete('1', 'bar'); // Returns `false`
167
+ * map.delete('1', 'foo'); // Returns `true`
168
+ * ```
169
+ *
170
+ * @param snapId - Snap ID present in the value to delete.
171
+ * @param key - Key of the element to delete.
172
+ * @returns `true` if the key was present in the map and the Snap ID of the
173
+ * value was equal to the given Snap ID, `false` otherwise.
174
+ */
175
+ delete(snapId, key) {
176
+ return this.has(snapId, key) && __classPrivateFieldGet(this, _SnapIdMap_map, "f").delete(key);
177
+ }
178
+ /* eslint-disable jsdoc/check-indentation */
179
+ /**
180
+ * Adds or updates a key-value pair in the map.
181
+ *
182
+ * Note that this method has a different behavior from the `Map.set`.
183
+ *
184
+ * - If the given key is not already present in the map, this method adds the
185
+ * key-value pair to the map.
186
+ *
187
+ * - If the given key is already present in the map and the Snap IDs match,
188
+ * this method updates the value associated with the key.
189
+ *
190
+ * - However, if the given key is already present in the map but the Snap IDs
191
+ * do not match, this method throws an error.
192
+ *
193
+ * @param key - Key of the element to add or update.
194
+ * @param value - Value of the element to add or update.
195
+ * @returns The map itself.
196
+ */
197
+ /* eslint-enable jsdoc/check-indentation */
198
+ set(key, value) {
199
+ // If the key is present in the map but isn't associated with the given
200
+ // Snap ID, it means that the item was added to the map by a different
201
+ // Snap. In this case, throw an error.
202
+ if (__classPrivateFieldGet(this, _SnapIdMap_map, "f").has(key) && !this.has(value.snapId, key)) {
203
+ throw new InvalidSnapIdError(value.snapId, key);
204
+ }
205
+ __classPrivateFieldGet(this, _SnapIdMap_map, "f").set(key, value);
206
+ return this;
207
+ }
208
+ /**
209
+ * Returns an iterable of the values in the map.
210
+ *
211
+ * Example:
212
+ *
213
+ * ```ts
214
+ * const map = new SnapIdMap([
215
+ * ['foo', { snapId: '1', name: 'foo' }],
216
+ * ['bar', { snapId: '1', name: 'bar' }],
217
+ * ]);
218
+ * const values = [...map.values()];
219
+ * // Returns
220
+ * // [
221
+ * // { snapId: '1', name: 'foo' },
222
+ * // { snapId: '1', name: 'bar' },
223
+ * // ]
224
+ * ```
225
+ *
226
+ * @returns An iterable of the values in the map.
227
+ */
228
+ values() {
229
+ return __classPrivateFieldGet(this, _SnapIdMap_map, "f").values();
230
+ }
231
+ /**
232
+ * Returns the number of key-value pairs in the map.
233
+ *
234
+ * @returns The number of key-value pairs in the map.
235
+ */
236
+ get size() {
237
+ return __classPrivateFieldGet(this, _SnapIdMap_map, "f").size;
238
+ }
239
+ }
240
+ exports.SnapIdMap = SnapIdMap;
241
+ _SnapIdMap_map = new WeakMap();
242
+ //# sourceMappingURL=SnapIdMap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SnapIdMap.js","sourceRoot":"","sources":["../src/SnapIdMap.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAEA,6DAA0D;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;QAlBzD,iCAAgC;QAmB9B,uBAAA,IAAI,kBAAQ,IAAI,uCAAkB,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AAM,IAAI,kBAAkB,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;SACjD;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;AAhND,8BAgNC","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 #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 * 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"]}