@@ -1417,54 +1417,6 @@ describe('SeedlessOnboardingController', () => {
1417
1417
) ;
1418
1418
} ) ;
1419
1419
1420
- it ( 'should throw error if encryptionKey is missing' , async ( ) => {
1421
- await withController (
1422
- {
1423
- state : getMockInitialControllerState ( {
1424
- withMockAuthenticatedUser : true ,
1425
- vault : MOCK_VAULT ,
1426
- } ) ,
1427
- } ,
1428
- async ( { controller, toprfClient, encryptor } ) => {
1429
- mockcreateLocalKey ( toprfClient , MOCK_PASSWORD ) ;
1430
-
1431
- // persist the local enc key
1432
- jest . spyOn ( toprfClient , 'persistLocalKey' ) . mockResolvedValueOnce ( ) ;
1433
- // encrypt and store the secret data
1434
- handleMockSecretDataAdd ( ) ;
1435
-
1436
- jest . spyOn ( encryptor , 'encryptWithDetail' ) . mockResolvedValueOnce ( {
1437
- vault : MOCK_VAULT ,
1438
- // @ts -expect-error intentional test case
1439
- exportedKeyString : undefined ,
1440
- } ) ;
1441
-
1442
- await controller . createToprfKeyAndBackupSeedPhrase (
1443
- MOCK_PASSWORD ,
1444
- NEW_KEY_RING_1 . seedPhrase ,
1445
- NEW_KEY_RING_1 . id ,
1446
- ) ;
1447
-
1448
- mockFetchAuthPubKey (
1449
- toprfClient ,
1450
- base64ToBytes ( controller . state . authPubKey as string ) ,
1451
- ) ;
1452
-
1453
- await expect (
1454
- controller . addNewSecretData (
1455
- NEW_KEY_RING_2 . seedPhrase ,
1456
- SecretType . Mnemonic ,
1457
- {
1458
- keyringId : NEW_KEY_RING_2 . id ,
1459
- } ,
1460
- ) ,
1461
- ) . rejects . toThrow (
1462
- SeedlessOnboardingControllerErrorMessage . MissingCredentials ,
1463
- ) ;
1464
- } ,
1465
- ) ;
1466
- } ) ;
1467
-
1468
1420
it ( 'should throw error if encryptionSalt is different from the one in the vault' , async ( ) => {
1469
1421
await withController (
1470
1422
{
@@ -1506,54 +1458,6 @@ describe('SeedlessOnboardingController', () => {
1506
1458
) ;
1507
1459
} ) ;
1508
1460
1509
- it ( 'should throw error if encryptionKey is of an unexpected type' , async ( ) => {
1510
- await withController (
1511
- {
1512
- state : getMockInitialControllerState ( {
1513
- withMockAuthenticatedUser : true ,
1514
- vault : MOCK_VAULT ,
1515
- } ) ,
1516
- } ,
1517
- async ( { controller, toprfClient, encryptor } ) => {
1518
- mockcreateLocalKey ( toprfClient , MOCK_PASSWORD ) ;
1519
-
1520
- // persist the local enc key
1521
- jest . spyOn ( toprfClient , 'persistLocalKey' ) . mockResolvedValueOnce ( ) ;
1522
- // encrypt and store the secret data
1523
- handleMockSecretDataAdd ( ) ;
1524
-
1525
- jest . spyOn ( encryptor , 'encryptWithDetail' ) . mockResolvedValueOnce ( {
1526
- vault : MOCK_VAULT ,
1527
- // @ts -expect-error intentional test case
1528
- exportedKeyString : 123 ,
1529
- } ) ;
1530
-
1531
- await controller . createToprfKeyAndBackupSeedPhrase (
1532
- MOCK_PASSWORD ,
1533
- NEW_KEY_RING_1 . seedPhrase ,
1534
- NEW_KEY_RING_1 . id ,
1535
- ) ;
1536
-
1537
- mockFetchAuthPubKey (
1538
- toprfClient ,
1539
- base64ToBytes ( controller . state . authPubKey as string ) ,
1540
- ) ;
1541
-
1542
- await expect (
1543
- controller . addNewSecretData (
1544
- NEW_KEY_RING_2 . seedPhrase ,
1545
- SecretType . Mnemonic ,
1546
- {
1547
- keyringId : NEW_KEY_RING_2 . id ,
1548
- } ,
1549
- ) ,
1550
- ) . rejects . toThrow (
1551
- SeedlessOnboardingControllerErrorMessage . WrongPasswordType ,
1552
- ) ;
1553
- } ,
1554
- ) ;
1555
- } ) ;
1556
-
1557
1461
it ( 'should throw an error if vault unlocked has an unexpected shape' , async ( ) => {
1558
1462
await withController (
1559
1463
{
@@ -2984,7 +2888,7 @@ describe('SeedlessOnboardingController', () => {
2984
2888
} ) ;
2985
2889
} ) ;
2986
2890
2987
- describe ( 'recoverKeyringEncryptionKey ' , ( ) => {
2891
+ describe ( 'store and recover keyring encryption key ' , ( ) => {
2988
2892
const GLOBAL_PASSWORD = 'global-password' ;
2989
2893
const RECOVERED_PASSWORD = 'recovered-password' ;
2990
2894
@@ -3029,19 +2933,56 @@ describe('SeedlessOnboardingController', () => {
3029
2933
password : bytesToBase64 ( recoveredEncKey ) ,
3030
2934
} ) ;
3031
2935
3032
- const result = await controller . recoverKeyringEncryptionKey ( {
2936
+ controller . setLocked ( ) ;
2937
+
2938
+ await controller . submitGlobalPassword ( {
3033
2939
globalPassword : GLOBAL_PASSWORD ,
3034
2940
} ) ;
3035
2941
3036
- expect ( result ) . toStrictEqual ( {
3037
- keyringEncryptionKey : MOCK_KEYRING_ENCRYPTION_KEY ,
3038
- } ) ;
2942
+ const keyringEncryptionKey =
2943
+ await controller . loadKeyringEncryptionKey ( ) ;
2944
+
2945
+ expect ( keyringEncryptionKey ) . toStrictEqual (
2946
+ MOCK_KEYRING_ENCRYPTION_KEY ,
2947
+ ) ;
3039
2948
expect ( toprfClient . recoverEncKey ) . toHaveBeenCalled ( ) ;
3040
2949
expect ( toprfClient . recoverPassword ) . toHaveBeenCalled ( ) ;
3041
2950
} ,
3042
2951
) ;
3043
2952
} ) ;
3044
2953
2954
+ it ( 'should throw if key not set' , async ( ) => {
2955
+ await withController (
2956
+ {
2957
+ state : getMockInitialControllerState ( {
2958
+ withMockAuthenticatedUser : true ,
2959
+ withMockAuthPubKey : true ,
2960
+ vault : 'mock-vault' ,
2961
+ } ) ,
2962
+ } ,
2963
+ async ( { controller, toprfClient } ) => {
2964
+ await expect (
2965
+ controller . storeKeyringEncryptionKey ( '' ) ,
2966
+ ) . rejects . toThrow (
2967
+ SeedlessOnboardingControllerErrorMessage . VaultEncryptionKeyUndefined ,
2968
+ ) ;
2969
+
2970
+ // Setup and store keyring encryption key.
2971
+ await mockCreateToprfKeyAndBackupSeedPhrase (
2972
+ toprfClient ,
2973
+ controller ,
2974
+ RECOVERED_PASSWORD ,
2975
+ MOCK_SEED_PHRASE ,
2976
+ MOCK_KEYRING_ID ,
2977
+ ) ;
2978
+
2979
+ await expect ( controller . loadKeyringEncryptionKey ( ) ) . rejects . toThrow (
2980
+ SeedlessOnboardingControllerErrorMessage . EncryptedKeyringEncryptionKeyNotSet ,
2981
+ ) ;
2982
+ } ,
2983
+ ) ;
2984
+ } ) ;
2985
+
3045
2986
it ( 'should store and load keyring encryption key' , async ( ) => {
3046
2987
await withController (
3047
2988
{
@@ -3158,13 +3099,18 @@ describe('SeedlessOnboardingController', () => {
3158
3099
password : bytesToBase64 ( recoveredEncKey ) ,
3159
3100
} ) ;
3160
3101
3161
- const result = await controller . recoverKeyringEncryptionKey ( {
3102
+ controller . setLocked ( ) ;
3103
+
3104
+ await controller . submitGlobalPassword ( {
3162
3105
globalPassword : GLOBAL_PASSWORD ,
3163
3106
} ) ;
3164
3107
3165
- expect ( result ) . toStrictEqual ( {
3166
- keyringEncryptionKey : MOCK_KEYRING_ENCRYPTION_KEY ,
3167
- } ) ;
3108
+ const keyringEncryptionKey =
3109
+ await controller . loadKeyringEncryptionKey ( ) ;
3110
+
3111
+ expect ( keyringEncryptionKey ) . toStrictEqual (
3112
+ MOCK_KEYRING_ENCRYPTION_KEY ,
3113
+ ) ;
3168
3114
} ,
3169
3115
) ;
3170
3116
} ) ;
@@ -3198,7 +3144,7 @@ describe('SeedlessOnboardingController', () => {
3198
3144
} ) ;
3199
3145
3200
3146
await expect (
3201
- controller . recoverKeyringEncryptionKey ( {
3147
+ controller . submitGlobalPassword ( {
3202
3148
globalPassword : GLOBAL_PASSWORD ,
3203
3149
} ) ,
3204
3150
) . rejects . toThrow (
@@ -3215,7 +3161,7 @@ describe('SeedlessOnboardingController', () => {
3215
3161
} ,
3216
3162
async ( { controller } ) => {
3217
3163
await expect (
3218
- controller . recoverKeyringEncryptionKey ( {
3164
+ controller . submitGlobalPassword ( {
3219
3165
globalPassword : GLOBAL_PASSWORD ,
3220
3166
} ) ,
3221
3167
) . rejects . toThrow (
@@ -3244,7 +3190,7 @@ describe('SeedlessOnboardingController', () => {
3244
3190
) ;
3245
3191
3246
3192
await expect (
3247
- controller . recoverKeyringEncryptionKey ( {
3193
+ controller . submitGlobalPassword ( {
3248
3194
globalPassword : GLOBAL_PASSWORD ,
3249
3195
} ) ,
3250
3196
) . rejects . toStrictEqual (
@@ -3286,7 +3232,7 @@ describe('SeedlessOnboardingController', () => {
3286
3232
) ;
3287
3233
3288
3234
await expect (
3289
- controller . recoverKeyringEncryptionKey ( {
3235
+ controller . submitGlobalPassword ( {
3290
3236
globalPassword : GLOBAL_PASSWORD ,
3291
3237
} ) ,
3292
3238
) . rejects . toStrictEqual (
@@ -3323,7 +3269,7 @@ describe('SeedlessOnboardingController', () => {
3323
3269
. mockRejectedValueOnce ( new Error ( 'Unknown error' ) ) ;
3324
3270
3325
3271
await expect (
3326
- controller . recoverKeyringEncryptionKey ( {
3272
+ controller . submitGlobalPassword ( {
3327
3273
globalPassword : GLOBAL_PASSWORD ,
3328
3274
} ) ,
3329
3275
) . rejects . toStrictEqual (
@@ -3405,6 +3351,29 @@ describe('SeedlessOnboardingController', () => {
3405
3351
// We still need verifyPassword to work conceptually, even if unlock is bypassed
3406
3352
// verifyPasswordSpy.mockResolvedValueOnce(); // Don't mock, let the real one run inside syncLatestGlobalPassword
3407
3353
3354
+ controller . setLocked ( ) ;
3355
+
3356
+ // Mock recoverEncKey for the global password
3357
+ const encKey = mockToprfEncryptor . deriveEncKey ( GLOBAL_PASSWORD ) ;
3358
+ const authKeyPair =
3359
+ mockToprfEncryptor . deriveAuthKeyPair ( GLOBAL_PASSWORD ) ;
3360
+ jest . spyOn ( toprfClient , 'recoverEncKey' ) . mockResolvedValueOnce ( {
3361
+ encKey,
3362
+ authKeyPair,
3363
+ rateLimitResetResult : Promise . resolve ( ) ,
3364
+ keyShareIndex : 1 ,
3365
+ } ) ;
3366
+
3367
+ // Mock toprfClient.recoverPassword
3368
+ const recoveredEncKey = mockToprfEncryptor . deriveEncKey ( OLD_PASSWORD ) ;
3369
+ jest . spyOn ( toprfClient , 'recoverPassword' ) . mockResolvedValueOnce ( {
3370
+ password : bytesToBase64 ( recoveredEncKey ) ,
3371
+ } ) ;
3372
+
3373
+ await controller . submitGlobalPassword ( {
3374
+ globalPassword : GLOBAL_PASSWORD ,
3375
+ } ) ;
3376
+
3408
3377
await controller . syncLatestGlobalPassword ( {
3409
3378
globalPassword : GLOBAL_PASSWORD ,
3410
3379
} ) ;
@@ -4220,7 +4189,7 @@ describe('SeedlessOnboardingController', () => {
4220
4189
} ) ;
4221
4190
} ) ;
4222
4191
4223
- describe ( 'recoverKeyringEncryptionKey with token refresh' , ( ) => {
4192
+ describe ( 'recover keyring encryption key with token refresh' , ( ) => {
4224
4193
// const OLD_PASSWORD = 'old-mock-password';
4225
4194
// const GLOBAL_PASSWORD = 'new-global-password';
4226
4195
let MOCK_VAULT : string ;
@@ -4253,7 +4222,7 @@ describe('SeedlessOnboardingController', () => {
4253
4222
mockResult . encryptedKeyringEncryptionKey ;
4254
4223
} ) ;
4255
4224
4256
- it ( 'should retry recoverKeyringEncryptionKey after refreshing expired tokens' , async ( ) => {
4225
+ it ( 'should retry after refreshing expired tokens' , async ( ) => {
4257
4226
await withController (
4258
4227
{
4259
4228
state : getMockInitialControllerState ( {
@@ -4295,7 +4264,7 @@ describe('SeedlessOnboardingController', () => {
4295
4264
isNewUser : false ,
4296
4265
} ) ;
4297
4266
4298
- await controller . recoverKeyringEncryptionKey ( {
4267
+ await controller . submitGlobalPassword ( {
4299
4268
globalPassword : MOCK_PASSWORD ,
4300
4269
} ) ;
4301
4270
0 commit comments