@metamask-previews/keyring-controller 22.0.2-preview-3636a128 → 22.0.2-preview-d7c00b1

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.
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_getSessionState, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_isNewEncryptionAvailable, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_assertNoDuplicateAccounts, _KeyringController_setUnlocked, _KeyringController_assertIsUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
12
+ var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringById, _KeyringController_getKeyringByIdOrDefault, _KeyringController_getKeyringMetadata, _KeyringController_getKeyringBuilderForType, _KeyringController_createNewVaultWithKeyring, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_getSessionState, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_isNewEncryptionAvailable, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_createKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_assertNoDuplicateAccounts, _KeyringController_setUnlocked, _KeyringController_assertIsUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
@@ -145,6 +145,17 @@ function assertIsValidPassword(password) {
145
145
  throw new Error(KeyringControllerError.InvalidEmptyPassword);
146
146
  }
147
147
  }
148
+ /**
149
+ * Assert that the provided encryption key is a valid non-empty string.
150
+ *
151
+ * @param encryptionKey - The encryption key to check.
152
+ * @throws If the encryption key is not a valid string.
153
+ */
154
+ function assertIsEncryptionKeySet(encryptionKey) {
155
+ if (!encryptionKey) {
156
+ throw new Error(KeyringControllerError.EncryptionKeyNotSet);
157
+ }
158
+ }
148
159
  /**
149
160
  * Checks if the provided value is a serialized keyrings array.
150
161
  *
@@ -251,7 +262,6 @@ export class KeyringController extends BaseController {
251
262
  _KeyringController_keyrings.set(this, void 0);
252
263
  _KeyringController_unsupportedKeyrings.set(this, void 0);
253
264
  _KeyringController_password.set(this, void 0);
254
- _KeyringController_qrKeyringStateListener.set(this, void 0);
255
265
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
256
266
  ? keyringBuilders.concat(defaultKeyringBuilders)
257
267
  : defaultKeyringBuilders, "f");
@@ -376,9 +386,6 @@ export class KeyringController extends BaseController {
376
386
  */
377
387
  async addNewKeyring(type, opts) {
378
388
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
379
- if (type === KeyringTypes.qr) {
380
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, await this.getOrAddQRKeyring());
381
- }
382
389
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, type, opts)));
383
390
  }
384
391
  /**
@@ -637,7 +644,6 @@ export class KeyringController extends BaseController {
637
644
  async setLocked() {
638
645
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
639
646
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
640
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unsubscribeFromQRKeyringsEvents).call(this);
641
647
  __classPrivateFieldSet(this, _KeyringController_password, undefined, "f");
642
648
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
643
649
  this.update((state) => {
@@ -820,6 +826,10 @@ export class KeyringController extends BaseController {
820
826
  */
821
827
  changePassword(password) {
822
828
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
829
+ // If the password is the same, do nothing.
830
+ if (__classPrivateFieldGet(this, _KeyringController_password, "f") === password) {
831
+ return Promise.resolve();
832
+ }
823
833
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
824
834
  assertIsValidPassword(password);
825
835
  __classPrivateFieldSet(this, _KeyringController_password, password, "f");
@@ -871,27 +881,11 @@ export class KeyringController extends BaseController {
871
881
  */
872
882
  async exportEncryptionKey() {
873
883
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
874
- // There is a case where the controller is unlocked but the encryption key
875
- // is not set, even when #cacheEncryptionKey is true. This happens when
876
- // calling changePassword with the existing password. In this case, the
877
- // encryption key is deleted, but the state is not recreated, because the
878
- // session state does not change in this case, and #updateVault is not
879
- // called in #persistOrRollback.
880
- if (!this.state.encryptionKey) {
881
- assertIsExportableKeyEncryptor(__classPrivateFieldGet(this, _KeyringController_encryptor, "f"));
882
- assertIsValidPassword(__classPrivateFieldGet(this, _KeyringController_password, "f"));
883
- const result = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithDetail(__classPrivateFieldGet(this, _KeyringController_password, "f"),
884
- // Ignoring undefined. Assuming vault is set when unlocked.
885
- this.state.vault);
886
- if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
887
- this.update((state) => {
888
- state.encryptionKey = result.exportedKeyString;
889
- state.encryptionSalt = result.salt;
890
- });
891
- }
892
- return result.exportedKeyString;
893
- }
894
- return this.state.encryptionKey;
884
+ return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => {
885
+ const { encryptionKey } = this.state;
886
+ assertIsEncryptionKeySet(encryptionKey);
887
+ return encryptionKey;
888
+ });
895
889
  }
896
890
  /**
897
891
  * Attempts to decrypt the current vault and load its keyrings,
@@ -967,205 +961,13 @@ export class KeyringController extends BaseController {
967
961
  return result;
968
962
  });
969
963
  }
970
- // QR Hardware related methods
971
- /**
972
- * Get QR Hardware keyring.
973
- *
974
- * @returns The QR Keyring if defined, otherwise undefined
975
- * @deprecated Use `withKeyring` instead.
976
- */
977
- getQRKeyring() {
978
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
979
- // QRKeyring is not yet compatible with Keyring type from @metamask/utils
980
- return this.getKeyringsByType(KeyringTypes.qr)[0];
981
- }
982
- /**
983
- * Get QR hardware keyring. If it doesn't exist, add it.
984
- *
985
- * @returns The added keyring
986
- * @deprecated Use `addNewKeyring` and `withKeyring` instead.
987
- */
988
- async getOrAddQRKeyring() {
989
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
990
- return (this.getQRKeyring() ||
991
- (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this))));
992
- }
993
- /**
994
- * Restore QR keyring from serialized data.
995
- *
996
- * @param serialized - Serialized data to restore the keyring from.
997
- * @returns Promise resolving when the operation completes.
998
- * @deprecated Use `withKeyring` instead.
999
- */
1000
- // TODO: Replace `any` with type
1001
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1002
- async restoreQRKeyring(serialized) {
1003
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1004
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1005
- const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1006
- keyring.deserialize(serialized);
1007
- });
1008
- }
1009
- /**
1010
- * Reset QR keyring state.
1011
- *
1012
- * @returns Promise resolving when the operation completes.
1013
- * @deprecated Use `withKeyring` instead.
1014
- */
1015
- async resetQRKeyringState() {
1016
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1017
- (await this.getOrAddQRKeyring()).resetStore();
1018
- }
1019
- /**
1020
- * Get QR keyring state.
1021
- *
1022
- * @returns Promise resolving to the keyring state.
1023
- * @deprecated Use `withKeyring` or subscribe to `"KeyringController:qrKeyringStateChange"`
1024
- * instead.
1025
- */
1026
- async getQRKeyringState() {
1027
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1028
- return (await this.getOrAddQRKeyring()).getMemStore();
1029
- }
1030
- /**
1031
- * Submit QR hardware wallet public HDKey.
1032
- *
1033
- * @param cryptoHDKey - The key to submit.
1034
- * @returns Promise resolving when the operation completes.
1035
- * @deprecated Use `withKeyring` instead.
1036
- */
1037
- async submitQRCryptoHDKey(cryptoHDKey) {
1038
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1039
- (await this.getOrAddQRKeyring()).submitCryptoHDKey(cryptoHDKey);
1040
- }
1041
- /**
1042
- * Submit QR hardware wallet account.
1043
- *
1044
- * @param cryptoAccount - The account to submit.
1045
- * @returns Promise resolving when the operation completes.
1046
- * @deprecated Use `withKeyring` instead.
1047
- */
1048
- async submitQRCryptoAccount(cryptoAccount) {
1049
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1050
- (await this.getOrAddQRKeyring()).submitCryptoAccount(cryptoAccount);
1051
- }
1052
- /**
1053
- * Submit QR hardware wallet signature.
1054
- *
1055
- * @param requestId - The request ID.
1056
- * @param ethSignature - The signature to submit.
1057
- * @returns Promise resolving when the operation completes.
1058
- * @deprecated Use `withKeyring` instead.
1059
- */
1060
- async submitQRSignature(requestId, ethSignature) {
1061
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1062
- (await this.getOrAddQRKeyring()).submitSignature(requestId, ethSignature);
1063
- }
1064
- /**
1065
- * Cancel QR sign request.
1066
- *
1067
- * @returns Promise resolving when the operation completes.
1068
- * @deprecated Use `withKeyring` instead.
1069
- */
1070
- async cancelQRSignRequest() {
1071
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1072
- (await this.getOrAddQRKeyring()).cancelSignRequest();
1073
- }
1074
- /**
1075
- * Cancels qr keyring sync.
1076
- *
1077
- * @returns Promise resolving when the operation completes.
1078
- * @deprecated Use `withKeyring` instead.
1079
- */
1080
- async cancelQRSynchronization() {
1081
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1082
- (await this.getOrAddQRKeyring()).cancelSync();
1083
- }
1084
- /**
1085
- * Connect to QR hardware wallet.
1086
- *
1087
- * @param page - The page to connect to.
1088
- * @returns Promise resolving to the connected accounts.
1089
- * @deprecated Use of this method is discouraged as it creates a dangling promise
1090
- * internal to the `QRKeyring`, which can lead to unpredictable deadlocks. Please use
1091
- * `withKeyring` instead.
1092
- */
1093
- async connectQRHardware(page) {
1094
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1095
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1096
- try {
1097
- const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1098
- let accounts;
1099
- switch (page) {
1100
- case -1:
1101
- accounts = await keyring.getPreviousPage();
1102
- break;
1103
- case 1:
1104
- accounts = await keyring.getNextPage();
1105
- break;
1106
- default:
1107
- accounts = await keyring.getFirstPage();
1108
- }
1109
- // TODO: Replace `any` with type
1110
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1111
- return accounts.map((account) => {
1112
- return {
1113
- ...account,
1114
- balance: '0x0',
1115
- };
1116
- });
1117
- }
1118
- catch (e) {
1119
- // TODO: Add test case for when keyring throws
1120
- /* istanbul ignore next */
1121
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
1122
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1123
- throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
1124
- }
1125
- });
1126
- }
1127
- /**
1128
- * Unlock a QR hardware wallet account.
1129
- *
1130
- * @param index - The index of the account to unlock.
1131
- * @returns Promise resolving when the operation completes.
1132
- * @deprecated Use `withKeyring` instead.
1133
- */
1134
- async unlockQRHardwareWalletAccount(index) {
1135
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1136
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1137
- const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1138
- keyring.setAccountToUnlock(index);
1139
- await keyring.addAccounts(1);
1140
- });
1141
- }
1142
964
  async getAccountKeyringType(account) {
1143
965
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1144
966
  const keyring = (await this.getKeyringForAccount(account));
1145
967
  return keyring.type;
1146
968
  }
1147
- /**
1148
- * Forget the QR hardware wallet.
1149
- *
1150
- * @returns Promise resolving to the removed accounts and the remaining accounts.
1151
- * @deprecated Use `withKeyring` instead.
1152
- */
1153
- async forgetQRDevice() {
1154
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1155
- return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1156
- const keyring = this.getQRKeyring();
1157
- if (!keyring) {
1158
- return { removedAccounts: [], remainingAccounts: [] };
1159
- }
1160
- const allAccounts = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this));
1161
- keyring.forgetDevice();
1162
- const remainingAccounts = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this));
1163
- const removedAccounts = allAccounts.filter((address) => !remainingAccounts.includes(address));
1164
- return { removedAccounts, remainingAccounts };
1165
- });
1166
- }
1167
969
  }
1168
- _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_cacheEncryptionKey = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_qrKeyringStateListener = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
970
+ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_vaultOperationMutex = new WeakMap(), _KeyringController_keyringBuilders = new WeakMap(), _KeyringController_encryptor = new WeakMap(), _KeyringController_cacheEncryptionKey = new WeakMap(), _KeyringController_keyrings = new WeakMap(), _KeyringController_unsupportedKeyrings = new WeakMap(), _KeyringController_password = new WeakMap(), _KeyringController_instances = new WeakSet(), _KeyringController_registerMessageHandlers = function _KeyringController_registerMessageHandlers() {
1169
971
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
1170
972
  this.messagingSystem.registerActionHandler(`${name}:signEip7702Authorization`, this.signEip7702Authorization.bind(this));
1171
973
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
@@ -1197,30 +999,6 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1197
999
  return keyringWithMetadata.metadata;
1198
1000
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1199
1001
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1200
- }, _KeyringController_addQRKeyring =
1201
- /**
1202
- * Add qr hardware keyring.
1203
- *
1204
- * @returns The added keyring
1205
- * @throws If a QRKeyring builder is not provided
1206
- * when initializing the controller
1207
- */
1208
- async function _KeyringController_addQRKeyring() {
1209
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1210
- // QRKeyring is not yet compatible with Keyring type from @metamask/utils
1211
- return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr));
1212
- }, _KeyringController_subscribeToQRKeyringEvents = function _KeyringController_subscribeToQRKeyringEvents(qrKeyring) {
1213
- __classPrivateFieldSet(this, _KeyringController_qrKeyringStateListener, (state) => {
1214
- this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
1215
- }, "f");
1216
- qrKeyring.getMemStore().subscribe(__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f"));
1217
- }, _KeyringController_unsubscribeFromQRKeyringsEvents = function _KeyringController_unsubscribeFromQRKeyringsEvents() {
1218
- const qrKeyrings = this.getKeyringsByType(KeyringTypes.qr);
1219
- qrKeyrings.forEach((qrKeyring) => {
1220
- if (__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f")) {
1221
- qrKeyring.getMemStore().unsubscribe(__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f"));
1222
- }
1223
- });
1224
1002
  }, _KeyringController_createNewVaultWithKeyring =
1225
1003
  /**
1226
1004
  * Create new vault with an initial keyring
@@ -1580,11 +1358,6 @@ async function _KeyringController_createKeyring(type, data) {
1580
1358
  await keyring.generateRandomMnemonic();
1581
1359
  await keyring.addAccounts(1);
1582
1360
  }
1583
- if (type === KeyringTypes.qr) {
1584
- // In case of a QR keyring type, we need to subscribe
1585
- // to its events after creating it
1586
- __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, keyring);
1587
- }
1588
1361
  return keyring;
1589
1362
  }, _KeyringController_clearKeyrings =
1590
1363
  /**