@metamask-previews/keyring-controller 19.0.3-preview-183f0b5a → 19.0.3-preview-4c4eb52a

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.
@@ -36,7 +36,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
36
36
  var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  return (mod && mod.__esModule) ? mod : { "default": mod };
38
38
  };
39
- var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringById, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _KeyringController_setUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
39
+ var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_keyrings, _KeyringController_keyringsMetadata, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringById, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _KeyringController_setUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
40
40
  Object.defineProperty(exports, "__esModule", { value: true });
41
41
  exports.KeyringController = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
42
42
  const util_1 = require("@ethereumjs/util");
@@ -48,11 +48,10 @@ const eth_simple_keyring_1 = __importDefault(require("@metamask/eth-simple-keyri
48
48
  const utils_1 = require("@metamask/utils");
49
49
  const async_mutex_1 = require("async-mutex");
50
50
  const ethereumjs_wallet_1 = __importStar(require("ethereumjs-wallet"));
51
- const constants_1 = require("./constants.cjs");
52
51
  // When generating a ULID within the same millisecond, monotonicFactory provides some guarantees regarding sort order.
53
- // eslint-disable-next-line @typescript-eslint/no-require-imports
54
- const { monotonicFactory } = require("ulidx");
55
- const ulid = monotonicFactory();
52
+ const ulid_1 = require("ulid");
53
+ const constants_1 = require("./constants.cjs");
54
+ const ulid = (0, ulid_1.monotonicFactory)();
56
55
  const name = 'KeyringController';
57
56
  /**
58
57
  * Available keyring types
@@ -283,6 +282,7 @@ class KeyringController extends base_controller_1.BaseController {
283
282
  _KeyringController_vaultOperationMutex.set(this, new async_mutex_1.Mutex());
284
283
  _KeyringController_keyringBuilders.set(this, void 0);
285
284
  _KeyringController_keyrings.set(this, void 0);
285
+ _KeyringController_keyringsMetadata.set(this, void 0);
286
286
  _KeyringController_unsupportedKeyrings.set(this, void 0);
287
287
  _KeyringController_password.set(this, void 0);
288
288
  _KeyringController_encryptor.set(this, void 0);
@@ -293,6 +293,7 @@ class KeyringController extends base_controller_1.BaseController {
293
293
  : defaultKeyringBuilders, "f");
294
294
  __classPrivateFieldSet(this, _KeyringController_encryptor, encryptor, "f");
295
295
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
296
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, state?.keyringsMetadata ?? [], "f");
296
297
  __classPrivateFieldSet(this, _KeyringController_unsupportedKeyrings, [], "f");
297
298
  // This option allows the controller to cache an exported key
298
299
  // for use in decrypting and encrypting data without password
@@ -312,6 +313,9 @@ class KeyringController extends base_controller_1.BaseController {
312
313
  async addNewAccount(accountCount) {
313
314
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
314
315
  const selectedKeyring = this.getKeyringsByType('HD Key Tree')[0];
316
+ if (!selectedKeyring) {
317
+ throw new Error('No HD keyring found');
318
+ }
315
319
  const oldAccounts = await selectedKeyring.getAccounts();
316
320
  if (accountCount && oldAccounts.length !== accountCount) {
317
321
  if (accountCount > oldAccounts.length) {
@@ -853,10 +857,18 @@ class KeyringController extends base_controller_1.BaseController {
853
857
  * @returns Promise resolving to the seed phrase as Uint8Array.
854
858
  */
855
859
  async verifySeedPhrase(keyringId) {
856
- const keyringIndex = this.state.keyringsMetadata.findIndex((keyring) => keyring.id === keyringId);
857
- const keyring = this.state.keyrings[keyringIndex];
858
- if (keyring.type !== KeyringTypes.hd) {
859
- throw new Error(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
860
+ let keyring;
861
+ if (!keyringId) {
862
+ keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0];
863
+ }
864
+ else {
865
+ keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, keyringId);
866
+ if (keyring.type !== KeyringTypes.hd) {
867
+ throw new Error(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
868
+ }
869
+ }
870
+ if (!keyring) {
871
+ throw new Error(constants_1.KeyringControllerError.NoHdKeyring);
860
872
  }
861
873
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this, keyringId));
862
874
  }
@@ -1076,7 +1088,7 @@ class KeyringController extends base_controller_1.BaseController {
1076
1088
  }
1077
1089
  }
1078
1090
  exports.KeyringController = KeyringController;
1079
- _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_cacheEncryptionKey = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1091
+ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_keyringsMetadata = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_cacheEncryptionKey = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1080
1092
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
1081
1093
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
1082
1094
  this.messagingSystem.registerActionHandler(`${name}:signTypedMessage`, this.signTypedMessage.bind(this));
@@ -1162,9 +1174,6 @@ async function _KeyringController_verifySeedPhrase(keyringId) {
1162
1174
  if (!keyring) {
1163
1175
  throw new Error('No HD keyring found.');
1164
1176
  }
1165
- if (keyring.type !== KeyringTypes.hd) {
1166
- throw new Error(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
1167
- }
1168
1177
  assertHasUint8ArrayMnemonic(keyring);
1169
1178
  const seedWords = keyring.mnemonic;
1170
1179
  const accounts = await keyring.getAccounts();
@@ -1237,11 +1246,8 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1237
1246
  for (const serializedKeyring of serializedKeyrings) {
1238
1247
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
1239
1248
  }
1240
- if (this.state.keyringsMetadata.length > __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1241
- this.update((state) => {
1242
- // remove metadata from the end of the array to have the same length as the keyrings array
1243
- state.keyringsMetadata = state.keyringsMetadata.slice(0, -1 * (state.keyringsMetadata.length - __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length));
1244
- });
1249
+ if (__classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length > __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1250
+ throw new Error(constants_1.KeyringControllerError.KeyringMetadataLengthMismatch);
1245
1251
  }
1246
1252
  }, _KeyringController_unlockKeyrings =
1247
1253
  /**
@@ -1356,12 +1362,13 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1356
1362
  this.update((state) => {
1357
1363
  state.vault = updatedState.vault;
1358
1364
  state.keyrings = updatedKeyrings;
1365
+ state.keyringsMetadata = __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f");
1359
1366
  if (updatedState.encryptionKey) {
1360
1367
  state.encryptionKey = updatedState.encryptionKey;
1361
1368
  state.encryptionSalt = JSON.parse(updatedState.vault).salt;
1362
1369
  }
1363
- if (updatedKeyrings.length < state.keyringsMetadata.length) {
1364
- state.keyringsMetadata = state.keyringsMetadata.slice(0, -1 * (state.keyringsMetadata.length - updatedKeyrings.length));
1370
+ if (updatedKeyrings.length < __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length) {
1371
+ throw new Error(constants_1.KeyringControllerError.KeyringMetadataLengthMismatch);
1365
1372
  }
1366
1373
  });
1367
1374
  return true;
@@ -1412,7 +1419,10 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1412
1419
  */
1413
1420
  async function _KeyringController_newKeyring(type, data) {
1414
1421
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1415
- const newKeyringMetadata = { id: ulid(), name: '' };
1422
+ const newKeyringMetadata = {
1423
+ id: ulid().toString(),
1424
+ name: '',
1425
+ };
1416
1426
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1417
1427
  if (!keyringBuilder) {
1418
1428
  throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
@@ -1444,13 +1454,8 @@ async function _KeyringController_newKeyring(type, data) {
1444
1454
  }
1445
1455
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
1446
1456
  if (newKeyringMetadata &&
1447
- this.state.keyringsMetadata.length < __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1448
- this.update((state) => {
1449
- state.keyringsMetadata = [
1450
- ...state.keyringsMetadata,
1451
- newKeyringMetadata,
1452
- ];
1453
- });
1457
+ __classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f").length < __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1458
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, [...__classPrivateFieldGet(this, _KeyringController_keyringsMetadata, "f"), newKeyringMetadata], "f");
1454
1459
  }
1455
1460
  return keyring;
1456
1461
  }, _KeyringController_clearKeyrings =
@@ -1464,9 +1469,10 @@ async function _KeyringController_clearKeyrings() {
1464
1469
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1465
1470
  }
1466
1471
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
1467
- this.update((state) => {
1468
- state.keyringsMetadata = [];
1469
- });
1472
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, [], "f");
1473
+ // this.update((state) => {
1474
+ // state.keyringsMetadata = [];
1475
+ // });
1470
1476
  }, _KeyringController_restoreKeyring =
1471
1477
  /**
1472
1478
  * Restore a Keyring from a provided serialized payload.
@@ -1522,9 +1528,7 @@ async function _KeyringController_removeEmptyKeyrings() {
1522
1528
  }
1523
1529
  }));
1524
1530
  __classPrivateFieldSet(this, _KeyringController_keyrings, validKeyrings, "f");
1525
- this.update((state) => {
1526
- state.keyringsMetadata = validKeyringMetadata;
1527
- });
1531
+ __classPrivateFieldSet(this, _KeyringController_keyringsMetadata, validKeyringMetadata, "f");
1528
1532
  }, _KeyringController_checkForDuplicate =
1529
1533
  /**
1530
1534
  * Checks for duplicate keypairs, using the the first account in the given