@metamask-previews/keyring-controller 22.0.2-preview-d7c00b1 → 22.0.2-preview-f9a5dd05

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_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;
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;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
@@ -262,6 +262,7 @@ export class KeyringController extends BaseController {
262
262
  _KeyringController_keyrings.set(this, void 0);
263
263
  _KeyringController_unsupportedKeyrings.set(this, void 0);
264
264
  _KeyringController_password.set(this, void 0);
265
+ _KeyringController_qrKeyringStateListener.set(this, void 0);
265
266
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
266
267
  ? keyringBuilders.concat(defaultKeyringBuilders)
267
268
  : defaultKeyringBuilders, "f");
@@ -386,6 +387,9 @@ export class KeyringController extends BaseController {
386
387
  */
387
388
  async addNewKeyring(type, opts) {
388
389
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
390
+ if (type === KeyringTypes.qr) {
391
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, await this.getOrAddQRKeyring());
392
+ }
389
393
  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)));
390
394
  }
391
395
  /**
@@ -644,6 +648,7 @@ export class KeyringController extends BaseController {
644
648
  async setLocked() {
645
649
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
646
650
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
651
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unsubscribeFromQRKeyringsEvents).call(this);
647
652
  __classPrivateFieldSet(this, _KeyringController_password, undefined, "f");
648
653
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
649
654
  this.update((state) => {
@@ -961,13 +966,205 @@ export class KeyringController extends BaseController {
961
966
  return result;
962
967
  });
963
968
  }
969
+ // QR Hardware related methods
970
+ /**
971
+ * Get QR Hardware keyring.
972
+ *
973
+ * @returns The QR Keyring if defined, otherwise undefined
974
+ * @deprecated Use `withKeyring` instead.
975
+ */
976
+ getQRKeyring() {
977
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
978
+ // QRKeyring is not yet compatible with Keyring type from @metamask/utils
979
+ return this.getKeyringsByType(KeyringTypes.qr)[0];
980
+ }
981
+ /**
982
+ * Get QR hardware keyring. If it doesn't exist, add it.
983
+ *
984
+ * @returns The added keyring
985
+ * @deprecated Use `addNewKeyring` and `withKeyring` instead.
986
+ */
987
+ async getOrAddQRKeyring() {
988
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
989
+ return (this.getQRKeyring() ||
990
+ (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this))));
991
+ }
992
+ /**
993
+ * Restore QR keyring from serialized data.
994
+ *
995
+ * @param serialized - Serialized data to restore the keyring from.
996
+ * @returns Promise resolving when the operation completes.
997
+ * @deprecated Use `withKeyring` instead.
998
+ */
999
+ // TODO: Replace `any` with type
1000
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1001
+ async restoreQRKeyring(serialized) {
1002
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1003
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1004
+ const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1005
+ keyring.deserialize(serialized);
1006
+ });
1007
+ }
1008
+ /**
1009
+ * Reset QR keyring state.
1010
+ *
1011
+ * @returns Promise resolving when the operation completes.
1012
+ * @deprecated Use `withKeyring` instead.
1013
+ */
1014
+ async resetQRKeyringState() {
1015
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1016
+ (await this.getOrAddQRKeyring()).resetStore();
1017
+ }
1018
+ /**
1019
+ * Get QR keyring state.
1020
+ *
1021
+ * @returns Promise resolving to the keyring state.
1022
+ * @deprecated Use `withKeyring` or subscribe to `"KeyringController:qrKeyringStateChange"`
1023
+ * instead.
1024
+ */
1025
+ async getQRKeyringState() {
1026
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1027
+ return (await this.getOrAddQRKeyring()).getMemStore();
1028
+ }
1029
+ /**
1030
+ * Submit QR hardware wallet public HDKey.
1031
+ *
1032
+ * @param cryptoHDKey - The key to submit.
1033
+ * @returns Promise resolving when the operation completes.
1034
+ * @deprecated Use `withKeyring` instead.
1035
+ */
1036
+ async submitQRCryptoHDKey(cryptoHDKey) {
1037
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1038
+ (await this.getOrAddQRKeyring()).submitCryptoHDKey(cryptoHDKey);
1039
+ }
1040
+ /**
1041
+ * Submit QR hardware wallet account.
1042
+ *
1043
+ * @param cryptoAccount - The account to submit.
1044
+ * @returns Promise resolving when the operation completes.
1045
+ * @deprecated Use `withKeyring` instead.
1046
+ */
1047
+ async submitQRCryptoAccount(cryptoAccount) {
1048
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1049
+ (await this.getOrAddQRKeyring()).submitCryptoAccount(cryptoAccount);
1050
+ }
1051
+ /**
1052
+ * Submit QR hardware wallet signature.
1053
+ *
1054
+ * @param requestId - The request ID.
1055
+ * @param ethSignature - The signature to submit.
1056
+ * @returns Promise resolving when the operation completes.
1057
+ * @deprecated Use `withKeyring` instead.
1058
+ */
1059
+ async submitQRSignature(requestId, ethSignature) {
1060
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1061
+ (await this.getOrAddQRKeyring()).submitSignature(requestId, ethSignature);
1062
+ }
1063
+ /**
1064
+ * Cancel QR sign request.
1065
+ *
1066
+ * @returns Promise resolving when the operation completes.
1067
+ * @deprecated Use `withKeyring` instead.
1068
+ */
1069
+ async cancelQRSignRequest() {
1070
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1071
+ (await this.getOrAddQRKeyring()).cancelSignRequest();
1072
+ }
1073
+ /**
1074
+ * Cancels qr keyring sync.
1075
+ *
1076
+ * @returns Promise resolving when the operation completes.
1077
+ * @deprecated Use `withKeyring` instead.
1078
+ */
1079
+ async cancelQRSynchronization() {
1080
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1081
+ (await this.getOrAddQRKeyring()).cancelSync();
1082
+ }
1083
+ /**
1084
+ * Connect to QR hardware wallet.
1085
+ *
1086
+ * @param page - The page to connect to.
1087
+ * @returns Promise resolving to the connected accounts.
1088
+ * @deprecated Use of this method is discouraged as it creates a dangling promise
1089
+ * internal to the `QRKeyring`, which can lead to unpredictable deadlocks. Please use
1090
+ * `withKeyring` instead.
1091
+ */
1092
+ async connectQRHardware(page) {
1093
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1094
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1095
+ try {
1096
+ const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1097
+ let accounts;
1098
+ switch (page) {
1099
+ case -1:
1100
+ accounts = await keyring.getPreviousPage();
1101
+ break;
1102
+ case 1:
1103
+ accounts = await keyring.getNextPage();
1104
+ break;
1105
+ default:
1106
+ accounts = await keyring.getFirstPage();
1107
+ }
1108
+ // TODO: Replace `any` with type
1109
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1110
+ return accounts.map((account) => {
1111
+ return {
1112
+ ...account,
1113
+ balance: '0x0',
1114
+ };
1115
+ });
1116
+ }
1117
+ catch (e) {
1118
+ // TODO: Add test case for when keyring throws
1119
+ /* istanbul ignore next */
1120
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
1121
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1122
+ throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
1123
+ }
1124
+ });
1125
+ }
1126
+ /**
1127
+ * Unlock a QR hardware wallet account.
1128
+ *
1129
+ * @param index - The index of the account to unlock.
1130
+ * @returns Promise resolving when the operation completes.
1131
+ * @deprecated Use `withKeyring` instead.
1132
+ */
1133
+ async unlockQRHardwareWalletAccount(index) {
1134
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1135
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1136
+ const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1137
+ keyring.setAccountToUnlock(index);
1138
+ await keyring.addAccounts(1);
1139
+ });
1140
+ }
964
1141
  async getAccountKeyringType(account) {
965
1142
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
966
1143
  const keyring = (await this.getKeyringForAccount(account));
967
1144
  return keyring.type;
968
1145
  }
1146
+ /**
1147
+ * Forget the QR hardware wallet.
1148
+ *
1149
+ * @returns Promise resolving to the removed accounts and the remaining accounts.
1150
+ * @deprecated Use `withKeyring` instead.
1151
+ */
1152
+ async forgetQRDevice() {
1153
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1154
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1155
+ const keyring = this.getQRKeyring();
1156
+ if (!keyring) {
1157
+ return { removedAccounts: [], remainingAccounts: [] };
1158
+ }
1159
+ const allAccounts = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this));
1160
+ keyring.forgetDevice();
1161
+ const remainingAccounts = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this));
1162
+ const removedAccounts = allAccounts.filter((address) => !remainingAccounts.includes(address));
1163
+ return { removedAccounts, remainingAccounts };
1164
+ });
1165
+ }
969
1166
  }
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() {
1167
+ _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() {
971
1168
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
972
1169
  this.messagingSystem.registerActionHandler(`${name}:signEip7702Authorization`, this.signEip7702Authorization.bind(this));
973
1170
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
@@ -999,6 +1196,30 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
999
1196
  return keyringWithMetadata.metadata;
1000
1197
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1001
1198
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1199
+ }, _KeyringController_addQRKeyring =
1200
+ /**
1201
+ * Add qr hardware keyring.
1202
+ *
1203
+ * @returns The added keyring
1204
+ * @throws If a QRKeyring builder is not provided
1205
+ * when initializing the controller
1206
+ */
1207
+ async function _KeyringController_addQRKeyring() {
1208
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1209
+ // QRKeyring is not yet compatible with Keyring type from @metamask/utils
1210
+ return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr));
1211
+ }, _KeyringController_subscribeToQRKeyringEvents = function _KeyringController_subscribeToQRKeyringEvents(qrKeyring) {
1212
+ __classPrivateFieldSet(this, _KeyringController_qrKeyringStateListener, (state) => {
1213
+ this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
1214
+ }, "f");
1215
+ qrKeyring.getMemStore().subscribe(__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f"));
1216
+ }, _KeyringController_unsubscribeFromQRKeyringsEvents = function _KeyringController_unsubscribeFromQRKeyringsEvents() {
1217
+ const qrKeyrings = this.getKeyringsByType(KeyringTypes.qr);
1218
+ qrKeyrings.forEach((qrKeyring) => {
1219
+ if (__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f")) {
1220
+ qrKeyring.getMemStore().unsubscribe(__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f"));
1221
+ }
1222
+ });
1002
1223
  }, _KeyringController_createNewVaultWithKeyring =
1003
1224
  /**
1004
1225
  * Create new vault with an initial keyring
@@ -1358,6 +1579,11 @@ async function _KeyringController_createKeyring(type, data) {
1358
1579
  await keyring.generateRandomMnemonic();
1359
1580
  await keyring.addAccounts(1);
1360
1581
  }
1582
+ if (type === KeyringTypes.qr) {
1583
+ // In case of a QR keyring type, we need to subscribe
1584
+ // to its events after creating it
1585
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, keyring);
1586
+ }
1361
1587
  return keyring;
1362
1588
  }, _KeyringController_clearKeyrings =
1363
1589
  /**