@metamask-previews/keyring-controller 19.0.3-preview-83c8a21 → 19.0.3-preview-2ba45577

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_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");
@@ -48,10 +48,7 @@ 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
- // When generating a ULID within the same millisecond, monotonicFactory provides some guarantees regarding sort order.
52
- const ulid_1 = require("ulid");
53
51
  const constants_1 = require("./constants.cjs");
54
- const ulid = (0, ulid_1.monotonicFactory)();
55
52
  const name = 'KeyringController';
56
53
  /**
57
54
  * Available keyring types
@@ -135,7 +132,6 @@ const getDefaultKeyringState = () => {
135
132
  return {
136
133
  isUnlocked: false,
137
134
  keyrings: [],
138
- keyringsMetadata: [],
139
135
  };
140
136
  };
141
137
  exports.getDefaultKeyringState = getDefaultKeyringState;
@@ -267,7 +263,6 @@ class KeyringController extends base_controller_1.BaseController {
267
263
  vault: { persist: true, anonymous: false },
268
264
  isUnlocked: { persist: false, anonymous: true },
269
265
  keyrings: { persist: false, anonymous: false },
270
- keyringsMetadata: { persist: true, anonymous: false },
271
266
  encryptionKey: { persist: false, anonymous: false },
272
267
  encryptionSalt: { persist: false, anonymous: false },
273
268
  },
@@ -305,20 +300,16 @@ class KeyringController extends base_controller_1.BaseController {
305
300
  * Adds a new account to the default (first) HD seed phrase keyring.
306
301
  *
307
302
  * @param accountCount - Number of accounts before adding a new one, used to
308
- * @param keyringId - The id of the keyring to add the account to.
309
303
  * make the method idempotent.
310
304
  * @returns Promise resolving to the added account address.
311
305
  */
312
- async addNewAccount(accountCount, keyringId) {
306
+ async addNewAccount(accountCount) {
313
307
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
314
- let selectedKeyring = this.getKeyringsByType('HD Key Tree')[0];
315
- if (keyringId) {
316
- selectedKeyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, keyringId);
317
- }
318
- if (!selectedKeyring) {
308
+ const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
309
+ if (!primaryKeyring) {
319
310
  throw new Error('No HD keyring found');
320
311
  }
321
- const oldAccounts = await selectedKeyring.getAccounts();
312
+ const oldAccounts = await primaryKeyring.getAccounts();
322
313
  if (accountCount && oldAccounts.length !== accountCount) {
323
314
  if (accountCount > oldAccounts.length) {
324
315
  throw new Error('Account out of sequence');
@@ -330,8 +321,8 @@ class KeyringController extends base_controller_1.BaseController {
330
321
  }
331
322
  return existingAccount;
332
323
  }
333
- const [addedAccountAddress] = await selectedKeyring.addAccounts(1);
334
- await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this, keyringId);
324
+ const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
325
+ await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this);
335
326
  return addedAccountAddress;
336
327
  });
337
328
  }
@@ -414,7 +405,7 @@ class KeyringController extends base_controller_1.BaseController {
414
405
  if (type === KeyringTypes.qr) {
415
406
  return this.getOrAddQRKeyring();
416
407
  }
417
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts, { id: ulid(), name: '' }));
408
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts));
418
409
  }
419
410
  /**
420
411
  * Method to verify a given password validity. Throws an
@@ -440,21 +431,12 @@ class KeyringController extends base_controller_1.BaseController {
440
431
  * Gets the seed phrase of the HD keyring.
441
432
  *
442
433
  * @param password - Password of the keyring.
443
- * @param keyringId - The id of the keyring.
444
434
  * @returns Promise resolving to the seed phrase.
445
435
  */
446
- async exportSeedPhrase(password, keyringId) {
436
+ async exportSeedPhrase(password) {
447
437
  await this.verifyPassword(password);
448
- if (!keyringId) {
449
- assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
450
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
451
- }
452
- const selectedKeyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, keyringId);
453
- if (!selectedKeyring) {
454
- throw new Error('Keyring not found');
455
- }
456
- assertHasUint8ArrayMnemonic(selectedKeyring);
457
- return selectedKeyring.mnemonic;
438
+ assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
439
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
458
440
  }
459
441
  /**
460
442
  * Gets the private key from the keyring controlling an address.
@@ -474,18 +456,9 @@ class KeyringController extends base_controller_1.BaseController {
474
456
  /**
475
457
  * Returns the public addresses of all accounts from every keyring.
476
458
  *
477
- * @param keyringId - The id of the keyring to get the accounts from.
478
459
  * @returns A promise resolving to an array of addresses.
479
460
  */
480
- async getAccounts(keyringId) {
481
- if (keyringId) {
482
- const keyringIndex = this.state.keyringsMetadata.findIndex((keyring) => keyring.id === keyringId);
483
- const keyring = this.state.keyrings[keyringIndex];
484
- if (!keyring) {
485
- throw new Error('Keyring not found');
486
- }
487
- return keyring.accounts;
488
- }
461
+ async getAccounts() {
489
462
  return this.state.keyrings.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
490
463
  }
491
464
  /**
@@ -625,7 +598,7 @@ class KeyringController extends base_controller_1.BaseController {
625
598
  }
626
599
  const newKeyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.simple, [
627
600
  privateKey,
628
- ], { id: ulid(), name: '' }));
601
+ ]));
629
602
  const accounts = await newKeyring.getAccounts();
630
603
  return accounts[0];
631
604
  });
@@ -864,11 +837,10 @@ class KeyringController extends base_controller_1.BaseController {
864
837
  /**
865
838
  * Verifies the that the seed phrase restores the current keychain's accounts.
866
839
  *
867
- * @param keyringId - The id of the keyring to verify.
868
840
  * @returns Promise resolving to the seed phrase as Uint8Array.
869
841
  */
870
- async verifySeedPhrase(keyringId) {
871
- 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));
872
844
  }
873
845
  async withKeyring(selector, operation, options = {
874
846
  createIfMissing: false,
@@ -878,16 +850,12 @@ class KeyringController extends base_controller_1.BaseController {
878
850
  if ('address' in selector) {
879
851
  keyring = (await this.getKeyringForAccount(selector.address));
880
852
  }
881
- else if ('type' in selector) {
853
+ else {
882
854
  keyring = this.getKeyringsByType(selector.type)[selector.index || 0];
883
855
  if (!keyring && options.createIfMissing) {
884
- const newMetadata = { id: ulid(), name: '' };
885
- keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData, newMetadata));
856
+ keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
886
857
  }
887
858
  }
888
- else if ('id' in selector) {
889
- keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
890
- }
891
859
  if (!keyring) {
892
860
  throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
893
861
  }
@@ -1101,9 +1069,6 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1101
1069
  this.messagingSystem.registerActionHandler(`${name}:patchUserOperation`, this.patchUserOperation.bind(this));
1102
1070
  this.messagingSystem.registerActionHandler(`${name}:signUserOperation`, this.signUserOperation.bind(this));
1103
1071
  this.messagingSystem.registerActionHandler(`${name}:addNewAccount`, this.addNewAccount.bind(this));
1104
- }, _KeyringController_getKeyringById = function _KeyringController_getKeyringById(keyringId) {
1105
- const index = this.state.keyringsMetadata.findIndex((metadata) => metadata.id === keyringId);
1106
- return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[index];
1107
1072
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1108
1073
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1109
1074
  }, _KeyringController_addQRKeyring =
@@ -1117,10 +1082,7 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1117
1082
  async function _KeyringController_addQRKeyring() {
1118
1083
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1119
1084
  // QRKeyring is not yet compatible with Keyring type from @metamask/utils
1120
- return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr, undefined, {
1121
- id: ulid(),
1122
- name: '',
1123
- }));
1085
+ return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr));
1124
1086
  }, _KeyringController_subscribeToQRKeyringEvents = function _KeyringController_subscribeToQRKeyringEvents(qrKeyring) {
1125
1087
  __classPrivateFieldSet(this, _KeyringController_qrKeyringStateListener, (state) => {
1126
1088
  this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
@@ -1165,20 +1127,17 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
1165
1127
  /**
1166
1128
  * Internal non-exclusive method to verify the seed phrase.
1167
1129
  *
1168
- * @param keyringId - The id of the keyring to verify the seed phrase for.
1169
1130
  * @returns A promise resolving to the seed phrase as Uint8Array.
1170
1131
  */
1171
- async function _KeyringController_verifySeedPhrase(keyringId) {
1132
+ async function _KeyringController_verifySeedPhrase() {
1172
1133
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1173
- const keyring = keyringId
1174
- ? __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, keyringId)
1175
- : this.getKeyringsByType(KeyringTypes.hd)[0];
1176
- if (!keyring) {
1134
+ const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0];
1135
+ if (!primaryKeyring) {
1177
1136
  throw new Error('No HD keyring found.');
1178
1137
  }
1179
- assertHasUint8ArrayMnemonic(keyring);
1180
- const seedWords = keyring.mnemonic;
1181
- const accounts = await keyring.getAccounts();
1138
+ assertHasUint8ArrayMnemonic(primaryKeyring);
1139
+ const seedWords = primaryKeyring.mnemonic;
1140
+ const accounts = await primaryKeyring.getAccounts();
1182
1141
  /* istanbul ignore if */
1183
1142
  if (accounts.length === 0) {
1184
1143
  throw new Error('Cannot verify an empty keyring.');
@@ -1226,7 +1185,7 @@ async function _KeyringController_getUpdatedKeyrings() {
1226
1185
  async function _KeyringController_getSerializedKeyrings({ includeUnsupported } = {
1227
1186
  includeUnsupported: true,
1228
1187
  }) {
1229
- 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) => {
1230
1189
  const [type, data] = await Promise.all([
1231
1190
  keyring.type,
1232
1191
  keyring.serialize(),
@@ -1248,12 +1207,6 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1248
1207
  for (const serializedKeyring of serializedKeyrings) {
1249
1208
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreKeyring).call(this, serializedKeyring);
1250
1209
  }
1251
- if (this.state.keyringsMetadata.length > __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1252
- this.update((state) => {
1253
- // remove metadata from the end of the array to have the same length as the keyrings array
1254
- state.keyringsMetadata = state.keyringsMetadata.slice(0, -1 * (state.keyringsMetadata.length - __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length));
1255
- });
1256
- }
1257
1210
  }, _KeyringController_unlockKeyrings =
1258
1211
  /**
1259
1212
  * Unlock Keyrings, decrypting the vault and deserializing all
@@ -1371,9 +1324,6 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1371
1324
  state.encryptionKey = updatedState.encryptionKey;
1372
1325
  state.encryptionSalt = JSON.parse(updatedState.vault).salt;
1373
1326
  }
1374
- if (updatedKeyrings.length < state.keyringsMetadata.length) {
1375
- state.keyringsMetadata = state.keyringsMetadata.slice(0, -1 * (state.keyringsMetadata.length - updatedKeyrings.length));
1376
- }
1377
1327
  });
1378
1328
  return true;
1379
1329
  });
@@ -1404,15 +1354,11 @@ async function _KeyringController_getAccountsFromKeyrings() {
1404
1354
  */
1405
1355
  async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1406
1356
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1407
- const keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts, {
1408
- id: ulid(),
1409
- name: '',
1410
- }));
1357
+ const keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts));
1411
1358
  const [firstAccount] = await keyring.getAccounts();
1412
1359
  if (!firstAccount) {
1413
1360
  throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
1414
1361
  }
1415
- return firstAccount;
1416
1362
  }, _KeyringController_newKeyring =
1417
1363
  /**
1418
1364
  * Instantiate, initialize and return a new keyring of the given `type`,
@@ -1422,11 +1368,10 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1422
1368
  *
1423
1369
  * @param type - The type of keyring to add.
1424
1370
  * @param data - The data to restore a previously serialized keyring.
1425
- * @param metadata - The metadata to add to the keyring.
1426
1371
  * @returns The new keyring.
1427
1372
  * @throws If the keyring includes duplicated accounts.
1428
1373
  */
1429
- async function _KeyringController_newKeyring(type, data, metadata) {
1374
+ async function _KeyringController_newKeyring(type, data) {
1430
1375
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1431
1376
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1432
1377
  if (!keyringBuilder) {
@@ -1458,11 +1403,6 @@ async function _KeyringController_newKeyring(type, data, metadata) {
1458
1403
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, keyring);
1459
1404
  }
1460
1405
  __classPrivateFieldGet(this, _KeyringController_keyrings, "f").push(keyring);
1461
- if (metadata && this.state.keyringsMetadata.length < __classPrivateFieldGet(this, _KeyringController_keyrings, "f").length) {
1462
- this.update((state) => {
1463
- state.keyringsMetadata = [...state.keyringsMetadata, metadata];
1464
- });
1465
- }
1466
1406
  return keyring;
1467
1407
  }, _KeyringController_clearKeyrings =
1468
1408
  /**
@@ -1518,7 +1458,7 @@ async function _KeyringController_removeEmptyKeyrings() {
1518
1458
  // Since getAccounts returns a Promise
1519
1459
  // We need to wait to hear back form each keyring
1520
1460
  // in order to decide which ones are now valid (accounts.length > 0)
1521
- await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring, index) => {
1461
+ await Promise.all(__classPrivateFieldGet(this, _KeyringController_keyrings, "f").map(async (keyring) => {
1522
1462
  const accounts = await keyring.getAccounts();
1523
1463
  if (accounts.length > 0) {
1524
1464
  validKeyrings.push(keyring);