@metamask-previews/keyring-controller 19.0.3-preview-183f0b5a → 19.0.4-preview-9df90b5

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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [19.0.4]
11
+
12
+ ### Changed
13
+
14
+ - Bump `@metamask/keyring-api` from `^13.0.0` to `^14.0.0` ([#5177](https://github.com/MetaMask/core/pull/5177))
15
+ - Bump `@metamask/keyring-internal-api` from `^2.0.0` to `^2.0.1` ([#5177](https://github.com/MetaMask/core/pull/5177))
16
+ - Bump `@metamask/message-manager` from `^12.0.0` to `^11.0.3` ([#5169](https://github.com/MetaMask/core/pull/5169))
17
+
10
18
  ## [19.0.3]
11
19
 
12
20
  ### Changed
@@ -632,7 +640,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
632
640
 
633
641
  All changes listed after this point were applied to this package following the monorepo conversion.
634
642
 
635
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/keyring-controller@19.0.3...HEAD
643
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/keyring-controller@19.0.4...HEAD
644
+ [19.0.4]: https://github.com/MetaMask/core/compare/@metamask/keyring-controller@19.0.3...@metamask/keyring-controller@19.0.4
636
645
  [19.0.3]: https://github.com/MetaMask/core/compare/@metamask/keyring-controller@19.0.2...@metamask/keyring-controller@19.0.3
637
646
  [19.0.2]: https://github.com/MetaMask/core/compare/@metamask/keyring-controller@19.0.1...@metamask/keyring-controller@19.0.2
638
647
  [19.0.1]: https://github.com/MetaMask/core/compare/@metamask/keyring-controller@19.0.0...@metamask/keyring-controller@19.0.1
@@ -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_unsupportedKeyrings, _KeyringController_password, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _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");
@@ -49,10 +49,6 @@ 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
51
  const constants_1 = require("./constants.cjs");
52
- // 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();
56
52
  const name = 'KeyringController';
57
53
  /**
58
54
  * Available keyring types
@@ -136,7 +132,6 @@ const getDefaultKeyringState = () => {
136
132
  return {
137
133
  isUnlocked: false,
138
134
  keyrings: [],
139
- keyringsMetadata: [],
140
135
  };
141
136
  };
142
137
  exports.getDefaultKeyringState = getDefaultKeyringState;
@@ -268,7 +263,6 @@ class KeyringController extends base_controller_1.BaseController {
268
263
  vault: { persist: true, anonymous: false },
269
264
  isUnlocked: { persist: false, anonymous: true },
270
265
  keyrings: { persist: false, anonymous: false },
271
- keyringsMetadata: { persist: true, anonymous: false },
272
266
  encryptionKey: { persist: false, anonymous: false },
273
267
  encryptionSalt: { persist: false, anonymous: false },
274
268
  },
@@ -311,8 +305,11 @@ class KeyringController extends base_controller_1.BaseController {
311
305
  */
312
306
  async addNewAccount(accountCount) {
313
307
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
314
- const selectedKeyring = this.getKeyringsByType('HD Key Tree')[0];
315
- const oldAccounts = await selectedKeyring.getAccounts();
308
+ const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
309
+ if (!primaryKeyring) {
310
+ throw new Error('No HD keyring found');
311
+ }
312
+ const oldAccounts = await primaryKeyring.getAccounts();
316
313
  if (accountCount && oldAccounts.length !== accountCount) {
317
314
  if (accountCount > oldAccounts.length) {
318
315
  throw new Error('Account out of sequence');
@@ -324,7 +321,7 @@ class KeyringController extends base_controller_1.BaseController {
324
321
  }
325
322
  return existingAccount;
326
323
  }
327
- const [addedAccountAddress] = await selectedKeyring.addAccounts(1);
324
+ const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
328
325
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this);
329
326
  return addedAccountAddress;
330
327
  });
@@ -434,21 +431,12 @@ class KeyringController extends base_controller_1.BaseController {
434
431
  * Gets the seed phrase of the HD keyring.
435
432
  *
436
433
  * @param password - Password of the keyring.
437
- * @param keyringId - The id of the keyring.
438
434
  * @returns Promise resolving to the seed phrase.
439
435
  */
440
- async exportSeedPhrase(password, keyringId) {
436
+ async exportSeedPhrase(password) {
441
437
  await this.verifyPassword(password);
442
- if (!keyringId) {
443
- assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
444
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
445
- }
446
- const selectedKeyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, keyringId);
447
- if (!selectedKeyring) {
448
- throw new Error('Keyring not found');
449
- }
450
- assertHasUint8ArrayMnemonic(selectedKeyring);
451
- return selectedKeyring.mnemonic;
438
+ assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
439
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
452
440
  }
453
441
  /**
454
442
  * Gets the private key from the keyring controlling an address.
@@ -849,16 +837,10 @@ class KeyringController extends base_controller_1.BaseController {
849
837
  /**
850
838
  * Verifies the that the seed phrase restores the current keychain's accounts.
851
839
  *
852
- * @param keyringId - The id of the keyring to verify.
853
840
  * @returns Promise resolving to the seed phrase as Uint8Array.
854
841
  */
855
- 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
- }
861
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this, keyringId));
842
+ async verifySeedPhrase() {
843
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this));
862
844
  }
863
845
  async withKeyring(selector, operation, options = {
864
846
  createIfMissing: false,
@@ -868,15 +850,12 @@ class KeyringController extends base_controller_1.BaseController {
868
850
  if ('address' in selector) {
869
851
  keyring = (await this.getKeyringForAccount(selector.address));
870
852
  }
871
- else if ('type' in selector) {
853
+ else {
872
854
  keyring = this.getKeyringsByType(selector.type)[selector.index || 0];
873
855
  if (!keyring && options.createIfMissing) {
874
856
  keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
875
857
  }
876
858
  }
877
- else if ('id' in selector) {
878
- keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
879
- }
880
859
  if (!keyring) {
881
860
  throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
882
861
  }
@@ -1090,9 +1069,6 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1090
1069
  this.messagingSystem.registerActionHandler(`${name}:patchUserOperation`, this.patchUserOperation.bind(this));
1091
1070
  this.messagingSystem.registerActionHandler(`${name}:signUserOperation`, this.signUserOperation.bind(this));
1092
1071
  this.messagingSystem.registerActionHandler(`${name}:addNewAccount`, this.addNewAccount.bind(this));
1093
- }, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
1094
- const index = this.state.keyringsMetadata.findIndex((metadata) => metadata.id === keyringId);
1095
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[index];
1096
1072
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1097
1073
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1098
1074
  }, _KeyringController_addQRKeyring =
@@ -1106,7 +1082,7 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1106
1082
  async function _KeyringController_addQRKeyring() {
1107
1083
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1108
1084
  // QRKeyring is not yet compatible with Keyring type from @metamask/utils
1109
- return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr, undefined));
1085
+ return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr));
1110
1086
  }, _KeyringController_subscribeToQRKeyringEvents = function _KeyringController_subscribeToQRKeyringEvents(qrKeyring) {
1111
1087
  __classPrivateFieldSet(this, _KeyringController_qrKeyringStateListener, (state) => {
1112
1088
  this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
@@ -1151,23 +1127,17 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
1151
1127
  /**
1152
1128
  * Internal non-exclusive method to verify the seed phrase.
1153
1129
  *
1154
- * @param keyringId - The id of the keyring to verify the seed phrase for.
1155
1130
  * @returns A promise resolving to the seed phrase as Uint8Array.
1156
1131
  */
1157
- async function _KeyringController_verifySeedPhrase(keyringId) {
1132
+ async function _KeyringController_verifySeedPhrase() {
1158
1133
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1159
- const keyring = keyringId
1160
- ? __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, keyringId)
1161
- : this.getKeyringsByType(KeyringTypes.hd)[0];
1162
- if (!keyring) {
1134
+ const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0];
1135
+ if (!primaryKeyring) {
1163
1136
  throw new Error('No HD keyring found.');
1164
1137
  }
1165
- if (keyring.type !== KeyringTypes.hd) {
1166
- throw new Error(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
1167
- }
1168
- assertHasUint8ArrayMnemonic(keyring);
1169
- const seedWords = keyring.mnemonic;
1170
- const accounts = await keyring.getAccounts();
1138
+ assertHasUint8ArrayMnemonic(primaryKeyring);
1139
+ const seedWords = primaryKeyring.mnemonic;
1140
+ const accounts = await primaryKeyring.getAccounts();
1171
1141
  /* istanbul ignore if */
1172
1142
  if (accounts.length === 0) {
1173
1143
  throw new Error('Cannot verify an empty keyring.');
@@ -1215,7 +1185,7 @@ async function _KeyringController_getUpdatedKeyrings() {
1215
1185
  async function _KeyringController_getSerializedKeyrings({ includeUnsupported } = {
1216
1186
  includeUnsupported: true,
1217
1187
  }) {
1218
- const serializedKeyrings = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring, index) => {
1188
+ const serializedKeyrings = await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring) => {
1219
1189
  const [type, data] = await Promise.all([
1220
1190
  keyring.type,
1221
1191
  keyring.serialize(),
@@ -1237,12 +1207,6 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1237
1207
  for (const serializedKeyring of serializedKeyrings) {
1238
1208
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
1239
1209
  }
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
- });
1245
- }
1246
1210
  }, _KeyringController_unlockKeyrings =
1247
1211
  /**
1248
1212
  * Unlock Keyrings, decrypting the vault and deserializing all
@@ -1360,9 +1324,6 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1360
1324
  state.encryptionKey = updatedState.encryptionKey;
1361
1325
  state.encryptionSalt = JSON.parse(updatedState.vault).salt;
1362
1326
  }
1363
- if (updatedKeyrings.length < state.keyringsMetadata.length) {
1364
- state.keyringsMetadata = state.keyringsMetadata.slice(0, -1 * (state.keyringsMetadata.length - updatedKeyrings.length));
1365
- }
1366
1327
  });
1367
1328
  return true;
1368
1329
  });
@@ -1398,13 +1359,13 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1398
1359
  if (!firstAccount) {
1399
1360
  throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
1400
1361
  }
1401
- return firstAccount;
1402
1362
  }, _KeyringController_newKeyring =
1403
1363
  /**
1404
1364
  * Instantiate, initialize and return a new keyring of the given `type`,
1405
1365
  * using the given `opts`. The keyring is built using the keyring builder
1406
1366
  * registered for the given `type`.
1407
1367
  *
1368
+ *
1408
1369
  * @param type - The type of keyring to add.
1409
1370
  * @param data - The data to restore a previously serialized keyring.
1410
1371
  * @returns The new keyring.
@@ -1412,7 +1373,6 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1412
1373
  */
1413
1374
  async function _KeyringController_newKeyring(type, data) {
1414
1375
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1415
- const newKeyringMetadata = { id: ulid(), name: '' };
1416
1376
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1417
1377
  if (!keyringBuilder) {
1418
1378
  throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
@@ -1443,15 +1403,6 @@ async function _KeyringController_newKeyring(type, data) {
1443
1403
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, keyring);
1444
1404
  }
1445
1405
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
1446
- 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
- });
1454
- }
1455
1406
  return keyring;
1456
1407
  }, _KeyringController_clearKeyrings =
1457
1408
  /**
@@ -1464,9 +1415,6 @@ async function _KeyringController_clearKeyrings() {
1464
1415
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1465
1416
  }
1466
1417
  __classPrivateFieldSet(this, _KeyringController_keyrings, [], "f");
1467
- this.update((state) => {
1468
- state.keyringsMetadata = [];
1469
- });
1470
1418
  }, _KeyringController_restoreKeyring =
1471
1419
  /**
1472
1420
  * Restore a Keyring from a provided serialized payload.
@@ -1507,24 +1455,19 @@ async function _KeyringController_destroyKeyring(keyring) {
1507
1455
  async function _KeyringController_removeEmptyKeyrings() {
1508
1456
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1509
1457
  const validKeyrings = [];
1510
- const validKeyringMetadata = [];
1511
1458
  // Since getAccounts returns a Promise
1512
1459
  // We need to wait to hear back form each keyring
1513
1460
  // in order to decide which ones are now valid (accounts.length > 0)
1514
- await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring, index) => {
1461
+ await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring) => {
1515
1462
  const accounts = await keyring.getAccounts();
1516
1463
  if (accounts.length > 0) {
1517
1464
  validKeyrings.push(keyring);
1518
- validKeyringMetadata.push(this.state.keyringsMetadata[index]);
1519
1465
  }
1520
1466
  else {
1521
1467
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_destroyKeyring).call(this, keyring);
1522
1468
  }
1523
1469
  }));
1524
1470
  __classPrivateFieldSet(this, _KeyringController_keyrings, validKeyrings, "f");
1525
- this.update((state) => {
1526
- state.keyringsMetadata = validKeyringMetadata;
1527
- });
1528
1471
  }, _KeyringController_checkForDuplicate =
1529
1472
  /**
1530
1473
  * Checks for duplicate keypairs, using the the first account in the given