@metamask-previews/seedless-onboarding-controller 1.0.0-preview-ca9d0265 → 1.0.0-preview-e149187d

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.
@@ -10,13 +10,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  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");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _SeedlessOnboardingController_instances, _SeedlessOnboardingController_vaultEncryptor, _SeedlessOnboardingController_controllerOperationMutex, _SeedlessOnboardingController_vaultOperationMutex, _SeedlessOnboardingController_refreshJWTToken, _SeedlessOnboardingController_revokeRefreshToken, _SeedlessOnboardingController_isUnlocked, _SeedlessOnboardingController_recoverPassword, _SeedlessOnboardingController_setUnlocked, _SeedlessOnboardingController_persistOprfKey, _SeedlessOnboardingController_persistAuthPubKey, _SeedlessOnboardingController_recoverAuthPubKey, _SeedlessOnboardingController_recoverEncKey, _SeedlessOnboardingController_changeEncryptionKey, _SeedlessOnboardingController_encryptAndStoreSecretData, _SeedlessOnboardingController_unlockVaultAndGetBackupEncKey, _SeedlessOnboardingController_withPersistedSecretMetadataBackupsState, _SeedlessOnboardingController_filterDupesAndUpdateSocialBackupsMetadata, _SeedlessOnboardingController_createNewVaultWithAuthData, _SeedlessOnboardingController_updateVault, _SeedlessOnboardingController_withControllerLock, _SeedlessOnboardingController_withVaultLock, _SeedlessOnboardingController_serializeKeyData, _SeedlessOnboardingController_parseVaultData, _SeedlessOnboardingController_assertIsUnlocked, _SeedlessOnboardingController_assertIsAuthenticatedUser, _SeedlessOnboardingController_assertIsSRPBackedUpUser, _SeedlessOnboardingController_assertPasswordInSync, _SeedlessOnboardingController_resetPasswordOutdatedCache, _SeedlessOnboardingController_assertIsValidVaultData, _SeedlessOnboardingController_isTokenExpiredError, _SeedlessOnboardingController_executeWithTokenRefresh;
13
+ var _SeedlessOnboardingController_instances, _SeedlessOnboardingController_vaultEncryptor, _SeedlessOnboardingController_controllerOperationMutex, _SeedlessOnboardingController_vaultOperationMutex, _SeedlessOnboardingController_refreshJWTToken, _SeedlessOnboardingController_revokeRefreshToken, _SeedlessOnboardingController_isUnlocked, _SeedlessOnboardingController_recoverKeyringEncryptionKey, _SeedlessOnboardingController_setUnlocked, _SeedlessOnboardingController_persistOprfKey, _SeedlessOnboardingController_persistAuthPubKey, _SeedlessOnboardingController_storeKeyringEncryptionKey, _SeedlessOnboardingController_loadKeyringEncryptionKey, _SeedlessOnboardingController_recoverAuthPubKey, _SeedlessOnboardingController_recoverEncKey, _SeedlessOnboardingController_changeEncryptionKey, _SeedlessOnboardingController_encryptAndStoreSecretData, _SeedlessOnboardingController_unlockVaultAndGetBackupEncKey, _SeedlessOnboardingController_withPersistedSecretMetadataBackupsState, _SeedlessOnboardingController_filterDupesAndUpdateSocialBackupsMetadata, _SeedlessOnboardingController_createNewVaultWithAuthData, _SeedlessOnboardingController_updateVault, _SeedlessOnboardingController_withControllerLock, _SeedlessOnboardingController_withVaultLock, _SeedlessOnboardingController_serializeKeyData, _SeedlessOnboardingController_parseVaultData, _SeedlessOnboardingController_assertIsUnlocked, _SeedlessOnboardingController_assertIsAuthenticatedUser, _SeedlessOnboardingController_assertIsSRPBackedUpUser, _SeedlessOnboardingController_assertPasswordInSync, _SeedlessOnboardingController_resetPasswordOutdatedCache, _SeedlessOnboardingController_assertIsValidVaultData, _SeedlessOnboardingController_isTokenExpiredError, _SeedlessOnboardingController_executeWithTokenRefresh;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SeedlessOnboardingController = exports.getDefaultSeedlessOnboardingControllerState = void 0;
16
16
  const auth_network_utils_1 = require("@metamask/auth-network-utils");
17
17
  const base_controller_1 = require("@metamask/base-controller");
18
18
  const toprf_secure_backup_1 = require("@metamask/toprf-secure-backup");
19
19
  const utils_1 = require("@metamask/utils");
20
+ const aes_1 = require("@noble/ciphers/aes");
21
+ const utils_2 = require("@noble/ciphers/utils");
22
+ const webcrypto_1 = require("@noble/ciphers/webcrypto");
20
23
  const secp256k1_1 = require("@noble/curves/secp256k1");
21
24
  const async_mutex_1 = require("async-mutex");
22
25
  const constants_1 = require("./constants.cjs");
@@ -99,6 +102,10 @@ const seedlessOnboardingMetadata = {
99
102
  persist: false,
100
103
  anonymous: true,
101
104
  },
105
+ encryptedKeyringEncryptionKey: {
106
+ persist: true,
107
+ anonymous: true,
108
+ },
102
109
  };
103
110
  class SeedlessOnboardingController extends base_controller_1.BaseController {
104
111
  /**
@@ -210,9 +217,10 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
210
217
  * @param password - The password used to create new wallet and seedphrase
211
218
  * @param seedPhrase - The initial seed phrase (Mnemonic) created together with the wallet.
212
219
  * @param keyringId - The keyring id of the backup seed phrase
220
+ * @param keyringEncryptionKey - The encryption key to be used for encrypting the keyring vault.
213
221
  * @returns A promise that resolves to the encrypted seed phrase and the encryption key.
214
222
  */
215
- async createToprfKeyAndBackupSeedPhrase(password, seedPhrase, keyringId) {
223
+ async createToprfKeyAndBackupSeedPhrase(password, seedPhrase, keyringId, keyringEncryptionKey) {
216
224
  return await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_withControllerLock).call(this, async () => {
217
225
  // to make sure that fail fast,
218
226
  // assert that the user is authenticated before creating the TOPRF key and backing up the seed phrase
@@ -246,6 +254,8 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
246
254
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_persistAuthPubKey).call(this, {
247
255
  authPubKey: authKeyPair.pk,
248
256
  });
257
+ // encrypt and store the keyring encryption key
258
+ await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_storeKeyringEncryptionKey).call(this, encKey, keyringEncryptionKey);
249
259
  };
250
260
  await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_executeWithTokenRefresh).call(this, performKeyCreationAndBackup, 'createToprfKeyAndBackupSeedPhrase');
251
261
  });
@@ -347,11 +357,13 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
347
357
  *
348
358
  * Changing password will also update the encryption key, metadata store and the vault with new encrypted values.
349
359
  *
350
- * @param newPassword - The new password to update.
351
- * @param oldPassword - The old password to verify.
360
+ * @param params - The function parameters.
361
+ * @param params.oldPassword - The old password to verify.
362
+ * @param params.newPassword - The new password to update.
363
+ * @param params.newKeyringEncryptionKey - The new keyring encryption key to store.
352
364
  * @returns A promise that resolves to the success of the operation.
353
365
  */
354
- async changePassword(newPassword, oldPassword) {
366
+ async changePassword({ oldPassword, newPassword, newKeyringEncryptionKey, }) {
355
367
  return await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_withControllerLock).call(this, async () => {
356
368
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsUnlocked).call(this);
357
369
  // verify the old password of the encrypted vault
@@ -364,7 +376,11 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
364
376
  });
365
377
  const attemptChangePassword = async () => {
366
378
  // update the encryption key with new password and update the Metadata Store
367
- const { encKey: newEncKey, authKeyPair: newAuthKeyPair } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_changeEncryptionKey).call(this, newPassword, oldPassword);
379
+ const { encKey: newEncKey, authKeyPair: newAuthKeyPair } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_changeEncryptionKey).call(this, {
380
+ oldPassword,
381
+ newPassword,
382
+ newKeyringEncryptionKey,
383
+ });
368
384
  // update and encrypt the vault with new password
369
385
  await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_createNewVaultWithAuthData).call(this, {
370
386
  password: newPassword,
@@ -503,18 +519,18 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
503
519
  * @param params.globalPassword - The latest global password.
504
520
  * @returns A promise that resolves to the password corresponding to the current authPubKey in state.
505
521
  */
506
- async recoverCurrentDevicePassword({ globalPassword, }) {
522
+ async recoverKeyringEncryptionKey({ globalPassword, }) {
507
523
  return await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_withControllerLock).call(this, async () => {
508
524
  return await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_executeWithTokenRefresh).call(this, async () => {
509
525
  const currentDeviceAuthPubKey = __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverAuthPubKey).call(this);
510
- const { password: currentDevicePassword } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverPassword).call(this, {
511
- targetPwPubKey: currentDeviceAuthPubKey,
526
+ const { keyringEncryptionKey } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverKeyringEncryptionKey).call(this, {
527
+ targetAuthPubKey: currentDeviceAuthPubKey,
512
528
  globalPassword,
513
529
  });
514
530
  return {
515
- password: currentDevicePassword,
531
+ keyringEncryptionKey,
516
532
  };
517
- }, 'recoverCurrentDevicePassword');
533
+ }, 'recoverKeyringEncryptionKey');
518
534
  });
519
535
  }
520
536
  /**
@@ -654,24 +670,27 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
654
670
  }
655
671
  }
656
672
  exports.SeedlessOnboardingController = SeedlessOnboardingController;
657
- _SeedlessOnboardingController_vaultEncryptor = new WeakMap(), _SeedlessOnboardingController_controllerOperationMutex = new WeakMap(), _SeedlessOnboardingController_vaultOperationMutex = new WeakMap(), _SeedlessOnboardingController_refreshJWTToken = new WeakMap(), _SeedlessOnboardingController_revokeRefreshToken = new WeakMap(), _SeedlessOnboardingController_isUnlocked = new WeakMap(), _SeedlessOnboardingController_instances = new WeakSet(), _SeedlessOnboardingController_recoverPassword =
673
+ _SeedlessOnboardingController_vaultEncryptor = new WeakMap(), _SeedlessOnboardingController_controllerOperationMutex = new WeakMap(), _SeedlessOnboardingController_vaultOperationMutex = new WeakMap(), _SeedlessOnboardingController_refreshJWTToken = new WeakMap(), _SeedlessOnboardingController_revokeRefreshToken = new WeakMap(), _SeedlessOnboardingController_isUnlocked = new WeakMap(), _SeedlessOnboardingController_instances = new WeakSet(), _SeedlessOnboardingController_recoverKeyringEncryptionKey =
658
674
  /**
659
- * @description Fetch the password corresponding to the targetPwPubKey.
675
+ * @description Fetch the keyring encryption key corresponding to the targetAuthPubKey.
660
676
  *
661
- * @param params - The parameters for fetching the password.
662
- * @param params.targetPwPubKey - The target public key of the password to recover.
677
+ * @param params - The parameters for fetching the keyring encryption key.
678
+ * @param params.targetAuthPubKey - The target public key of the keyring encryption key to recover.
663
679
  * @param params.globalPassword - The latest global password.
664
- * @returns A promise that resolves to the password corresponding to the current authPubKey in state.
680
+ * @returns A promise that resolves to the keyring encryption key corresponding to the current authPubKey in state.
665
681
  */
666
- async function _SeedlessOnboardingController_recoverPassword({ targetPwPubKey, globalPassword, }) {
682
+ async function _SeedlessOnboardingController_recoverKeyringEncryptionKey({ targetAuthPubKey, globalPassword, }) {
667
683
  const { encKey: latestPwEncKey, authKeyPair: latestPwAuthKeyPair } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverEncKey).call(this, globalPassword);
668
684
  try {
669
685
  const res = await this.toprfClient.recoverPassword({
670
- targetPwPubKey,
686
+ targetPwPubKey: targetAuthPubKey,
671
687
  curEncKey: latestPwEncKey,
672
688
  curAuthKeyPair: latestPwAuthKeyPair,
673
689
  });
674
- return res;
690
+ const { password: recoveredEncryptionKeyBase64 } = res;
691
+ const recoveredEncryptionKey = (0, utils_1.base64ToBytes)(recoveredEncryptionKeyBase64);
692
+ const keyringEncryptionKey = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_loadKeyringEncryptionKey).call(this, recoveredEncryptionKey);
693
+ return { keyringEncryptionKey };
675
694
  }
676
695
  catch (error) {
677
696
  if (__classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_isTokenExpiredError).call(this, error)) {
@@ -713,6 +732,33 @@ async function _SeedlessOnboardingController_persistOprfKey(oprfKey, authPubKey)
713
732
  this.update((state) => {
714
733
  state.authPubKey = (0, utils_1.bytesToBase64)(params.authPubKey);
715
734
  });
735
+ }, _SeedlessOnboardingController_storeKeyringEncryptionKey =
736
+ /**
737
+ * Encrypt the keyring encryption key and store it in state.
738
+ *
739
+ * @param encKey - The encryption key.
740
+ * @param keyringEncryptionKey - The keyring encryption key.
741
+ */
742
+ async function _SeedlessOnboardingController_storeKeyringEncryptionKey(encKey, keyringEncryptionKey) {
743
+ const aes = (0, webcrypto_1.managedNonce)(aes_1.gcm)(encKey);
744
+ const encryptedKeyringEncryptionKey = aes.encrypt((0, utils_2.utf8ToBytes)(keyringEncryptionKey));
745
+ this.update((state) => {
746
+ state.encryptedKeyringEncryptionKey = (0, utils_1.bytesToBase64)(encryptedKeyringEncryptionKey);
747
+ });
748
+ }, _SeedlessOnboardingController_loadKeyringEncryptionKey =
749
+ /**
750
+ * Decrypt the keyring encryption key from state.
751
+ *
752
+ * @param encKey - The encryption key.
753
+ * @returns The keyring encryption key.
754
+ */
755
+ async function _SeedlessOnboardingController_loadKeyringEncryptionKey(encKey) {
756
+ const { encryptedKeyringEncryptionKey: encryptedKey } = this.state;
757
+ assertIsEncryptedKeyringEncryptionKeySet(encryptedKey);
758
+ const encryptedPasswordBytes = (0, utils_1.base64ToBytes)(encryptedKey);
759
+ const aes = (0, webcrypto_1.managedNonce)(aes_1.gcm)(encKey);
760
+ const password = aes.decrypt(encryptedPasswordBytes);
761
+ return (0, utils_2.bytesToUtf8)(password);
716
762
  }, _SeedlessOnboardingController_recoverAuthPubKey = function _SeedlessOnboardingController_recoverAuthPubKey() {
717
763
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsSRPBackedUpUser).call(this, this.state);
718
764
  const { authPubKey } = this.state;
@@ -751,15 +797,17 @@ async function _SeedlessOnboardingController_recoverEncKey(password) {
751
797
  /**
752
798
  * Update the encryption key with new password and update the Metadata Store with new encryption key.
753
799
  *
754
- * @param newPassword - The new password to update.
755
- * @param oldPassword - The old password to verify.
800
+ * @param params - The function parameters.
801
+ * @param params.oldPassword - The old password to verify.
802
+ * @param params.newPassword - The new password to update.
803
+ * @param params.newKeyringEncryptionKey - The new keyring encryption key to store.
756
804
  * @returns A promise that resolves to new encryption key and authentication key pair.
757
805
  */
758
- async function _SeedlessOnboardingController_changeEncryptionKey(newPassword, oldPassword) {
806
+ async function _SeedlessOnboardingController_changeEncryptionKey({ oldPassword, newPassword, newKeyringEncryptionKey, }) {
759
807
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
760
808
  const { authConnectionId, groupedAuthConnectionId, userId } = this.state;
761
809
  const { encKey, authKeyPair, keyShareIndex: newKeyShareIndex, } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverEncKey).call(this, oldPassword);
762
- return await this.toprfClient.changeEncKey({
810
+ const result = await this.toprfClient.changeEncKey({
763
811
  nodeAuthTokens: this.state.nodeAuthTokens,
764
812
  authConnectionId,
765
813
  groupedAuthConnectionId,
@@ -767,9 +815,11 @@ async function _SeedlessOnboardingController_changeEncryptionKey(newPassword, ol
767
815
  oldEncKey: encKey,
768
816
  oldAuthKeyPair: authKeyPair,
769
817
  newKeyShareIndex,
770
- oldPassword,
818
+ oldPassword: (0, utils_1.bytesToBase64)(encKey),
771
819
  newPassword,
772
820
  });
821
+ await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_storeKeyringEncryptionKey).call(this, result.encKey, newKeyringEncryptionKey);
822
+ return result;
773
823
  }, _SeedlessOnboardingController_encryptAndStoreSecretData =
774
824
  /**
775
825
  * Encrypt and store the secret data backup in the metadata store.
@@ -1196,4 +1246,15 @@ async function withLock(mutex, callback) {
1196
1246
  releaseLock();
1197
1247
  }
1198
1248
  }
1249
+ /**
1250
+ * Assert that the provided encrypted keyring encryption key is a valid non-empty string.
1251
+ *
1252
+ * @param encryptedKeyringEncryptionKey - The encrypted keyring encryption key to check.
1253
+ * @throws If the encrypted keyring encryption key is not a valid string.
1254
+ */
1255
+ function assertIsEncryptedKeyringEncryptionKeySet(encryptedKeyringEncryptionKey) {
1256
+ if (!encryptedKeyringEncryptionKey) {
1257
+ throw new Error(constants_1.SeedlessOnboardingControllerErrorMessage.EncryptedKeyringEncryptionKeyNotSet);
1258
+ }
1259
+ }
1199
1260
  //# sourceMappingURL=SeedlessOnboardingController.cjs.map