@metamask-previews/keyring-controller 25.0.0-preview-7bc2d97e → 25.0.0-preview-6bed60a6

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
@@ -13,10 +13,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
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
15
 
16
- ### Changed
17
-
18
- - Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
19
-
20
16
  ## [25.0.0]
21
17
 
22
18
  ### Added
@@ -51,15 +51,13 @@ 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
57
58
  */
58
59
  var KeyringTypes;
59
60
  (function (KeyringTypes) {
60
- // Changing this would be a breaking change, and not worth the effort at this
61
- // time, so we disable the linting rule for this block.
62
- /* eslint-disable @typescript-eslint/naming-convention */
63
61
  KeyringTypes["simple"] = "Simple Key Pair";
64
62
  KeyringTypes["hd"] = "HD Key Tree";
65
63
  KeyringTypes["qr"] = "QR Hardware Wallet Device";
@@ -68,7 +66,6 @@ var KeyringTypes;
68
66
  KeyringTypes["ledger"] = "Ledger Hardware";
69
67
  KeyringTypes["lattice"] = "Lattice Hardware";
70
68
  KeyringTypes["snap"] = "Snap Keyring";
71
- /* eslint-enable @typescript-eslint/naming-convention */
72
69
  })(KeyringTypes || (exports.KeyringTypes = KeyringTypes = {}));
73
70
  /**
74
71
  * Custody keyring types are a special case, as they are not a single type
@@ -86,12 +83,8 @@ exports.isCustodyKeyring = isCustodyKeyring;
86
83
  */
87
84
  var AccountImportStrategy;
88
85
  (function (AccountImportStrategy) {
89
- // Changing this would be a breaking change, and not worth the effort at this
90
- // time, so we disable the linting rule for this block.
91
- /* eslint-disable @typescript-eslint/naming-convention */
92
86
  AccountImportStrategy["privateKey"] = "privateKey";
93
87
  AccountImportStrategy["json"] = "json";
94
- /* eslint-enable @typescript-eslint/naming-convention */
95
88
  })(AccountImportStrategy || (exports.AccountImportStrategy = AccountImportStrategy = {}));
96
89
  /**
97
90
  * The `signTypedMessage` version
@@ -140,7 +133,7 @@ exports.getDefaultKeyringState = getDefaultKeyringState;
140
133
  */
141
134
  function assertHasUint8ArrayMnemonic(keyring) {
142
135
  if (!((0, utils_1.hasProperty)(keyring, 'mnemonic') && keyring.mnemonic instanceof Uint8Array)) {
143
- throw new Error("Can't get mnemonic bytes from keyring");
136
+ throw new errors_1.KeyringControllerError("Can't get mnemonic bytes from keyring");
144
137
  }
145
138
  }
146
139
  /**
@@ -151,10 +144,10 @@ function assertHasUint8ArrayMnemonic(keyring) {
151
144
  */
152
145
  function assertIsValidPassword(password) {
153
146
  if (typeof password !== 'string') {
154
- throw new Error(constants_1.KeyringControllerError.WrongPasswordType);
147
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.WrongPasswordType);
155
148
  }
156
149
  if (!password?.length) {
157
- throw new Error(constants_1.KeyringControllerError.InvalidEmptyPassword);
150
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.InvalidEmptyPassword);
158
151
  }
159
152
  }
160
153
  /**
@@ -165,7 +158,7 @@ function assertIsValidPassword(password) {
165
158
  */
166
159
  function assertIsEncryptionKeySet(encryptionKey) {
167
160
  if (!encryptionKey) {
168
- throw new Error(constants_1.KeyringControllerError.EncryptionKeyNotSet);
161
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.EncryptionKeyNotSet);
169
162
  }
170
163
  }
171
164
  /**
@@ -318,17 +311,17 @@ class KeyringController extends base_controller_1.BaseController {
318
311
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
319
312
  const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
320
313
  if (!primaryKeyring) {
321
- throw new Error('No HD keyring found');
314
+ throw new errors_1.KeyringControllerError('No HD keyring found');
322
315
  }
323
316
  const oldAccounts = await primaryKeyring.getAccounts();
324
317
  if (accountCount && oldAccounts.length !== accountCount) {
325
318
  if (accountCount > oldAccounts.length) {
326
- throw new Error('Account out of sequence');
319
+ throw new errors_1.KeyringControllerError('Account out of sequence');
327
320
  }
328
321
  // we return the account already existing at index `accountCount`
329
322
  const existingAccount = oldAccounts[accountCount];
330
323
  if (!existingAccount) {
331
- throw new Error(`Can't find account at index ${accountCount}`);
324
+ throw new errors_1.KeyringControllerError(`Can't find account at index ${accountCount}`);
332
325
  }
333
326
  return existingAccount;
334
327
  }
@@ -354,7 +347,7 @@ class KeyringController extends base_controller_1.BaseController {
354
347
  const oldAccounts = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this);
355
348
  if (accountCount && oldAccounts.length !== accountCount) {
356
349
  if (accountCount > oldAccounts.length) {
357
- throw new Error('Account out of sequence');
350
+ throw new errors_1.KeyringControllerError('Account out of sequence');
358
351
  }
359
352
  const existingAccount = oldAccounts[accountCount];
360
353
  (0, utils_1.assertIsStrictHexString)(existingAccount);
@@ -426,7 +419,7 @@ class KeyringController extends base_controller_1.BaseController {
426
419
  */
427
420
  async verifyPassword(password) {
428
421
  if (!this.state.vault) {
429
- throw new Error(constants_1.KeyringControllerError.VaultError);
422
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.VaultError);
430
423
  }
431
424
  await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decrypt(password, this.state.vault);
432
425
  }
@@ -450,7 +443,7 @@ class KeyringController extends base_controller_1.BaseController {
450
443
  await this.verifyPassword(password);
451
444
  const selectedKeyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringByIdOrDefault).call(this, keyringId);
452
445
  if (!selectedKeyring) {
453
- throw new Error('Keyring not found');
446
+ throw new errors_1.KeyringControllerError('Keyring not found');
454
447
  }
455
448
  assertHasUint8ArrayMnemonic(selectedKeyring);
456
449
  return selectedKeyring.mnemonic;
@@ -466,7 +459,7 @@ class KeyringController extends base_controller_1.BaseController {
466
459
  await this.verifyPassword(password);
467
460
  const keyring = (await this.getKeyringForAccount(address));
468
461
  if (!keyring.exportAccount) {
469
- throw new Error(constants_1.KeyringControllerError.UnsupportedExportAccount);
462
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedExportAccount);
470
463
  }
471
464
  return await keyring.exportAccount(normalize(address));
472
465
  }
@@ -492,7 +485,7 @@ class KeyringController extends base_controller_1.BaseController {
492
485
  const address = (0, eth_sig_util_1.normalize)(account);
493
486
  const keyring = (await this.getKeyringForAccount(account));
494
487
  if (!keyring.getEncryptionPublicKey) {
495
- throw new Error(constants_1.KeyringControllerError.UnsupportedGetEncryptionPublicKey);
488
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedGetEncryptionPublicKey);
496
489
  }
497
490
  return await keyring.getEncryptionPublicKey(address, opts);
498
491
  }
@@ -509,7 +502,7 @@ class KeyringController extends base_controller_1.BaseController {
509
502
  const address = (0, eth_sig_util_1.normalize)(messageParams.from);
510
503
  const keyring = (await this.getKeyringForAccount(address));
511
504
  if (!keyring.decryptMessage) {
512
- throw new Error(constants_1.KeyringControllerError.UnsupportedDecryptMessage);
505
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedDecryptMessage);
513
506
  }
514
507
  return keyring.decryptMessage(address, messageParams.data);
515
508
  }
@@ -544,7 +537,7 @@ class KeyringController extends base_controller_1.BaseController {
544
537
  else if (!winners.length) {
545
538
  errorInfo = 'There are keyrings, but none match the address';
546
539
  }
547
- throw new Error(`${constants_1.KeyringControllerError.NoKeyring}. Error info: ${errorInfo}`);
540
+ throw new errors_1.KeyringControllerError(`${constants_1.KeyringControllerError.NoKeyring}. Error info: ${errorInfo}`);
548
541
  }
549
542
  /**
550
543
  * Returns all keyrings of the given type.
@@ -594,7 +587,7 @@ class KeyringController extends base_controller_1.BaseController {
594
587
  case AccountImportStrategy.privateKey: {
595
588
  const [importedKey] = args;
596
589
  if (!importedKey) {
597
- throw new Error('Cannot import an empty key.');
590
+ throw new errors_1.KeyringControllerError('Cannot import an empty key.');
598
591
  }
599
592
  const prefixed = (0, utils_1.add0x)(importedKey);
600
593
  let bufferedPrivateKey;
@@ -602,12 +595,12 @@ class KeyringController extends base_controller_1.BaseController {
602
595
  bufferedPrivateKey = (0, utils_1.hexToBytes)(prefixed);
603
596
  }
604
597
  catch {
605
- throw new Error('Cannot import invalid private key.');
598
+ throw new errors_1.KeyringControllerError('Cannot import invalid private key.');
606
599
  }
607
600
  if (!(0, util_1.isValidPrivate)(bufferedPrivateKey) ||
608
601
  // ensures that the key is 64 bytes long
609
602
  (0, util_1.getBinarySize)(prefixed) !== 64 + '0x'.length) {
610
- throw new Error('Cannot import invalid private key.');
603
+ throw new errors_1.KeyringControllerError('Cannot import invalid private key.');
611
604
  }
612
605
  privateKey = (0, utils_1.remove0x)(prefixed);
613
606
  break;
@@ -625,7 +618,7 @@ class KeyringController extends base_controller_1.BaseController {
625
618
  break;
626
619
  }
627
620
  default:
628
- throw new Error(`Unexpected import strategy: '${String(strategy)}'`);
621
+ throw new errors_1.KeyringControllerError(`Unexpected import strategy: '${String(strategy)}'`);
629
622
  }
630
623
  const newKeyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.simple, [
631
624
  privateKey,
@@ -650,11 +643,11 @@ class KeyringController extends base_controller_1.BaseController {
650
643
  const shouldRemoveKeyring = (await keyring.getAccounts()).length === 1;
651
644
  // Primary keyring should never be removed, so we need to keep at least one account in it
652
645
  if (isPrimaryKeyring && shouldRemoveKeyring) {
653
- throw new Error(constants_1.KeyringControllerError.LastAccountInPrimaryKeyring);
646
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.LastAccountInPrimaryKeyring);
654
647
  }
655
648
  // Not all the keyrings support this, so we have to check
656
649
  if (!keyring.removeAccount) {
657
- throw new Error(constants_1.KeyringControllerError.UnsupportedRemoveAccount);
650
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedRemoveAccount);
658
651
  }
659
652
  // The `removeAccount` method of snaps keyring is async. We have to update
660
653
  // the interface of the other keyrings to be async as well.
@@ -696,12 +689,12 @@ class KeyringController extends base_controller_1.BaseController {
696
689
  async signMessage(messageParams) {
697
690
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
698
691
  if (!messageParams.data) {
699
- throw new Error("Can't sign an empty message");
692
+ throw new errors_1.KeyringControllerError("Can't sign an empty message");
700
693
  }
701
694
  const address = (0, eth_sig_util_1.normalize)(messageParams.from);
702
695
  const keyring = (await this.getKeyringForAccount(address));
703
696
  if (!keyring.signMessage) {
704
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignMessage);
697
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedSignMessage);
705
698
  }
706
699
  return await keyring.signMessage(address, messageParams.data);
707
700
  }
@@ -716,12 +709,12 @@ class KeyringController extends base_controller_1.BaseController {
716
709
  const from = (0, eth_sig_util_1.normalize)(params.from);
717
710
  const keyring = (await this.getKeyringForAccount(from));
718
711
  if (!keyring.signEip7702Authorization) {
719
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignEip7702Authorization);
712
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedSignEip7702Authorization);
720
713
  }
721
714
  const { chainId, nonce } = params;
722
715
  const contractAddress = (0, eth_sig_util_1.normalize)(params.contractAddress);
723
716
  if (contractAddress === undefined) {
724
- throw new Error(constants_1.KeyringControllerError.MissingEip7702AuthorizationContractAddress);
717
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.MissingEip7702AuthorizationContractAddress);
725
718
  }
726
719
  return await keyring.signEip7702Authorization(from, [
727
720
  chainId,
@@ -740,7 +733,7 @@ class KeyringController extends base_controller_1.BaseController {
740
733
  const address = (0, eth_sig_util_1.normalize)(messageParams.from);
741
734
  const keyring = (await this.getKeyringForAccount(address));
742
735
  if (!keyring.signPersonalMessage) {
743
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignPersonalMessage);
736
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedSignPersonalMessage);
744
737
  }
745
738
  const normalizedData = normalize(messageParams.data);
746
739
  return await keyring.signPersonalMessage(address, normalizedData);
@@ -761,14 +754,14 @@ class KeyringController extends base_controller_1.BaseController {
761
754
  SignTypedDataVersion.V3,
762
755
  SignTypedDataVersion.V4,
763
756
  ].includes(version)) {
764
- throw new Error(`Unexpected signTypedMessage version: '${version}'`);
757
+ throw new errors_1.KeyringControllerError(`Unexpected signTypedMessage version: '${version}'`);
765
758
  }
766
759
  // Cast to `Hex` here is safe here because `messageParams.from` is not nullish.
767
760
  // `normalize` returns `Hex` unless given a nullish value.
768
761
  const address = (0, eth_sig_util_1.normalize)(messageParams.from);
769
762
  const keyring = (await this.getKeyringForAccount(address));
770
763
  if (!keyring.signTypedData) {
771
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignTypedMessage);
764
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedSignTypedMessage);
772
765
  }
773
766
  return await keyring.signTypedData(address, version !== SignTypedDataVersion.V1 &&
774
767
  typeof messageParams.data === 'string'
@@ -776,9 +769,10 @@ class KeyringController extends base_controller_1.BaseController {
776
769
  : messageParams.data, { version });
777
770
  }
778
771
  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}`);
772
+ const errorMessage = error instanceof Error
773
+ ? `${error.name}: ${error.message}`
774
+ : String(error);
775
+ throw new errors_1.KeyringControllerError(`Keyring Controller signTypedMessage: ${errorMessage}`, error instanceof Error ? error : undefined);
782
776
  }
783
777
  }
784
778
  /**
@@ -794,7 +788,7 @@ class KeyringController extends base_controller_1.BaseController {
794
788
  const address = (0, eth_sig_util_1.normalize)(from);
795
789
  const keyring = (await this.getKeyringForAccount(address));
796
790
  if (!keyring.signTransaction) {
797
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignTransaction);
791
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedSignTransaction);
798
792
  }
799
793
  return await keyring.signTransaction(address, transaction, opts);
800
794
  }
@@ -811,7 +805,7 @@ class KeyringController extends base_controller_1.BaseController {
811
805
  const address = (0, eth_sig_util_1.normalize)(from);
812
806
  const keyring = (await this.getKeyringForAccount(address));
813
807
  if (!keyring.prepareUserOperation) {
814
- throw new Error(constants_1.KeyringControllerError.UnsupportedPrepareUserOperation);
808
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedPrepareUserOperation);
815
809
  }
816
810
  return await keyring.prepareUserOperation(address, transactions, executionContext);
817
811
  }
@@ -829,7 +823,7 @@ class KeyringController extends base_controller_1.BaseController {
829
823
  const address = (0, eth_sig_util_1.normalize)(from);
830
824
  const keyring = (await this.getKeyringForAccount(address));
831
825
  if (!keyring.patchUserOperation) {
832
- throw new Error(constants_1.KeyringControllerError.UnsupportedPatchUserOperation);
826
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedPatchUserOperation);
833
827
  }
834
828
  return await keyring.patchUserOperation(address, userOp, executionContext);
835
829
  }
@@ -846,7 +840,7 @@ class KeyringController extends base_controller_1.BaseController {
846
840
  const address = (0, eth_sig_util_1.normalize)(from);
847
841
  const keyring = (await this.getKeyringForAccount(address));
848
842
  if (!keyring.signUserOperation) {
849
- throw new Error(constants_1.KeyringControllerError.UnsupportedSignUserOperation);
843
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedSignUserOperation);
850
844
  }
851
845
  return await keyring.signUserOperation(address, userOp, executionContext);
852
846
  }
@@ -974,7 +968,7 @@ class KeyringController extends base_controller_1.BaseController {
974
968
  keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringById).call(this, selector.id);
975
969
  }
976
970
  if (!keyring) {
977
- throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
971
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.KeyringNotFound);
978
972
  }
979
973
  const result = await operation({
980
974
  keyring,
@@ -985,7 +979,7 @@ class KeyringController extends base_controller_1.BaseController {
985
979
  // should be discouraged, as it can lead to unexpected behavior.
986
980
  // This error is thrown to prevent consumers using `withKeyring`
987
981
  // as a way to get a reference to a keyring instance.
988
- throw new Error(constants_1.KeyringControllerError.UnsafeDirectKeyringAccess);
982
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsafeDirectKeyringAccess);
989
983
  }
990
984
  return result;
991
985
  });
@@ -1028,7 +1022,7 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1028
1022
  }, _KeyringController_getKeyringMetadata = function _KeyringController_getKeyringMetadata(keyring) {
1029
1023
  const keyringWithMetadata = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find((candidate) => candidate.keyring === keyring);
1030
1024
  if (!keyringWithMetadata) {
1031
- throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
1025
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.KeyringNotFound);
1032
1026
  }
1033
1027
  return keyringWithMetadata.metadata;
1034
1028
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
@@ -1115,7 +1109,7 @@ async function _KeyringController_deriveAndSetEncryptionKey(password, options =
1115
1109
  }
1116
1110
  const { vault } = this.state;
1117
1111
  if (vault && JSON.parse(vault).salt !== keyDerivationSalt) {
1118
- throw new Error(constants_1.KeyringControllerError.ExpiredCredentials);
1112
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.ExpiredCredentials);
1119
1113
  }
1120
1114
  __classPrivateFieldSet(this, _KeyringController_encryptionKey, {
1121
1115
  salt: keyDerivationSalt,
@@ -1132,17 +1126,17 @@ async function _KeyringController_verifySeedPhrase(keyringId) {
1132
1126
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1133
1127
  const keyring = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringByIdOrDefault).call(this, keyringId);
1134
1128
  if (!keyring) {
1135
- throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
1129
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.KeyringNotFound);
1136
1130
  }
1137
1131
  if (keyring.type !== KeyringTypes.hd) {
1138
- throw new Error(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
1132
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedVerifySeedPhrase);
1139
1133
  }
1140
1134
  assertHasUint8ArrayMnemonic(keyring);
1141
1135
  const seedWords = keyring.mnemonic;
1142
1136
  const accounts = await keyring.getAccounts();
1143
1137
  /* istanbul ignore if */
1144
1138
  if (accounts.length === 0) {
1145
- throw new Error('Cannot verify an empty keyring.');
1139
+ throw new errors_1.KeyringControllerError('Cannot verify an empty keyring.');
1146
1140
  }
1147
1141
  // The HD Keyring Builder is a default keyring builder
1148
1142
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -1157,12 +1151,12 @@ async function _KeyringController_verifySeedPhrase(keyringId) {
1157
1151
  const testAccounts = await hdKeyring.getAccounts();
1158
1152
  /* istanbul ignore if */
1159
1153
  if (testAccounts.length !== accounts.length) {
1160
- throw new Error('Seed phrase imported incorrect number of accounts.');
1154
+ throw new errors_1.KeyringControllerError('Seed phrase imported incorrect number of accounts.');
1161
1155
  }
1162
1156
  testAccounts.forEach((account, i) => {
1163
1157
  /* istanbul ignore if */
1164
1158
  if (account.toLowerCase() !== accounts[i].toLowerCase()) {
1165
- throw new Error('Seed phrase imported different accounts.');
1159
+ throw new errors_1.KeyringControllerError('Seed phrase imported different accounts.');
1166
1160
  }
1167
1161
  });
1168
1162
  return seedWords;
@@ -1243,7 +1237,7 @@ async function _KeyringController_restoreSerializedKeyrings(serializedKeyrings)
1243
1237
  async function _KeyringController_unlockKeyrings(credentials) {
1244
1238
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withVaultLock).call(this, async () => {
1245
1239
  if (!this.state.vault) {
1246
- throw new Error(constants_1.KeyringControllerError.VaultError);
1240
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.VaultError);
1247
1241
  }
1248
1242
  const parsedEncryptedVault = JSON.parse(this.state.vault);
1249
1243
  if ('password' in credentials) {
@@ -1254,12 +1248,12 @@ async function _KeyringController_unlockKeyrings(credentials) {
1254
1248
  }
1255
1249
  const encryptionKey = __classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")?.serialized;
1256
1250
  if (!encryptionKey) {
1257
- throw new Error(constants_1.KeyringControllerError.MissingCredentials);
1251
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.MissingCredentials);
1258
1252
  }
1259
1253
  const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(encryptionKey);
1260
1254
  const vault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithKey(key, parsedEncryptedVault);
1261
1255
  if (!isSerializedKeyringsArray(vault)) {
1262
- throw new Error(constants_1.KeyringControllerError.VaultDataError);
1256
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.VaultDataError);
1263
1257
  }
1264
1258
  const { keyrings, newMetadata } = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_restoreSerializedKeyrings).call(this, vault);
1265
1259
  const updatedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getUpdatedKeyrings).call(this);
@@ -1275,11 +1269,11 @@ async function _KeyringController_unlockKeyrings(credentials) {
1275
1269
  // Ensure no duplicate accounts are persisted.
1276
1270
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertNoDuplicateAccounts).call(this);
1277
1271
  if (!__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f")) {
1278
- throw new Error(constants_1.KeyringControllerError.MissingCredentials);
1272
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.MissingCredentials);
1279
1273
  }
1280
1274
  const serializedKeyrings = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getSerializedKeyrings).call(this);
1281
1275
  if (!serializedKeyrings.some((keyring) => keyring.type === KeyringTypes.hd)) {
1282
- throw new Error(constants_1.KeyringControllerError.NoHdKeyring);
1276
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.NoHdKeyring);
1283
1277
  }
1284
1278
  const key = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").importKey(__classPrivateFieldGet(this, _KeyringController_encryptionKey, "f").serialized);
1285
1279
  const encryptedVault = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").encryptWithKey(key, serializedKeyrings);
@@ -1338,7 +1332,7 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
1338
1332
  const keyring = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts);
1339
1333
  const [firstAccount] = await keyring.getAccounts();
1340
1334
  if (!firstAccount) {
1341
- throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
1335
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.NoFirstAccount);
1342
1336
  }
1343
1337
  return firstAccount;
1344
1338
  }, _KeyringController_newKeyring =
@@ -1381,7 +1375,7 @@ async function _KeyringController_createKeyring(type, data) {
1381
1375
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1382
1376
  const keyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, type);
1383
1377
  if (!keyringBuilder) {
1384
- throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
1378
+ throw new errors_1.KeyringControllerError(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
1385
1379
  }
1386
1380
  const keyring = keyringBuilder();
1387
1381
  if (data) {
@@ -1391,9 +1385,10 @@ async function _KeyringController_createKeyring(type, data) {
1391
1385
  if (keyring.init) {
1392
1386
  await keyring.init();
1393
1387
  }
1394
- if (type === KeyringTypes.hd && (!(0, utils_1.isObject)(data) || !data.mnemonic)) {
1388
+ if (type === KeyringTypes.hd &&
1389
+ (!(0, utils_1.isObject)(data) || !data.mnemonic)) {
1395
1390
  if (!keyring.generateRandomMnemonic) {
1396
- throw new Error(constants_1.KeyringControllerError.UnsupportedGenerateRandomMnemonic);
1391
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.UnsupportedGenerateRandomMnemonic);
1397
1392
  }
1398
1393
  // NOTE: Not all keyrings implement this method in a asynchronous-way. Using `await` for
1399
1394
  // non-thenable will still be valid (despite not being really useful). It allows us to cover both
@@ -1494,7 +1489,7 @@ async function _KeyringController_removeEmptyKeyrings() {
1494
1489
  async function _KeyringController_assertNoDuplicateAccounts(additionalKeyrings = []) {
1495
1490
  const accounts = await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this, additionalKeyrings);
1496
1491
  if (new Set(accounts).size !== accounts.length) {
1497
- throw new Error(constants_1.KeyringControllerError.DuplicatedAccount);
1492
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.DuplicatedAccount);
1498
1493
  }
1499
1494
  }, _KeyringController_setUnlocked = function _KeyringController_setUnlocked() {
1500
1495
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
@@ -1504,7 +1499,7 @@ async function _KeyringController_assertNoDuplicateAccounts(additionalKeyrings =
1504
1499
  this.messenger.publish(`${name}:unlock`);
1505
1500
  }, _KeyringController_assertIsUnlocked = function _KeyringController_assertIsUnlocked() {
1506
1501
  if (!this.state.isUnlocked) {
1507
- throw new Error(constants_1.KeyringControllerError.ControllerLocked);
1502
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.ControllerLocked);
1508
1503
  }
1509
1504
  }, _KeyringController_persistOrRollback =
1510
1505
  /**
@@ -1550,7 +1545,7 @@ async function _KeyringController_withRollback(callback) {
1550
1545
  });
1551
1546
  }, _KeyringController_assertControllerMutexIsLocked = function _KeyringController_assertControllerMutexIsLocked() {
1552
1547
  if (!__classPrivateFieldGet(this, _KeyringController_controllerOperationMutex, "f").isLocked()) {
1553
- throw new Error(constants_1.KeyringControllerError.ControllerLockRequired);
1548
+ throw new errors_1.KeyringControllerError(constants_1.KeyringControllerError.ControllerLockRequired);
1554
1549
  }
1555
1550
  }, _KeyringController_withControllerLock =
1556
1551
  /**