@metamask-previews/keyring-controller 22.0.2-preview-c6e5eb7 → 22.0.2-preview-ee33e55

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
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - Add method `exportEncryptionKey` ([#5984](https://github.com/MetaMask/core/pull/5984))
13
+
14
+ ### Changed
15
+
16
+ - Make salt optional with method `submitEncryptionKey` ([#5984](https://github.com/MetaMask/core/pull/5984))
17
+
10
18
  ## [22.0.2]
11
19
 
12
20
  ### Fixed
@@ -168,6 +168,17 @@ function assertIsValidPassword(password) {
168
168
  throw new Error(constants_1.KeyringControllerError.InvalidEmptyPassword);
169
169
  }
170
170
  }
171
+ /**
172
+ * Assert that the provided encryption key is a valid non-empty string.
173
+ *
174
+ * @param encryptionKey - The encryption key to check.
175
+ * @throws If the encryption key is not a valid string.
176
+ */
177
+ function assertIsEncryptionKeySet(encryptionKey) {
178
+ if (!encryptionKey) {
179
+ throw new Error(constants_1.KeyringControllerError.EncryptionKeyNotSet);
180
+ }
181
+ }
171
182
  /**
172
183
  * Checks if the provided value is a serialized keyrings array.
173
184
  *
@@ -843,6 +854,10 @@ class KeyringController extends base_controller_1.BaseController {
843
854
  */
844
855
  changePassword(password) {
845
856
  __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
857
+ // If the password is the same, do nothing.
858
+ if (__classPrivateFieldGet(this, _KeyringController_password, "f") === password) {
859
+ return Promise.resolve();
860
+ }
846
861
  return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
847
862
  assertIsValidPassword(password);
848
863
  __classPrivateFieldSet(this, _KeyringController_password, password, "f");
@@ -858,11 +873,12 @@ class KeyringController extends base_controller_1.BaseController {
858
873
  });
859
874
  }
860
875
  /**
861
- * Attempts to decrypt the current vault and load its keyrings,
862
- * using the given encryption key and salt.
876
+ * Attempts to decrypt the current vault and load its keyrings, using the
877
+ * given encryption key and salt. The optional salt can be used to check for
878
+ * consistency with the vault salt.
863
879
  *
864
880
  * @param encryptionKey - Key to unlock the keychain.
865
- * @param encryptionSalt - Salt to unlock the keychain.
881
+ * @param encryptionSalt - Optional salt to unlock the keychain.
866
882
  * @returns Promise resolving when the operation completes.
867
883
  */
868
884
  async submitEncryptionKey(encryptionKey, encryptionSalt) {
@@ -886,6 +902,19 @@ class KeyringController extends base_controller_1.BaseController {
886
902
  console.error('Failed to update vault during login:', error);
887
903
  }
888
904
  }
905
+ /**
906
+ * Exports the vault encryption key.
907
+ *
908
+ * @returns The vault encryption key.
909
+ */
910
+ async exportEncryptionKey() {
911
+ __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertIsUnlocked).call(this);
912
+ return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => {
913
+ const { encryptionKey } = this.state;
914
+ assertIsEncryptionKeySet(encryptionKey);
915
+ return encryptionKey;
916
+ });
917
+ }
889
918
  /**
890
919
  * Attempts to decrypt the current vault and load its keyrings,
891
920
  * using the given password.
@@ -1384,9 +1413,12 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1384
1413
  }
1385
1414
  else {
1386
1415
  const parsedEncryptedVault = JSON.parse(encryptedVault);
1387
- if (encryptionSalt !== parsedEncryptedVault.salt) {
1416
+ if (encryptionSalt && encryptionSalt !== parsedEncryptedVault.salt) {
1388
1417
  throw new Error(constants_1.KeyringControllerError.ExpiredCredentials);
1389
1418
  }
1419
+ else {
1420
+ encryptionSalt = parsedEncryptedVault.salt;
1421
+ }
1390
1422
  if (typeof encryptionKey !== 'string') {
1391
1423
  throw new TypeError(constants_1.KeyringControllerError.WrongPasswordType);
1392
1424
  }
@@ -1395,9 +1427,6 @@ async function _KeyringController_unlockKeyrings(password, encryptionKey, encryp
1395
1427
  // This call is required on the first call because encryptionKey
1396
1428
  // is not yet inside the memStore
1397
1429
  updatedState.encryptionKey = encryptionKey;
1398
- // we can safely assume that encryptionSalt is defined here
1399
- // because we compare it with the salt from the vault
1400
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1401
1430
  updatedState.encryptionSalt = encryptionSalt;
1402
1431
  }
1403
1432
  }