@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.
@@ -36,7 +36,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
36
36
  var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  return (mod && mod.__esModule) ? mod : { "default": mod };
38
38
  };
39
- var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_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;
39
+ 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;
40
40
  Object.defineProperty(exports, "__esModule", { value: true });
41
41
  exports.KeyringController = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
42
42
  const util_1 = require("@ethereumjs/util");
@@ -285,6 +285,7 @@ class KeyringController extends base_controller_1.BaseController {
285
285
  _KeyringController_keyrings.set(this, void 0);
286
286
  _KeyringController_unsupportedKeyrings.set(this, void 0);
287
287
  _KeyringController_password.set(this, void 0);
288
+ _KeyringController_qrKeyringStateListener.set(this, void 0);
288
289
  __classPrivateFieldSet(this, _KeyringController_keyringBuilders, keyringBuilders
289
290
  ? keyringBuilders.concat(defaultKeyringBuilders)
290
291
  : defaultKeyringBuilders, "f");
@@ -409,6 +410,9 @@ class KeyringController extends base_controller_1.BaseController {
409
410
  */
410
411
  async addNewKeyring(type, opts) {
411
412
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
413
+ if (type === KeyringTypes.qr) {
414
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringMetadata).call(this, await this.getOrAddQRKeyring());
415
+ }
412
416
  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)));
413
417
  }
414
418
  /**
@@ -667,6 +671,7 @@ class KeyringController extends base_controller_1.BaseController {
667
671
  async setLocked() {
668
672
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
669
673
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withRollback).call(this, async () => {
674
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_unsubscribeFromQRKeyringsEvents).call(this);
670
675
  __classPrivateFieldSet(this, _KeyringController_password, undefined, "f");
671
676
  await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
672
677
  this.update((state) => {
@@ -984,14 +989,206 @@ class KeyringController extends base_controller_1.BaseController {
984
989
  return result;
985
990
  });
986
991
  }
992
+ // QR Hardware related methods
993
+ /**
994
+ * Get QR Hardware keyring.
995
+ *
996
+ * @returns The QR Keyring if defined, otherwise undefined
997
+ * @deprecated Use `withKeyring` instead.
998
+ */
999
+ getQRKeyring() {
1000
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1001
+ // QRKeyring is not yet compatible with Keyring type from @metamask/utils
1002
+ return this.getKeyringsByType(KeyringTypes.qr)[0];
1003
+ }
1004
+ /**
1005
+ * Get QR hardware keyring. If it doesn't exist, add it.
1006
+ *
1007
+ * @returns The added keyring
1008
+ * @deprecated Use `addNewKeyring` and `withKeyring` instead.
1009
+ */
1010
+ async getOrAddQRKeyring() {
1011
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1012
+ return (this.getQRKeyring() ||
1013
+ (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this))));
1014
+ }
1015
+ /**
1016
+ * Restore QR keyring from serialized data.
1017
+ *
1018
+ * @param serialized - Serialized data to restore the keyring from.
1019
+ * @returns Promise resolving when the operation completes.
1020
+ * @deprecated Use `withKeyring` instead.
1021
+ */
1022
+ // TODO: Replace `any` with type
1023
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1024
+ async restoreQRKeyring(serialized) {
1025
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1026
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1027
+ const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1028
+ keyring.deserialize(serialized);
1029
+ });
1030
+ }
1031
+ /**
1032
+ * Reset QR keyring state.
1033
+ *
1034
+ * @returns Promise resolving when the operation completes.
1035
+ * @deprecated Use `withKeyring` instead.
1036
+ */
1037
+ async resetQRKeyringState() {
1038
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1039
+ (await this.getOrAddQRKeyring()).resetStore();
1040
+ }
1041
+ /**
1042
+ * Get QR keyring state.
1043
+ *
1044
+ * @returns Promise resolving to the keyring state.
1045
+ * @deprecated Use `withKeyring` or subscribe to `"KeyringController:qrKeyringStateChange"`
1046
+ * instead.
1047
+ */
1048
+ async getQRKeyringState() {
1049
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1050
+ return (await this.getOrAddQRKeyring()).getMemStore();
1051
+ }
1052
+ /**
1053
+ * Submit QR hardware wallet public HDKey.
1054
+ *
1055
+ * @param cryptoHDKey - The key to submit.
1056
+ * @returns Promise resolving when the operation completes.
1057
+ * @deprecated Use `withKeyring` instead.
1058
+ */
1059
+ async submitQRCryptoHDKey(cryptoHDKey) {
1060
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1061
+ (await this.getOrAddQRKeyring()).submitCryptoHDKey(cryptoHDKey);
1062
+ }
1063
+ /**
1064
+ * Submit QR hardware wallet account.
1065
+ *
1066
+ * @param cryptoAccount - The account to submit.
1067
+ * @returns Promise resolving when the operation completes.
1068
+ * @deprecated Use `withKeyring` instead.
1069
+ */
1070
+ async submitQRCryptoAccount(cryptoAccount) {
1071
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1072
+ (await this.getOrAddQRKeyring()).submitCryptoAccount(cryptoAccount);
1073
+ }
1074
+ /**
1075
+ * Submit QR hardware wallet signature.
1076
+ *
1077
+ * @param requestId - The request ID.
1078
+ * @param ethSignature - The signature to submit.
1079
+ * @returns Promise resolving when the operation completes.
1080
+ * @deprecated Use `withKeyring` instead.
1081
+ */
1082
+ async submitQRSignature(requestId, ethSignature) {
1083
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1084
+ (await this.getOrAddQRKeyring()).submitSignature(requestId, ethSignature);
1085
+ }
1086
+ /**
1087
+ * Cancel QR sign request.
1088
+ *
1089
+ * @returns Promise resolving when the operation completes.
1090
+ * @deprecated Use `withKeyring` instead.
1091
+ */
1092
+ async cancelQRSignRequest() {
1093
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1094
+ (await this.getOrAddQRKeyring()).cancelSignRequest();
1095
+ }
1096
+ /**
1097
+ * Cancels qr keyring sync.
1098
+ *
1099
+ * @returns Promise resolving when the operation completes.
1100
+ * @deprecated Use `withKeyring` instead.
1101
+ */
1102
+ async cancelQRSynchronization() {
1103
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1104
+ (await this.getOrAddQRKeyring()).cancelSync();
1105
+ }
1106
+ /**
1107
+ * Connect to QR hardware wallet.
1108
+ *
1109
+ * @param page - The page to connect to.
1110
+ * @returns Promise resolving to the connected accounts.
1111
+ * @deprecated Use of this method is discouraged as it creates a dangling promise
1112
+ * internal to the `QRKeyring`, which can lead to unpredictable deadlocks. Please use
1113
+ * `withKeyring` instead.
1114
+ */
1115
+ async connectQRHardware(page) {
1116
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1117
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1118
+ try {
1119
+ const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1120
+ let accounts;
1121
+ switch (page) {
1122
+ case -1:
1123
+ accounts = await keyring.getPreviousPage();
1124
+ break;
1125
+ case 1:
1126
+ accounts = await keyring.getNextPage();
1127
+ break;
1128
+ default:
1129
+ accounts = await keyring.getFirstPage();
1130
+ }
1131
+ // TODO: Replace `any` with type
1132
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1133
+ return accounts.map((account) => {
1134
+ return {
1135
+ ...account,
1136
+ balance: '0x0',
1137
+ };
1138
+ });
1139
+ }
1140
+ catch (e) {
1141
+ // TODO: Add test case for when keyring throws
1142
+ /* istanbul ignore next */
1143
+ // TODO: Either fix this lint violation or explain why it's necessary to ignore.
1144
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1145
+ throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
1146
+ }
1147
+ });
1148
+ }
1149
+ /**
1150
+ * Unlock a QR hardware wallet account.
1151
+ *
1152
+ * @param index - The index of the account to unlock.
1153
+ * @returns Promise resolving when the operation completes.
1154
+ * @deprecated Use `withKeyring` instead.
1155
+ */
1156
+ async unlockQRHardwareWalletAccount(index) {
1157
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1158
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1159
+ const keyring = this.getQRKeyring() || (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_addQRKeyring).call(this));
1160
+ keyring.setAccountToUnlock(index);
1161
+ await keyring.addAccounts(1);
1162
+ });
1163
+ }
987
1164
  async getAccountKeyringType(account) {
988
1165
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
989
1166
  const keyring = (await this.getKeyringForAccount(account));
990
1167
  return keyring.type;
991
1168
  }
1169
+ /**
1170
+ * Forget the QR hardware wallet.
1171
+ *
1172
+ * @returns Promise resolving to the removed accounts and the remaining accounts.
1173
+ * @deprecated Use `withKeyring` instead.
1174
+ */
1175
+ async forgetQRDevice() {
1176
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
1177
+ return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
1178
+ const keyring = this.getQRKeyring();
1179
+ if (!keyring) {
1180
+ return { removedAccounts: [], remainingAccounts: [] };
1181
+ }
1182
+ const allAccounts = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this));
1183
+ keyring.forgetDevice();
1184
+ const remainingAccounts = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getAccountsFromKeyrings).call(this));
1185
+ const removedAccounts = allAccounts.filter((address) => !remainingAccounts.includes(address));
1186
+ return { removedAccounts, remainingAccounts };
1187
+ });
1188
+ }
992
1189
  }
993
1190
  exports.KeyringController = KeyringController;
994
- _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() {
1191
+ _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() {
995
1192
  this.messagingSystem.registerActionHandler(`${name}:signMessage`, this.signMessage.bind(this));
996
1193
  this.messagingSystem.registerActionHandler(`${name}:signEip7702Authorization`, this.signEip7702Authorization.bind(this));
997
1194
  this.messagingSystem.registerActionHandler(`${name}:signPersonalMessage`, this.signPersonalMessage.bind(this));
@@ -1023,6 +1220,30 @@ _KeyringController_controllerOperationMutex = new WeakMap(), _KeyringController_
1023
1220
  return keyringWithMetadata.metadata;
1024
1221
  }, _KeyringController_getKeyringBuilderForType = function _KeyringController_getKeyringBuilderForType(type) {
1025
1222
  return __classPrivateFieldGet(this, _KeyringController_keyringBuilders, "f").find((keyringBuilder) => keyringBuilder.type === type);
1223
+ }, _KeyringController_addQRKeyring =
1224
+ /**
1225
+ * Add qr hardware keyring.
1226
+ *
1227
+ * @returns The added keyring
1228
+ * @throws If a QRKeyring builder is not provided
1229
+ * when initializing the controller
1230
+ */
1231
+ async function _KeyringController_addQRKeyring() {
1232
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
1233
+ // QRKeyring is not yet compatible with Keyring type from @metamask/utils
1234
+ return (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, KeyringTypes.qr));
1235
+ }, _KeyringController_subscribeToQRKeyringEvents = function _KeyringController_subscribeToQRKeyringEvents(qrKeyring) {
1236
+ __classPrivateFieldSet(this, _KeyringController_qrKeyringStateListener, (state) => {
1237
+ this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
1238
+ }, "f");
1239
+ qrKeyring.getMemStore().subscribe(__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f"));
1240
+ }, _KeyringController_unsubscribeFromQRKeyringsEvents = function _KeyringController_unsubscribeFromQRKeyringsEvents() {
1241
+ const qrKeyrings = this.getKeyringsByType(KeyringTypes.qr);
1242
+ qrKeyrings.forEach((qrKeyring) => {
1243
+ if (__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f")) {
1244
+ qrKeyring.getMemStore().unsubscribe(__classPrivateFieldGet(this, _KeyringController_qrKeyringStateListener, "f"));
1245
+ }
1246
+ });
1026
1247
  }, _KeyringController_createNewVaultWithKeyring =
1027
1248
  /**
1028
1249
  * Create new vault with an initial keyring
@@ -1382,6 +1603,11 @@ async function _KeyringController_createKeyring(type, data) {
1382
1603
  await keyring.generateRandomMnemonic();
1383
1604
  await keyring.addAccounts(1);
1384
1605
  }
1606
+ if (type === KeyringTypes.qr) {
1607
+ // In case of a QR keyring type, we need to subscribe
1608
+ // to its events after creating it
1609
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_subscribeToQRKeyringEvents).call(this, keyring);
1610
+ }
1385
1611
  return keyring;
1386
1612
  }, _KeyringController_clearKeyrings =
1387
1613
  /**