@metamask-previews/eth-snap-keyring 4.3.2-preview-37040eb → 4.3.2-preview-399ef96

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,63 @@
1
+ /**
2
+ * A case-insensitive map that stores key-value pairs.
3
+ */
4
+ export declare class CaseInsensitiveMap<Value> extends Map<string, Value> {
5
+ /**
6
+ * Create a new case-insensitive map from a plain object.
7
+ *
8
+ * @param obj - An object with entries to initialize the map with.
9
+ * @returns A new case-insensitive map with all entries from `obj`.
10
+ */
11
+ static fromObject<Value>(obj: Record<string, Value>): CaseInsensitiveMap<Value>;
12
+ /**
13
+ * Return a plain object with all entries from this map.
14
+ *
15
+ * @returns A plain object with all entries from this map.
16
+ */
17
+ toObject(): Record<string, Value>;
18
+ /**
19
+ * Return the value associated to the given key, or `undefined` if the key is
20
+ * not found.
21
+ *
22
+ * @param key - The key to get the value for.
23
+ * @returns The value associated to the given key, or `undefined` if the key
24
+ * is not found.
25
+ */
26
+ get(key: string): Value | undefined;
27
+ /**
28
+ * Return the value associated with the given key, or throw an error if the
29
+ * key is not found.
30
+ *
31
+ * @param key - The key to look up in the map.
32
+ * @param name - Optional name of the key to include in the error message.
33
+ * @returns The value associated with the given key.
34
+ */
35
+ getOrThrow(key: string, name?: string): Value;
36
+ /**
37
+ * Check whether the given key is present in the map.
38
+ *
39
+ * @param key - The key to check for.
40
+ * @returns `true` if the key is present in the map, `false` otherwise.
41
+ */
42
+ has(key: string): boolean;
43
+ /**
44
+ * Set the value for the given key. If the key already exists in the map, its
45
+ * value will be updated.
46
+ *
47
+ * The key is converted to lowercase before being stored in the map to ensure
48
+ * case-insensitivity.
49
+ *
50
+ * @param key - The key to set the value for.
51
+ * @param value - The value to set.
52
+ * @returns The map instance.
53
+ */
54
+ set(key: string, value: Value): this;
55
+ /**
56
+ * Delete the entry for the given key.
57
+ *
58
+ * @param key - The key to delete the entry for.
59
+ * @returns `true` if the entry was present in the map, `false` otherwise.
60
+ */
61
+ delete(key: string): boolean;
62
+ }
63
+ //# sourceMappingURL=CaseInsensitiveMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CaseInsensitiveMap.d.ts","sourceRoot":"","sources":["../src/CaseInsensitiveMap.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,qBAAa,kBAAkB,CAAC,KAAK,CAAE,SAAQ,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC;IAC/D;;;;;OAKG;IACH,MAAM,CAAC,UAAU,CAAC,KAAK,EACrB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GACzB,kBAAkB,CAAC,KAAK,CAAC;IAI5B;;;;OAIG;IACH,QAAQ,IAAI,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;IAIjC;;;;;;;OAOG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS;IAInC;;;;;;;OAOG;IACH,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,SAAQ,GAAG,KAAK;IAI5C;;;;;OAKG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB;;;;;;;;;;OAUG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI;IAIpC;;;;;OAKG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;CAG7B"}
@@ -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,16 @@
1
+ /**
2
+ * A deferred promise can be resolved by a caller different from the one who
3
+ * created it.
4
+ *
5
+ * Example:
6
+ * - "A" creates a deferred promise "P", adds it to a list, and awaits it
7
+ * - "B" gets "P" from the list and resolves it
8
+ * - "A" gets the resolved value
9
+ */
10
+ export declare class DeferredPromise<Type> {
11
+ promise: Promise<Type>;
12
+ resolve: (value: Type | PromiseLike<Type>) => void;
13
+ reject: (reason?: any) => void;
14
+ constructor();
15
+ }
16
+ //# sourceMappingURL=DeferredPromise.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeferredPromise.d.ts","sourceRoot":"","sources":["../src/DeferredPromise.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,qBAAa,eAAe,CAAC,IAAI;IAC/B,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB,OAAO,EAAE,CAAC,KAAK,EAAE,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,KAAK,IAAI,CAAoB;IAEtE,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAoB;;CAenD"}
@@ -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,46 @@
1
+ import { KeyringClient } from '@metamask/keyring-api';
2
+ import type { SnapController } from '@metamask/snaps-controllers';
3
+ import type { SnapId } from '@metamask/snaps-sdk';
4
+ import type { HandlerType } from '@metamask/snaps-utils';
5
+ /**
6
+ * A `KeyringClient` that allows the communication with a snap through the
7
+ * `SnapController`.
8
+ */
9
+ export declare class KeyringSnapControllerClient extends KeyringClient {
10
+ #private;
11
+ /**
12
+ * Create a new instance of `KeyringSnapControllerClient`.
13
+ *
14
+ * The `handlerType` argument has a hard-coded default `string` value instead
15
+ * of a `HandlerType` value to prevent the `@metamask/snaps-utils` module
16
+ * from being required at runtime.
17
+ *
18
+ * @param args - Constructor arguments.
19
+ * @param args.controller - The `SnapController` instance to use.
20
+ * @param args.snapId - The ID of the snap to use (default: `'undefined'`).
21
+ * @param args.origin - The sender's origin (default: `'metamask'`).
22
+ * @param args.handler - The handler type (default: `'onKeyringRequest'`).
23
+ */
24
+ constructor({ controller, snapId, origin, handler, }: {
25
+ controller: SnapController;
26
+ snapId?: SnapId;
27
+ origin?: string;
28
+ handler?: HandlerType;
29
+ });
30
+ /**
31
+ * Create a new instance of `KeyringSnapControllerClient` with the specified
32
+ * `snapId`.
33
+ *
34
+ * @param snapId - The ID of the snap to use in the new instance.
35
+ * @returns A new instance of `KeyringSnapControllerClient` with the
36
+ * specified snap ID.
37
+ */
38
+ withSnapId(snapId: SnapId): KeyringSnapControllerClient;
39
+ /**
40
+ * Get the `SnapController` instance used by this client.
41
+ *
42
+ * @returns The `SnapController` instance used by this client.
43
+ */
44
+ getController(): SnapController;
45
+ }
46
+ //# sourceMappingURL=KeyringSnapControllerClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyringSnapControllerClient.d.ts","sourceRoot":"","sources":["../src/KeyringSnapControllerClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAe,MAAM,uBAAuB,CAAC;AAEnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAoDzD;;;GAGG;AACH,qBAAa,2BAA4B,SAAQ,aAAa;;IAG5D;;;;;;;;;;;;OAYG;gBACS,EACV,UAAU,EACV,MAA8B,EAC9B,MAAmB,EACnB,OAA2C,GAC5C,EAAE;QACD,UAAU,EAAE,cAAc,CAAC;QAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,OAAO,CAAC,EAAE,WAAW,CAAC;KACvB;IAKD;;;;;;;OAOG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,2BAA2B;IAOvD;;;;OAIG;IACH,aAAa,IAAI,cAAc;CAGhC"}
@@ -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":";;;;;;;;;;;;;;;AAAA,uDAAmE;AAOnE;;;GAGG;AACH,MAAM,oBAAoB;IASxB;;;;;;;OAOG;IACH,YACE,UAAe,EACf,MAAc,EACd,MAAc,EACd,OAAoB;QApBtB,+CAAgB;QAEhB,+CAAgB;QAEhB,mDAA4B;QAE5B,gDAAsB;QAgBpB,uBAAA,IAAI,oCAAe,UAAU,MAAA,CAAC;QAC9B,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,gCAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,IAAI,CAAC,OAAuB;QAChC,OAAO,uBAAA,IAAI,wCAAY,CAAC,aAAa,CAAC;YACpC,MAAM,EAAE,uBAAA,IAAI,oCAAQ;YACpB,MAAM,EAAE,uBAAA,IAAI,oCAAQ;YACpB,OAAO,EAAE,uBAAA,IAAI,qCAAS;YACtB,OAAO;SACR,CAAkB,CAAC;IACtB,CAAC;CACF;;AAED;;;GAGG;AACH,MAAa,2BAA4B,SAAQ,2BAAa;IAG5D;;;;;;;;;;;;OAYG;IACH,YAAY,EACV,UAAU,EACV,MAAM,GAAG,WAAqB,EAC9B,MAAM,GAAG,UAAU,EACnB,OAAO,GAAG,kBAAiC,GAM5C;QACC,KAAK,CAAC,IAAI,oBAAoB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;QA1BvE,0DAA4B;QA2B1B,uBAAA,IAAI,2CAAe,UAAU,MAAA,CAAC;IAChC,CAAC;IAED;;;;;;;OAOG;IACH,UAAU,CAAC,MAAc;QACvB,OAAO,IAAI,2BAA2B,CAAC;YACrC,UAAU,EAAE,uBAAA,IAAI,+CAAY;YAC5B,MAAM;SACP,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,aAAa;QACX,OAAO,uBAAA,IAAI,+CAAY,CAAC;IAC1B,CAAC;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,203 @@
1
+ import type { SnapId } from '@metamask/snaps-sdk';
2
+ /**
3
+ * Error thrown when an invalid Snap ID is encountered.
4
+ */
5
+ export declare class InvalidSnapIdError extends Error {
6
+ /**
7
+ * The ID of the Snap that caused the error.
8
+ */
9
+ snapId: SnapId;
10
+ /**
11
+ * The key of the element that caused the error.
12
+ */
13
+ key: string;
14
+ /**
15
+ * Creates an instance of `InvalidSnapIdError`.
16
+ *
17
+ * @param snapId - The invalid Snap ID.
18
+ * @param key - The key associated with the invalid Snap ID.
19
+ */
20
+ constructor(snapId: SnapId, key: string);
21
+ }
22
+ /**
23
+ * A map that associates a string key with a value that has a `snapId`
24
+ * property. Note that the key is case-insensitive.
25
+ *
26
+ * The `snapId` property is used to ensure that only the Snap that added an
27
+ * item to the map can modify or delete it.
28
+ */
29
+ export declare class SnapIdMap<Value extends {
30
+ snapId: SnapId;
31
+ }> {
32
+ #private;
33
+ /**
34
+ * Creates a new `SnapIdMap` object.
35
+ *
36
+ * Example:
37
+ *
38
+ * ```ts
39
+ * const items = [
40
+ * ['foo', { snapId: '1', name: 'foo' }],
41
+ * ['bar', { snapId: '1', name: 'bar' }],
42
+ * ];
43
+ * const map = new SnapIdMap(items);
44
+ * ```
45
+ *
46
+ * @param iterable - An iterable object whose elements are key-value pairs.
47
+ * Each key-value pair will be added to the new map.
48
+ */
49
+ constructor(iterable?: Iterable<readonly [string, Value]>);
50
+ /**
51
+ * Returns a plain object with the same key-value pairs as this map.
52
+ *
53
+ * Example:
54
+ *
55
+ * ```ts
56
+ * const items = [
57
+ * ['foo', { snapId: '1', name: 'foo' }],
58
+ * ['bar', { snapId: '1', name: 'bar' }],
59
+ * ];
60
+ * const map = new SnapIdMap(items);
61
+ * map.toObject();
62
+ * // Returns
63
+ * // {
64
+ * // foo: { snapId: '1', name: 'foo' },
65
+ * // bar: { snapId: '1', name: 'bar' },
66
+ * // }
67
+ * ```
68
+ *
69
+ * @returns A plain object with the same key-value pairs as this map.
70
+ */
71
+ toObject(): Record<string, Value>;
72
+ /**
73
+ * Returns a new `SnapIdMap` object from an plain object.
74
+ *
75
+ * Example:
76
+ *
77
+ * ```ts
78
+ * const obj = {
79
+ * foo: { snapId: '1', name: 'foo' },
80
+ * bar: { snapId: '1', name: 'bar' },
81
+ * };
82
+ * const map = SnapIdMap.fromObject(obj);
83
+ * ```
84
+ *
85
+ * @param obj - A plain object whose elements will be added to the new map.
86
+ * @returns A new `SnapIdMap` containing the elements of the given object.
87
+ */
88
+ static fromObject<Value extends {
89
+ snapId: SnapId;
90
+ }>(obj: Record<string, Value>): SnapIdMap<Value>;
91
+ /**
92
+ * Gets a value from the map.
93
+ *
94
+ * If the given key is not present in the map or the Snap ID of the value is
95
+ * different from the given Snap ID, returns `undefined`.
96
+ *
97
+ * Example:
98
+ *
99
+ * ```ts
100
+ * const map = new SnapIdMap();
101
+ * map.set('foo', { snapId: '1', name: 'foo' });
102
+ * map.get('1', 'foo'); // Returns { snapId: '1', name: 'foo' }
103
+ * map.get('2', 'foo'); // Returns `undefined`
104
+ * map.get('1', 'bar'); // Returns `undefined`
105
+ * ```
106
+ *
107
+ * @param snapId - Snap ID present in the value to get.
108
+ * @param key - Key of the element to get.
109
+ * @returns The value associated with the given key and Snap ID.
110
+ */
111
+ get(snapId: SnapId, key: string): Value | undefined;
112
+ /**
113
+ * Checks if a key is present in 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 `false`.
117
+ *
118
+ * Example:
119
+ *
120
+ * ```ts
121
+ * const map = new SnapIdMap();
122
+ * map.set('foo', { snapId: '1', name: 'foo' });
123
+ * map.has('1', 'foo'); // Returns `true`
124
+ * map.has('2', 'foo'); // Returns `false`
125
+ * map.has('1', 'bar'); // Returns `false`
126
+ * ```
127
+ *
128
+ * @param snapId - Snap ID present in the value to check.
129
+ * @param key - Key of the element to check.
130
+ * @returns `true` if the key is present in the map and the Snap ID of the
131
+ * value is equal to the given Snap ID, `false` otherwise.
132
+ */
133
+ has(snapId: SnapId, key: string): boolean;
134
+ /**
135
+ * Deletes a key from the map.
136
+ *
137
+ * If the given key is not present in the map or the Snap IDs don't match,
138
+ * returns `false` and does nothing.
139
+ *
140
+ * Example:
141
+ *
142
+ * ```ts
143
+ * const map = new SnapIdMap();
144
+ * map.set('foo', { snapId: '1', name: 'foo' });
145
+ * map.delete('2', 'foo'); // Returns `false`
146
+ * map.delete('1', 'bar'); // Returns `false`
147
+ * map.delete('1', 'foo'); // Returns `true`
148
+ * ```
149
+ *
150
+ * @param snapId - Snap ID present in the value to delete.
151
+ * @param key - Key of the element to delete.
152
+ * @returns `true` if the key was present in the map and the Snap ID of the
153
+ * value was equal to the given Snap ID, `false` otherwise.
154
+ */
155
+ delete(snapId: SnapId, key: string): boolean;
156
+ /**
157
+ * Adds or updates a key-value pair in the map.
158
+ *
159
+ * Note that this method has a different behavior from the `Map.set`.
160
+ *
161
+ * - If the given key is not already present in the map, this method adds the
162
+ * key-value pair to the map.
163
+ *
164
+ * - If the given key is already present in the map and the Snap IDs match,
165
+ * this method updates the value associated with the key.
166
+ *
167
+ * - However, if the given key is already present in the map but the Snap IDs
168
+ * do not match, this method throws an error.
169
+ *
170
+ * @param key - Key of the element to add or update.
171
+ * @param value - Value of the element to add or update.
172
+ * @returns The map itself.
173
+ */
174
+ set(key: string, value: Value): this;
175
+ /**
176
+ * Returns an iterable of the values in the map.
177
+ *
178
+ * Example:
179
+ *
180
+ * ```ts
181
+ * const map = new SnapIdMap([
182
+ * ['foo', { snapId: '1', name: 'foo' }],
183
+ * ['bar', { snapId: '1', name: 'bar' }],
184
+ * ]);
185
+ * const values = [...map.values()];
186
+ * // Returns
187
+ * // [
188
+ * // { snapId: '1', name: 'foo' },
189
+ * // { snapId: '1', name: 'bar' },
190
+ * // ]
191
+ * ```
192
+ *
193
+ * @returns An iterable of the values in the map.
194
+ */
195
+ values(): IterableIterator<Value>;
196
+ /**
197
+ * Returns the number of key-value pairs in the map.
198
+ *
199
+ * @returns The number of key-value pairs in the map.
200
+ */
201
+ get size(): number;
202
+ }
203
+ //# sourceMappingURL=SnapIdMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SnapIdMap.d.ts","sourceRoot":"","sources":["../src/SnapIdMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;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;;;;;;;;;;;;;;;;;;;;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"}