@metamask-previews/keyring-controller 25.2.0-preview-97aa3f6 → 25.2.0-preview-d9d1316f5

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.
@@ -33,17 +33,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
33
33
  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");
34
34
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
35
35
  };
36
- var __importDefault = (this && this.__importDefault) || function (mod) {
37
- return (mod && mod.__esModule) ? mod : { "default": mod };
38
- };
39
- 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;
36
+ 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_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;
40
37
  Object.defineProperty(exports, "__esModule", { value: true });
41
38
  exports.KeyringController = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
42
39
  const util_1 = require("@ethereumjs/util");
43
40
  const base_controller_1 = require("@metamask/base-controller");
44
41
  const eth_hd_keyring_1 = require("@metamask/eth-hd-keyring");
45
42
  const eth_sig_util_1 = require("@metamask/eth-sig-util");
46
- const eth_simple_keyring_1 = __importDefault(require("@metamask/eth-simple-keyring"));
43
+ const eth_simple_keyring_1 = __importStar(require("@metamask/eth-simple-keyring"));
47
44
  const utils_1 = require("@metamask/utils");
48
45
  const async_mutex_1 = require("async-mutex");
49
46
  const ethereumjs_wallet_1 = __importStar(require("ethereumjs-wallet"));
@@ -71,6 +68,8 @@ const MESSENGER_EXPOSED_METHODS = [
71
68
  'addNewAccount',
72
69
  'withKeyring',
73
70
  'withKeyringUnsafe',
71
+ 'withKeyringV2',
72
+ 'withKeyringV2Unsafe',
74
73
  'addNewKeyring',
75
74
  'createNewVaultAndKeychain',
76
75
  'createNewVaultAndRestore',
@@ -149,6 +148,17 @@ const defaultKeyringBuilders = [
149
148
  keyringBuilderFactory(eth_simple_keyring_1.default),
150
149
  keyringBuilderFactory(eth_hd_keyring_1.HdKeyring),
151
150
  ];
151
+ const hdKeyringV2Builder = Object.assign((keyring, metadata) => new eth_hd_keyring_1.HdKeyringV2({
152
+ legacyKeyring: keyring,
153
+ entropySource: metadata.id,
154
+ }), { type: KeyringTypes.hd });
155
+ const simpleKeyringV2Builder = Object.assign((keyring) => new eth_simple_keyring_1.SimpleKeyringV2({
156
+ legacyKeyring: keyring,
157
+ }), { type: KeyringTypes.simple });
158
+ const defaultKeyringV2Builders = [
159
+ simpleKeyringV2Builder,
160
+ hdKeyringV2Builder,
161
+ ];
152
162
  const getDefaultKeyringState = () => {
153
163
  return {
154
164
  isUnlocked: false,
@@ -274,7 +284,7 @@ class KeyringController extends base_controller_1.BaseController {
274
284
  * @param options.state - Initial state to set on this controller.
275
285
  */
276
286
  constructor(options) {
277
- const { encryptor, keyringBuilders, messenger, state } = options;
287
+ const { encryptor, keyringBuilders, keyringV2Builders, messenger, state } = options;
278
288
  super({
279
289
  name,
280
290
  metadata: {
@@ -319,6 +329,7 @@ class KeyringController extends base_controller_1.BaseController {
319
329
  _KeyringController_controllerOperationMutex.set(this, new async_mutex_1.Mutex());
320
330
  _KeyringController_vaultOperationMutex.set(this, new async_mutex_1.Mutex());
321
331
  _KeyringController_keyringBuilders.set(this, void 0);
332
+ _KeyringController_keyringV2Builders.set(this, void 0);
322
333
  _KeyringController_encryptor.set(this, void 0);
323
334
  _KeyringController_keyrings.set(this, void 0);
324
335
  _KeyringController_unsupportedKeyrings.set(this, void 0);
@@ -326,6 +337,9 @@ class KeyringController extends base_controller_1.BaseController {
326
337
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
327
338
  ? keyringBuilders.concat(defaultKeyringBuilders)
328
339
  : defaultKeyringBuilders, "f");
340
+ __classPrivateFieldSet(this, _KeyringController_keyringV2Builders, keyringV2Builders
341
+ ? keyringV2Builders.concat(defaultKeyringV2Builders)
342
+ : defaultKeyringV2Builders, "f");
329
343
  __classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
330
344
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
331
345
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
@@ -570,9 +584,7 @@ class KeyringController extends base_controller_1.BaseController {
570
584
  */
571
585
  getKeyringsByType(type) {
572
586
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
573
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")
574
- .filter(({ keyring }) => keyring.type === type)
575
- .map(({ keyring }) => keyring);
587
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntriesByType).call(this, type).map(({ keyring }) => keyring);
576
588
  }
577
589
  /**
578
590
  * Persist all serialized keyrings in the vault.
@@ -661,7 +673,7 @@ class KeyringController extends base_controller_1.BaseController {
661
673
  if (keyringIndex === -1) {
662
674
  throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.NoKeyring);
663
675
  }
664
- const { keyring } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
676
+ const { keyring, keyringV2 } = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
665
677
  const isPrimaryKeyring = keyringIndex === 0;
666
678
  const shouldRemoveKeyring = (await keyring.getAccounts()).length === 1;
667
679
  // Primary keyring should never be removed, so we need to keep at least one account in it
@@ -682,7 +694,7 @@ class KeyringController extends base_controller_1.BaseController {
682
694
  await keyring.removeAccount(address);
683
695
  if (shouldRemoveKeyring) {
684
696
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").splice(keyringIndex, 1);
685
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
697
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring, keyringV2);
686
698
  }
687
699
  });
688
700
  this.messenger.publish(`${name}:accountRemoved`, address);
@@ -980,17 +992,20 @@ class KeyringController extends base_controller_1.BaseController {
980
992
  }) {
981
993
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
982
994
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
983
- let keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyring).call(this, selector);
984
- if (!keyring && 'type' in selector && options.createIfMissing) {
985
- keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
995
+ let entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
996
+ v2: false,
997
+ selector,
998
+ });
999
+ if (!entry && 'type' in selector && options.createIfMissing) {
1000
+ const newKeyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
1001
+ entry = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring }) => keyring === newKeyring);
986
1002
  }
987
- if (!keyring) {
1003
+ if (!entry) {
988
1004
  throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
989
1005
  }
990
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
991
- keyring,
992
- metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
993
- }), keyring);
1006
+ const { metadata } = entry;
1007
+ const keyring = entry.keyring;
1008
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({ keyring, metadata }), keyring);
994
1009
  });
995
1010
  }
996
1011
  /**
@@ -1029,16 +1044,109 @@ class KeyringController extends base_controller_1.BaseController {
1029
1044
  */
1030
1045
  async withKeyringUnsafe(selector, operation) {
1031
1046
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1032
- const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyring).call(this, selector);
1033
- if (!keyring) {
1047
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, { v2: false, selector });
1048
+ if (!entry) {
1034
1049
  throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
1035
1050
  }
1051
+ const { metadata } = entry;
1052
+ const keyring = entry.keyring;
1036
1053
  // Even if this method is "unsafe", we still want to prevent returning
1037
1054
  // the keyring directly.
1038
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
1039
- keyring,
1040
- metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
1041
- }), keyring);
1055
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({ keyring, metadata }), keyring);
1056
+ }
1057
+ /**
1058
+ * Select a keyring using its `KeyringV2` adapter, and execute
1059
+ * the given operation with the wrapped keyring as a mutually
1060
+ * exclusive atomic operation.
1061
+ *
1062
+ * The cached `KeyringV2` adapter is retrieved from the keyring
1063
+ * entry.
1064
+ *
1065
+ * A `KeyringV2Builder` for the selected keyring's type must exist
1066
+ * (either as a default or registered via the `keyringV2Builders`
1067
+ * constructor option); otherwise an error is thrown.
1068
+ *
1069
+ * The method automatically persists changes at the end of the
1070
+ * function execution, or rolls back the changes if an error
1071
+ * is thrown.
1072
+ *
1073
+ * @param selector - Keyring selector object.
1074
+ * @param operation - Function to execute with the wrapped V2 keyring.
1075
+ * @returns Promise resolving to the result of the function execution.
1076
+ * @template CallbackResult - The type of the value resolved by the callback function.
1077
+ */
1078
+ async withKeyringV2(selector, operation) {
1079
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1080
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1081
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
1082
+ v2: true,
1083
+ selector,
1084
+ });
1085
+ if (!entry) {
1086
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
1087
+ }
1088
+ if (!entry.keyringV2) {
1089
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringV2NotSupported);
1090
+ }
1091
+ const { metadata } = entry;
1092
+ const keyring = entry.keyringV2;
1093
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({
1094
+ keyring,
1095
+ metadata,
1096
+ }), keyring);
1097
+ });
1098
+ }
1099
+ /**
1100
+ * Select a keyring, wrap it in a `KeyringV2` adapter, and execute
1101
+ * the given read-only operation **without** acquiring the controller's
1102
+ * mutual exclusion lock.
1103
+ *
1104
+ * ## When to use this method
1105
+ *
1106
+ * This method is an escape hatch for read-only access to keyring data that
1107
+ * is immutable once the keyring is initialized. A typical safe use case is
1108
+ * reading immutable fields from a `KeyringV2` adapter: data that is set
1109
+ * during initialization and never mutated afterwards.
1110
+ *
1111
+ * ## Why it is "unsafe"
1112
+ *
1113
+ * The "unsafe" designation mirrors the semantics of `unsafe { }` blocks in
1114
+ * Rust: the method itself does not enforce thread-safety guarantees. By
1115
+ * calling this method the **caller** explicitly takes responsibility for
1116
+ * ensuring that:
1117
+ *
1118
+ * - The operation is **read-only** — no state is mutated.
1119
+ * - The data being read is **immutable** after the keyring is initialized,
1120
+ * so concurrent locked operations cannot alter it while this callback
1121
+ * runs.
1122
+ *
1123
+ * Do **not** use this method to:
1124
+ * - Mutate keyring state (add accounts, sign, etc.) — use `withKeyringV2`.
1125
+ * - Read mutable fields that could change during concurrent operations.
1126
+ *
1127
+ * @param selector - Keyring selector object.
1128
+ * @param operation - Read-only function to execute with the wrapped V2 keyring.
1129
+ * @returns Promise resolving to the result of the function execution.
1130
+ * @template SelectedKeyring - The type of the selected V2 keyring.
1131
+ * @template CallbackResult - The type of the value resolved by the callback function.
1132
+ */
1133
+ async withKeyringV2Unsafe(selector, operation) {
1134
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1135
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
1136
+ v2: true,
1137
+ selector,
1138
+ });
1139
+ if (!entry) {
1140
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
1141
+ }
1142
+ if (!entry.keyringV2) {
1143
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringV2NotSupported);
1144
+ }
1145
+ const { metadata } = entry;
1146
+ const keyring = entry.keyringV2;
1147
+ // Even if this method is "unsafe", we still want to prevent returning
1148
+ // the keyring directly.
1149
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoUnsafeDirectKeyringAccess).call(this, await operation({ keyring, metadata }), keyring);
1042
1150
  }
1043
1151
  async getAccountKeyringType(account) {
1044
1152
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
@@ -1047,11 +1155,15 @@ class KeyringController extends base_controller_1.BaseController {
1047
1155
  }
1048
1156
  }
1049
1157
  exports.KeyringController = KeyringController;
1050
- _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) {
1158
+ _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) {
1159
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1160
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, account);
1161
+ return entry?.keyring;
1162
+ }, _KeyringController_getKeyringEntryForAccount = async function _KeyringController_getKeyringEntryForAccount(account) {
1051
1163
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1052
1164
  const keyringIndex = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_findKeyringIndexForAccount).call(this, account);
1053
1165
  if (keyringIndex > -1) {
1054
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex].keyring;
1166
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
1055
1167
  }
1056
1168
  return undefined;
1057
1169
  }, _KeyringController_findKeyringIndexForAccount = async function _KeyringController_findKeyringIndexForAccount(account) {
@@ -1059,6 +1171,9 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1059
1171
  const address = account.toLowerCase();
1060
1172
  const accountsPerKeyring = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(({ keyring }) => keyring.getAccounts()));
1061
1173
  return accountsPerKeyring.findIndex((accounts) => accounts.map((a) => a.toLowerCase()).includes(address));
1174
+ }, _KeyringController_getKeyringEntriesByType = function _KeyringController_getKeyringEntriesByType(type) {
1175
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1176
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter(({ keyring }) => keyring.type === type);
1062
1177
  }, _KeyringController_assertNoUnsafeDirectKeyringAccess = function _KeyringController_assertNoUnsafeDirectKeyringAccess(value, keyring) {
1063
1178
  if (Object.is(value, keyring)) {
1064
1179
  // Access to a keyring instance outside of controller safeguards
@@ -1070,32 +1185,46 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1070
1185
  return value;
1071
1186
  }, _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1072
1187
  this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
1073
- }, _KeyringController_selectKeyring =
1188
+ }, _KeyringController_selectKeyringEntry =
1074
1189
  /**
1075
- * Select a keyring using a selector without acquiring the controller lock.
1190
+ * Select a keyring entry using a selector without acquiring the controller lock.
1076
1191
  *
1077
- * @param selector - Keyring selector object.
1078
- * @returns The selected keyring, or `undefined` if no match is found.
1192
+ * @param options - Selection options.
1193
+ * @param options.v2 - Tag to indicate whether the selector is for a V2 keyring.
1194
+ * @param options.selector - Keyring selector object.
1195
+ * @returns The selected keyring entry, or `undefined` if no match is found.
1079
1196
  * @template SelectedKeyring - The expected type of the selected keyring.
1197
+ * @template SelectedKeyringV2 - The expected type of the selected keyring (v2).
1080
1198
  */
1081
- async function _KeyringController_selectKeyring(selector) {
1082
- let keyring;
1199
+ async function _KeyringController_selectKeyringEntry({ v2, selector, }) {
1200
+ let entry;
1083
1201
  if ('address' in selector) {
1084
- keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringForAccount).call(this, selector.address));
1202
+ entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, selector.address);
1085
1203
  }
1086
1204
  else if ('type' in selector) {
1087
- keyring = this.getKeyringsByType(selector.type)[selector.index ?? 0];
1205
+ entry = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntriesByType).call(this, selector.type)[selector.index ?? 0];
1088
1206
  }
1089
1207
  else if ('id' in selector) {
1090
- keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
1208
+ entry = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryById).call(this, selector.id);
1091
1209
  }
1092
1210
  else if ('filter' in selector) {
1093
- keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring: filteredKeyring, metadata }) => selector.filter(filteredKeyring, metadata))?.keyring;
1211
+ entry = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring, keyringV2, metadata }) => {
1212
+ // If v2, then we'll use the v2 selector which expects a `KeyringV2` instance.
1213
+ if (v2) {
1214
+ // However, some keyrings do not have a v2 wrapper, so we just skip them.
1215
+ if (!keyringV2) {
1216
+ return false;
1217
+ }
1218
+ return selector.filter(keyringV2, metadata);
1219
+ }
1220
+ return selector.filter(keyring, metadata);
1221
+ });
1094
1222
  }
1095
- return keyring;
1223
+ return entry;
1096
1224
  }, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
1097
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId)
1098
- ?.keyring;
1225
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryById).call(this, keyringId)?.keyring;
1226
+ }, _KeyringController_getKeyringEntryById = function _KeyringController_getKeyringEntryById(keyringId) {
1227
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId);
1099
1228
  }, _KeyringController_getKeyringByIdOrDefault = function _KeyringController_getKeyringByIdOrDefault(keyringId) {
1100
1229
  if (!keyringId) {
1101
1230
  return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]?.keyring;
@@ -1109,6 +1238,8 @@ async function _KeyringController_selectKeyring(selector) {
1109
1238
  return keyringWithMetadata.metadata;
1110
1239
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1111
1240
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1241
+ }, _KeyringController_getKeyringV2BuilderForType = function _KeyringController_getKeyringV2BuilderForType(type) {
1242
+ return __classPrivateFieldGet(this, _KeyringController_keyringV2Builders, "f").find((builder) => builder.type === type);
1112
1243
  }, _KeyringController_createNewVaultWithKeyring =
1113
1244
  /**
1114
1245
  * Create new vault with an initial keyring
@@ -1432,8 +1563,8 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1432
1563
  * @throws If the keyring includes duplicated accounts.
1433
1564
  */
1434
1565
  async function _KeyringController_newKeyring(type, data) {
1435
- const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1436
- __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, metadata: getDefaultKeyringMetadata() });
1566
+ const { keyring, keyringV2, metadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1567
+ __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, keyringV2, metadata });
1437
1568
  return keyring;
1438
1569
  }, _KeyringController_createKeyring =
1439
1570
  /**
@@ -1450,11 +1581,13 @@ async function _KeyringController_newKeyring(type, data) {
1450
1581
  *
1451
1582
  * @param type - The type of keyring to add.
1452
1583
  * @param data - Keyring initialization options.
1584
+ * @param metadata - Keyring metadata if available.
1453
1585
  * @returns The new keyring.
1454
1586
  * @throws If the keyring includes duplicated accounts.
1455
1587
  */
1456
- async function _KeyringController_createKeyring(type, data) {
1588
+ async function _KeyringController_createKeyring(type, data, metadata) {
1457
1589
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1590
+ const keyringMetadata = metadata ?? getDefaultKeyringMetadata();
1458
1591
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1459
1592
  if (!keyringBuilder) {
1460
1593
  throw new errors_1.KeyringControllerError(`${constants_1.KeyringControllerErrorMessage.NoKeyringBuilder}. Keyring type: ${type}`);
@@ -1478,7 +1611,13 @@ async function _KeyringController_createKeyring(type, data) {
1478
1611
  await keyring.generateRandomMnemonic();
1479
1612
  await keyring.addAccounts(1);
1480
1613
  }
1481
- return keyring;
1614
+ // We now create the keyring V2 wrappers and store them in memory.
1615
+ const keyringBuilderV2 = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringV2BuilderForType).call(this, type);
1616
+ let keyringV2;
1617
+ if (keyringBuilderV2) {
1618
+ keyringV2 = keyringBuilderV2(keyring, keyringMetadata);
1619
+ }
1620
+ return { keyring, keyringV2, metadata: keyringMetadata };
1482
1621
  }, _KeyringController_clearKeyrings =
1483
1622
  /**
1484
1623
  * Remove all managed keyrings, destroying all their
@@ -1486,8 +1625,8 @@ async function _KeyringController_createKeyring(type, data) {
1486
1625
  */
1487
1626
  async function _KeyringController_clearKeyrings() {
1488
1627
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1489
- for (const { keyring } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
1490
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1628
+ for (const { keyring, keyringV2 } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
1629
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring, keyringV2);
1491
1630
  }
1492
1631
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
1493
1632
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
@@ -1503,11 +1642,8 @@ async function _KeyringController_restoreKeyring(serialized) {
1503
1642
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1504
1643
  try {
1505
1644
  const { type, data, metadata: serializedMetadata } = serialized;
1506
- const oldState = JSON.stringify(data);
1507
- const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1508
- const newState = JSON.stringify(await keyring.serialize());
1509
- let hasChanged = oldState !== newState;
1510
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
1645
+ // Track if we need to trigger a vault update.
1646
+ let hasChanged = false;
1511
1647
  // If metadata is missing, assume the data is from an installation before we had
1512
1648
  // keyring metadata.
1513
1649
  let metadata = serializedMetadata;
@@ -1515,13 +1651,19 @@ async function _KeyringController_restoreKeyring(serialized) {
1515
1651
  hasChanged = true;
1516
1652
  metadata = getDefaultKeyringMetadata();
1517
1653
  }
1654
+ const oldState = JSON.stringify(data);
1655
+ const { keyring, keyringV2 } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data, metadata);
1656
+ const newState = JSON.stringify(await keyring.serialize());
1657
+ hasChanged || (hasChanged = oldState !== newState);
1658
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
1518
1659
  // The keyring is added to the keyrings array only if it's successfully restored
1519
1660
  // and the metadata is successfully added to the controller
1520
1661
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({
1521
1662
  keyring,
1663
+ keyringV2,
1522
1664
  metadata,
1523
1665
  });
1524
- return { keyring, metadata, hasChanged };
1666
+ return { keyring, keyringV2, metadata, hasChanged };
1525
1667
  }
1526
1668
  catch (error) {
1527
1669
  console.error(error);
@@ -1537,9 +1679,13 @@ async function _KeyringController_restoreKeyring(serialized) {
1537
1679
  * clears the keyring bridge iframe from the DOM.
1538
1680
  *
1539
1681
  * @param keyring - The keyring to destroy.
1682
+ * @param keyringV2 - The keyring v2 to destroy (if any).
1540
1683
  */
1541
- async function _KeyringController_destroyKeyring(keyring) {
1684
+ async function _KeyringController_destroyKeyring(keyring, keyringV2) {
1542
1685
  await keyring.destroy?.();
1686
+ if (keyringV2) {
1687
+ await keyringV2.destroy?.();
1688
+ }
1543
1689
  }, _KeyringController_assertNoDuplicateAccounts =
1544
1690
  /**
1545
1691
  * Assert that there are no duplicate accounts in the keyrings.