@metamask-previews/eth-snap-keyring 4.3.2-672cc7b
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 +344 -0
- package/README.md +71 -0
- package/dist/CaseInsensitiveMap.d.ts +62 -0
- package/dist/CaseInsensitiveMap.js +82 -0
- package/dist/CaseInsensitiveMap.js.map +1 -0
- package/dist/DeferredPromise.d.ts +15 -0
- package/dist/DeferredPromise.js +30 -0
- package/dist/DeferredPromise.js.map +1 -0
- package/dist/SnapIdMap.d.ts +202 -0
- package/dist/SnapIdMap.js +242 -0
- package/dist/SnapIdMap.js.map +1 -0
- package/dist/SnapKeyring.d.ts +182 -0
- package/dist/SnapKeyring.js +599 -0
- package/dist/SnapKeyring.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +4 -0
- package/dist/logger.js +8 -0
- package/dist/logger.js.map +1 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +10 -0
- package/dist/types.js.map +1 -0
- package/dist/util.d.ts +52 -0
- package/dist/util.js +76 -0
- package/dist/util.js.map +1 -0
- package/package.json +72 -0
|
@@ -0,0 +1,202 @@
|
|
|
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
|
+
}
|
|
@@ -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,CAAC,QAAQ,CAAC,MAAA,CAAC;IAC/C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ;QACN,OAAO,uBAAA,IAAI,sBAAK,CAAC,QAAQ,EAAE,CAAC;IAC9B,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,MAAM,CAAC,UAAU,CACf,GAA0B;QAE1B,OAAO,IAAI,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,GAAG,CAAC,MAAc,EAAE,GAAW;QAC7B,MAAM,KAAK,GAAG,uBAAA,IAAI,sBAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACjC,OAAO,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;IACtD,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,GAAG,CAAC,MAAc,EAAE,GAAW;QAC7B,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,SAAS,CAAC;IAC7C,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;YACtD,MAAM,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"]}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { TypedTransaction } from '@ethereumjs/tx';
|
|
3
|
+
import type { TypedDataV1, TypedMessage } from '@metamask/eth-sig-util';
|
|
4
|
+
import { SignTypedDataVersion } from '@metamask/eth-sig-util';
|
|
5
|
+
import type { KeyringAccount, KeyringExecutionContext } from '@metamask/keyring-api';
|
|
6
|
+
import type { EthBaseTransaction, EthBaseUserOperation, EthUserOperation, EthUserOperationPatch, InternalAccount } from '@metamask/keyring-internal-api';
|
|
7
|
+
import type { SnapController } from '@metamask/snaps-controllers';
|
|
8
|
+
import type { SnapId } from '@metamask/snaps-sdk';
|
|
9
|
+
import type { Json } from '@metamask/utils';
|
|
10
|
+
import { EventEmitter } from 'events';
|
|
11
|
+
import type { SnapMessage } from './types';
|
|
12
|
+
export declare const SNAP_KEYRING_TYPE = "Snap Keyring";
|
|
13
|
+
/**
|
|
14
|
+
* Snap keyring state.
|
|
15
|
+
*
|
|
16
|
+
* This state is persisted by the keyring controller and passed to the Snap
|
|
17
|
+
* keyring when it's created.
|
|
18
|
+
*/
|
|
19
|
+
export declare type KeyringState = {
|
|
20
|
+
accounts: Record<string, {
|
|
21
|
+
account: KeyringAccount;
|
|
22
|
+
snapId: SnapId;
|
|
23
|
+
}>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Snap keyring callbacks.
|
|
27
|
+
*
|
|
28
|
+
* These callbacks are used to interact with other components.
|
|
29
|
+
*/
|
|
30
|
+
export declare type SnapKeyringCallbacks = {
|
|
31
|
+
saveState: () => Promise<void>;
|
|
32
|
+
addressExists(address: string): Promise<boolean>;
|
|
33
|
+
addAccount(address: string, snapId: SnapId, handleUserInput: (accepted: boolean) => Promise<void>, accountNameSuggestion?: string, displayConfirmation?: boolean): Promise<void>;
|
|
34
|
+
removeAccount(address: string, snapId: SnapId, handleUserInput: (accepted: boolean) => Promise<void>): Promise<void>;
|
|
35
|
+
redirectUser(snapId: SnapId, url: string, message: string): Promise<void>;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Keyring bridge implementation to support Snaps.
|
|
39
|
+
*/
|
|
40
|
+
export declare class SnapKeyring extends EventEmitter {
|
|
41
|
+
#private;
|
|
42
|
+
static type: string;
|
|
43
|
+
type: string;
|
|
44
|
+
/**
|
|
45
|
+
* Create a new Snap keyring.
|
|
46
|
+
*
|
|
47
|
+
* @param controller - Snaps controller.
|
|
48
|
+
* @param callbacks - Callbacks used to interact with other components.
|
|
49
|
+
* @returns A new Snap keyring.
|
|
50
|
+
*/
|
|
51
|
+
constructor(controller: SnapController, callbacks: SnapKeyringCallbacks);
|
|
52
|
+
/**
|
|
53
|
+
* Handle a message from a Snap.
|
|
54
|
+
*
|
|
55
|
+
* @param snapId - ID of the Snap.
|
|
56
|
+
* @param message - Message sent by the Snap.
|
|
57
|
+
* @returns The execution result.
|
|
58
|
+
*/
|
|
59
|
+
handleKeyringSnapMessage(snapId: SnapId, message: SnapMessage): Promise<Json>;
|
|
60
|
+
/**
|
|
61
|
+
* Serialize the keyring state.
|
|
62
|
+
*
|
|
63
|
+
* @returns Serialized keyring state.
|
|
64
|
+
*/
|
|
65
|
+
serialize(): Promise<KeyringState>;
|
|
66
|
+
/**
|
|
67
|
+
* Deserialize the keyring state into this keyring.
|
|
68
|
+
*
|
|
69
|
+
* @param state - Serialized keyring state.
|
|
70
|
+
*/
|
|
71
|
+
deserialize(state: KeyringState | undefined): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Get the addresses of the accounts in this keyring.
|
|
74
|
+
*
|
|
75
|
+
* @returns The addresses of the accounts in this keyring.
|
|
76
|
+
*/
|
|
77
|
+
getAccounts(): Promise<string[]>;
|
|
78
|
+
/**
|
|
79
|
+
* Get the addresses of the accounts associated with a given Snap.
|
|
80
|
+
*
|
|
81
|
+
* @param snapId - Snap ID to filter by.
|
|
82
|
+
* @returns The addresses of the accounts associated with the given Snap.
|
|
83
|
+
*/
|
|
84
|
+
getAccountsBySnapId(snapId: SnapId): Promise<string[]>;
|
|
85
|
+
/**
|
|
86
|
+
* Sign a transaction.
|
|
87
|
+
*
|
|
88
|
+
* @param address - Sender's address.
|
|
89
|
+
* @param transaction - Transaction.
|
|
90
|
+
* @param _opts - Transaction options (not used).
|
|
91
|
+
*/
|
|
92
|
+
signTransaction(address: string, transaction: TypedTransaction, _opts?: {}): Promise<Json | TypedTransaction>;
|
|
93
|
+
/**
|
|
94
|
+
* Sign a typed data message.
|
|
95
|
+
*
|
|
96
|
+
* @param address - Signer's address.
|
|
97
|
+
* @param data - Data to sign.
|
|
98
|
+
* @param opts - Signing options.
|
|
99
|
+
* @returns The signature.
|
|
100
|
+
*/
|
|
101
|
+
signTypedData(address: string, data: Record<string, unknown>[] | TypedDataV1 | TypedMessage<any>, opts?: {
|
|
102
|
+
version: SignTypedDataVersion;
|
|
103
|
+
}): Promise<string>;
|
|
104
|
+
/**
|
|
105
|
+
* Sign a message.
|
|
106
|
+
*
|
|
107
|
+
* @param address - Signer's address.
|
|
108
|
+
* @param hash - Data to sign.
|
|
109
|
+
* @returns The signature.
|
|
110
|
+
*/
|
|
111
|
+
signMessage(address: string, hash: any): Promise<string>;
|
|
112
|
+
/**
|
|
113
|
+
* Sign a personal message.
|
|
114
|
+
*
|
|
115
|
+
* Note: KeyringController says this should return a Buffer but it actually
|
|
116
|
+
* expects a string.
|
|
117
|
+
*
|
|
118
|
+
* @param address - Signer's address.
|
|
119
|
+
* @param data - Data to sign.
|
|
120
|
+
* @returns Promise of the signature.
|
|
121
|
+
*/
|
|
122
|
+
signPersonalMessage(address: string, data: any): Promise<string>;
|
|
123
|
+
/**
|
|
124
|
+
* Convert a base transaction to a base UserOperation.
|
|
125
|
+
*
|
|
126
|
+
* @param address - Address of the sender.
|
|
127
|
+
* @param transactions - Base transactions to include in the UserOperation.
|
|
128
|
+
* @param context - Keyring execution context.
|
|
129
|
+
* @returns A pseudo-UserOperation that can be used to construct a real.
|
|
130
|
+
*/
|
|
131
|
+
prepareUserOperation(address: string, transactions: EthBaseTransaction[], context: KeyringExecutionContext): Promise<EthBaseUserOperation>;
|
|
132
|
+
/**
|
|
133
|
+
* Patches properties of a UserOperation. Currently, only the
|
|
134
|
+
* `paymasterAndData` can be patched.
|
|
135
|
+
*
|
|
136
|
+
* @param address - Address of the sender.
|
|
137
|
+
* @param userOp - UserOperation to patch.
|
|
138
|
+
* @param context - Keyring execution context.
|
|
139
|
+
* @returns A patch to apply to the UserOperation.
|
|
140
|
+
*/
|
|
141
|
+
patchUserOperation(address: string, userOp: EthUserOperation, context: KeyringExecutionContext): Promise<EthUserOperationPatch>;
|
|
142
|
+
/**
|
|
143
|
+
* Signs an UserOperation.
|
|
144
|
+
*
|
|
145
|
+
* @param address - Address of the sender.
|
|
146
|
+
* @param userOp - UserOperation to sign.
|
|
147
|
+
* @param context - Leyring execution context.
|
|
148
|
+
* @returns The signature of the UserOperation.
|
|
149
|
+
*/
|
|
150
|
+
signUserOperation(address: string, userOp: EthUserOperation, context: KeyringExecutionContext): Promise<string>;
|
|
151
|
+
/**
|
|
152
|
+
* Gets the private data associated with the given address so
|
|
153
|
+
* that it may be exported.
|
|
154
|
+
*
|
|
155
|
+
* If this keyring contains duplicate public keys the first
|
|
156
|
+
* matching address is exported.
|
|
157
|
+
*
|
|
158
|
+
* Used by the UI to export an account.
|
|
159
|
+
*
|
|
160
|
+
* @param _address - Address of the account to export.
|
|
161
|
+
*/
|
|
162
|
+
exportAccount(_address: string): [Uint8Array, Json] | undefined;
|
|
163
|
+
/**
|
|
164
|
+
* Removes the account matching the given address.
|
|
165
|
+
*
|
|
166
|
+
* @param address - Address of the account to remove.
|
|
167
|
+
*/
|
|
168
|
+
removeAccount(address: string): Promise<void>;
|
|
169
|
+
/**
|
|
170
|
+
* Return an internal account object for a given address.
|
|
171
|
+
*
|
|
172
|
+
* @param address - Address of the account to return.
|
|
173
|
+
* @returns An internal account object for the given address.
|
|
174
|
+
*/
|
|
175
|
+
getAccountByAddress(address: string): InternalAccount | undefined;
|
|
176
|
+
/**
|
|
177
|
+
* List all Snap keyring accounts.
|
|
178
|
+
*
|
|
179
|
+
* @returns An array containing all Snap keyring accounts.
|
|
180
|
+
*/
|
|
181
|
+
listAccounts(): InternalAccount[];
|
|
182
|
+
}
|