@metamask-previews/keyring-controller 25.2.0-preview-8fff4f2dc → 25.2.0-preview-54abb25

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,7 +9,7 @@ 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 _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_encryptionKey, _KeyringController_getKeyringForAccount, _KeyringController_findKeyringIndexForAccount, _KeyringController_assertNoUnsafeDirectKeyringAccess, _KeyringController_registerMessageHandlers, _KeyringController_selectKeyring, _KeyringController_getKeyringById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _KeyringController_createNewVaultWithKeyring, _KeyringController_deriveAndSetEncryptionKey, _KeyringController_setEncryptionKey, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_getSessionState, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_isNewEncryptionAvailable, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_assertNoDuplicateAccounts, _KeyringController_setUnlocked, _KeyringController_assertIsUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
12
+ var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_keyringV2Builders, _KeyringController_encryptor, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_encryptionKey, _KeyringController_getKeyringForAccount, _KeyringController_getKeyringEntryForAccount, _KeyringController_findKeyringIndexForAccount, _KeyringController_getKeyringEntriesByType, _KeyringController_assertNoUnsafeDirectKeyringAccess, _KeyringController_registerMessageHandlers, _KeyringController_selectKeyringEntry, _KeyringController_selectKeyring, _KeyringController_getKeyringById, _KeyringController_getKeyringEntryById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _KeyringController_getKeyringV2BuilderForType, _KeyringController_createNewVaultWithKeyring, _KeyringController_deriveAndSetEncryptionKey, _KeyringController_setEncryptionKey, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_getSessionState, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_isNewEncryptionAvailable, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_assertNoDuplicateAccounts, _KeyringController_setUnlocked, _KeyringController_assertIsUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
@@ -18,9 +18,9 @@ function $importDefault(module) {
18
18
  }
19
19
  import { isValidPrivate, getBinarySize } from "@ethereumjs/util";
20
20
  import { BaseController } from "@metamask/base-controller";
21
- import { HdKeyring } from "@metamask/eth-hd-keyring";
21
+ import { HdKeyring, HdKeyringV2 } from "@metamask/eth-hd-keyring";
22
22
  import { normalize as ethNormalize } from "@metamask/eth-sig-util";
23
- import SimpleKeyring from "@metamask/eth-simple-keyring";
23
+ import SimpleKeyring, { SimpleKeyringV2 } from "@metamask/eth-simple-keyring";
24
24
  import { add0x, assertIsStrictHexString, bytesToHex, hasProperty, hexToBytes, isObject, isStrictHexString, isValidHexAddress, isValidJson, remove0x } from "@metamask/utils";
25
25
  import { Mutex } from "async-mutex";
26
26
  import $Wallet from "ethereumjs-wallet";
@@ -104,6 +104,17 @@ const defaultKeyringBuilders = [
104
104
  keyringBuilderFactory(SimpleKeyring),
105
105
  keyringBuilderFactory(HdKeyring),
106
106
  ];
107
+ const hdKeyringV2Builder = Object.assign((keyring, metadata) => new HdKeyringV2({
108
+ legacyKeyring: keyring,
109
+ entropySource: metadata.id,
110
+ }), { type: KeyringTypes.hd });
111
+ const simpleKeyringV2Builder = Object.assign((keyring) => new SimpleKeyringV2({
112
+ legacyKeyring: keyring,
113
+ }), { type: KeyringTypes.simple });
114
+ const defaultKeyringV2Builders = [
115
+ simpleKeyringV2Builder,
116
+ hdKeyringV2Builder,
117
+ ];
107
118
  export const getDefaultKeyringState = () => {
108
119
  return {
109
120
  isUnlocked: false,
@@ -228,7 +239,7 @@ export class KeyringController extends BaseController {
228
239
  * @param options.state - Initial state to set on this controller.
229
240
  */
230
241
  constructor(options) {
231
- const { encryptor, keyringBuilders, messenger, state } = options;
242
+ const { encryptor, keyringBuilders, keyringV2Builders, messenger, state } = options;
232
243
  super({
233
244
  name,
234
245
  metadata: {
@@ -273,6 +284,7 @@ export class KeyringController extends BaseController {
273
284
  _KeyringController_controllerOperationMutex.set(this, new Mutex());
274
285
  _KeyringController_vaultOperationMutex.set(this, new Mutex());
275
286
  _KeyringController_keyringBuilders.set(this, void 0);
287
+ _KeyringController_keyringV2Builders.set(this, void 0);
276
288
  _KeyringController_encryptor.set(this, void 0);
277
289
  _KeyringController_keyrings.set(this, void 0);
278
290
  _KeyringController_unsupportedKeyrings.set(this, void 0);
@@ -280,6 +292,9 @@ export class KeyringController extends BaseController {
280
292
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
281
293
  ? keyringBuilders.concat(defaultKeyringBuilders)
282
294
  : defaultKeyringBuilders, "f");
295
+ __classPrivateFieldSet(this, _KeyringController_keyringV2Builders, keyringV2Builders
296
+ ? keyringV2Builders.concat(defaultKeyringV2Builders)
297
+ : defaultKeyringV2Builders, "f");
283
298
  __classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
284
299
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
285
300
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
@@ -524,9 +539,7 @@ export class KeyringController extends BaseController {
524
539
  */
525
540
  getKeyringsByType(type) {
526
541
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
527
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")
528
- .filter(({ keyring }) => keyring.type === type)
529
- .map(({ keyring }) => keyring);
542
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntriesByType).call(this, type).map(({ keyring }) => keyring);
530
543
  }
531
544
  /**
532
545
  * Persist all serialized keyrings in the vault.
@@ -615,7 +628,7 @@ export class KeyringController extends BaseController {
615
628
  if (keyringIndex === -1) {
616
629
  throw new KeyringControllerError(KeyringControllerErrorMessage.NoKeyring);
617
630
  }
618
- const { keyring } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
631
+ const { keyring, keyringV2 } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
619
632
  const isPrimaryKeyring = keyringIndex === 0;
620
633
  const shouldRemoveKeyring = (await keyring.getAccounts()).length === 1;
621
634
  // Primary keyring should never be removed, so we need to keep at least one account in it
@@ -636,7 +649,7 @@ export class KeyringController extends BaseController {
636
649
  await keyring.removeAccount(address);
637
650
  if (shouldRemoveKeyring) {
638
651
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").splice(keyringIndex, 1);
639
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
652
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring, keyringV2);
640
653
  }
641
654
  });
642
655
  this.messenger.publish(`${name}:accountRemoved`, address);
@@ -994,22 +1007,124 @@ export class KeyringController extends BaseController {
994
1007
  metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
995
1008
  }), keyring);
996
1009
  }
1010
+ /**
1011
+ * Select a keyring, wrap it in a `KeyringV2` adapter, and execute
1012
+ * the given operation with the wrapped keyring as a mutually
1013
+ * exclusive atomic operation.
1014
+ *
1015
+ * We re-wrap the keyring in a `KeyringV2` adapter on each invocation,
1016
+ * since V2 wrappers are ephemeral adapters created on-the-fly, and cheap to create.
1017
+ *
1018
+ * The method automatically persists changes at the end of the
1019
+ * function execution, or rolls back the changes if an error
1020
+ * is thrown.
1021
+ *
1022
+ * A `KeyringV2Builder` for the selected keyring's type must exist
1023
+ * (either as a default or registered via the `keyringV2Builders`
1024
+ * constructor option); otherwise an error is thrown.
1025
+ *
1026
+ * Selection is performed against the V1 keyrings in `#keyrings`, since
1027
+ * V2 wrappers are ephemeral adapters created on-the-fly.
1028
+ *
1029
+ * @param selector - Keyring selector object.
1030
+ * @param operation - Function to execute with the wrapped V2 keyring.
1031
+ * @returns Promise resolving to the result of the function execution.
1032
+ * @template CallbackResult - The type of the value resolved by the callback function.
1033
+ */
1034
+ async withKeyringV2(selector, operation) {
1035
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1036
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1037
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
1038
+ v2: true,
1039
+ selector,
1040
+ });
1041
+ if (!entry) {
1042
+ throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringNotFound);
1043
+ }
1044
+ if (!entry.keyringV2) {
1045
+ throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringV2NotSupported);
1046
+ }
1047
+ const { metadata } = entry;
1048
+ const keyring = entry.keyringV2;
1049
+ const result = await operation({
1050
+ keyring,
1051
+ metadata,
1052
+ });
1053
+ if (Object.is(result, keyring)) {
1054
+ throw new KeyringControllerError(KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
1055
+ }
1056
+ return result;
1057
+ });
1058
+ }
1059
+ /**
1060
+ * Select a keyring, wrap it in a `KeyringV2` adapter, and execute
1061
+ * the given read-only operation **without** acquiring the controller's
1062
+ * mutual exclusion lock.
1063
+ *
1064
+ * ## When to use this method
1065
+ *
1066
+ * This method is an escape hatch for read-only access to keyring data that
1067
+ * is immutable once the keyring is initialized. A typical safe use case is
1068
+ * reading immutable fields from a `KeyringV2` adapter: data that is set
1069
+ * during initialization and never mutated afterwards.
1070
+ *
1071
+ * ## Why it is "unsafe"
1072
+ *
1073
+ * The "unsafe" designation mirrors the semantics of `unsafe { }` blocks in
1074
+ * Rust: the method itself does not enforce thread-safety guarantees. By
1075
+ * calling this method the **caller** explicitly takes responsibility for
1076
+ * ensuring that:
1077
+ *
1078
+ * - The operation is **read-only** — no state is mutated.
1079
+ * - The data being read is **immutable** after the keyring is initialized,
1080
+ * so concurrent locked operations cannot alter it while this callback
1081
+ * runs.
1082
+ *
1083
+ * Do **not** use this method to:
1084
+ * - Mutate keyring state (add accounts, sign, etc.) — use `withKeyringV2`.
1085
+ * - Read mutable fields that could change during concurrent operations.
1086
+ *
1087
+ * @param selector - Keyring selector object.
1088
+ * @param operation - Read-only function to execute with the wrapped V2 keyring.
1089
+ * @returns Promise resolving to the result of the function execution.
1090
+ * @template SelectedKeyring - The type of the selected V2 keyring.
1091
+ * @template CallbackResult - The type of the value resolved by the callback function.
1092
+ */
1093
+ async withKeyringV2Unsafe(selector, operation) {
1094
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1095
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
1096
+ v2: true,
1097
+ selector,
1098
+ });
1099
+ if (!entry) {
1100
+ throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringNotFound);
1101
+ }
1102
+ if (!entry.keyringV2) {
1103
+ throw new KeyringControllerError(KeyringControllerErrorMessage.KeyringV2NotSupported);
1104
+ }
1105
+ const { metadata } = entry;
1106
+ const keyring = entry.keyringV2;
1107
+ const result = await operation({ keyring, metadata });
1108
+ if (Object.is(result, keyring)) {
1109
+ throw new KeyringControllerError(KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
1110
+ }
1111
+ return result;
1112
+ }
997
1113
  async getAccountKeyringType(account) {
998
1114
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
999
1115
  const keyring = (await this.getKeyringForAccount(account));
1000
1116
  return keyring.type;
1001
1117
  }
1002
- async accountSupports7702(account) {
1003
- const keyringType = await this.getAccountKeyringType(account);
1004
- return (keyringType === KeyringTypes.hd ||
1005
- keyringType === KeyringTypes.simple);
1006
- }
1007
1118
  }
1008
- _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_encryptionKey = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_getKeyringForAccount = async function _KeyringController_getKeyringForAccount(account) {
1119
+ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_keyringV2Builders = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_encryptionKey = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_getKeyringForAccount = async function _KeyringController_getKeyringForAccount(account) {
1120
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1121
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, account);
1122
+ return entry?.keyring;
1123
+ }, _KeyringController_getKeyringEntryForAccount = async function _KeyringController_getKeyringEntryForAccount(account) {
1009
1124
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1010
1125
  const keyringIndex = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_findKeyringIndexForAccount).call(this, account);
1011
1126
  if (keyringIndex > -1) {
1012
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex].keyring;
1127
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
1013
1128
  }
1014
1129
  return undefined;
1015
1130
  }, _KeyringController_findKeyringIndexForAccount = async function _KeyringController_findKeyringIndexForAccount(account) {
@@ -1017,6 +1132,9 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1017
1132
  const address = account.toLowerCase();
1018
1133
  const accountsPerKeyring = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(({ keyring }) => keyring.getAccounts()));
1019
1134
  return accountsPerKeyring.findIndex((accounts) => accounts.map((a) => a.toLowerCase()).includes(address));
1135
+ }, _KeyringController_getKeyringEntriesByType = function _KeyringController_getKeyringEntriesByType(type) {
1136
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1137
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter(({ keyring }) => keyring.type === type);
1020
1138
  }, _KeyringController_assertNoUnsafeDirectKeyringAccess = function _KeyringController_assertNoUnsafeDirectKeyringAccess(value, keyring) {
1021
1139
  if (Object.is(value, keyring)) {
1022
1140
  // Access to a keyring instance outside of controller safeguards
@@ -1043,37 +1161,66 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1043
1161
  this.messenger.registerActionHandler(`${name}:addNewAccount`, this.addNewAccount.bind(this));
1044
1162
  this.messenger.registerActionHandler(`${name}:withKeyring`, this.withKeyring.bind(this));
1045
1163
  this.messenger.registerActionHandler(`${name}:withKeyringUnsafe`, this.withKeyringUnsafe.bind(this));
1164
+ this.messenger.registerActionHandler(`${name}:withKeyringV2`, this.withKeyringV2.bind(this));
1165
+ this.messenger.registerActionHandler(`${name}:withKeyringV2Unsafe`, this.withKeyringV2Unsafe.bind(this));
1046
1166
  this.messenger.registerActionHandler(`${name}:addNewKeyring`, this.addNewKeyring.bind(this));
1047
1167
  this.messenger.registerActionHandler(`${name}:createNewVaultAndKeychain`, this.createNewVaultAndKeychain.bind(this));
1048
1168
  this.messenger.registerActionHandler(`${name}:createNewVaultAndRestore`, this.createNewVaultAndRestore.bind(this));
1049
1169
  this.messenger.registerActionHandler(`${name}:removeAccount`, this.removeAccount.bind(this));
1050
- this.messenger.registerActionHandler(`${name}:accountSupports7702`, this.accountSupports7702.bind(this));
1051
- }, _KeyringController_selectKeyring =
1170
+ }, _KeyringController_selectKeyringEntry =
1052
1171
  /**
1053
- * Select a keyring using a selector without acquiring the controller lock.
1172
+ * Select a keyring entry using a selector without acquiring the controller lock.
1054
1173
  *
1055
- * @param selector - Keyring selector object.
1056
- * @returns The selected keyring, or `undefined` if no match is found.
1174
+ * @param options - Selection options.
1175
+ * @param options.v2 - Tag to indicate whether the selector is for a V2 keyring.
1176
+ * @param options.selector - Keyring selector object.
1177
+ * @returns The selected keyring entry, or `undefined` if no match is found.
1057
1178
  * @template SelectedKeyring - The expected type of the selected keyring.
1179
+ * @template SelectedKeyringV2 - The expected type of the selected keyring (v2).
1058
1180
  */
1059
- async function _KeyringController_selectKeyring(selector) {
1060
- let keyring;
1181
+ async function _KeyringController_selectKeyringEntry({ v2, selector, }) {
1182
+ let entry;
1061
1183
  if ('address' in selector) {
1062
- keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringForAccount).call(this, selector.address));
1184
+ entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, selector.address);
1063
1185
  }
1064
1186
  else if ('type' in selector) {
1065
- keyring = this.getKeyringsByType(selector.type)[selector.index ?? 0];
1187
+ entry = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntriesByType).call(this, selector.type)[selector.index ?? 0];
1066
1188
  }
1067
1189
  else if ('id' in selector) {
1068
- keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
1190
+ entry = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryById).call(this, selector.id);
1069
1191
  }
1070
1192
  else if ('filter' in selector) {
1071
- keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring: filteredKeyring, metadata }) => selector.filter(filteredKeyring, metadata))?.keyring;
1193
+ entry = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring, keyringV2, metadata }) => {
1194
+ // If v2, then we'll use the v2 selector which expects a `KeyringV2` instance.
1195
+ if (v2) {
1196
+ // However, some keyrings do not have a v2 wrapper, so we just skip them.
1197
+ if (!keyringV2) {
1198
+ return false;
1199
+ }
1200
+ return selector.filter(keyringV2, metadata);
1201
+ }
1202
+ return selector.filter(keyring, metadata);
1203
+ });
1072
1204
  }
1073
- return keyring;
1205
+ return entry;
1206
+ }, _KeyringController_selectKeyring =
1207
+ /**
1208
+ * Select a keyring using a selector without acquiring the controller lock.
1209
+ *
1210
+ * @param selector - Keyring selector object.
1211
+ * @returns The selected keyring, or `undefined` if no match is found.
1212
+ * @template SelectedKeyring - The expected type of the selected keyring.
1213
+ */
1214
+ async function _KeyringController_selectKeyring(selector) {
1215
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
1216
+ v2: false,
1217
+ selector,
1218
+ });
1219
+ return entry?.keyring;
1074
1220
  }, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
1075
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId)
1076
- ?.keyring;
1221
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryById).call(this, keyringId)?.keyring;
1222
+ }, _KeyringController_getKeyringEntryById = function _KeyringController_getKeyringEntryById(keyringId) {
1223
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId);
1077
1224
  }, _KeyringController_getKeyringByIdOrDefault = function _KeyringController_getKeyringByIdOrDefault(keyringId) {
1078
1225
  if (!keyringId) {
1079
1226
  return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]?.keyring;
@@ -1087,6 +1234,8 @@ async function _KeyringController_selectKeyring(selector) {
1087
1234
  return keyringWithMetadata.metadata;
1088
1235
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1089
1236
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1237
+ }, _KeyringController_getKeyringV2BuilderForType = function _KeyringController_getKeyringV2BuilderForType(type) {
1238
+ return __classPrivateFieldGet(this, _KeyringController_keyringV2Builders, "f").find((builder) => builder.type === type);
1090
1239
  }, _KeyringController_createNewVaultWithKeyring =
1091
1240
  /**
1092
1241
  * Create new vault with an initial keyring
@@ -1410,8 +1559,8 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1410
1559
  * @throws If the keyring includes duplicated accounts.
1411
1560
  */
1412
1561
  async function _KeyringController_newKeyring(type, data) {
1413
- const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1414
- __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, metadata: getDefaultKeyringMetadata() });
1562
+ const { keyring, keyringV2, metadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1563
+ __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, keyringV2, metadata });
1415
1564
  return keyring;
1416
1565
  }, _KeyringController_createKeyring =
1417
1566
  /**
@@ -1428,11 +1577,13 @@ async function _KeyringController_newKeyring(type, data) {
1428
1577
  *
1429
1578
  * @param type - The type of keyring to add.
1430
1579
  * @param data - Keyring initialization options.
1580
+ * @param metadata - Keyring metadata if available.
1431
1581
  * @returns The new keyring.
1432
1582
  * @throws If the keyring includes duplicated accounts.
1433
1583
  */
1434
- async function _KeyringController_createKeyring(type, data) {
1584
+ async function _KeyringController_createKeyring(type, data, metadata) {
1435
1585
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1586
+ const keyringMetadata = metadata ?? getDefaultKeyringMetadata();
1436
1587
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1437
1588
  if (!keyringBuilder) {
1438
1589
  throw new KeyringControllerError(`${KeyringControllerErrorMessage.NoKeyringBuilder}. Keyring type: ${type}`);
@@ -1456,7 +1607,13 @@ async function _KeyringController_createKeyring(type, data) {
1456
1607
  await keyring.generateRandomMnemonic();
1457
1608
  await keyring.addAccounts(1);
1458
1609
  }
1459
- return keyring;
1610
+ // We now create the keyring V2 wrappers and store them in memory.
1611
+ const keyringBuilderV2 = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringV2BuilderForType).call(this, type);
1612
+ let keyringV2;
1613
+ if (keyringBuilderV2) {
1614
+ keyringV2 = keyringBuilderV2(keyring, keyringMetadata);
1615
+ }
1616
+ return { keyring, keyringV2, metadata: keyringMetadata };
1460
1617
  }, _KeyringController_clearKeyrings =
1461
1618
  /**
1462
1619
  * Remove all managed keyrings, destroying all their
@@ -1464,8 +1621,8 @@ async function _KeyringController_createKeyring(type, data) {
1464
1621
  */
1465
1622
  async function _KeyringController_clearKeyrings() {
1466
1623
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1467
- for (const { keyring } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
1468
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1624
+ for (const { keyring, keyringV2 } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
1625
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring, keyringV2);
1469
1626
  }
1470
1627
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
1471
1628
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
@@ -1483,21 +1640,22 @@ async function _KeyringController_restoreKeyring(serialized) {
1483
1640
  const { type, data, metadata: serializedMetadata } = serialized;
1484
1641
  let newMetadata = false;
1485
1642
  let metadata = serializedMetadata;
1486
- const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1487
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
1488
1643
  // If metadata is missing, assume the data is from an installation before
1489
1644
  // we had keyring metadata.
1490
1645
  if (!metadata) {
1491
1646
  newMetadata = true;
1492
1647
  metadata = getDefaultKeyringMetadata();
1493
1648
  }
1649
+ const { keyring, keyringV2 } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data, metadata);
1650
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
1494
1651
  // The keyring is added to the keyrings array only if it's successfully restored
1495
1652
  // and the metadata is successfully added to the controller
1496
1653
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({
1497
1654
  keyring,
1655
+ keyringV2,
1498
1656
  metadata,
1499
1657
  });
1500
- return { keyring, metadata, newMetadata };
1658
+ return { keyring, keyringV2, metadata, newMetadata };
1501
1659
  }
1502
1660
  catch (error) {
1503
1661
  console.error(error);
@@ -1513,9 +1671,13 @@ async function _KeyringController_restoreKeyring(serialized) {
1513
1671
  * clears the keyring bridge iframe from the DOM.
1514
1672
  *
1515
1673
  * @param keyring - The keyring to destroy.
1674
+ * @param keyringV2 - The keyring v2 to destroy (if any).
1516
1675
  */
1517
- async function _KeyringController_destroyKeyring(keyring) {
1676
+ async function _KeyringController_destroyKeyring(keyring, keyringV2) {
1518
1677
  await keyring.destroy?.();
1678
+ if (keyringV2) {
1679
+ await keyringV2.destroy?.();
1680
+ }
1519
1681
  }, _KeyringController_assertNoDuplicateAccounts =
1520
1682
  /**
1521
1683
  * Assert that there are no duplicate accounts in the keyrings.