@metamask-previews/keyring-controller 25.0.0-preview-a9886279 → 25.0.0-preview-cb897e9

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
@@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
12
12
  - Added new `KeyringBuilder` type ([#7334](https://github.com/MetaMask/core/pull/7334))
13
13
  - Added an action to call `removeAccount` ([#7241](https://github.com/MetaMask/core/pull/7241))
14
14
  - This action is meant to be consumed by the `MultichainAccountService` to encapsulate the act of removing a wallet when seed phrase backup fails in the clients.
15
+ - Added new `KeyringControllerError` ([#7498](https://github.com/MetaMask/core/pull/7498))
16
+ - All controller's errors are now using this error type.
17
+ - Keyring instance operation errors are also now also wrapped with this error type.
15
18
 
16
19
  ### Changed
17
20
 
@@ -51,6 +51,7 @@ const lodash_1 = require("lodash");
51
51
  // When generating a ULID within the same millisecond, monotonicFactory provides some guarantees regarding sort order.
52
52
  const ulid_1 = require("ulid");
53
53
  const constants_1 = require("./constants.cjs");
54
+ const errors_1 = require("./errors.cjs");
54
55
  const name = 'KeyringController';
55
56
  /**
56
57
  * Available keyring types
@@ -140,7 +141,7 @@ exports.getDefaultKeyringState = getDefaultKeyringState;
140
141
  */
141
142
  function assertHasUint8ArrayMnemonic(keyring) {
142
143
  if (!((0, utils_1.hasProperty)(keyring, 'mnemonic') && keyring.mnemonic instanceof Uint8Array)) {
143
- throw new Error("Can't get mnemonic bytes from keyring");
144
+ throw new errors_1.KeyringControllerError("Can't get mnemonic bytes from keyring");
144
145
  }
145
146
  }
146
147
  /**
@@ -151,10 +152,10 @@ function assertHasUint8ArrayMnemonic(keyring) {
151
152
  */
152
153
  function assertIsValidPassword(password) {
153
154
  if (typeof password !== 'string') {
154
- throw new Error(constants_1.KeyringControllerError.WrongPasswordType);
155
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.WrongPasswordType);
155
156
  }
156
157
  if (!password?.length) {
157
- throw new Error(constants_1.KeyringControllerError.InvalidEmptyPassword);
158
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.InvalidEmptyPassword);
158
159
  }
159
160
  }
160
161
  /**
@@ -165,7 +166,7 @@ function assertIsValidPassword(password) {
165
166
  */
166
167
  function assertIsEncryptionKeySet(encryptionKey) {
167
168
  if (!encryptionKey) {
168
- throw new Error(constants_1.KeyringControllerError.EncryptionKeyNotSet);
169
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.EncryptionKeyNotSet);
169
170
  }
170
171
  }
171
172
  /**
@@ -318,17 +319,17 @@ class KeyringController extends base_controller_1.BaseController {
318
319
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
319
320
  const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
320
321
  if (!primaryKeyring) {
321
- throw new Error('No HD keyring found');
322
+ throw new errors_1.KeyringControllerError('No HD keyring found');
322
323
  }
323
324
  const oldAccounts = await primaryKeyring.getAccounts();
324
325
  if (accountCount && oldAccounts.length !== accountCount) {
325
326
  if (accountCount > oldAccounts.length) {
326
- throw new Error('Account out of sequence');
327
+ throw new errors_1.KeyringControllerError('Account out of sequence');
327
328
  }
328
329
  // we return the account already existing at index `accountCount`
329
330
  const existingAccount = oldAccounts[accountCount];
330
331
  if (!existingAccount) {
331
- throw new Error(`Can't find account at index ${accountCount}`);
332
+ throw new errors_1.KeyringControllerError(`Can't find account at index ${accountCount}`);
332
333
  }
333
334
  return existingAccount;
334
335
  }
@@ -354,7 +355,7 @@ class KeyringController extends base_controller_1.BaseController {
354
355
  const oldAccounts = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this);
355
356
  if (accountCount && oldAccounts.length !== accountCount) {
356
357
  if (accountCount > oldAccounts.length) {
357
- throw new Error('Account out of sequence');
358
+ throw new errors_1.KeyringControllerError('Account out of sequence');
358
359
  }
359
360
  const existingAccount = oldAccounts[accountCount];
360
361
  (0, utils_1.assertIsStrictHexString)(existingAccount);
@@ -426,7 +427,7 @@ class KeyringController extends base_controller_1.BaseController {
426
427
  */
427
428
  async verifyPassword(password) {
428
429
  if (!this.state.vault) {
429
- throw new Error(constants_1.KeyringControllerError.VaultError);
430
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.VaultError);
430
431
  }
431
432
  await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decrypt(password, this.state.vault);
432
433
  }
@@ -450,7 +451,7 @@ class KeyringController extends base_controller_1.BaseController {
450
451
  await this.verifyPassword(password);
451
452
  const selectedKeyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringByIdOrDefault).call(this, keyringId);
452
453
  if (!selectedKeyring) {
453
- throw new Error('Keyring not found');
454
+ throw new errors_1.KeyringControllerError('Keyring not found');
454
455
  }
455
456
  assertHasUint8ArrayMnemonic(selectedKeyring);
456
457
  return selectedKeyring.mnemonic;
@@ -466,7 +467,7 @@ class KeyringController extends base_controller_1.BaseController {
466
467
  await this.verifyPassword(password);
467
468
  const keyring = (await this.getKeyringForAccount(address));
468
469
  if (!keyring.exportAccount) {
469
- throw new Error(constants_1.KeyringControllerError.UnsupportedExportAccount);
470
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedExportAccount);
470
471
  }
471
472
  return await keyring.exportAccount(normalize(address));
472
473
  }
@@ -492,7 +493,7 @@ class KeyringController extends base_controller_1.BaseController {
492
493
  const address = (0, eth_sig_util_1.normalize)(account);
493
494
  const keyring = (await this.getKeyringForAccount(account));
494
495
  if (!keyring.getEncryptionPublicKey) {
495
- throw new Error(constants_1.KeyringControllerError.UnsupportedGetEncryptionPublicKey);
496
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedGetEncryptionPublicKey);
496
497
  }
497
498
  return await keyring.getEncryptionPublicKey(address, opts);
498
499
  }
@@ -509,7 +510,7 @@ class KeyringController extends base_controller_1.BaseController {
509
510
  const address = (0, eth_sig_util_1.normalize)(messageParams.from);
510
511
  const keyring = (await this.getKeyringForAccount(address));
511
512
  if (!keyring.decryptMessage) {
512
- throw new Error(constants_1.KeyringControllerError.UnsupportedDecryptMessage);
513
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedDecryptMessage);
513
514
  }
514
515
  return keyring.decryptMessage(address, messageParams.data);
515
516
  }
@@ -544,7 +545,7 @@ class KeyringController extends base_controller_1.BaseController {
544
545
  else if (!winners.length) {
545
546
  errorInfo = 'There are keyrings, but none match the address';
546
547
  }
547
- throw new Error(`${constants_1.KeyringControllerError.NoKeyring}. Error info: ${errorInfo}`);
548
+ throw new errors_1.KeyringControllerError(`${constants_1.KeyringControllerErrorMessage.NoKeyring}. Error info: ${errorInfo}`);
548
549
  }
549
550
  /**
550
551
  * Returns all keyrings of the given type.
@@ -594,7 +595,7 @@ class KeyringController extends base_controller_1.BaseController {
594
595
  case AccountImportStrategy.privateKey: {
595
596
  const [importedKey] = args;
596
597
  if (!importedKey) {
597
- throw new Error('Cannot import an empty key.');
598
+ throw new errors_1.KeyringControllerError('Cannot import an empty key.');
598
599
  }
599
600
  const prefixed = (0, utils_1.add0x)(importedKey);
600
601
  let bufferedPrivateKey;
@@ -602,12 +603,12 @@ class KeyringController extends base_controller_1.BaseController {
602
603
  bufferedPrivateKey = (0, utils_1.hexToBytes)(prefixed);
603
604
  }
604
605
  catch {
605
- throw new Error('Cannot import invalid private key.');
606
+ throw new errors_1.KeyringControllerError('Cannot import invalid private key.');
606
607
  }
607
608
  if (!(0, util_1.isValidPrivate)(bufferedPrivateKey) ||
608
609
  // ensures that the key is 64 bytes long
609
610
  (0, util_1.getBinarySize)(prefixed) !== 64 + '0x'.length) {
610
- throw new Error('Cannot import invalid private key.');
611
+ throw new errors_1.KeyringControllerError('Cannot import invalid private key.');
611
612
  }
612
613
  privateKey = (0, utils_1.remove0x)(prefixed);
613
614
  break;
@@ -625,7 +626,7 @@ class KeyringController extends base_controller_1.BaseController {
625
626
  break;
626
627
  }
627
628
  default:
628
- throw new Error(`Unexpected import strategy: '${String(strategy)}'`);
629
+ throw new errors_1.KeyringControllerError(`Unexpected import strategy: '${String(strategy)}'`);
629
630
  }
630
631
  const newKeyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.simple, [
631
632
  privateKey,
@@ -650,11 +651,11 @@ class KeyringController extends base_controller_1.BaseController {
650
651
  const shouldRemoveKeyring = (await keyring.getAccounts()).length === 1;
651
652
  // Primary keyring should never be removed, so we need to keep at least one account in it
652
653
  if (isPrimaryKeyring && shouldRemoveKeyring) {
653
- throw new Error(constants_1.KeyringControllerError.LastAccountInPrimaryKeyring);
654
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.LastAccountInPrimaryKeyring);
654
655
  }
655
656
  // Not all the keyrings support this, so we have to check
656
657
  if (!keyring.removeAccount) {
657
- throw new Error(constants_1.KeyringControllerError.UnsupportedRemoveAccount);
658
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedRemoveAccount);
658
659
  }
659
660
  // The `removeAccount` method of snaps keyring is async. We have to update
660
661
  // the interface of the other keyrings to be async as well.
@@ -696,12 +697,12 @@ class KeyringController extends base_controller_1.BaseController {
696
697
  async signMessage(messageParams) {
697
698
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
698
699
  if (!messageParams.data) {
699
- throw new Error("Can't sign an empty message");
700
+ throw new errors_1.KeyringControllerError("Can't sign an empty message");
700
701
  }
701
702
  const address = (0, eth_sig_util_1.normalize)(messageParams.from);
702
703
  const keyring = (await this.getKeyringForAccount(address));
703
704
  if (!keyring.signMessage) {
704
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignMessage);
705
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedSignMessage);
705
706
  }
706
707
  return await keyring.signMessage(address, messageParams.data);
707
708
  }
@@ -716,12 +717,12 @@ class KeyringController extends base_controller_1.BaseController {
716
717
  const from = (0, eth_sig_util_1.normalize)(params.from);
717
718
  const keyring = (await this.getKeyringForAccount(from));
718
719
  if (!keyring.signEip7702Authorization) {
719
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignEip7702Authorization);
720
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedSignEip7702Authorization);
720
721
  }
721
722
  const { chainId, nonce } = params;
722
723
  const contractAddress = (0, eth_sig_util_1.normalize)(params.contractAddress);
723
724
  if (contractAddress === undefined) {
724
- throw new Error(constants_1.KeyringControllerError.MissingEip7702AuthorizationContractAddress);
725
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.MissingEip7702AuthorizationContractAddress);
725
726
  }
726
727
  return await keyring.signEip7702Authorization(from, [
727
728
  chainId,
@@ -740,7 +741,7 @@ class KeyringController extends base_controller_1.BaseController {
740
741
  const address = (0, eth_sig_util_1.normalize)(messageParams.from);
741
742
  const keyring = (await this.getKeyringForAccount(address));
742
743
  if (!keyring.signPersonalMessage) {
743
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignPersonalMessage);
744
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedSignPersonalMessage);
744
745
  }
745
746
  const normalizedData = normalize(messageParams.data);
746
747
  return await keyring.signPersonalMessage(address, normalizedData);
@@ -761,14 +762,14 @@ class KeyringController extends base_controller_1.BaseController {
761
762
  SignTypedDataVersion.V3,
762
763
  SignTypedDataVersion.V4,
763
764
  ].includes(version)) {
764
- throw new Error(`Unexpected signTypedMessage version: '${version}'`);
765
+ throw new errors_1.KeyringControllerError(`Unexpected signTypedMessage version: '${version}'`);
765
766
  }
766
767
  // Cast to `Hex` here is safe here because `messageParams.from` is not nullish.
767
768
  // `normalize` returns `Hex` unless given a nullish value.
768
769
  const address = (0, eth_sig_util_1.normalize)(messageParams.from);
769
770
  const keyring = (await this.getKeyringForAccount(address));
770
771
  if (!keyring.signTypedData) {
771
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignTypedMessage);
772
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedSignTypedMessage);
772
773
  }
773
774
  return await keyring.signTypedData(address, version !== SignTypedDataVersion.V1 &&
774
775
  typeof messageParams.data === 'string'
@@ -776,9 +777,10 @@ class KeyringController extends base_controller_1.BaseController {
776
777
  : messageParams.data, { version });
777
778
  }
778
779
  catch (error) {
779
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
780
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
781
- throw new Error(`Keyring Controller signTypedMessage: ${error}`);
780
+ const errorMessage = error instanceof Error
781
+ ? `${error.name}: ${error.message}`
782
+ : String(error);
783
+ throw new errors_1.KeyringControllerError(`Keyring Controller signTypedMessage: ${errorMessage}`, error instanceof Error ? error : undefined);
782
784
  }
783
785
  }
784
786
  /**
@@ -794,7 +796,7 @@ class KeyringController extends base_controller_1.BaseController {
794
796
  const address = (0, eth_sig_util_1.normalize)(from);
795
797
  const keyring = (await this.getKeyringForAccount(address));
796
798
  if (!keyring.signTransaction) {
797
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignTransaction);
799
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedSignTransaction);
798
800
  }
799
801
  return await keyring.signTransaction(address, transaction, opts);
800
802
  }
@@ -811,7 +813,7 @@ class KeyringController extends base_controller_1.BaseController {
811
813
  const address = (0, eth_sig_util_1.normalize)(from);
812
814
  const keyring = (await this.getKeyringForAccount(address));
813
815
  if (!keyring.prepareUserOperation) {
814
- throw new Error(constants_1.KeyringControllerError.UnsupportedPrepareUserOperation);
816
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedPrepareUserOperation);
815
817
  }
816
818
  return await keyring.prepareUserOperation(address, transactions, executionContext);
817
819
  }
@@ -829,7 +831,7 @@ class KeyringController extends base_controller_1.BaseController {
829
831
  const address = (0, eth_sig_util_1.normalize)(from);
830
832
  const keyring = (await this.getKeyringForAccount(address));
831
833
  if (!keyring.patchUserOperation) {
832
- throw new Error(constants_1.KeyringControllerError.UnsupportedPatchUserOperation);
834
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedPatchUserOperation);
833
835
  }
834
836
  return await keyring.patchUserOperation(address, userOp, executionContext);
835
837
  }
@@ -846,7 +848,7 @@ class KeyringController extends base_controller_1.BaseController {
846
848
  const address = (0, eth_sig_util_1.normalize)(from);
847
849
  const keyring = (await this.getKeyringForAccount(address));
848
850
  if (!keyring.signUserOperation) {
849
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignUserOperation);
851
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedSignUserOperation);
850
852
  }
851
853
  return await keyring.signUserOperation(address, userOp, executionContext);
852
854
  }
@@ -974,7 +976,7 @@ class KeyringController extends base_controller_1.BaseController {
974
976
  keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
975
977
  }
976
978
  if (!keyring) {
977
- throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
979
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
978
980
  }
979
981
  const result = await operation({
980
982
  keyring,
@@ -985,7 +987,7 @@ class KeyringController extends base_controller_1.BaseController {
985
987
  // should be discouraged, as it can lead to unexpected behavior.
986
988
  // This error is thrown to prevent consumers using `withKeyring`
987
989
  // as a way to get a reference to a keyring instance.
988
- throw new Error(constants_1.KeyringControllerError.UnsafeDirectKeyringAccess);
990
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsafeDirectKeyringAccess);
989
991
  }
990
992
  return result;
991
993
  });
@@ -1028,7 +1030,7 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1028
1030
  }, _KeyringController_getKeyringMetadata = function _KeyringController_getKeyringMetadata(keyring) {
1029
1031
  const keyringWithMetadata = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find((candidate) => candidate.keyring === keyring);
1030
1032
  if (!keyringWithMetadata) {
1031
- throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
1033
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
1032
1034
  }
1033
1035
  return keyringWithMetadata.metadata;
1034
1036
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
@@ -1051,7 +1053,7 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1051
1053
  async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
1052
1054
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1053
1055
  if (typeof password !== 'string') {
1054
- throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
1056
+ throw new TypeError(constants_1.KeyringControllerErrorMessage.WrongPasswordType);
1055
1057
  }
1056
1058
  this.update((state) => {
1057
1059
  delete state.encryptionKey;
@@ -1087,7 +1089,7 @@ async function _KeyringController_deriveAndSetEncryptionKey(password, options =
1087
1089
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1088
1090
  const { vault } = this.state;
1089
1091
  if (typeof password !== 'string') {
1090
- throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
1092
+ throw new TypeError(constants_1.KeyringControllerErrorMessage.WrongPasswordType);
1091
1093
  }
1092
1094
  let serializedEncryptionKey, salt;
1093
1095
  if (vault && !options.ignoreExistingVault) {
@@ -1111,11 +1113,11 @@ async function _KeyringController_deriveAndSetEncryptionKey(password, options =
1111
1113
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1112
1114
  if (typeof encryptionKey !== 'string' ||
1113
1115
  typeof keyDerivationSalt !== 'string') {
1114
- throw new TypeError(constants_1.KeyringControllerError.WrongEncryptionKeyType);
1116
+ throw new TypeError(constants_1.KeyringControllerErrorMessage.WrongEncryptionKeyType);
1115
1117
  }
1116
1118
  const { vault } = this.state;
1117
1119
  if (vault && JSON.parse(vault).salt !== keyDerivationSalt) {
1118
- throw new Error(constants_1.KeyringControllerError.ExpiredCredentials);
1120
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.ExpiredCredentials);
1119
1121
  }
1120
1122
  __classPrivateFieldSet(this, _KeyringController_encryptionKey, {
1121
1123
  salt: keyDerivationSalt,
@@ -1132,17 +1134,17 @@ async function _KeyringController_verifySeedPhrase(keyringId) {
1132
1134
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1133
1135
  const keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringByIdOrDefault).call(this, keyringId);
1134
1136
  if (!keyring) {
1135
- throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
1137
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.KeyringNotFound);
1136
1138
  }
1137
1139
  if (keyring.type !== KeyringTypes.hd) {
1138
- throw new Error(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
1140
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedVerifySeedPhrase);
1139
1141
  }
1140
1142
  assertHasUint8ArrayMnemonic(keyring);
1141
1143
  const seedWords = keyring.mnemonic;
1142
1144
  const accounts = await keyring.getAccounts();
1143
1145
  /* istanbul ignore if */
1144
1146
  if (accounts.length === 0) {
1145
- throw new Error('Cannot verify an empty keyring.');
1147
+ throw new errors_1.KeyringControllerError('Cannot verify an empty keyring.');
1146
1148
  }
1147
1149
  // The HD Keyring Builder is a default keyring builder
1148
1150
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -1157,12 +1159,12 @@ async function _KeyringController_verifySeedPhrase(keyringId) {
1157
1159
  const testAccounts = await hdKeyring.getAccounts();
1158
1160
  /* istanbul ignore if */
1159
1161
  if (testAccounts.length !== accounts.length) {
1160
- throw new Error('Seed phrase imported incorrect number of accounts.');
1162
+ throw new errors_1.KeyringControllerError('Seed phrase imported incorrect number of accounts.');
1161
1163
  }
1162
1164
  testAccounts.forEach((account, i) => {
1163
1165
  /* istanbul ignore if */
1164
1166
  if (account.toLowerCase() !== accounts[i].toLowerCase()) {
1165
- throw new Error('Seed phrase imported different accounts.');
1167
+ throw new errors_1.KeyringControllerError('Seed phrase imported different accounts.');
1166
1168
  }
1167
1169
  });
1168
1170
  return seedWords;
@@ -1243,7 +1245,7 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1243
1245
  async function _KeyringController_unlockKeyrings(credentials) {
1244
1246
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1245
1247
  if (!this.state.vault) {
1246
- throw new Error(constants_1.KeyringControllerError.VaultError);
1248
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.VaultError);
1247
1249
  }
1248
1250
  const parsedEncryptedVault = JSON.parse(this.state.vault);
1249
1251
  if ('password' in credentials) {
@@ -1254,12 +1256,12 @@ async function _KeyringController_unlockKeyrings(credentials) {
1254
1256
  }
1255
1257
  const encryptionKey = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.serialized;
1256
1258
  if (!encryptionKey) {
1257
- throw new Error(constants_1.KeyringControllerError.MissingCredentials);
1259
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.MissingCredentials);
1258
1260
  }
1259
1261
  const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1260
1262
  const vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithKey(key, parsedEncryptedVault);
1261
1263
  if (!isSerializedKeyringsArray(vault)) {
1262
- throw new Error(constants_1.KeyringControllerError.VaultDataError);
1264
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.VaultDataError);
1263
1265
  }
1264
1266
  const { keyrings, newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, vault);
1265
1267
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
@@ -1275,11 +1277,11 @@ async function _KeyringController_unlockKeyrings(credentials) {
1275
1277
  // Ensure no duplicate accounts are persisted.
1276
1278
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this);
1277
1279
  if (!__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")) {
1278
- throw new Error(constants_1.KeyringControllerError.MissingCredentials);
1280
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.MissingCredentials);
1279
1281
  }
1280
1282
  const serializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1281
1283
  if (!serializedKeyrings.some((keyring) => keyring.type === KeyringTypes.hd)) {
1282
- throw new Error(constants_1.KeyringControllerError.NoHdKeyring);
1284
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.NoHdKeyring);
1283
1285
  }
1284
1286
  const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").serialized);
1285
1287
  const encryptedVault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithKey(key, serializedKeyrings);
@@ -1338,7 +1340,7 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1338
1340
  const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts);
1339
1341
  const [firstAccount] = await keyring.getAccounts();
1340
1342
  if (!firstAccount) {
1341
- throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
1343
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.NoFirstAccount);
1342
1344
  }
1343
1345
  return firstAccount;
1344
1346
  }, _KeyringController_newKeyring =
@@ -1381,7 +1383,7 @@ async function _KeyringController_createKeyring(type, data) {
1381
1383
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1382
1384
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1383
1385
  if (!keyringBuilder) {
1384
- throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
1386
+ throw new errors_1.KeyringControllerError(`${constants_1.KeyringControllerErrorMessage.NoKeyringBuilder}. Keyring type: ${type}`);
1385
1387
  }
1386
1388
  const keyring = keyringBuilder();
1387
1389
  if (data) {
@@ -1391,9 +1393,10 @@ async function _KeyringController_createKeyring(type, data) {
1391
1393
  if (keyring.init) {
1392
1394
  await keyring.init();
1393
1395
  }
1394
- if (type === KeyringTypes.hd && (!(0, utils_1.isObject)(data) || !data.mnemonic)) {
1396
+ if (type === KeyringTypes.hd &&
1397
+ (!(0, utils_1.isObject)(data) || !data.mnemonic)) {
1395
1398
  if (!keyring.generateRandomMnemonic) {
1396
- throw new Error(constants_1.KeyringControllerError.UnsupportedGenerateRandomMnemonic);
1399
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.UnsupportedGenerateRandomMnemonic);
1397
1400
  }
1398
1401
  // NOTE: Not all keyrings implement this method in a asynchronous-way. Using `await` for
1399
1402
  // non-thenable will still be valid (despite not being really useful). It allows us to cover both
@@ -1494,7 +1497,7 @@ async function _KeyringController_removeEmptyKeyrings() {
1494
1497
  async function _KeyringController_assertNoDuplicateAccounts(additionalKeyrings = []) {
1495
1498
  const accounts = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this, additionalKeyrings);
1496
1499
  if (new Set(accounts).size !== accounts.length) {
1497
- throw new Error(constants_1.KeyringControllerError.DuplicatedAccount);
1500
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.DuplicatedAccount);
1498
1501
  }
1499
1502
  }, _KeyringController_setUnlocked = function _KeyringController_setUnlocked() {
1500
1503
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
@@ -1504,7 +1507,7 @@ async function _KeyringController_assertNoDuplicateAccounts(additionalKeyrings =
1504
1507
  this.messenger.publish(`${name}:unlock`);
1505
1508
  }, _KeyringController_assertIsUnlocked = function _KeyringController_assertIsUnlocked() {
1506
1509
  if (!this.state.isUnlocked) {
1507
- throw new Error(constants_1.KeyringControllerError.ControllerLocked);
1510
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.ControllerLocked);
1508
1511
  }
1509
1512
  }, _KeyringController_persistOrRollback =
1510
1513
  /**
@@ -1550,7 +1553,7 @@ async function _KeyringController_withRollback(callback) {
1550
1553
  });
1551
1554
  }, _KeyringController_assertControllerMutexIsLocked = function _KeyringController_assertControllerMutexIsLocked() {
1552
1555
  if (!__classPrivateFieldGet(this, _KeyringController_controllerOperationMutex, "f").isLocked()) {
1553
- throw new Error(constants_1.KeyringControllerError.ControllerLockRequired);
1556
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerErrorMessage.ControllerLockRequired);
1554
1557
  }
1555
1558
  }, _KeyringController_withControllerLock =
1556
1559
  /**