@metamask-previews/keyring-controller 22.0.2-preview-e149187d → 22.0.2-preview-abf6aaef

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.
@@ -154,17 +154,6 @@ function assertIsExportableKeyEncryptor(encryptor) {
154
154
  throw new Error(constants_1.KeyringControllerError.UnsupportedEncryptionKeyExport);
155
155
  }
156
156
  }
157
- /**
158
- * Assert that the encryption key is set.
159
- *
160
- * @param encryptionKey - The encryption key to check.
161
- * @throws If the encryption key is not set.
162
- */
163
- function assertIsEncryptionKeySet(encryptionKey) {
164
- if (!encryptionKey) {
165
- throw new Error(constants_1.KeyringControllerError.EncryptionKeyNotSet);
166
- }
167
- }
168
157
  /**
169
158
  * Assert that the provided password is a valid non-empty string.
170
159
  *
@@ -904,7 +893,27 @@ class KeyringController extends base_controller_1.BaseController {
904
893
  * @returns The vault encryption key.
905
894
  */
906
895
  async exportEncryptionKey() {
907
- assertIsEncryptionKeySet(this.state.encryptionKey);
896
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
897
+ // There is a case where the controller is unlocked but the encryption key
898
+ // is not set, even when #cacheEncryptionKey is true. This happens when
899
+ // calling changePassword with the existing password. In this case, the
900
+ // encryption key is deleted, but the state is not recreated, because the
901
+ // session state does not change in this case, and #updateVault is not
902
+ // called in #persistOrRollback.
903
+ if (!this.state.encryptionKey) {
904
+ assertIsExportableKeyEncryptor(__classPrivateFieldGet(this, _KeyringController_encryptor, "f"));
905
+ assertIsValidPassword(__classPrivateFieldGet(this, _KeyringController_password, "f"));
906
+ const result = await __classPrivateFieldGet(this, _KeyringController_encryptor, "f").decryptWithDetail(__classPrivateFieldGet(this, _KeyringController_password, "f"),
907
+ // Ignoring undefined. Assuming vault is set when unlocked.
908
+ this.state.vault);
909
+ if (__classPrivateFieldGet(this, _KeyringController_cacheEncryptionKey, "f")) {
910
+ this.update((state) => {
911
+ state.encryptionKey = result.exportedKeyString;
912
+ state.encryptionSalt = result.salt;
913
+ });
914
+ }
915
+ return result.exportedKeyString;
916
+ }
908
917
  return this.state.encryptionKey;
909
918
  }
910
919
  /**