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

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,
@@ -467,17 +483,12 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
467
483
  * persist the latest global password authPubKey
468
484
  *
469
485
  * @param params - The parameters for syncing the latest global password.
470
- * @param params.oldPassword - The old password to verify.
471
486
  * @param params.globalPassword - The latest global password.
472
487
  * @returns A promise that resolves to the success of the operation.
473
488
  */
474
- async syncLatestGlobalPassword({ oldPassword, globalPassword, }) {
489
+ async syncLatestGlobalPassword({ globalPassword, }) {
475
490
  return await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_withControllerLock).call(this, async () => {
476
491
  const doSyncPassword = async () => {
477
- // verify correct old password
478
- await this.verifyVaultPassword(oldPassword, {
479
- skipLock: true, // skip lock since we already have the lock
480
- });
481
492
  // update vault with latest globalPassword
482
493
  const { encKey, authKeyPair } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverEncKey).call(this, globalPassword);
483
494
  // update and encrypt the vault with new password
@@ -503,18 +514,18 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
503
514
  * @param params.globalPassword - The latest global password.
504
515
  * @returns A promise that resolves to the password corresponding to the current authPubKey in state.
505
516
  */
506
- async recoverCurrentDevicePassword({ globalPassword, }) {
517
+ async recoverKeyringEncryptionKey({ globalPassword, }) {
507
518
  return await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_withControllerLock).call(this, async () => {
508
519
  return await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_executeWithTokenRefresh).call(this, async () => {
509
520
  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,
521
+ const { keyringEncryptionKey } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverKeyringEncryptionKey).call(this, {
522
+ targetAuthPubKey: currentDeviceAuthPubKey,
512
523
  globalPassword,
513
524
  });
514
525
  return {
515
- password: currentDevicePassword,
526
+ keyringEncryptionKey,
516
527
  };
517
- }, 'recoverCurrentDevicePassword');
528
+ }, 'recoverKeyringEncryptionKey');
518
529
  });
519
530
  }
520
531
  /**
@@ -654,24 +665,27 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
654
665
  }
655
666
  }
656
667
  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 =
668
+ _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
669
  /**
659
- * @description Fetch the password corresponding to the targetPwPubKey.
670
+ * @description Fetch the keyring encryption key corresponding to the targetAuthPubKey.
660
671
  *
661
- * @param params - The parameters for fetching the password.
662
- * @param params.targetPwPubKey - The target public key of the password to recover.
672
+ * @param params - The parameters for fetching the keyring encryption key.
673
+ * @param params.targetAuthPubKey - The target public key of the keyring encryption key to recover.
663
674
  * @param params.globalPassword - The latest global password.
664
- * @returns A promise that resolves to the password corresponding to the current authPubKey in state.
675
+ * @returns A promise that resolves to the keyring encryption key corresponding to the current authPubKey in state.
665
676
  */
666
- async function _SeedlessOnboardingController_recoverPassword({ targetPwPubKey, globalPassword, }) {
677
+ async function _SeedlessOnboardingController_recoverKeyringEncryptionKey({ targetAuthPubKey, globalPassword, }) {
667
678
  const { encKey: latestPwEncKey, authKeyPair: latestPwAuthKeyPair } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverEncKey).call(this, globalPassword);
668
679
  try {
669
680
  const res = await this.toprfClient.recoverPassword({
670
- targetPwPubKey,
681
+ targetPwPubKey: targetAuthPubKey,
671
682
  curEncKey: latestPwEncKey,
672
683
  curAuthKeyPair: latestPwAuthKeyPair,
673
684
  });
674
- return res;
685
+ const { password: recoveredEncryptionKeyBase64 } = res;
686
+ const recoveredEncryptionKey = (0, utils_1.base64ToBytes)(recoveredEncryptionKeyBase64);
687
+ const keyringEncryptionKey = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_loadKeyringEncryptionKey).call(this, recoveredEncryptionKey);
688
+ return { keyringEncryptionKey };
675
689
  }
676
690
  catch (error) {
677
691
  if (__classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_isTokenExpiredError).call(this, error)) {
@@ -713,6 +727,33 @@ async function _SeedlessOnboardingController_persistOprfKey(oprfKey, authPubKey)
713
727
  this.update((state) => {
714
728
  state.authPubKey = (0, utils_1.bytesToBase64)(params.authPubKey);
715
729
  });
730
+ }, _SeedlessOnboardingController_storeKeyringEncryptionKey =
731
+ /**
732
+ * Encrypt the keyring encryption key and store it in state.
733
+ *
734
+ * @param encKey - The encryption key.
735
+ * @param keyringEncryptionKey - The keyring encryption key.
736
+ */
737
+ async function _SeedlessOnboardingController_storeKeyringEncryptionKey(encKey, keyringEncryptionKey) {
738
+ const aes = (0, webcrypto_1.managedNonce)(aes_1.gcm)(encKey);
739
+ const encryptedKeyringEncryptionKey = aes.encrypt((0, utils_2.utf8ToBytes)(keyringEncryptionKey));
740
+ this.update((state) => {
741
+ state.encryptedKeyringEncryptionKey = (0, utils_1.bytesToBase64)(encryptedKeyringEncryptionKey);
742
+ });
743
+ }, _SeedlessOnboardingController_loadKeyringEncryptionKey =
744
+ /**
745
+ * Decrypt the keyring encryption key from state.
746
+ *
747
+ * @param encKey - The encryption key.
748
+ * @returns The keyring encryption key.
749
+ */
750
+ async function _SeedlessOnboardingController_loadKeyringEncryptionKey(encKey) {
751
+ const { encryptedKeyringEncryptionKey: encryptedKey } = this.state;
752
+ assertIsEncryptedKeyringEncryptionKeySet(encryptedKey);
753
+ const encryptedPasswordBytes = (0, utils_1.base64ToBytes)(encryptedKey);
754
+ const aes = (0, webcrypto_1.managedNonce)(aes_1.gcm)(encKey);
755
+ const password = aes.decrypt(encryptedPasswordBytes);
756
+ return (0, utils_2.bytesToUtf8)(password);
716
757
  }, _SeedlessOnboardingController_recoverAuthPubKey = function _SeedlessOnboardingController_recoverAuthPubKey() {
717
758
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsSRPBackedUpUser).call(this, this.state);
718
759
  const { authPubKey } = this.state;
@@ -751,15 +792,17 @@ async function _SeedlessOnboardingController_recoverEncKey(password) {
751
792
  /**
752
793
  * Update the encryption key with new password and update the Metadata Store with new encryption key.
753
794
  *
754
- * @param newPassword - The new password to update.
755
- * @param oldPassword - The old password to verify.
795
+ * @param params - The function parameters.
796
+ * @param params.oldPassword - The old password to verify.
797
+ * @param params.newPassword - The new password to update.
798
+ * @param params.newKeyringEncryptionKey - The new keyring encryption key to store.
756
799
  * @returns A promise that resolves to new encryption key and authentication key pair.
757
800
  */
758
- async function _SeedlessOnboardingController_changeEncryptionKey(newPassword, oldPassword) {
801
+ async function _SeedlessOnboardingController_changeEncryptionKey({ oldPassword, newPassword, newKeyringEncryptionKey, }) {
759
802
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
760
803
  const { authConnectionId, groupedAuthConnectionId, userId } = this.state;
761
804
  const { encKey, authKeyPair, keyShareIndex: newKeyShareIndex, } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverEncKey).call(this, oldPassword);
762
- return await this.toprfClient.changeEncKey({
805
+ const result = await this.toprfClient.changeEncKey({
763
806
  nodeAuthTokens: this.state.nodeAuthTokens,
764
807
  authConnectionId,
765
808
  groupedAuthConnectionId,
@@ -767,9 +810,11 @@ async function _SeedlessOnboardingController_changeEncryptionKey(newPassword, ol
767
810
  oldEncKey: encKey,
768
811
  oldAuthKeyPair: authKeyPair,
769
812
  newKeyShareIndex,
770
- oldPassword,
813
+ oldPassword: (0, utils_1.bytesToBase64)(encKey),
771
814
  newPassword,
772
815
  });
816
+ await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_storeKeyringEncryptionKey).call(this, result.encKey, newKeyringEncryptionKey);
817
+ return result;
773
818
  }, _SeedlessOnboardingController_encryptAndStoreSecretData =
774
819
  /**
775
820
  * Encrypt and store the secret data backup in the metadata store.
@@ -1196,4 +1241,15 @@ async function withLock(mutex, callback) {
1196
1241
  releaseLock();
1197
1242
  }
1198
1243
  }
1244
+ /**
1245
+ * Assert that the provided encrypted keyring encryption key is a valid non-empty string.
1246
+ *
1247
+ * @param encryptedKeyringEncryptionKey - The encrypted keyring encryption key to check.
1248
+ * @throws If the encrypted keyring encryption key is not a valid string.
1249
+ */
1250
+ function assertIsEncryptedKeyringEncryptionKeySet(encryptedKeyringEncryptionKey) {
1251
+ if (!encryptedKeyringEncryptionKey) {
1252
+ throw new Error(constants_1.SeedlessOnboardingControllerErrorMessage.EncryptedKeyringEncryptionKeyNotSet);
1253
+ }
1254
+ }
1199
1255
  //# sourceMappingURL=SeedlessOnboardingController.cjs.map