@metamask-previews/keyring-controller 25.2.0-preview-dff83af4c → 25.2.0-preview-6b4f746

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_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;
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);
@@ -895,7 +907,7 @@ class KeyringController extends base_controller_1.BaseController {
895
907
  * @returns Promise resolving when the operation completes.
896
908
  */
897
909
  async submitEncryptionKey(encryptionKey, encryptionSalt) {
898
- const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
910
+ const { hasChanged } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
899
911
  const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, {
900
912
  encryptionKey,
901
913
  encryptionSalt,
@@ -907,7 +919,7 @@ class KeyringController extends base_controller_1.BaseController {
907
919
  // if new metadata has been generated during login, we
908
920
  // can attempt to upgrade the vault.
909
921
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
910
- if (newMetadata) {
922
+ if (hasChanged) {
911
923
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
912
924
  }
913
925
  });
@@ -938,17 +950,17 @@ class KeyringController extends base_controller_1.BaseController {
938
950
  * @returns Promise resolving when the operation completes.
939
951
  */
940
952
  async submitPassword(password) {
941
- const { newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
953
+ const { hasChanged } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
942
954
  const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unlockKeyrings).call(this, { password });
943
955
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
944
956
  return result;
945
957
  });
946
958
  try {
947
959
  // If there are stronger encryption params available, or
948
- // if new metadata has been generated during login, we
960
+ // if the keyring state has changed during deserialization, we
949
961
  // can attempt to upgrade the vault.
950
962
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
951
- if (newMetadata || __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_isNewEncryptionAvailable).call(this)) {
963
+ if (hasChanged || __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_isNewEncryptionAvailable).call(this)) {
952
964
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_deriveAndSetEncryptionKey).call(this, password, {
953
965
  // If the vault is being upgraded, we want to ignore the metadata
954
966
  // that is already in the vault, so we can effectively
@@ -1040,6 +1052,109 @@ class KeyringController extends base_controller_1.BaseController {
1040
1052
  metadata: __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, keyring),
1041
1053
  }), keyring);
1042
1054
  }
1055
+ /**
1056
+ * Select a keyring, wrap it in a `KeyringV2` adapter, and execute
1057
+ * the given operation with the wrapped keyring as a mutually
1058
+ * exclusive atomic operation.
1059
+ *
1060
+ * We re-wrap the keyring in a `KeyringV2` adapter on each invocation,
1061
+ * since V2 wrappers are ephemeral adapters created on-the-fly, and cheap to create.
1062
+ *
1063
+ * The method automatically persists changes at the end of the
1064
+ * function execution, or rolls back the changes if an error
1065
+ * is thrown.
1066
+ *
1067
+ * A `KeyringV2Builder` for the selected keyring's type must exist
1068
+ * (either as a default or registered via the `keyringV2Builders`
1069
+ * constructor option); otherwise an error is thrown.
1070
+ *
1071
+ * Selection is performed against the V1 keyrings in `#keyrings`, since
1072
+ * V2 wrappers are ephemeral adapters created on-the-fly.
1073
+ *
1074
+ * @param selector - Keyring selector object.
1075
+ * @param operation - Function to execute with the wrapped V2 keyring.
1076
+ * @returns Promise resolving to the result of the function execution.
1077
+ * @template CallbackResult - The type of the value resolved by the callback function.
1078
+ */
1079
+ async withKeyringV2(selector, operation) {
1080
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1081
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1082
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
1083
+ v2: true,
1084
+ selector,
1085
+ });
1086
+ if (!entry) {
1087
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
1088
+ }
1089
+ if (!entry.keyringV2) {
1090
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringV2NotSupported);
1091
+ }
1092
+ const { metadata } = entry;
1093
+ const keyring = entry.keyringV2;
1094
+ const result = await operation({
1095
+ keyring,
1096
+ metadata,
1097
+ });
1098
+ if (Object.is(result, keyring)) {
1099
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
1100
+ }
1101
+ return result;
1102
+ });
1103
+ }
1104
+ /**
1105
+ * Select a keyring, wrap it in a `KeyringV2` adapter, and execute
1106
+ * the given read-only operation **without** acquiring the controller's
1107
+ * mutual exclusion lock.
1108
+ *
1109
+ * ## When to use this method
1110
+ *
1111
+ * This method is an escape hatch for read-only access to keyring data that
1112
+ * is immutable once the keyring is initialized. A typical safe use case is
1113
+ * reading immutable fields from a `KeyringV2` adapter: data that is set
1114
+ * during initialization and never mutated afterwards.
1115
+ *
1116
+ * ## Why it is "unsafe"
1117
+ *
1118
+ * The "unsafe" designation mirrors the semantics of `unsafe { }` blocks in
1119
+ * Rust: the method itself does not enforce thread-safety guarantees. By
1120
+ * calling this method the **caller** explicitly takes responsibility for
1121
+ * ensuring that:
1122
+ *
1123
+ * - The operation is **read-only** — no state is mutated.
1124
+ * - The data being read is **immutable** after the keyring is initialized,
1125
+ * so concurrent locked operations cannot alter it while this callback
1126
+ * runs.
1127
+ *
1128
+ * Do **not** use this method to:
1129
+ * - Mutate keyring state (add accounts, sign, etc.) — use `withKeyringV2`.
1130
+ * - Read mutable fields that could change during concurrent operations.
1131
+ *
1132
+ * @param selector - Keyring selector object.
1133
+ * @param operation - Read-only function to execute with the wrapped V2 keyring.
1134
+ * @returns Promise resolving to the result of the function execution.
1135
+ * @template SelectedKeyring - The type of the selected V2 keyring.
1136
+ * @template CallbackResult - The type of the value resolved by the callback function.
1137
+ */
1138
+ async withKeyringV2Unsafe(selector, operation) {
1139
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1140
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
1141
+ v2: true,
1142
+ selector,
1143
+ });
1144
+ if (!entry) {
1145
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
1146
+ }
1147
+ if (!entry.keyringV2) {
1148
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringV2NotSupported);
1149
+ }
1150
+ const { metadata } = entry;
1151
+ const keyring = entry.keyringV2;
1152
+ const result = await operation({ keyring, metadata });
1153
+ if (Object.is(result, keyring)) {
1154
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
1155
+ }
1156
+ return result;
1157
+ }
1043
1158
  async getAccountKeyringType(account) {
1044
1159
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1045
1160
  const keyring = (await this.getKeyringForAccount(account));
@@ -1047,11 +1162,15 @@ class KeyringController extends base_controller_1.BaseController {
1047
1162
  }
1048
1163
  }
1049
1164
  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) {
1165
+ _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) {
1166
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1167
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, account);
1168
+ return entry?.keyring;
1169
+ }, _KeyringController_getKeyringEntryForAccount = async function _KeyringController_getKeyringEntryForAccount(account) {
1051
1170
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1052
1171
  const keyringIndex = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_findKeyringIndexForAccount).call(this, account);
1053
1172
  if (keyringIndex > -1) {
1054
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex].keyring;
1173
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[keyringIndex];
1055
1174
  }
1056
1175
  return undefined;
1057
1176
  }, _KeyringController_findKeyringIndexForAccount = async function _KeyringController_findKeyringIndexForAccount(account) {
@@ -1059,6 +1178,9 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1059
1178
  const address = account.toLowerCase();
1060
1179
  const accountsPerKeyring = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(({ keyring }) => keyring.getAccounts()));
1061
1180
  return accountsPerKeyring.findIndex((accounts) => accounts.map((a) => a.toLowerCase()).includes(address));
1181
+ }, _KeyringController_getKeyringEntriesByType = function _KeyringController_getKeyringEntriesByType(type) {
1182
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1183
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter(({ keyring }) => keyring.type === type);
1062
1184
  }, _KeyringController_assertNoUnsafeDirectKeyringAccess = function _KeyringController_assertNoUnsafeDirectKeyringAccess(value, keyring) {
1063
1185
  if (Object.is(value, keyring)) {
1064
1186
  // Access to a keyring instance outside of controller safeguards
@@ -1070,32 +1192,60 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1070
1192
  return value;
1071
1193
  }, _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1072
1194
  this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
1073
- }, _KeyringController_selectKeyring =
1195
+ }, _KeyringController_selectKeyringEntry =
1074
1196
  /**
1075
- * Select a keyring using a selector without acquiring the controller lock.
1197
+ * Select a keyring entry using a selector without acquiring the controller lock.
1076
1198
  *
1077
- * @param selector - Keyring selector object.
1078
- * @returns The selected keyring, or `undefined` if no match is found.
1199
+ * @param options - Selection options.
1200
+ * @param options.v2 - Tag to indicate whether the selector is for a V2 keyring.
1201
+ * @param options.selector - Keyring selector object.
1202
+ * @returns The selected keyring entry, or `undefined` if no match is found.
1079
1203
  * @template SelectedKeyring - The expected type of the selected keyring.
1204
+ * @template SelectedKeyringV2 - The expected type of the selected keyring (v2).
1080
1205
  */
1081
- async function _KeyringController_selectKeyring(selector) {
1082
- let keyring;
1206
+ async function _KeyringController_selectKeyringEntry({ v2, selector, }) {
1207
+ let entry;
1083
1208
  if ('address' in selector) {
1084
- keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringForAccount).call(this, selector.address));
1209
+ entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryForAccount).call(this, selector.address);
1085
1210
  }
1086
1211
  else if ('type' in selector) {
1087
- keyring = this.getKeyringsByType(selector.type)[selector.index ?? 0];
1212
+ entry = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntriesByType).call(this, selector.type)[selector.index ?? 0];
1088
1213
  }
1089
1214
  else if ('id' in selector) {
1090
- keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
1215
+ entry = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryById).call(this, selector.id);
1091
1216
  }
1092
1217
  else if ('filter' in selector) {
1093
- keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring: filteredKeyring, metadata }) => selector.filter(filteredKeyring, metadata))?.keyring;
1218
+ entry = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ keyring, keyringV2, metadata }) => {
1219
+ // If v2, then we'll use the v2 selector which expects a `KeyringV2` instance.
1220
+ if (v2) {
1221
+ // However, some keyrings do not have a v2 wrapper, so we just skip them.
1222
+ if (!keyringV2) {
1223
+ return false;
1224
+ }
1225
+ return selector.filter(keyringV2, metadata);
1226
+ }
1227
+ return selector.filter(keyring, metadata);
1228
+ });
1094
1229
  }
1095
- return keyring;
1230
+ return entry;
1231
+ }, _KeyringController_selectKeyring =
1232
+ /**
1233
+ * Select a keyring using a selector without acquiring the controller lock.
1234
+ *
1235
+ * @param selector - Keyring selector object.
1236
+ * @returns The selected keyring, or `undefined` if no match is found.
1237
+ * @template SelectedKeyring - The expected type of the selected keyring.
1238
+ */
1239
+ async function _KeyringController_selectKeyring(selector) {
1240
+ const entry = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_selectKeyringEntry).call(this, {
1241
+ v2: false,
1242
+ selector,
1243
+ });
1244
+ return entry?.keyring;
1096
1245
  }, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
1097
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId)
1098
- ?.keyring;
1246
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringEntryById).call(this, keyringId)?.keyring;
1247
+ }, _KeyringController_getKeyringEntryById = function _KeyringController_getKeyringEntryById(keyringId) {
1248
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find(({ metadata }) => metadata.id === keyringId);
1099
1249
  }, _KeyringController_getKeyringByIdOrDefault = function _KeyringController_getKeyringByIdOrDefault(keyringId) {
1100
1250
  if (!keyringId) {
1101
1251
  return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]?.keyring;
@@ -1109,6 +1259,8 @@ async function _KeyringController_selectKeyring(selector) {
1109
1259
  return keyringWithMetadata.metadata;
1110
1260
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1111
1261
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1262
+ }, _KeyringController_getKeyringV2BuilderForType = function _KeyringController_getKeyringV2BuilderForType(type) {
1263
+ return __classPrivateFieldGet(this, _KeyringController_keyringV2Builders, "f").find((builder) => builder.type === type);
1112
1264
  }, _KeyringController_createNewVaultWithKeyring =
1113
1265
  /**
1114
1266
  * Create new vault with an initial keyring
@@ -1296,18 +1448,18 @@ async function _KeyringController_getSessionState() {
1296
1448
  async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings) {
1297
1449
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
1298
1450
  const keyrings = [];
1299
- let newMetadata = false;
1451
+ let hasChanged = false;
1300
1452
  for (const serializedKeyring of serializedKeyrings) {
1301
1453
  const result = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
1302
1454
  if (result) {
1303
1455
  const { keyring, metadata } = result;
1304
1456
  keyrings.push({ keyring, metadata });
1305
- if (result.newMetadata) {
1306
- newMetadata = true;
1457
+ if (result.hasChanged) {
1458
+ hasChanged = true;
1307
1459
  }
1308
1460
  }
1309
1461
  }
1310
- return { keyrings, newMetadata };
1462
+ return { keyrings, hasChanged };
1311
1463
  }, _KeyringController_unlockKeyrings =
1312
1464
  /**
1313
1465
  * Unlock Keyrings, decrypting the vault and deserializing all
@@ -1337,14 +1489,14 @@ async function _KeyringController_unlockKeyrings(credentials) {
1337
1489
  if (!isSerializedKeyringsArray(vault)) {
1338
1490
  throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.VaultDataError);
1339
1491
  }
1340
- const { keyrings, newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, vault);
1492
+ const { keyrings, hasChanged } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, vault);
1341
1493
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
1342
1494
  this.update((state) => {
1343
1495
  state.keyrings = updatedKeyrings;
1344
1496
  state.encryptionKey = encryptionKey;
1345
1497
  state.encryptionSalt = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.salt;
1346
1498
  });
1347
- return { keyrings, newMetadata };
1499
+ return { keyrings, hasChanged };
1348
1500
  });
1349
1501
  }, _KeyringController_updateVault = function _KeyringController_updateVault() {
1350
1502
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
@@ -1432,8 +1584,8 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1432
1584
  * @throws If the keyring includes duplicated accounts.
1433
1585
  */
1434
1586
  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() });
1587
+ const { keyring, keyringV2, metadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1588
+ __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({ keyring, keyringV2, metadata });
1437
1589
  return keyring;
1438
1590
  }, _KeyringController_createKeyring =
1439
1591
  /**
@@ -1450,11 +1602,13 @@ async function _KeyringController_newKeyring(type, data) {
1450
1602
  *
1451
1603
  * @param type - The type of keyring to add.
1452
1604
  * @param data - Keyring initialization options.
1605
+ * @param metadata - Keyring metadata if available.
1453
1606
  * @returns The new keyring.
1454
1607
  * @throws If the keyring includes duplicated accounts.
1455
1608
  */
1456
- async function _KeyringController_createKeyring(type, data) {
1609
+ async function _KeyringController_createKeyring(type, data, metadata) {
1457
1610
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1611
+ const keyringMetadata = metadata ?? getDefaultKeyringMetadata();
1458
1612
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1459
1613
  if (!keyringBuilder) {
1460
1614
  throw new errors_1.KeyringControllerError(`${constants_1.KeyringControllerErrorMessage.NoKeyringBuilder}. Keyring type: ${type}`);
@@ -1478,7 +1632,13 @@ async function _KeyringController_createKeyring(type, data) {
1478
1632
  await keyring.generateRandomMnemonic();
1479
1633
  await keyring.addAccounts(1);
1480
1634
  }
1481
- return keyring;
1635
+ // We now create the keyring V2 wrappers and store them in memory.
1636
+ const keyringBuilderV2 = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringV2BuilderForType).call(this, type);
1637
+ let keyringV2;
1638
+ if (keyringBuilderV2) {
1639
+ keyringV2 = keyringBuilderV2(keyring, keyringMetadata);
1640
+ }
1641
+ return { keyring, keyringV2, metadata: keyringMetadata };
1482
1642
  }, _KeyringController_clearKeyrings =
1483
1643
  /**
1484
1644
  * Remove all managed keyrings, destroying all their
@@ -1486,8 +1646,8 @@ async function _KeyringController_createKeyring(type, data) {
1486
1646
  */
1487
1647
  async function _KeyringController_clearKeyrings() {
1488
1648
  __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);
1649
+ for (const { keyring, keyringV2 } of __classPrivateFieldGet(this, _KeyringController_keyrings, "f")) {
1650
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring, keyringV2);
1491
1651
  }
1492
1652
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
1493
1653
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
@@ -1503,23 +1663,28 @@ async function _KeyringController_restoreKeyring(serialized) {
1503
1663
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1504
1664
  try {
1505
1665
  const { type, data, metadata: serializedMetadata } = serialized;
1506
- let newMetadata = false;
1666
+ // Track if we need to trigger a vault update.
1667
+ let hasChanged = false;
1668
+ // If metadata is missing, assume the data is from an installation before we had
1669
+ // keyring metadata.
1507
1670
  let metadata = serializedMetadata;
1508
- const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data);
1509
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
1510
- // If metadata is missing, assume the data is from an installation before
1511
- // we had keyring metadata.
1512
1671
  if (!metadata) {
1513
- newMetadata = true;
1672
+ hasChanged = true;
1514
1673
  metadata = getDefaultKeyringMetadata();
1515
1674
  }
1675
+ const oldState = JSON.stringify(data);
1676
+ const { keyring, keyringV2 } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyring).call(this, type, data, metadata);
1677
+ const newState = JSON.stringify(await keyring.serialize());
1678
+ hasChanged || (hasChanged = oldState !== newState);
1679
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this, [keyring]);
1516
1680
  // The keyring is added to the keyrings array only if it's successfully restored
1517
1681
  // and the metadata is successfully added to the controller
1518
1682
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push({
1519
1683
  keyring,
1684
+ keyringV2,
1520
1685
  metadata,
1521
1686
  });
1522
- return { keyring, metadata, newMetadata };
1687
+ return { keyring, keyringV2, metadata, hasChanged };
1523
1688
  }
1524
1689
  catch (error) {
1525
1690
  console.error(error);
@@ -1535,9 +1700,13 @@ async function _KeyringController_restoreKeyring(serialized) {
1535
1700
  * clears the keyring bridge iframe from the DOM.
1536
1701
  *
1537
1702
  * @param keyring - The keyring to destroy.
1703
+ * @param keyringV2 - The keyring v2 to destroy (if any).
1538
1704
  */
1539
- async function _KeyringController_destroyKeyring(keyring) {
1705
+ async function _KeyringController_destroyKeyring(keyring, keyringV2) {
1540
1706
  await keyring.destroy?.();
1707
+ if (keyringV2) {
1708
+ await keyringV2.destroy?.();
1709
+ }
1541
1710
  }, _KeyringController_assertNoDuplicateAccounts =
1542
1711
  /**
1543
1712
  * Assert that there are no duplicate accounts in the keyrings.
@@ -1575,7 +1744,7 @@ async function _KeyringController_persistOrRollback(callback) {
1575
1744
  const callbackResult = await callback({ releaseLock });
1576
1745
  const newState = JSON.stringify(await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSessionState).call(this));
1577
1746
  // State is committed only if the operation is successful and need to trigger a vault update.
1578
- if (!(0, lodash_1.isEqual)(oldState, newState)) {
1747
+ if (oldState !== newState) {
1579
1748
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
1580
1749
  }
1581
1750
  return callbackResult;