@metamask-previews/eth-snap-keyring 22.3.0-e51c895 → 23.0.1-1fa477f

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.
@@ -9,15 +9,25 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  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");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _SnapKeyring_instances, _SnapKeyring_callbacks, _SnapKeyring_lock, _SnapKeyring_withLock;
12
+ var _SnapKeyring_instances, _SnapKeyring_registry, _SnapKeyring_messenger, _SnapKeyring_callbacks, _SnapKeyring_isAnyAccountTypeAllowed, _SnapKeyring_lock, _SnapKeyring_context, _SnapKeyring_v1, _SnapKeyring_client_get, _SnapKeyring_withLock, _SnapKeyring_assertInitialized, _SnapKeyring_resolveKeyringCapabilities, _SnapKeyring_getExistingAccount;
13
13
  import { KeyringAccountStruct } from "@metamask/keyring-api";
14
14
  import { KeyringType } from "@metamask/keyring-api/v2";
15
+ import { KeyringInternalSnapClient } from "@metamask/keyring-internal-snap-client/v2";
16
+ import { KeyringAccountRegistry } from "@metamask/keyring-sdk";
15
17
  import { assert, object, record, string, union } from "@metamask/superstruct";
16
18
  import { Mutex } from "async-mutex";
17
- import { KeyringAccountV1Struct, normalizeAccount, transformAccount } from "../account.mjs";
19
+ import { KeyringAccountV1Struct, normalizeAccount, normalizeAccountAddress, transformAccount } from "../account.mjs";
18
20
  import { isAccountV1, migrateAccountV1 } from "../migrations/index.mjs";
19
21
  import { SnapKeyringV1 } from "../SnapKeyringV1.mjs";
20
22
  import { equalsIgnoreCase } from "../util.mjs";
23
+ /**
24
+ * Default, empty capabilities used until the snap manifest is read on
25
+ * `deserialize`. Typed (no cast) so that adding a new required
26
+ * `KeyringCapabilities` field surfaces here as a compile error.
27
+ */
28
+ export const EMPTY_CAPABILITIES = Object.freeze({
29
+ scopes: [],
30
+ });
21
31
  /**
22
32
  * Superstruct schema for {@link SnapKeyringState}.
23
33
  *
@@ -38,50 +48,103 @@ export function isSnapKeyring(keyring) {
38
48
  return keyring.type === KeyringType.Snap;
39
49
  }
40
50
  /**
41
- * Per-snap keyring wrapper that implements `Keyring`.
51
+ * Per-snap keyring that implements `Keyring` (v2).
42
52
  *
43
- * Extends `SnapKeyringV1` — the v1 event-driven flow (account lifecycle,
44
- * request handling, asset events) is inherited. This class adds the
45
- * `KeyringV2` batch interface (`createAccounts`, `deleteAccount`, etc.) and
46
- * owns the `capabilities` property.
47
- *
48
- * The `KeyringAccountRegistry`, snap client, and messenger are all inherited
49
- * from `SnapKeyringV1` so there is no duplicated state.
53
+ * Owns the account registry and messenger. For v1 snaps (those that do not
54
+ * declare `endowment:keyring` capabilities in their manifest), a
55
+ * {@link SnapKeyringV1} instance is created on `deserialize` and held under
56
+ * {@link SnapKeyring.v1}. V2 snaps have `v1 === undefined` and communicate
57
+ * directly through the v2 client without the `{ pending, result }` envelope.
50
58
  */
51
- export class SnapKeyring extends SnapKeyringV1 {
52
- constructor(options) {
53
- super(options);
59
+ export class SnapKeyring {
60
+ constructor({ messenger, callbacks, isAnyAccountTypeAllowed = false, }) {
54
61
  _SnapKeyring_instances.add(this);
55
- /**
56
- * V2-typed view of the callbacks. Stored separately from the V1 private
57
- * field so that V2-specific callbacks are accessible without casting.
58
- * Both fields hold the same object reference.
59
- */
62
+ /** Account registry — shared by reference with the v1 instance when present. */
63
+ _SnapKeyring_registry.set(this, void 0);
64
+ /** Messenger for snap controller calls and event publishing. */
65
+ _SnapKeyring_messenger.set(this, void 0);
60
66
  _SnapKeyring_callbacks.set(this, void 0);
67
+ _SnapKeyring_isAnyAccountTypeAllowed.set(this, void 0);
61
68
  /**
62
69
  * Mutex that serializes `createAccounts` calls on this snap instance.
63
70
  * Owned here so that each `SnapKeyring` is fully self-contained.
64
71
  */
65
72
  _SnapKeyring_lock.set(this, void 0);
73
+ /** V2 snap client. Set via {@link bindSnapId}. */
74
+ _SnapKeyring_context.set(this, void 0);
75
+ /**
76
+ * V1 instance, present only when the snap does not declare v2 capabilities.
77
+ * Created on `deserialize()` after reading the snap manifest.
78
+ */
79
+ _SnapKeyring_v1.set(this, void 0);
66
80
  // ──────────────────────────────────────────────
67
81
  // Keyring properties
68
82
  // ──────────────────────────────────────────────
69
83
  this.type = `${KeyringType.Snap}`;
70
- __classPrivateFieldSet(this, _SnapKeyring_callbacks, options.callbacks, "f");
84
+ __classPrivateFieldSet(this, _SnapKeyring_registry, new KeyringAccountRegistry(), "f");
85
+ __classPrivateFieldSet(this, _SnapKeyring_messenger, messenger, "f");
86
+ __classPrivateFieldSet(this, _SnapKeyring_callbacks, callbacks, "f");
87
+ __classPrivateFieldSet(this, _SnapKeyring_isAnyAccountTypeAllowed, isAnyAccountTypeAllowed, "f");
71
88
  __classPrivateFieldSet(this, _SnapKeyring_lock, new Mutex(), "f");
72
- // Default capabilities — parent updates this when snap metadata is available.
73
- // We cast here because KeyringCapabilities requires a non-empty scopes array,
74
- // but we don't have the snap metadata at construction time (e.g. during deserialization).
75
- this.capabilities = { scopes: [] };
89
+ // Default capabilities; replaced from the snap manifest on `deserialize`.
90
+ this.capabilities = EMPTY_CAPABILITIES;
91
+ }
92
+ /**
93
+ * Gets the v1 instance for this snap, or `undefined` if the snap is v2-only.
94
+ *
95
+ * @returns The v1 instance, or `undefined` if the snap is v2-only.
96
+ *
97
+ * Use this to make v1 calls explicit:
98
+ * ```ts
99
+ * keyring.v1?.signTransaction(account, tx);
100
+ * ```
101
+ */
102
+ get v1() {
103
+ return __classPrivateFieldGet(this, _SnapKeyring_v1, "f");
76
104
  }
77
105
  /**
78
- * Destroy this keyring.
106
+ * The snap ID this instance is scoped to.
79
107
  *
80
- * Delegates to the parent `SnapKeyringV1.destroy()` to reject any pending
81
- * requests inherited from the v1 flow.
108
+ * @returns The snap ID.
109
+ * @throws If the keyring has not been initialized yet.
110
+ */
111
+ get snapId() {
112
+ /* istanbul ignore next */
113
+ if (__classPrivateFieldGet(this, _SnapKeyring_context, "f") === undefined) {
114
+ throw new Error('SnapKeyring has not been initialized: call deserialize() first');
115
+ }
116
+ return __classPrivateFieldGet(this, _SnapKeyring_context, "f").snapId;
117
+ }
118
+ /**
119
+ * Bind this keyring to a snap ID and initialize the v2 client.
120
+ *
121
+ * Idempotent for the same `snapId`; throws if called again with a different
122
+ * one to prevent accidentally swapping a keyring's identity.
123
+ *
124
+ * @param snapId - The snap ID to bind to.
125
+ */
126
+ bindSnapId(snapId) {
127
+ if (__classPrivateFieldGet(this, _SnapKeyring_context, "f") !== undefined && __classPrivateFieldGet(this, _SnapKeyring_context, "f").snapId !== snapId) {
128
+ throw new Error(`SnapKeyring bound to '${__classPrivateFieldGet(this, _SnapKeyring_context, "f").snapId}' cannot be rebound to '${snapId}'`);
129
+ }
130
+ if (__classPrivateFieldGet(this, _SnapKeyring_context, "f") === undefined) {
131
+ __classPrivateFieldSet(this, _SnapKeyring_context, {
132
+ snapId,
133
+ client: new KeyringInternalSnapClient({
134
+ messenger: __classPrivateFieldGet(this, _SnapKeyring_messenger, "f"),
135
+ snapId,
136
+ }),
137
+ }, "f");
138
+ }
139
+ }
140
+ /**
141
+ * Destroy this keyring, rejecting any pending v1 requests.
142
+ *
143
+ * @returns A promise that resolves when the keyring is destroyed.
82
144
  */
83
145
  async destroy() {
84
- await super.destroy();
146
+ await __classPrivateFieldGet(this, _SnapKeyring_v1, "f")?.destroy();
147
+ __classPrivateFieldSet(this, _SnapKeyring_v1, undefined, "f");
85
148
  }
86
149
  // ──────────────────────────────────────────────
87
150
  // Keyring methods
@@ -92,6 +155,7 @@ export class SnapKeyring extends SnapKeyringV1 {
92
155
  * @returns A promise that resolves to an array of all accounts.
93
156
  */
94
157
  async getAccounts() {
158
+ __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_assertInitialized).call(this);
95
159
  return this.accounts();
96
160
  }
97
161
  /**
@@ -101,6 +165,7 @@ export class SnapKeyring extends SnapKeyringV1 {
101
165
  * @returns A promise that resolves to the account.
102
166
  */
103
167
  async getAccount(accountId) {
168
+ __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_assertInitialized).call(this);
104
169
  const account = this.lookupAccount(accountId);
105
170
  if (!account) {
106
171
  throw new Error(`Account '${accountId}' not found in snap '${this.snapId}'`);
@@ -123,19 +188,30 @@ export class SnapKeyring extends SnapKeyringV1 {
123
188
  * @returns A promise that resolves to an array of the created account objects.
124
189
  */
125
190
  async createAccounts(options) {
191
+ __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_assertInitialized).call(this);
126
192
  return __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_withLock).call(this, async () => {
127
193
  // Keep track of address/account ID part of this batch, to avoid having duplicates.
128
194
  const batchAddresses = new Set();
129
195
  const batchIds = new Set();
130
196
  const accounts = [];
131
197
  const newAccounts = [];
132
- const snapAccounts = await this.client.createAccounts(options);
198
+ let snapAccounts;
199
+ if (__classPrivateFieldGet(this, _SnapKeyring_v1, "f")) {
200
+ // v1 snap: the v1 client wraps options in `{ options }` before sending
201
+ // `keyring_createAccounts`, while the v2 client sends flat options.
202
+ // Route through v1 so v1 snaps receive the format they expect.
203
+ snapAccounts = await __classPrivateFieldGet(this, _SnapKeyring_v1, "f").createAccounts(options);
204
+ }
205
+ else {
206
+ // v2 snap: call snap directly with flat options.
207
+ snapAccounts = await __classPrivateFieldGet(this, _SnapKeyring_instances, "a", _SnapKeyring_client_get).createAccounts(options);
208
+ }
133
209
  try {
134
210
  for (const snapAccount of snapAccounts) {
135
211
  let account = normalizeAccount(transformAccount(snapAccount));
136
212
  const { address } = account;
137
213
  // Check for idempotency.
138
- const existingAccount = this.getExistingAccount(account);
214
+ const existingAccount = __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getExistingAccount).call(this, account);
139
215
  if (existingAccount) {
140
216
  // NOTE: We re-use the account from the internal state to avoid having the Snap
141
217
  // mutating the account object without updating the map.
@@ -162,9 +238,6 @@ export class SnapKeyring extends SnapKeyringV1 {
162
238
  for (const account of newAccounts) {
163
239
  this.setAccount(account);
164
240
  }
165
- // NOTE: We assume this will never fail, thus, we don't need to rollback the
166
- // keyring state if anything goes wrong here.
167
- await __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").saveState();
168
241
  }
169
242
  return accounts;
170
243
  }
@@ -172,9 +245,9 @@ export class SnapKeyring extends SnapKeyringV1 {
172
245
  // Rollback Snap state.
173
246
  for (const snapAccount of snapAccounts) {
174
247
  // Make sure to only delete accounts that were not part of the keyring state.
175
- if (!this.getExistingAccount(snapAccount)) {
248
+ if (!__classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_getExistingAccount).call(this, snapAccount)) {
176
249
  try {
177
- await this.client.deleteAccount(snapAccount.id);
250
+ await __classPrivateFieldGet(this, _SnapKeyring_instances, "a", _SnapKeyring_client_get).deleteAccount(snapAccount.id);
178
251
  }
179
252
  catch (rollbackError) {
180
253
  // Best-effort rollback; log snap-side failures for observability.
@@ -195,11 +268,12 @@ export class SnapKeyring extends SnapKeyringV1 {
195
268
  * @param accountId - ID of the account to delete.
196
269
  */
197
270
  async deleteAccount(accountId) {
271
+ __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_assertInitialized).call(this);
198
272
  // Always remove the account from the registry, even if the Snap is going to
199
273
  // fail to delete it. removeAccount fires onUnregister to clean #accountIndex.
200
274
  this.removeAccount(accountId);
201
275
  try {
202
- await this.client.deleteAccount(accountId);
276
+ await __classPrivateFieldGet(this, _SnapKeyring_instances, "a", _SnapKeyring_client_get).deleteAccount(accountId);
203
277
  }
204
278
  catch (error) {
205
279
  // If the Snap failed to delete the account, log the error and continue
@@ -211,12 +285,13 @@ export class SnapKeyring extends SnapKeyringV1 {
211
285
  /**
212
286
  * Submits a request to the keyring.
213
287
  *
214
- * Validates that the account belongs to this wrapper, then delegates
215
- * to the inherited `submitSnapRequest` for the full request lifecycle
216
- * (sync / async / redirect).
288
+ * For v1 snaps (those without declared capabilities), delegates to the v1
289
+ * event-driven flow that handles the `{ pending, result }` envelope.
290
+ * For v2 snaps, calls the snap directly via the v2 client which returns
291
+ * `Json` with no envelope.
217
292
  *
218
293
  * @param request - The keyring request to submit.
219
- * @param request.id - The request ID (unused — a fresh ID is generated internally).
294
+ * @param request.id - The request ID.
220
295
  * @param request.origin - The sender origin.
221
296
  * @param request.scope - The CAIP-2 chain ID.
222
297
  * @param request.account - The account ID.
@@ -226,17 +301,29 @@ export class SnapKeyring extends SnapKeyringV1 {
226
301
  * @returns A promise that resolves to the response.
227
302
  */
228
303
  async submitRequest(request) {
304
+ __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_assertInitialized).call(this);
229
305
  const account = this.lookupAccount(request.account);
230
306
  if (!account) {
231
307
  throw new Error(`Account '${request.account}' not found in snap '${this.snapId}'`);
232
308
  }
233
- return this.submitSnapRequest({
309
+ if (__classPrivateFieldGet(this, _SnapKeyring_v1, "f")) {
310
+ // v1 snap: use event-driven flow with { pending, result } envelope handling.
311
+ return __classPrivateFieldGet(this, _SnapKeyring_v1, "f").submitSnapRequest({
312
+ origin: request.origin,
313
+ account,
314
+ method: request.request.method,
315
+ params: request.request.params,
316
+ scope: request.scope,
317
+ noPending: false,
318
+ });
319
+ }
320
+ // v2 snap: call snap directly, returns Json (no envelope).
321
+ return __classPrivateFieldGet(this, _SnapKeyring_instances, "a", _SnapKeyring_client_get).submitRequest({
322
+ id: request.id,
234
323
  origin: request.origin,
235
- account,
236
- method: request.request.method,
237
- params: request.request.params,
238
324
  scope: request.scope,
239
- noPending: false,
325
+ account: request.account,
326
+ request: request.request,
240
327
  });
241
328
  }
242
329
  // ──────────────────────────────────────────────
@@ -252,8 +339,8 @@ export class SnapKeyring extends SnapKeyringV1 {
252
339
  * @param account - The account to add or update.
253
340
  */
254
341
  setAccount(account) {
255
- const isNew = !this.registry.has(account.id);
256
- this.registry.set(account);
342
+ const isNew = !__classPrivateFieldGet(this, _SnapKeyring_registry, "f").has(account.id);
343
+ __classPrivateFieldGet(this, _SnapKeyring_registry, "f").set(account);
257
344
  if (isNew) {
258
345
  __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").onRegister?.(account.id);
259
346
  }
@@ -267,10 +354,10 @@ export class SnapKeyring extends SnapKeyringV1 {
267
354
  * @returns `true` if the account was removed, `false` if it was not found.
268
355
  */
269
356
  removeAccount(id) {
270
- if (!this.registry.has(id)) {
357
+ if (!__classPrivateFieldGet(this, _SnapKeyring_registry, "f").has(id)) {
271
358
  return false;
272
359
  }
273
- this.registry.delete(id);
360
+ __classPrivateFieldGet(this, _SnapKeyring_registry, "f").delete(id);
274
361
  __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").onUnregister?.(id);
275
362
  return true;
276
363
  }
@@ -281,7 +368,7 @@ export class SnapKeyring extends SnapKeyringV1 {
281
368
  * @returns `true` if the account exists.
282
369
  */
283
370
  hasAccount(id) {
284
- return this.registry.has(id);
371
+ return __classPrivateFieldGet(this, _SnapKeyring_registry, "f").has(id);
285
372
  }
286
373
  /**
287
374
  * Get an account by its ID.
@@ -290,7 +377,7 @@ export class SnapKeyring extends SnapKeyringV1 {
290
377
  * @returns The account, or `undefined` if not found.
291
378
  */
292
379
  lookupAccount(id) {
293
- return this.registry.get(id);
380
+ return __classPrivateFieldGet(this, _SnapKeyring_registry, "f").get(id);
294
381
  }
295
382
  /**
296
383
  * Get an account by address (case-insensitive).
@@ -302,15 +389,15 @@ export class SnapKeyring extends SnapKeyringV1 {
302
389
  * @returns The account, or `undefined` if not found.
303
390
  */
304
391
  lookupByAddress(address) {
305
- const id = this.registry.getAccountId(address);
392
+ const id = __classPrivateFieldGet(this, _SnapKeyring_registry, "f").getAccountId(address);
306
393
  if (id !== undefined) {
307
- return this.registry.get(id);
394
+ return __classPrivateFieldGet(this, _SnapKeyring_registry, "f").get(id);
308
395
  }
309
396
  // The fallback only runs when the exact-match branch above misses,
310
397
  // which in practice only happens for EVM addresses with casing
311
398
  // differences (checksummed vs lowercase). Non-EVM addresses are
312
399
  // case-sensitive and always resolve on the exact branch.
313
- return this.registry
400
+ return __classPrivateFieldGet(this, _SnapKeyring_registry, "f")
314
401
  .values()
315
402
  .find((account) => equalsIgnoreCase(account.address, address));
316
403
  }
@@ -324,7 +411,7 @@ export class SnapKeyring extends SnapKeyringV1 {
324
411
  * @returns An array of all accounts.
325
412
  */
326
413
  accounts() {
327
- return this.registry.values();
414
+ return __classPrivateFieldGet(this, _SnapKeyring_registry, "f").values();
328
415
  }
329
416
  /**
330
417
  * Serialize this keyring's state.
@@ -337,7 +424,7 @@ export class SnapKeyring extends SnapKeyringV1 {
337
424
  */
338
425
  async serialize() {
339
426
  const accounts = {};
340
- for (const account of this.registry.values()) {
427
+ for (const account of __classPrivateFieldGet(this, _SnapKeyring_registry, "f").values()) {
341
428
  accounts[account.id] = account;
342
429
  }
343
430
  const state = {
@@ -349,9 +436,11 @@ export class SnapKeyring extends SnapKeyringV1 {
349
436
  /**
350
437
  * Restore this keyring from a serialized state.
351
438
  *
352
- * Validates the payload (accepting both v1 and v2 accounts), migrates any
353
- * v1 accounts to v2, then replaces the registry. If validation fails, the
354
- * existing registry is left untouched.
439
+ * Validates the payload (accepting both v1 and v2 account shapes), migrates
440
+ * any v1 accounts to v2, then replaces the registry. Also determines whether
441
+ * the snap is v1 or v2 by reading its manifest capabilities: if no
442
+ * capabilities are declared, a {@link SnapKeyringV1} instance is created and
443
+ * held under {@link SnapKeyring.v1}.
355
444
  *
356
445
  * @param state - The state to deserialize.
357
446
  * @returns A promise that resolves when deserialization is complete.
@@ -362,6 +451,31 @@ export class SnapKeyring extends SnapKeyringV1 {
362
451
  // Bind the keyring to its snap ID (idempotent for the same ID, throws on
363
452
  // mismatch to prevent swapping a keyring's identity via deserialize).
364
453
  this.bindSnapId(state.snapId);
454
+ // Refresh capabilities from the snap manifest on every deserialize, falling
455
+ // back to the empty default so a re-hydrate clears any previously-loaded
456
+ // capabilities when the snap no longer declares them.
457
+ const capabilities = __classPrivateFieldGet(this, _SnapKeyring_instances, "m", _SnapKeyring_resolveKeyringCapabilities).call(this);
458
+ this.capabilities = capabilities ?? EMPTY_CAPABILITIES;
459
+ // Determine snap version and create a v1 instance if needed.
460
+ const v1 = capabilities === undefined;
461
+ if (v1) {
462
+ // v1 snap: no declared capabilities. Create a SnapKeyringV1 instance
463
+ // that shares the registry and messenger owned by this class.
464
+ if (__classPrivateFieldGet(this, _SnapKeyring_v1, "f") === undefined) {
465
+ __classPrivateFieldSet(this, _SnapKeyring_v1, new SnapKeyringV1({
466
+ messenger: __classPrivateFieldGet(this, _SnapKeyring_messenger, "f"),
467
+ callbacks: __classPrivateFieldGet(this, _SnapKeyring_callbacks, "f"),
468
+ registry: __classPrivateFieldGet(this, _SnapKeyring_registry, "f"),
469
+ isAnyAccountTypeAllowed: __classPrivateFieldGet(this, _SnapKeyring_isAnyAccountTypeAllowed, "f"),
470
+ }), "f");
471
+ __classPrivateFieldGet(this, _SnapKeyring_v1, "f").bindSnapId(state.snapId);
472
+ }
473
+ }
474
+ else {
475
+ // v2 snap: tear down any stale v1 instance (e.g. after a manifest update).
476
+ await __classPrivateFieldGet(this, _SnapKeyring_v1, "f")?.destroy();
477
+ __classPrivateFieldSet(this, _SnapKeyring_v1, undefined, "f");
478
+ }
365
479
  // Migrate v1 accounts to v2.
366
480
  const migratedAccounts = {};
367
481
  for (const [id, rawAccount] of Object.entries(state.accounts)) {
@@ -374,7 +488,7 @@ export class SnapKeyring extends SnapKeyringV1 {
374
488
  }
375
489
  }
376
490
  // Apply the migrated state to the registry.
377
- for (const id of [...this.registry.keys()]) {
491
+ for (const id of [...__classPrivateFieldGet(this, _SnapKeyring_registry, "f").keys()]) {
378
492
  this.removeAccount(id);
379
493
  }
380
494
  for (const account of Object.values(migratedAccounts)) {
@@ -382,14 +496,20 @@ export class SnapKeyring extends SnapKeyringV1 {
382
496
  }
383
497
  }
384
498
  }
385
- _SnapKeyring_callbacks = new WeakMap(), _SnapKeyring_lock = new WeakMap(), _SnapKeyring_instances = new WeakSet(), _SnapKeyring_withLock =
499
+ _SnapKeyring_registry = new WeakMap(), _SnapKeyring_messenger = new WeakMap(), _SnapKeyring_callbacks = new WeakMap(), _SnapKeyring_isAnyAccountTypeAllowed = new WeakMap(), _SnapKeyring_lock = new WeakMap(), _SnapKeyring_context = new WeakMap(), _SnapKeyring_v1 = new WeakMap(), _SnapKeyring_instances = new WeakSet(), _SnapKeyring_client_get = function _SnapKeyring_client_get() {
500
+ /* istanbul ignore next */
501
+ if (__classPrivateFieldGet(this, _SnapKeyring_context, "f") === undefined) {
502
+ throw new Error('SnapKeyring is not bound to a snap ID');
503
+ }
504
+ return __classPrivateFieldGet(this, _SnapKeyring_context, "f").client;
505
+ }, _SnapKeyring_withLock =
386
506
  /**
387
507
  * Run a callback under the appropriate lock.
388
508
  *
389
509
  * Prefers the injected `withLock` callback (global lock provided by
390
510
  * `SnapKeyring`) so that `createAccounts` calls across different snaps
391
511
  * are serialized. Falls back to the per-instance `#lock` when no global
392
- * lock is provided.g. standalone use in tests).
512
+ * lock is provided (e.g. standalone use in tests).
393
513
  *
394
514
  * @param callback - Operation to run under the lock.
395
515
  * @returns The result of the callback.
@@ -397,6 +517,27 @@ _SnapKeyring_callbacks = new WeakMap(), _SnapKeyring_lock = new WeakMap(), _Snap
397
517
  async function _SnapKeyring_withLock(callback) {
398
518
  return (__classPrivateFieldGet(this, _SnapKeyring_callbacks, "f").withLock ??
399
519
  (async (operation) => __classPrivateFieldGet(this, _SnapKeyring_lock, "f").runExclusive(operation)))(callback);
520
+ }, _SnapKeyring_assertInitialized = function _SnapKeyring_assertInitialized() {
521
+ if (__classPrivateFieldGet(this, _SnapKeyring_context, "f") === undefined) {
522
+ throw new Error('SnapKeyring has not been initialized: call deserialize() first');
523
+ }
524
+ }, _SnapKeyring_resolveKeyringCapabilities = function _SnapKeyring_resolveKeyringCapabilities() {
525
+ const snap = __classPrivateFieldGet(this, _SnapKeyring_messenger, "f").call('SnapController:getSnap', this.snapId);
526
+ // READ THIS CAREFULLY:
527
+ // We are not validating the shape of the capabilities here, because there is
528
+ // manifest validation done already on the snaps side, the snaps repo maintains
529
+ // a copy of the `KeyringCapabilitiesStruct`.
530
+ // We must ensure that both structs are always in-sync, otherwise the type-cast
531
+ // could cause runtime issues!
532
+ return snap?.manifest.initialPermissions['endowment:keyring']
533
+ ?.capabilities;
534
+ }, _SnapKeyring_getExistingAccount = function _SnapKeyring_getExistingAccount(account) {
535
+ const address = normalizeAccountAddress(account);
536
+ const existing = __classPrivateFieldGet(this, _SnapKeyring_registry, "f").get(account.id);
537
+ if (existing && normalizeAccountAddress(existing) === address) {
538
+ return existing;
539
+ }
540
+ return undefined;
400
541
  };
401
542
  SnapKeyring.type = `${KeyringType.Snap}`;
402
543
  //# sourceMappingURL=SnapKeyring.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"SnapKeyring.mjs","sourceRoot":"","sources":["../../src/v2/SnapKeyring.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,oBAAoB,EAAE,8BAA8B;AAM7D,OAAO,EAAE,WAAW,EAAE,iCAAiC;AAIvD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,8BAA8B;AAE9E,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAEpC,OAAO,EACL,sBAAsB,EACtB,gBAAgB,EAChB,gBAAgB,EACjB,uBAAmB;AACpB,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,gCAAsB;AAC9D,OAAO,EAAE,aAAa,EAAE,6BAAyB;AAMjD,OAAO,EAAE,gBAAgB,EAAE,oBAAgB;AAE3C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;IAC3C,MAAM,EAAE,MAAM,EAAE;IACh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,CAAC;QAC3B,CAAC;IACH,CAAC;;;AA7WD;;;;;;;;;;GAUG;AACH,KAAK,gCAAmB,QAA+B;IACrD,OAAO,CACL,uBAAA,IAAI,8BAAW,CAAC,QAAQ;QACxB,CAAC,KAAK,EAAE,SAAgC,EAAmB,EAAE,CAC3D,uBAAA,IAAI,yBAAM,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,CACtC,CAAC,QAAQ,CAAC,CAAC;AACd,CAAC;AA9Ce,gBAAI,GAAG,GAAG,WAAW,CAAC,IAAI,EAAW,AAAjC,CAAkC","sourcesContent":["import type { KeyringAccount } from '@metamask/keyring-api';\nimport { KeyringAccountStruct } from '@metamask/keyring-api';\nimport type {\n CreateAccountOptions,\n Keyring,\n KeyringCapabilities,\n} from '@metamask/keyring-api/v2';\nimport { KeyringType } from '@metamask/keyring-api/v2';\nimport type { AccountId } from '@metamask/keyring-utils';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport type { Infer } from '@metamask/superstruct';\nimport { assert, object, record, string, union } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport {\n KeyringAccountV1Struct,\n normalizeAccount,\n transformAccount,\n} from '../account';\nimport { isAccountV1, migrateAccountV1 } from '../migrations';\nimport { SnapKeyringV1 } from '../SnapKeyringV1';\nimport type {\n AccountMethod,\n SnapKeyringV1Callbacks,\n SnapKeyringV1Options,\n} from '../SnapKeyringV1';\nimport { equalsIgnoreCase } from '../util';\n\n/**\n * Superstruct schema for {@link SnapKeyringState}.\n *\n * Accepts both v1 accounts (missing `scopes`) and v2 accounts so that\n * persisted state can be validated before migration.\n */\nexport const SnapKeyringStateStruct = object({\n snapId: string(),\n accounts: record(\n string(),\n union([KeyringAccountStruct, KeyringAccountV1Struct]),\n ),\n});\n\n/**\n * Serialized state of a single SnapKeyring instance.\n *\n * Note: this is an internal format only used between SnapKeyring and its\n * parent SnapKeyring. The external KeyringState format (flat `{ account,\n * snapId }` map) is preserved by SnapKeyring.serialize / deserialize.\n *\n * Inferred from {@link SnapKeyringStateStruct}: `snapId` is `string`\n * (not the branded `SnapId`) so the shape stays JSON-compatible without\n * unsafe casts.\n */\nexport type SnapKeyringState = Infer<typeof SnapKeyringStateStruct>;\n\n/**\n * Callbacks injected by the parent `SnapKeyring` for global coordination.\n */\nexport type SnapKeyringCallbacks = SnapKeyringV1Callbacks & {\n /**\n * Run a callback under a global lock to prevent TOCTOU races in\n * `createAccounts` when multiple snaps call `assertAccountCanBeUsed`\n * concurrently.\n *\n * Optional: if omitted, `SnapKeyringV2` falls back to its own per-instance\n * `Mutex`, which is sufficient when the keyring is used standalone.\n */\n withLock?: <Result>(callback: () => Promise<Result>) => Promise<Result>;\n};\n\ntype SnapKeyringOptions = Omit<SnapKeyringV1Options, 'callbacks'> & {\n callbacks: SnapKeyringCallbacks;\n};\n\n/**\n * Checks if a given keyring is a Snap keyring (v2).\n *\n * @param keyring - The keyring to check.\n * @returns `true` if the keyring is a Snap keyring (v2), `false` otherwise.\n */\nexport function isSnapKeyring(keyring: Keyring): keyring is SnapKeyring {\n return keyring.type === KeyringType.Snap;\n}\n\n/**\n * Per-snap keyring wrapper that implements `Keyring`.\n *\n * Extends `SnapKeyringV1` — the v1 event-driven flow (account lifecycle,\n * request handling, asset events) is inherited. This class adds the\n * `KeyringV2` batch interface (`createAccounts`, `deleteAccount`, etc.) and\n * owns the `capabilities` property.\n *\n * The `KeyringAccountRegistry`, snap client, and messenger are all inherited\n * from `SnapKeyringV1` so there is no duplicated state.\n */\nexport class SnapKeyring extends SnapKeyringV1 implements Keyring {\n /**\n * V2-typed view of the callbacks. Stored separately from the V1 private\n * field so that V2-specific callbacks are accessible without casting.\n * Both fields hold the same object reference.\n */\n readonly #callbacks: SnapKeyringCallbacks;\n\n /**\n * Mutex that serializes `createAccounts` calls on this snap instance.\n * Owned here so that each `SnapKeyring` is fully self-contained.\n */\n readonly #lock: Mutex;\n\n // ──────────────────────────────────────────────\n // Keyring properties\n // ──────────────────────────────────────────────\n\n readonly type = `${KeyringType.Snap}` as const;\n\n static readonly type = `${KeyringType.Snap}` as const;\n\n /**\n * Capabilities are snap-specific. Initialized empty and can be updated\n * by the parent when snap metadata becomes available.\n */\n capabilities: KeyringCapabilities;\n\n constructor(options: SnapKeyringOptions) {\n super(options);\n this.#callbacks = options.callbacks;\n this.#lock = new Mutex();\n\n // Default capabilities — parent updates this when snap metadata is available.\n // We cast here because KeyringCapabilities requires a non-empty scopes array,\n // but we don't have the snap metadata at construction time (e.g. during deserialization).\n this.capabilities = { scopes: [] } as unknown as KeyringCapabilities;\n }\n\n /**\n * Destroy this keyring.\n *\n * Delegates to the parent `SnapKeyringV1.destroy()` to reject any pending\n * requests inherited from the v1 flow.\n */\n override async destroy(): Promise<void> {\n await super.destroy();\n }\n\n /**\n * Run a callback under the appropriate lock.\n *\n * Prefers the injected `withLock` callback (global lock provided by\n * `SnapKeyring`) so that `createAccounts` calls across different snaps\n * are serialized. Falls back to the per-instance `#lock` when no global\n * lock is provided.g. standalone use in tests).\n *\n * @param callback - Operation to run under the lock.\n * @returns The result of the callback.\n */\n async #withLock<Result>(callback: () => Promise<Result>): Promise<Result> {\n return (\n this.#callbacks.withLock ??\n (async (operation: () => Promise<Result>): Promise<Result> =>\n this.#lock.runExclusive(operation))\n )(callback);\n }\n\n // ──────────────────────────────────────────────\n // Keyring methods\n // ──────────────────────────────────────────────\n\n /**\n * Returns all accounts managed by this keyring.\n *\n * @returns A promise that resolves to an array of all accounts.\n */\n async getAccounts(): Promise<KeyringAccount[]> {\n return this.accounts();\n }\n\n /**\n * Returns the account with the specified ID.\n *\n * @param accountId - ID of the account to retrieve.\n * @returns A promise that resolves to the account.\n */\n async getAccount(accountId: AccountId): Promise<KeyringAccount> {\n const account = this.lookupAccount(accountId);\n if (!account) {\n throw new Error(\n `Account '${accountId}' not found in snap '${this.snapId}'`,\n );\n }\n return account;\n }\n\n /**\n * Creates one or more new accounts according to the provided options.\n *\n * Deterministic account creation MUST be idempotent, meaning that for\n * deterministic algorithms, like BIP-44, calling this method with the same\n * options should always return the same accounts, even if the accounts\n * already exist in the keyring.\n *\n * NOTE: If some accounts are not allowed (non-unique address, unsupported\n * generic account), this method will skip their creation and ask the Snap\n * to remove them from its state.\n *\n * @param options - Options describing how to create the account(s).\n * @returns A promise that resolves to an array of the created account objects.\n */\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<KeyringAccount[]> {\n return this.#withLock(async () => {\n // Keep track of address/account ID part of this batch, to avoid having duplicates.\n const batchAddresses = new Set<string>();\n const batchIds = new Set<string>();\n\n const accounts: KeyringAccount[] = [];\n const newAccounts: KeyringAccount[] = [];\n const snapAccounts = await this.client.createAccounts(options);\n\n try {\n for (const snapAccount of snapAccounts) {\n let account = normalizeAccount(transformAccount(snapAccount));\n const { address } = account;\n\n // Check for idempotency.\n const existingAccount = this.getExistingAccount(account);\n if (existingAccount) {\n // NOTE: We re-use the account from the internal state to avoid having the Snap\n // mutating the account object without updating the map.\n account = existingAccount;\n } else {\n await this.#callbacks.assertAccountCanBeUsed(account);\n\n // Also check for transient accounts that are not yet part of the keyring\n // state.\n if (batchAddresses.has(address) || batchIds.has(account.id)) {\n throw new Error(\n `Account '${account.id}' is already part of this batch (same address or account ID)`,\n );\n }\n batchAddresses.add(address);\n batchIds.add(account.id);\n\n // NOTE: This method does not rely on the `AccountCreated` event to add\n // accounts to the keyring, so we have to add them to the state manually.\n newAccounts.push(account);\n }\n\n // New AND existing accounts are returned to the caller no matter what.\n accounts.push(account);\n }\n\n // We update the keyring state only if needed.\n if (newAccounts.length > 0) {\n for (const account of newAccounts) {\n this.setAccount(account);\n }\n\n // NOTE: We assume this will never fail, thus, we don't need to rollback the\n // keyring state if anything goes wrong here.\n await this.#callbacks.saveState();\n }\n\n return accounts;\n } catch (error) {\n // Rollback Snap state.\n for (const snapAccount of snapAccounts) {\n // Make sure to only delete accounts that were not part of the keyring state.\n if (!this.getExistingAccount(snapAccount)) {\n try {\n await this.client.deleteAccount(snapAccount.id);\n } catch (rollbackError) {\n // Best-effort rollback; log snap-side failures for observability.\n console.error(\n `Account '${snapAccount.id}' may not have been removed from snap '${this.snapId}' during createAccounts rollback:`,\n rollbackError,\n );\n }\n }\n }\n\n throw error;\n }\n });\n }\n\n /**\n * Deletes the account with the specified ID.\n *\n * Removes the account from the local registry (firing `onUnregister` so the\n * parent can update its index), then asks the snap to delete it.\n *\n * @param accountId - ID of the account to delete.\n */\n async deleteAccount(accountId: AccountId): Promise<void> {\n // Always remove the account from the registry, even if the Snap is going to\n // fail to delete it. removeAccount fires onUnregister to clean #accountIndex.\n this.removeAccount(accountId);\n\n try {\n await this.client.deleteAccount(accountId);\n } catch (error) {\n // If the Snap failed to delete the account, log the error and continue\n // with the account deletion, otherwise the account will be stuck in the\n // keyring.\n console.error(\n `Account '${accountId}' may not have been removed from snap '${this.snapId}':`,\n error,\n );\n }\n }\n\n /**\n * Submits a request to the keyring.\n *\n * Validates that the account belongs to this wrapper, then delegates\n * to the inherited `submitSnapRequest` for the full request lifecycle\n * (sync / async / redirect).\n *\n * @param request - The keyring request to submit.\n * @param request.id - The request ID (unused — a fresh ID is generated internally).\n * @param request.origin - The sender origin.\n * @param request.scope - The CAIP-2 chain ID.\n * @param request.account - The account ID.\n * @param request.request - The inner JSON-RPC request.\n * @param request.request.method - The method to call.\n * @param request.request.params - The method parameters.\n * @returns A promise that resolves to the response.\n */\n async submitRequest(request: {\n id: string;\n origin: string;\n scope: string;\n account: AccountId;\n request: {\n method: string;\n params?: Json[] | Record<string, Json>;\n };\n }): Promise<Json> {\n const account = this.lookupAccount(request.account);\n if (!account) {\n throw new Error(\n `Account '${request.account}' not found in snap '${this.snapId}'`,\n );\n }\n return this.submitSnapRequest({\n origin: request.origin,\n account,\n method: request.request.method as AccountMethod,\n params: request.request.params,\n scope: request.scope,\n noPending: false,\n });\n }\n\n // ──────────────────────────────────────────────\n // Internal API (used by parent SnapKeyring for event handling, routing, etc.)\n // ──────────────────────────────────────────────\n\n /**\n * Upsert an account into the registry.\n *\n * Fires `onRegister` only when the account is new (not already present).\n * Safe to call for updates -- the parent index is already populated in that\n * case and the no-op path avoids a redundant write.\n *\n * @param account - The account to add or update.\n */\n setAccount(account: KeyringAccount): void {\n const isNew = !this.registry.has(account.id);\n this.registry.set(account);\n if (isNew) {\n this.#callbacks.onRegister?.(account.id);\n }\n }\n\n /**\n * Remove an account from the registry.\n *\n * Fires `onUnregister` so the parent can drop the account from its index.\n *\n * @param id - The account ID to remove.\n * @returns `true` if the account was removed, `false` if it was not found.\n */\n removeAccount(id: AccountId): boolean {\n if (!this.registry.has(id)) {\n return false;\n }\n this.registry.delete(id);\n this.#callbacks.onUnregister?.(id);\n return true;\n }\n\n /**\n * Check whether an account exists in this keyring.\n *\n * @param id - The account ID to check.\n * @returns `true` if the account exists.\n */\n hasAccount(id: AccountId): boolean {\n return this.registry.has(id);\n }\n\n /**\n * Get an account by its ID.\n *\n * @param id - The account ID to look up.\n * @returns The account, or `undefined` if not found.\n */\n lookupAccount(id: AccountId): KeyringAccount | undefined {\n return this.registry.get(id);\n }\n\n /**\n * Get an account by address (case-insensitive).\n *\n * Performs an O(1) exact lookup first; falls back to a linear scan to\n * handle addresses stored with different casing.\n *\n * @param address - The address to look up.\n * @returns The account, or `undefined` if not found.\n */\n lookupByAddress(address: string): KeyringAccount | undefined {\n const id = this.registry.getAccountId(address);\n if (id !== undefined) {\n return this.registry.get(id);\n }\n // The fallback only runs when the exact-match branch above misses,\n // which in practice only happens for EVM addresses with casing\n // differences (checksummed vs lowercase). Non-EVM addresses are\n // case-sensitive and always resolve on the exact branch.\n return this.registry\n .values()\n .find((account) => equalsIgnoreCase(account.address, address));\n }\n\n /**\n * Get all accounts in this keyring (synchronous).\n *\n * This exists alongside the async `getAccounts()` (from `Keyring`) because\n * `SnapKeyring` needs synchronous access for iteration in `serialize`,\n * `listAccounts`, `hasSnapId`, etc. without awaiting.\n *\n * @returns An array of all accounts.\n */\n accounts(): KeyringAccount[] {\n return this.registry.values();\n }\n\n /**\n * Serialize this keyring's state.\n *\n * The returned object uses the internal per-snap format; the parent\n * `SnapKeyring` reconstructs the flat external format in its own\n * `serialize()`.\n *\n * @returns The serialized state.\n */\n async serialize(): Promise<SnapKeyringState> {\n const accounts: SnapKeyringState['accounts'] = {};\n for (const account of this.registry.values()) {\n accounts[account.id] = account;\n }\n const state: SnapKeyringState = {\n snapId: this.snapId,\n accounts,\n };\n return state;\n }\n\n /**\n * Restore this keyring from a serialized state.\n *\n * Validates the payload (accepting both v1 and v2 accounts), migrates any\n * v1 accounts to v2, then replaces the registry. If validation fails, the\n * existing registry is left untouched.\n *\n * @param state - The state to deserialize.\n * @returns A promise that resolves when deserialization is complete.\n */\n async deserialize(state: Json): Promise<void> {\n // Validate the raw payload — accepts both v1 and v2 account shapes.\n assert(state, SnapKeyringStateStruct);\n\n // Bind the keyring to its snap ID (idempotent for the same ID, throws on\n // mismatch to prevent swapping a keyring's identity via deserialize).\n this.bindSnapId(state.snapId as SnapId);\n\n // Migrate v1 accounts to v2.\n const migratedAccounts: Record<string, KeyringAccount> = {};\n for (const [id, rawAccount] of Object.entries(state.accounts)) {\n if (isAccountV1(rawAccount)) {\n console.info(\n `SnapKeyring - Found a KeyringAccountV1, migrating to V2: ${rawAccount.id}`,\n );\n migratedAccounts[id] = migrateAccountV1(rawAccount);\n } else {\n migratedAccounts[id] = rawAccount as KeyringAccount;\n }\n }\n\n // Apply the migrated state to the registry.\n for (const id of [...this.registry.keys()]) {\n this.removeAccount(id);\n }\n\n for (const account of Object.values(migratedAccounts)) {\n this.setAccount(account);\n }\n }\n\n // ──────────────────────────────────────────────\n // Private helpers\n // ──────────────────────────────────────────────\n}\n"]}
1
+ {"version":3,"file":"SnapKeyring.mjs","sourceRoot":"","sources":["../../src/v2/SnapKeyring.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,oBAAoB,EAAE,8BAA8B;AAM7D,OAAO,EAAE,WAAW,EAAE,iCAAiC;AACvD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,uBAAA,IAAI,8BAAW,CAAC,QAAQ;QACxB,CAAC,KAAK,EAAE,SAAgC,EAAmB,EAAE,CAC3D,uBAAA,IAAI,yBAAM,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,CACtC,CAAC,QAAQ,CAAC,CAAC;AACd,CAAC;IA0ZC,IAAI,uBAAA,IAAI,4BAAS,KAAK,SAAS,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;IACJ,CAAC;AACH,CAAC;IAQC,MAAM,IAAI,GAAG,uBAAA,IAAI,8BAAW,CAAC,IAAI,CAAC,wBAAwB,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACzE,uBAAuB;IACvB,6EAA6E;IAC7E,+EAA+E;IAC/E,6CAA6C;IAC7C,+EAA+E;IAC/E,8BAA8B;IAC9B,OAAO,IAAI,EAAE,QAAQ,CAAC,kBAAkB,CAAC,mBAAmB,CAAC;QAC3D,EAAE,YAA+C,CAAC;AACtD,CAAC,6EASmB,OAAuB;IACzC,MAAM,OAAO,GAAG,uBAAuB,CAAC,OAAO,CAAC,CAAC;IACjD,MAAM,QAAQ,GAAG,uBAAA,IAAI,6BAAU,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAChD,IAAI,QAAQ,IAAI,uBAAuB,CAAC,QAAQ,CAAC,KAAK,OAAO,EAAE,CAAC;QAC9D,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAvjBe,gBAAI,GAAG,GAAG,WAAW,CAAC,IAAI,EAAW,AAAjC,CAAkC","sourcesContent":["import type { KeyringAccount } from '@metamask/keyring-api';\nimport { KeyringAccountStruct } from '@metamask/keyring-api';\nimport type {\n CreateAccountOptions,\n Keyring,\n KeyringCapabilities,\n} from '@metamask/keyring-api/v2';\nimport { KeyringType } from '@metamask/keyring-api/v2';\nimport { KeyringInternalSnapClient } from '@metamask/keyring-internal-snap-client/v2';\nimport { KeyringAccountRegistry } from '@metamask/keyring-sdk';\nimport type { AccountId } from '@metamask/keyring-utils';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport type { Infer } from '@metamask/superstruct';\nimport { assert, object, record, string, union } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport {\n KeyringAccountV1Struct,\n normalizeAccount,\n normalizeAccountAddress,\n transformAccount,\n} from '../account';\nimport { isAccountV1, migrateAccountV1 } from '../migrations';\nimport type { SnapKeyringMessenger } from '../SnapKeyringMessenger';\nimport { SnapKeyringV1 } from '../SnapKeyringV1';\nimport type { AccountMethod, SnapKeyringV1Callbacks } from '../SnapKeyringV1';\nimport { equalsIgnoreCase } from '../util';\n\n/**\n * Default, empty capabilities used until the snap manifest is read on\n * `deserialize`. Typed (no cast) so that adding a new required\n * `KeyringCapabilities` field surfaces here as a compile error.\n */\nexport const EMPTY_CAPABILITIES: KeyringCapabilities = Object.freeze({\n scopes: [],\n});\n\n/**\n * Superstruct schema for {@link SnapKeyringState}.\n *\n * Accepts both v1 accounts (missing `scopes`) and v2 accounts so that\n * persisted state can be validated before migration.\n */\nexport const SnapKeyringStateStruct = object({\n snapId: string(),\n accounts: record(\n string(),\n union([KeyringAccountStruct, KeyringAccountV1Struct]),\n ),\n});\n\n/**\n * Serialized state of a single SnapKeyring instance.\n *\n * Note: this is an internal format only used between SnapKeyring and its\n * parent SnapKeyring. The external KeyringState format (flat `{ account,\n * snapId }` map) is preserved by SnapKeyring.serialize / deserialize.\n *\n * Inferred from {@link SnapKeyringStateStruct}: `snapId` is `string`\n * (not the branded `SnapId`) so the shape stays JSON-compatible without\n * unsafe casts.\n */\nexport type SnapKeyringState = Infer<typeof SnapKeyringStateStruct>;\n\n/**\n * Callbacks injected by the parent `SnapKeyring` for global coordination.\n */\nexport type SnapKeyringCallbacks = SnapKeyringV1Callbacks & {\n /**\n * Run a callback under a global lock to prevent TOCTOU races in\n * `createAccounts` when multiple snaps call `assertAccountCanBeUsed`\n * concurrently.\n *\n * Optional: if omitted, `SnapKeyringV2` falls back to its own per-instance\n * `Mutex`, which is sufficient when the keyring is used standalone.\n */\n withLock?: <Result>(callback: () => Promise<Result>) => Promise<Result>;\n};\n\ntype SnapKeyringOptions = {\n messenger: SnapKeyringMessenger;\n callbacks: SnapKeyringCallbacks;\n isAnyAccountTypeAllowed?: boolean;\n};\n\n/**\n * Holds the v2 snap client once a {@link SnapKeyring} instance has been bound\n * via {@link SnapKeyring.bindSnapId}.\n */\ntype SnapKeyringContext = {\n snapId: SnapId;\n client: KeyringInternalSnapClient;\n};\n\n/**\n * Checks if a given keyring is a Snap keyring (v2).\n *\n * @param keyring - The keyring to check.\n * @returns `true` if the keyring is a Snap keyring (v2), `false` otherwise.\n */\nexport function isSnapKeyring(keyring: Keyring): keyring is SnapKeyring {\n return keyring.type === KeyringType.Snap;\n}\n\n/**\n * Per-snap keyring that implements `Keyring` (v2).\n *\n * Owns the account registry and messenger. For v1 snaps (those that do not\n * declare `endowment:keyring` capabilities in their manifest), a\n * {@link SnapKeyringV1} instance is created on `deserialize` and held under\n * {@link SnapKeyring.v1}. V2 snaps have `v1 === undefined` and communicate\n * directly through the v2 client without the `{ pending, result }` envelope.\n */\nexport class SnapKeyring implements Keyring {\n /** Account registry — shared by reference with the v1 instance when present. */\n readonly #registry: KeyringAccountRegistry;\n\n /** Messenger for snap controller calls and event publishing. */\n readonly #messenger: SnapKeyringMessenger;\n\n readonly #callbacks: SnapKeyringCallbacks;\n\n readonly #isAnyAccountTypeAllowed: boolean;\n\n /**\n * Mutex that serializes `createAccounts` calls on this snap instance.\n * Owned here so that each `SnapKeyring` is fully self-contained.\n */\n readonly #lock: Mutex;\n\n /** V2 snap client. Set via {@link bindSnapId}. */\n #context: SnapKeyringContext | undefined;\n\n /**\n * V1 instance, present only when the snap does not declare v2 capabilities.\n * Created on `deserialize()` after reading the snap manifest.\n */\n #v1: SnapKeyringV1 | undefined;\n\n // ──────────────────────────────────────────────\n // Keyring properties\n // ──────────────────────────────────────────────\n\n readonly type = `${KeyringType.Snap}` as const;\n\n static readonly type = `${KeyringType.Snap}` as const;\n\n /**\n * Capabilities are snap-specific. Initialized empty and can be updated\n * by the parent when snap metadata becomes available.\n */\n capabilities: KeyringCapabilities;\n\n constructor({\n messenger,\n callbacks,\n isAnyAccountTypeAllowed = false,\n }: SnapKeyringOptions) {\n this.#registry = new KeyringAccountRegistry();\n this.#messenger = messenger;\n this.#callbacks = callbacks;\n this.#isAnyAccountTypeAllowed = isAnyAccountTypeAllowed;\n this.#lock = new Mutex();\n\n // Default capabilities; replaced from the snap manifest on `deserialize`.\n this.capabilities = EMPTY_CAPABILITIES;\n }\n\n /**\n * Gets the v1 instance for this snap, or `undefined` if the snap is v2-only.\n *\n * @returns The v1 instance, or `undefined` if the snap is v2-only.\n *\n * Use this to make v1 calls explicit:\n * ```ts\n * keyring.v1?.signTransaction(account, tx);\n * ```\n */\n get v1(): SnapKeyringV1 | undefined {\n return this.#v1;\n }\n\n /**\n * The snap ID this instance is scoped to.\n *\n * @returns The snap ID.\n * @throws If the keyring has not been initialized yet.\n */\n get snapId(): SnapId {\n /* istanbul ignore next */\n if (this.#context === undefined) {\n throw new Error(\n 'SnapKeyring has not been initialized: call deserialize() first',\n );\n }\n return this.#context.snapId;\n }\n\n /**\n * Bind this keyring to a snap ID and initialize the v2 client.\n *\n * Idempotent for the same `snapId`; throws if called again with a different\n * one to prevent accidentally swapping a keyring's identity.\n *\n * @param snapId - The snap ID to bind to.\n */\n protected bindSnapId(snapId: SnapId): void {\n if (this.#context !== undefined && this.#context.snapId !== snapId) {\n throw new Error(\n `SnapKeyring bound to '${this.#context.snapId}' cannot be rebound to '${snapId}'`,\n );\n }\n if (this.#context === undefined) {\n this.#context = {\n snapId,\n client: new KeyringInternalSnapClient({\n messenger: this.#messenger,\n snapId,\n }),\n };\n }\n }\n\n /**\n * Returns the v2 snap client.\n *\n * @returns The v2 snap client.\n * @throws If the keyring is not yet initialized.\n */\n get #client(): KeyringInternalSnapClient {\n /* istanbul ignore next */\n if (this.#context === undefined) {\n throw new Error('SnapKeyring is not bound to a snap ID');\n }\n return this.#context.client;\n }\n\n /**\n * Destroy this keyring, rejecting any pending v1 requests.\n *\n * @returns A promise that resolves when the keyring is destroyed.\n */\n async destroy(): Promise<void> {\n await this.#v1?.destroy();\n this.#v1 = undefined;\n }\n\n /**\n * Run a callback under the appropriate lock.\n *\n * Prefers the injected `withLock` callback (global lock provided by\n * `SnapKeyring`) so that `createAccounts` calls across different snaps\n * are serialized. Falls back to the per-instance `#lock` when no global\n * lock is provided (e.g. standalone use in tests).\n *\n * @param callback - Operation to run under the lock.\n * @returns The result of the callback.\n */\n async #withLock<Result>(callback: () => Promise<Result>): Promise<Result> {\n return (\n this.#callbacks.withLock ??\n (async (operation: () => Promise<Result>): Promise<Result> =>\n this.#lock.runExclusive(operation))\n )(callback);\n }\n\n // ──────────────────────────────────────────────\n // Keyring methods\n // ──────────────────────────────────────────────\n\n /**\n * Returns all accounts managed by this keyring.\n *\n * @returns A promise that resolves to an array of all accounts.\n */\n async getAccounts(): Promise<KeyringAccount[]> {\n this.#assertInitialized();\n return this.accounts();\n }\n\n /**\n * Returns the account with the specified ID.\n *\n * @param accountId - ID of the account to retrieve.\n * @returns A promise that resolves to the account.\n */\n async getAccount(accountId: AccountId): Promise<KeyringAccount> {\n this.#assertInitialized();\n const account = this.lookupAccount(accountId);\n if (!account) {\n throw new Error(\n `Account '${accountId}' not found in snap '${this.snapId}'`,\n );\n }\n return account;\n }\n\n /**\n * Creates one or more new accounts according to the provided options.\n *\n * Deterministic account creation MUST be idempotent, meaning that for\n * deterministic algorithms, like BIP-44, calling this method with the same\n * options should always return the same accounts, even if the accounts\n * already exist in the keyring.\n *\n * NOTE: If some accounts are not allowed (non-unique address, unsupported\n * generic account), this method will skip their creation and ask the Snap\n * to remove them from its state.\n *\n * @param options - Options describing how to create the account(s).\n * @returns A promise that resolves to an array of the created account objects.\n */\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<KeyringAccount[]> {\n this.#assertInitialized();\n return this.#withLock(async () => {\n // Keep track of address/account ID part of this batch, to avoid having duplicates.\n const batchAddresses = new Set<string>();\n const batchIds = new Set<string>();\n\n const accounts: KeyringAccount[] = [];\n const newAccounts: KeyringAccount[] = [];\n let snapAccounts: KeyringAccount[];\n if (this.#v1) {\n // v1 snap: the v1 client wraps options in `{ options }` before sending\n // `keyring_createAccounts`, while the v2 client sends flat options.\n // Route through v1 so v1 snaps receive the format they expect.\n snapAccounts = await this.#v1.createAccounts(options);\n } else {\n // v2 snap: call snap directly with flat options.\n snapAccounts = await this.#client.createAccounts(options);\n }\n\n try {\n for (const snapAccount of snapAccounts) {\n let account = normalizeAccount(transformAccount(snapAccount));\n const { address } = account;\n\n // Check for idempotency.\n const existingAccount = this.#getExistingAccount(account);\n if (existingAccount) {\n // NOTE: We re-use the account from the internal state to avoid having the Snap\n // mutating the account object without updating the map.\n account = existingAccount;\n } else {\n await this.#callbacks.assertAccountCanBeUsed(account);\n\n // Also check for transient accounts that are not yet part of the keyring\n // state.\n if (batchAddresses.has(address) || batchIds.has(account.id)) {\n throw new Error(\n `Account '${account.id}' is already part of this batch (same address or account ID)`,\n );\n }\n batchAddresses.add(address);\n batchIds.add(account.id);\n\n // NOTE: This method does not rely on the `AccountCreated` event to add\n // accounts to the keyring, so we have to add them to the state manually.\n newAccounts.push(account);\n }\n\n // New AND existing accounts are returned to the caller no matter what.\n accounts.push(account);\n }\n\n // We update the keyring state only if needed.\n if (newAccounts.length > 0) {\n for (const account of newAccounts) {\n this.setAccount(account);\n }\n }\n\n return accounts;\n } catch (error) {\n // Rollback Snap state.\n for (const snapAccount of snapAccounts) {\n // Make sure to only delete accounts that were not part of the keyring state.\n if (!this.#getExistingAccount(snapAccount)) {\n try {\n await this.#client.deleteAccount(snapAccount.id);\n } catch (rollbackError) {\n // Best-effort rollback; log snap-side failures for observability.\n console.error(\n `Account '${snapAccount.id}' may not have been removed from snap '${this.snapId}' during createAccounts rollback:`,\n rollbackError,\n );\n }\n }\n }\n\n throw error;\n }\n });\n }\n\n /**\n * Deletes the account with the specified ID.\n *\n * Removes the account from the local registry (firing `onUnregister` so the\n * parent can update its index), then asks the snap to delete it.\n *\n * @param accountId - ID of the account to delete.\n */\n async deleteAccount(accountId: AccountId): Promise<void> {\n this.#assertInitialized();\n // Always remove the account from the registry, even if the Snap is going to\n // fail to delete it. removeAccount fires onUnregister to clean #accountIndex.\n this.removeAccount(accountId);\n\n try {\n await this.#client.deleteAccount(accountId);\n } catch (error) {\n // If the Snap failed to delete the account, log the error and continue\n // with the account deletion, otherwise the account will be stuck in the\n // keyring.\n console.error(\n `Account '${accountId}' may not have been removed from snap '${this.snapId}':`,\n error,\n );\n }\n }\n\n /**\n * Submits a request to the keyring.\n *\n * For v1 snaps (those without declared capabilities), delegates to the v1\n * event-driven flow that handles the `{ pending, result }` envelope.\n * For v2 snaps, calls the snap directly via the v2 client which returns\n * `Json` with no envelope.\n *\n * @param request - The keyring request to submit.\n * @param request.id - The request ID.\n * @param request.origin - The sender origin.\n * @param request.scope - The CAIP-2 chain ID.\n * @param request.account - The account ID.\n * @param request.request - The inner JSON-RPC request.\n * @param request.request.method - The method to call.\n * @param request.request.params - The method parameters.\n * @returns A promise that resolves to the response.\n */\n async submitRequest(request: {\n id: string;\n origin: string;\n scope: string;\n account: AccountId;\n request: {\n method: string;\n params?: Json[] | Record<string, Json>;\n };\n }): Promise<Json> {\n this.#assertInitialized();\n const account = this.lookupAccount(request.account);\n if (!account) {\n throw new Error(\n `Account '${request.account}' not found in snap '${this.snapId}'`,\n );\n }\n if (this.#v1) {\n // v1 snap: use event-driven flow with { pending, result } envelope handling.\n return this.#v1.submitSnapRequest({\n origin: request.origin,\n account,\n method: request.request.method as AccountMethod,\n params: request.request.params,\n scope: request.scope,\n noPending: false,\n });\n }\n // v2 snap: call snap directly, returns Json (no envelope).\n return this.#client.submitRequest({\n id: request.id,\n origin: request.origin,\n scope: request.scope,\n account: request.account,\n request: request.request,\n });\n }\n\n // ──────────────────────────────────────────────\n // Internal API (used by parent SnapKeyring for event handling, routing, etc.)\n // ──────────────────────────────────────────────\n\n /**\n * Upsert an account into the registry.\n *\n * Fires `onRegister` only when the account is new (not already present).\n * Safe to call for updates -- the parent index is already populated in that\n * case and the no-op path avoids a redundant write.\n *\n * @param account - The account to add or update.\n */\n setAccount(account: KeyringAccount): void {\n const isNew = !this.#registry.has(account.id);\n this.#registry.set(account);\n if (isNew) {\n this.#callbacks.onRegister?.(account.id);\n }\n }\n\n /**\n * Remove an account from the registry.\n *\n * Fires `onUnregister` so the parent can drop the account from its index.\n *\n * @param id - The account ID to remove.\n * @returns `true` if the account was removed, `false` if it was not found.\n */\n removeAccount(id: AccountId): boolean {\n if (!this.#registry.has(id)) {\n return false;\n }\n this.#registry.delete(id);\n this.#callbacks.onUnregister?.(id);\n return true;\n }\n\n /**\n * Check whether an account exists in this keyring.\n *\n * @param id - The account ID to check.\n * @returns `true` if the account exists.\n */\n hasAccount(id: AccountId): boolean {\n return this.#registry.has(id);\n }\n\n /**\n * Get an account by its ID.\n *\n * @param id - The account ID to look up.\n * @returns The account, or `undefined` if not found.\n */\n lookupAccount(id: AccountId): KeyringAccount | undefined {\n return this.#registry.get(id);\n }\n\n /**\n * Get an account by address (case-insensitive).\n *\n * Performs an O(1) exact lookup first; falls back to a linear scan to\n * handle addresses stored with different casing.\n *\n * @param address - The address to look up.\n * @returns The account, or `undefined` if not found.\n */\n lookupByAddress(address: string): KeyringAccount | undefined {\n const id = this.#registry.getAccountId(address);\n if (id !== undefined) {\n return this.#registry.get(id);\n }\n // The fallback only runs when the exact-match branch above misses,\n // which in practice only happens for EVM addresses with casing\n // differences (checksummed vs lowercase). Non-EVM addresses are\n // case-sensitive and always resolve on the exact branch.\n return this.#registry\n .values()\n .find((account) => equalsIgnoreCase(account.address, address));\n }\n\n /**\n * Get all accounts in this keyring (synchronous).\n *\n * This exists alongside the async `getAccounts()` (from `Keyring`) because\n * `SnapKeyring` needs synchronous access for iteration in `serialize`,\n * `listAccounts`, `hasSnapId`, etc. without awaiting.\n *\n * @returns An array of all accounts.\n */\n accounts(): KeyringAccount[] {\n return this.#registry.values();\n }\n\n /**\n * Serialize this keyring's state.\n *\n * The returned object uses the internal per-snap format; the parent\n * `SnapKeyring` reconstructs the flat external format in its own\n * `serialize()`.\n *\n * @returns The serialized state.\n */\n async serialize(): Promise<SnapKeyringState> {\n const accounts: SnapKeyringState['accounts'] = {};\n for (const account of this.#registry.values()) {\n accounts[account.id] = account;\n }\n const state: SnapKeyringState = {\n snapId: this.snapId,\n accounts,\n };\n return state;\n }\n\n /**\n * Restore this keyring from a serialized state.\n *\n * Validates the payload (accepting both v1 and v2 account shapes), migrates\n * any v1 accounts to v2, then replaces the registry. Also determines whether\n * the snap is v1 or v2 by reading its manifest capabilities: if no\n * capabilities are declared, a {@link SnapKeyringV1} instance is created and\n * held under {@link SnapKeyring.v1}.\n *\n * @param state - The state to deserialize.\n * @returns A promise that resolves when deserialization is complete.\n */\n async deserialize(state: Json): Promise<void> {\n // Validate the raw payload — accepts both v1 and v2 account shapes.\n assert(state, SnapKeyringStateStruct);\n\n // Bind the keyring to its snap ID (idempotent for the same ID, throws on\n // mismatch to prevent swapping a keyring's identity via deserialize).\n this.bindSnapId(state.snapId as SnapId);\n\n // Refresh capabilities from the snap manifest on every deserialize, falling\n // back to the empty default so a re-hydrate clears any previously-loaded\n // capabilities when the snap no longer declares them.\n const capabilities = this.#resolveKeyringCapabilities();\n this.capabilities = capabilities ?? EMPTY_CAPABILITIES;\n\n // Determine snap version and create a v1 instance if needed.\n const v1 = capabilities === undefined;\n if (v1) {\n // v1 snap: no declared capabilities. Create a SnapKeyringV1 instance\n // that shares the registry and messenger owned by this class.\n if (this.#v1 === undefined) {\n this.#v1 = new SnapKeyringV1({\n messenger: this.#messenger,\n callbacks: this.#callbacks,\n registry: this.#registry,\n isAnyAccountTypeAllowed: this.#isAnyAccountTypeAllowed,\n });\n this.#v1.bindSnapId(state.snapId as SnapId);\n }\n } else {\n // v2 snap: tear down any stale v1 instance (e.g. after a manifest update).\n await this.#v1?.destroy();\n this.#v1 = undefined;\n }\n\n // Migrate v1 accounts to v2.\n const migratedAccounts: Record<string, KeyringAccount> = {};\n for (const [id, rawAccount] of Object.entries(state.accounts)) {\n if (isAccountV1(rawAccount)) {\n console.info(\n `SnapKeyring - Found a KeyringAccountV1, migrating to V2: ${rawAccount.id}`,\n );\n migratedAccounts[id] = migrateAccountV1(rawAccount);\n } else {\n migratedAccounts[id] = rawAccount as KeyringAccount;\n }\n }\n\n // Apply the migrated state to the registry.\n for (const id of [...this.#registry.keys()]) {\n this.removeAccount(id);\n }\n\n for (const account of Object.values(migratedAccounts)) {\n this.setAccount(account);\n }\n }\n\n // ──────────────────────────────────────────────\n // Private helpers\n // ──────────────────────────────────────────────\n\n /**\n * Assert that the keyring has been initialized.\n *\n * @throws An error if the keyring has not been initialized.\n */\n #assertInitialized(): void {\n if (this.#context === undefined) {\n throw new Error(\n 'SnapKeyring has not been initialized: call deserialize() first',\n );\n }\n }\n\n /**\n * Resolve the keyring capabilities from the snap manifest.\n *\n * @returns The keyring capabilities, or `undefined` if the snap manifest does not declare any capabilities.\n */\n #resolveKeyringCapabilities(): KeyringCapabilities | undefined {\n const snap = this.#messenger.call('SnapController:getSnap', this.snapId);\n // READ THIS CAREFULLY:\n // We are not validating the shape of the capabilities here, because there is\n // manifest validation done already on the snaps side, the snaps repo maintains\n // a copy of the `KeyringCapabilitiesStruct`.\n // We must ensure that both structs are always in-sync, otherwise the type-cast\n // could cause runtime issues!\n return snap?.manifest.initialPermissions['endowment:keyring']\n ?.capabilities as KeyringCapabilities | undefined;\n }\n\n /**\n * Check whether an account with the same ID and address already exists in\n * the registry. Used for idempotent account creation.\n *\n * @param account - The account to check against the registry.\n * @returns The existing account if found, `undefined` otherwise.\n */\n #getExistingAccount(account: KeyringAccount): KeyringAccount | undefined {\n const address = normalizeAccountAddress(account);\n const existing = this.#registry.get(account.id);\n if (existing && normalizeAccountAddress(existing) === address) {\n return existing;\n }\n return undefined;\n }\n}\n"]}