@metamask-previews/keyring-controller 19.0.5-preview-37653b42 → 19.0.5-preview-fbd9878

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,9 +36,9 @@ 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_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_unsupportedKeyrings, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_keyrings, _KeyringController_password, _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
- exports.KeyringController = exports.getKeyringByFingerprint = exports.displayForKeyring = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
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");
43
43
  const base_controller_1 = require("@metamask/base-controller");
44
44
  const encryptorUtils = __importStar(require("@metamask/browser-passworder"));
@@ -55,31 +55,18 @@ const name = 'KeyringController';
55
55
  */
56
56
  var KeyringTypes;
57
57
  (function (KeyringTypes) {
58
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
59
- // eslint-disable-next-line @typescript-eslint/naming-convention
60
58
  KeyringTypes["simple"] = "Simple Key Pair";
61
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
62
- // eslint-disable-next-line @typescript-eslint/naming-convention
63
59
  KeyringTypes["hd"] = "HD Key Tree";
64
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
65
- // eslint-disable-next-line @typescript-eslint/naming-convention
66
60
  KeyringTypes["qr"] = "QR Hardware Wallet Device";
67
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
68
- // eslint-disable-next-line @typescript-eslint/naming-convention
69
61
  KeyringTypes["trezor"] = "Trezor Hardware";
70
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
71
- // eslint-disable-next-line @typescript-eslint/naming-convention
72
62
  KeyringTypes["ledger"] = "Ledger Hardware";
73
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
74
- // eslint-disable-next-line @typescript-eslint/naming-convention
75
63
  KeyringTypes["lattice"] = "Lattice Hardware";
76
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
77
- // eslint-disable-next-line @typescript-eslint/naming-convention
78
64
  KeyringTypes["snap"] = "Snap Keyring";
79
65
  })(KeyringTypes || (exports.KeyringTypes = KeyringTypes = {}));
80
66
  /**
81
67
  * Custody keyring types are a special case, as they are not a single type
82
68
  * but they all start with the prefix "Custody".
69
+ *
83
70
  * @param keyringType - The type of the keyring.
84
71
  * @returns Whether the keyring type is a custody keyring.
85
72
  */
@@ -92,11 +79,7 @@ exports.isCustodyKeyring = isCustodyKeyring;
92
79
  */
93
80
  var AccountImportStrategy;
94
81
  (function (AccountImportStrategy) {
95
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
96
- // eslint-disable-next-line @typescript-eslint/naming-convention
97
82
  AccountImportStrategy["privateKey"] = "privateKey";
98
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
99
- // eslint-disable-next-line @typescript-eslint/naming-convention
100
83
  AccountImportStrategy["json"] = "json";
101
84
  })(AccountImportStrategy || (exports.AccountImportStrategy = AccountImportStrategy = {}));
102
85
  /**
@@ -204,30 +187,8 @@ async function displayForKeyring(keyring) {
204
187
  // Cast to `string[]` here is safe here because `accounts` has no nullish
205
188
  // values, and `normalize` returns `string` unless given a nullish value
206
189
  accounts: accounts.map(normalize),
207
- // @ts-expect-error TODO: update type in @metamask/utils
208
- fingerprint: await keyring?.getFingerprint?.(),
209
190
  };
210
191
  }
211
- exports.displayForKeyring = displayForKeyring;
212
- /**
213
- * Retrieves a keyring from an array of keyrings based on its fingerprint.
214
- *
215
- * @param keyrings - Array of keyrings to search through.
216
- * @param fingerprint - The fingerprint to match against.
217
- * @returns Promise resolving to the matching keyring, or undefined if not found.
218
- */
219
- async function getKeyringByFingerprint(keyrings, fingerprint) {
220
- // Do not attempt to return a keyring if the fingerprint is not provided
221
- if (!fingerprint) {
222
- return undefined;
223
- }
224
- const fingerprints = await Promise.all(
225
- // @ts-expect-error TODO: update type in @metamask/utils
226
- keyrings.map((kr) => kr?.getFingerprint?.()));
227
- const index = fingerprints.indexOf(fingerprint);
228
- return keyrings[index];
229
- }
230
- exports.getKeyringByFingerprint = getKeyringByFingerprint;
231
192
  /**
232
193
  * Check if address is an ethereum address
233
194
  *
@@ -298,11 +259,11 @@ class KeyringController extends base_controller_1.BaseController {
298
259
  _KeyringController_controllerOperationMutex.set(this, new async_mutex_1.Mutex());
299
260
  _KeyringController_vaultOperationMutex.set(this, new async_mutex_1.Mutex());
300
261
  _KeyringController_keyringBuilders.set(this, void 0);
301
- _KeyringController_keyrings.set(this, void 0);
302
262
  _KeyringController_unsupportedKeyrings.set(this, void 0);
303
- _KeyringController_password.set(this, void 0);
304
263
  _KeyringController_encryptor.set(this, void 0);
305
264
  _KeyringController_cacheEncryptionKey.set(this, void 0);
265
+ _KeyringController_keyrings.set(this, void 0);
266
+ _KeyringController_password.set(this, void 0);
306
267
  _KeyringController_qrKeyringStateListener.set(this, void 0);
307
268
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
308
269
  ? keyringBuilders.concat(defaultKeyringBuilders)
@@ -404,6 +365,7 @@ class KeyringController extends base_controller_1.BaseController {
404
365
  * If there is a pre-existing locked vault, it will be replaced.
405
366
  *
406
367
  * @param password - Password to unlock the new vault.
368
+ * @returns Promise resolving when the operation ends successfully.
407
369
  */
408
370
  async createNewVaultAndKeychain(password) {
409
371
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
@@ -453,27 +415,12 @@ class KeyringController extends base_controller_1.BaseController {
453
415
  * Gets the seed phrase of the HD keyring.
454
416
  *
455
417
  * @param password - Password of the keyring.
456
- * @param keyringId - The keyring identifier.
457
418
  * @returns Promise resolving to the seed phrase.
458
419
  */
459
- async exportSeedPhrase(password, keyringId) {
420
+ async exportSeedPhrase(password) {
460
421
  await this.verifyPassword(password);
461
- let keyring;
462
- if (keyringId) {
463
- keyring = await getKeyringByFingerprint(__classPrivateFieldGet(this, _KeyringController_keyrings, "f"), keyringId);
464
- if (!keyring) {
465
- throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
466
- }
467
- if (keyring.type !== KeyringTypes.hd) {
468
- throw new Error(constants_1.KeyringControllerError.UnsupportedExportSeedPhrase);
469
- }
470
- }
471
- else {
472
- // There will always be an HD keyring
473
- keyring = this.getKeyringsByType(KeyringTypes.hd)[0];
474
- }
475
- assertHasUint8ArrayMnemonic(keyring);
476
- return keyring.mnemonic;
422
+ assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
423
+ return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
477
424
  }
478
425
  /**
479
426
  * Gets the private key from the keyring controlling an address.
@@ -599,7 +546,7 @@ class KeyringController extends base_controller_1.BaseController {
599
546
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
600
547
  let privateKey;
601
548
  switch (strategy) {
602
- case 'privateKey':
549
+ case AccountImportStrategy.privateKey:
603
550
  const [importedKey] = args;
604
551
  if (!importedKey) {
605
552
  throw new Error('Cannot import an empty key.');
@@ -619,7 +566,7 @@ class KeyringController extends base_controller_1.BaseController {
619
566
  }
620
567
  privateKey = (0, utils_1.remove0x)(prefixed);
621
568
  break;
622
- case 'json':
569
+ case AccountImportStrategy.json:
623
570
  let wallet;
624
571
  const [input, password] = args;
625
572
  try {
@@ -631,7 +578,7 @@ class KeyringController extends base_controller_1.BaseController {
631
578
  privateKey = (0, utils_1.bytesToHex)(wallet.getPrivateKey());
632
579
  break;
633
580
  default:
634
- throw new Error(`Unexpected import strategy: '${strategy}'`);
581
+ throw new Error(`Unexpected import strategy: '${String(strategy)}'`);
635
582
  }
636
583
  const newKeyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.simple, [
637
584
  privateKey,
@@ -656,7 +603,6 @@ class KeyringController extends base_controller_1.BaseController {
656
603
  }
657
604
  // The `removeAccount` method of snaps keyring is async. We have to update
658
605
  // the interface of the other keyrings to be async as well.
659
- // eslint-disable-next-line @typescript-eslint/await-thenable
660
606
  // FIXME: We do cast to `Hex` to makes the type checker happy here, and
661
607
  // because `Keyring<State>.removeAccount` requires address to be `Hex`. Those
662
608
  // type would need to be updated for a full non-EVM support.
@@ -887,9 +833,6 @@ class KeyringController extends base_controller_1.BaseController {
887
833
  if ('address' in selector) {
888
834
  keyring = (await this.getKeyringForAccount(selector.address));
889
835
  }
890
- else if ('fingerprint' in selector) {
891
- keyring = (await getKeyringByFingerprint(__classPrivateFieldGet(this, _KeyringController_keyrings, "f"), selector.fingerprint));
892
- }
893
836
  else {
894
837
  keyring = this.getKeyringsByType(selector.type)[selector.index || 0];
895
838
  if (!keyring && options.createIfMissing) {
@@ -1095,7 +1038,7 @@ class KeyringController extends base_controller_1.BaseController {
1095
1038
  }
1096
1039
  }
1097
1040
  exports.KeyringController = KeyringController;
1098
- _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() {
1041
+ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_cacheEncryptionKey = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1099
1042
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
1100
1043
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
1101
1044
  this.messagingSystem.registerActionHandler(`${name}:signTypedMessage`, this.signTypedMessage.bind(this));
@@ -1430,9 +1373,6 @@ async function _KeyringController_newKeyring(type, data) {
1430
1373
  // NOTE: Not all keyrings implement this method in a asynchronous-way. Using `await` for
1431
1374
  // non-thenable will still be valid (despite not being really useful). It allows us to cover both
1432
1375
  // cases and allow retro-compatibility too.
1433
- // FIXME: For some reason, it seems that eslint is complaining about this call being non-thenable
1434
- // even though it is... For now, we just disable it:
1435
- // eslint-disable-next-line @typescript-eslint/await-thenable
1436
1376
  await keyring.generateRandomMnemonic();
1437
1377
  await keyring.addAccounts(1);
1438
1378
  }
@@ -1547,14 +1487,12 @@ async function _KeyringController_checkForDuplicate(type, newAccountArray) {
1547
1487
  * and save the keyrings to state after it, or rollback to their
1548
1488
  * previous state in case of error.
1549
1489
  *
1550
- * @param fn - The function to execute.
1490
+ * @param callback - The function to execute.
1551
1491
  * @returns The result of the function.
1552
1492
  */
1553
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
1554
- // eslint-disable-next-line @typescript-eslint/naming-convention
1555
- async function _KeyringController_persistOrRollback(fn) {
1493
+ async function _KeyringController_persistOrRollback(callback) {
1556
1494
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async ({ releaseLock }) => {
1557
- const callbackResult = await fn({ releaseLock });
1495
+ const callbackResult = await callback({ releaseLock });
1558
1496
  // State is committed only if the operation is successful
1559
1497
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_updateVault).call(this);
1560
1498
  return callbackResult;
@@ -1564,17 +1502,15 @@ async function _KeyringController_persistOrRollback(fn) {
1564
1502
  * Execute the given function after acquiring the controller lock
1565
1503
  * and rollback keyrings and password states in case of error.
1566
1504
  *
1567
- * @param fn - The function to execute atomically.
1505
+ * @param callback - The function to execute atomically.
1568
1506
  * @returns The result of the function.
1569
1507
  */
1570
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
1571
- // eslint-disable-next-line @typescript-eslint/naming-convention
1572
- async function _KeyringController_withRollback(fn) {
1508
+ async function _KeyringController_withRollback(callback) {
1573
1509
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async ({ releaseLock }) => {
1574
1510
  const currentSerializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1575
1511
  const currentPassword = __classPrivateFieldGet(this, _KeyringController_password, "f");
1576
1512
  try {
1577
- return await fn({ releaseLock });
1513
+ return await callback({ releaseLock });
1578
1514
  }
1579
1515
  catch (e) {
1580
1516
  // Keyrings and password are restored to their previous state
@@ -1597,13 +1533,11 @@ async function _KeyringController_withRollback(fn) {
1597
1533
  * controller and that changes its state is executed in a mutually exclusive way,
1598
1534
  * preventing unsafe concurrent access that could lead to unpredictable behavior.
1599
1535
  *
1600
- * @param fn - The function to execute while the controller mutex is locked.
1536
+ * @param callback - The function to execute while the controller mutex is locked.
1601
1537
  * @returns The result of the function.
1602
1538
  */
1603
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
1604
- // eslint-disable-next-line @typescript-eslint/naming-convention
1605
- async function _KeyringController_withControllerLock(fn) {
1606
- return withLock(__classPrivateFieldGet(this, _KeyringController_controllerOperationMutex, "f"), fn);
1539
+ async function _KeyringController_withControllerLock(callback) {
1540
+ return withLock(__classPrivateFieldGet(this, _KeyringController_controllerOperationMutex, "f"), callback);
1607
1541
  }, _KeyringController_withVaultLock =
1608
1542
  /**
1609
1543
  * Lock the vault mutex before executing the given function,
@@ -1613,14 +1547,12 @@ async function _KeyringController_withControllerLock(fn) {
1613
1547
  * This ensures that each operation that interacts with the vault
1614
1548
  * is executed in a mutually exclusive way.
1615
1549
  *
1616
- * @param fn - The function to execute while the vault mutex is locked.
1550
+ * @param callback - The function to execute while the vault mutex is locked.
1617
1551
  * @returns The result of the function.
1618
1552
  */
1619
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
1620
- // eslint-disable-next-line @typescript-eslint/naming-convention
1621
- async function _KeyringController_withVaultLock(fn) {
1553
+ async function _KeyringController_withVaultLock(callback) {
1622
1554
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1623
- return withLock(__classPrivateFieldGet(this, _KeyringController_vaultOperationMutex, "f"), fn);
1555
+ return withLock(__classPrivateFieldGet(this, _KeyringController_vaultOperationMutex, "f"), callback);
1624
1556
  };
1625
1557
  /**
1626
1558
  * Lock the given mutex before executing the given function,
@@ -1628,15 +1560,13 @@ async function _KeyringController_withVaultLock(fn) {
1628
1560
  * error is thrown.
1629
1561
  *
1630
1562
  * @param mutex - The mutex to lock.
1631
- * @param fn - The function to execute while the mutex is locked.
1563
+ * @param callback - The function to execute while the mutex is locked.
1632
1564
  * @returns The result of the function.
1633
1565
  */
1634
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
1635
- // eslint-disable-next-line @typescript-eslint/naming-convention
1636
- async function withLock(mutex, fn) {
1566
+ async function withLock(mutex, callback) {
1637
1567
  const releaseLock = await mutex.acquire();
1638
1568
  try {
1639
- return await fn({ releaseLock });
1569
+ return await callback({ releaseLock });
1640
1570
  }
1641
1571
  finally {
1642
1572
  releaseLock();