@@ -37,7 +37,9 @@ import {
37
37
keyringBuilderFactory ,
38
38
} from './KeyringController' ;
39
39
import MockEncryptor , {
40
+ DECRYPTION_ERROR ,
40
41
MOCK_ENCRYPTION_KEY ,
42
+ SALT ,
41
43
} from '../tests/mocks/mockEncryptor' ;
42
44
import { MockErc4337Keyring } from '../tests/mocks/mockErc4337Keyring' ;
43
45
import { MockKeyring } from '../tests/mocks/mockKeyring' ;
@@ -67,6 +69,8 @@ const uint8ArraySeed = new Uint8Array(
67
69
const privateKey =
68
70
'1e4e6a4c0c077f4ae8ddfbf372918e61dd0fb4a4cfa592cb16e7546d505e68fc' ;
69
71
const password = 'password123' ;
72
+ const freshVault =
73
+ '{"data":"{\\"tag\\":{\\"key\\":{\\"password\\":\\"password123\\",\\"salt\\":\\"salt\\"},\\"iv\\":\\"iv\\"},\\"value\\":[{\\"type\\":\\"HD Key Tree\\",\\"data\\":{\\"mnemonic\\":[119,97,114,114,105,111,114,32,108,97,110,103,117,97,103,101,32,106,111,107,101,32,98,111,110,117,115,32,117,110,102,97,105,114,32,97,114,116,105,115,116,32,107,97,110,103,97,114,111,111,32,99,105,114,99,108,101,32,101,120,112,97,110,100,32,104,111,112,101,32,109,105,100,100,108,101,32,103,97,117,103,101],\\"numberOfAccounts\\":1,\\"hdPath\\":\\"m/44\'/60\'/0\'/0\\"},\\"metadata\\":{\\"id\\":\\"01JXEFM7DAX2VJ0YFR4ESNY3GQ\\",\\"name\\":\\"\\"}}]}","iv":"iv","salt":"salt"}' ;
70
74
71
75
const commonConfig = { chain : Chain . Goerli , hardfork : Hardfork . Berlin } ;
72
76
@@ -553,15 +557,13 @@ describe('KeyringController', () => {
553
557
await withController (
554
558
{ cacheEncryptionKey } ,
555
559
async ( { controller, initialState } ) => {
556
- const initialVault = controller . state . vault ;
557
560
const initialKeyrings = controller . state . keyrings ;
558
561
await controller . createNewVaultAndRestore (
559
562
password ,
560
563
uint8ArraySeed ,
561
564
) ;
562
565
expect ( controller . state ) . not . toBe ( initialState ) ;
563
566
expect ( controller . state . vault ) . toBeDefined ( ) ;
564
- expect ( controller . state . vault ) . toStrictEqual ( initialVault ) ;
565
567
expect ( controller . state . keyrings ) . toHaveLength (
566
568
initialKeyrings . length ,
567
569
) ;
@@ -577,7 +579,7 @@ describe('KeyringController', () => {
577
579
await withController (
578
580
{ cacheEncryptionKey } ,
579
581
async ( { controller, encryptor } ) => {
580
- const encryptSpy = jest . spyOn ( encryptor , 'encrypt ' ) ;
582
+ const encryptSpy = jest . spyOn ( encryptor , 'encryptWithKey ' ) ;
581
583
const serializedKeyring = await controller . withKeyring (
582
584
{ type : 'HD Key Tree' } ,
583
585
async ( { keyring } ) => keyring . serialize ( ) ,
@@ -590,7 +592,8 @@ describe('KeyringController', () => {
590
592
currentSeedWord ,
591
593
) ;
592
594
593
- expect ( encryptSpy ) . toHaveBeenCalledWith ( password , [
595
+ const key = JSON . parse ( MOCK_ENCRYPTION_KEY ) ;
596
+ expect ( encryptSpy ) . toHaveBeenCalledWith ( key , [
594
597
{
595
598
data : serializedKeyring ,
596
599
type : 'HD Key Tree' ,
@@ -1302,7 +1305,15 @@ describe('KeyringController', () => {
1302
1305
} ,
1303
1306
] ,
1304
1307
} ;
1305
- expect ( controller . state ) . toStrictEqual ( modifiedState ) ;
1308
+ const modifiedStateWithoutVault = {
1309
+ ...modifiedState ,
1310
+ vault : undefined ,
1311
+ } ;
1312
+ const stateWithoutVault = {
1313
+ ...controller . state ,
1314
+ vault : undefined ,
1315
+ } ;
1316
+ expect ( stateWithoutVault ) . toStrictEqual ( modifiedStateWithoutVault ) ;
1306
1317
expect ( importedAccountAddress ) . toBe ( address ) ;
1307
1318
} ) ;
1308
1319
} ) ;
@@ -1381,7 +1392,15 @@ describe('KeyringController', () => {
1381
1392
} ,
1382
1393
] ,
1383
1394
} ;
1384
- expect ( controller . state ) . toStrictEqual ( modifiedState ) ;
1395
+ const modifiedStateWithoutVault = {
1396
+ ...modifiedState ,
1397
+ vault : undefined ,
1398
+ } ;
1399
+ const stateWithoutVault = {
1400
+ ...controller . state ,
1401
+ vault : undefined ,
1402
+ } ;
1403
+ expect ( stateWithoutVault ) . toStrictEqual ( modifiedStateWithoutVault ) ;
1385
1404
expect ( importedAccountAddress ) . toBe ( address ) ;
1386
1405
} ) ;
1387
1406
} ) ;
@@ -2678,10 +2697,10 @@ describe('KeyringController', () => {
2678
2697
{
2679
2698
cacheEncryptionKey,
2680
2699
skipVaultCreation : true ,
2681
- state : { vault : 'my vault' } ,
2700
+ state : { vault : freshVault } ,
2682
2701
} ,
2683
2702
async ( { controller, encryptor } ) => {
2684
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
2703
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
2685
2704
{
2686
2705
type : 'UnsupportedKeyring' ,
2687
2706
data : '0x1234' ,
@@ -2700,10 +2719,10 @@ describe('KeyringController', () => {
2700
2719
{
2701
2720
cacheEncryptionKey,
2702
2721
skipVaultCreation : true ,
2703
- state : { vault : 'my vault' } ,
2722
+ state : { vault : freshVault } ,
2704
2723
} ,
2705
2724
async ( { controller, encryptor } ) => {
2706
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
2725
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
2707
2726
{
2708
2727
foo : 'bar' ,
2709
2728
} ,
@@ -2733,11 +2752,11 @@ describe('KeyringController', () => {
2733
2752
await withController (
2734
2753
{
2735
2754
cacheEncryptionKey,
2736
- state : { vault : 'my vault' } ,
2755
+ state : { vault : freshVault } ,
2737
2756
skipVaultCreation : true ,
2738
2757
} ,
2739
2758
async ( { controller, encryptor } ) => {
2740
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
2759
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
2741
2760
{
2742
2761
type : KeyringTypes . hd ,
2743
2762
data : {
@@ -2776,7 +2795,7 @@ describe('KeyringController', () => {
2776
2795
{
2777
2796
cacheEncryptionKey : true ,
2778
2797
state : {
2779
- vault : 'my vault' ,
2798
+ vault : freshVault ,
2780
2799
} ,
2781
2800
skipVaultCreation : true ,
2782
2801
} ,
@@ -2787,9 +2806,8 @@ describe('KeyringController', () => {
2787
2806
) ;
2788
2807
jest
2789
2808
. spyOn ( encryptor , 'importKey' )
2790
- // @ts -expect-error we are assigning a mock value
2791
2809
. mockResolvedValue ( 'imported key' ) ;
2792
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
2810
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
2793
2811
{
2794
2812
type : KeyringTypes . hd ,
2795
2813
data : {
@@ -2840,7 +2858,7 @@ describe('KeyringController', () => {
2840
2858
{
2841
2859
cacheEncryptionKey : false ,
2842
2860
state : {
2843
- vault : 'my vault' ,
2861
+ vault : freshVault ,
2844
2862
} ,
2845
2863
skipVaultCreation : true ,
2846
2864
} ,
@@ -2895,14 +2913,14 @@ describe('KeyringController', () => {
2895
2913
{
2896
2914
skipVaultCreation : true ,
2897
2915
cacheEncryptionKey,
2898
- state : { vault : 'my vault' } ,
2916
+ state : { vault : freshVault } ,
2899
2917
keyringBuilders : [ keyringBuilderFactory ( MockKeyring ) ] ,
2900
2918
} ,
2901
2919
async ( { controller, encryptor, messenger } ) => {
2902
2920
const unlockListener = jest . fn ( ) ;
2903
2921
messenger . subscribe ( 'KeyringController:unlock' , unlockListener ) ;
2904
2922
jest . spyOn ( encryptor , 'isVaultUpdated' ) . mockReturnValue ( false ) ;
2905
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
2923
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
2906
2924
{
2907
2925
type : KeyringTypes . hd ,
2908
2926
data : { } ,
@@ -2926,12 +2944,12 @@ describe('KeyringController', () => {
2926
2944
{
2927
2945
skipVaultCreation : true ,
2928
2946
cacheEncryptionKey,
2929
- state : { vault : 'my vault' } ,
2947
+ state : { vault : freshVault } ,
2930
2948
} ,
2931
2949
async ( { controller, encryptor } ) => {
2932
2950
jest . spyOn ( encryptor , 'isVaultUpdated' ) . mockReturnValue ( false ) ;
2933
2951
jest . spyOn ( encryptor , 'encrypt' ) . mockRejectedValue ( new Error ( ) ) ;
2934
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
2952
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
2935
2953
{
2936
2954
type : KeyringTypes . hd ,
2937
2955
data : {
@@ -2955,13 +2973,13 @@ describe('KeyringController', () => {
2955
2973
{
2956
2974
skipVaultCreation : true ,
2957
2975
cacheEncryptionKey,
2958
- state : { vault : 'my vault' } ,
2976
+ state : { vault : freshVault } ,
2959
2977
keyringBuilders : [ keyringBuilderFactory ( MockKeyring ) ] ,
2960
2978
} ,
2961
2979
async ( { controller, encryptor, messenger } ) => {
2962
2980
const unlockListener = jest . fn ( ) ;
2963
2981
messenger . subscribe ( 'KeyringController:unlock' , unlockListener ) ;
2964
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
2982
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
2965
2983
{
2966
2984
type : KeyringTypes . hd ,
2967
2985
data : { } ,
@@ -2986,12 +3004,12 @@ describe('KeyringController', () => {
2986
3004
{
2987
3005
skipVaultCreation : true ,
2988
3006
cacheEncryptionKey,
2989
- state : { vault : 'my vault' } ,
3007
+ state : { vault : freshVault } ,
2990
3008
} ,
2991
3009
async ( { controller, encryptor } ) => {
2992
3010
jest . spyOn ( encryptor , 'isVaultUpdated' ) . mockReturnValue ( false ) ;
2993
- const encryptSpy = jest . spyOn ( encryptor , 'encrypt ' ) ;
2994
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
3011
+ const encryptSpy = jest . spyOn ( encryptor , 'encryptWithKey ' ) ;
3012
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
2995
3013
{
2996
3014
type : KeyringTypes . hd ,
2997
3015
data : {
@@ -3013,12 +3031,12 @@ describe('KeyringController', () => {
3013
3031
{
3014
3032
skipVaultCreation : true ,
3015
3033
cacheEncryptionKey,
3016
- state : { vault : 'my vault' } ,
3034
+ state : { vault : freshVault } ,
3017
3035
} ,
3018
3036
async ( { controller, encryptor } ) => {
3019
3037
jest . spyOn ( encryptor , 'isVaultUpdated' ) . mockReturnValue ( true ) ;
3020
3038
const encryptSpy = jest . spyOn ( encryptor , 'encrypt' ) ;
3021
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
3039
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
3022
3040
{
3023
3041
type : KeyringTypes . hd ,
3024
3042
data : {
@@ -3027,6 +3045,10 @@ describe('KeyringController', () => {
3027
3045
} ,
3028
3046
] ) ;
3029
3047
3048
+ // TODO actually this does trigger re-encryption. The catch is
3049
+ // that this test is run with cacheEncryptionKey enabled, so
3050
+ // `encryptWithKey` is being used instead of `encrypt`. Hence,
3051
+ // the spy on `encrypt` doesn't trigger.
3030
3052
await controller . submitPassword ( password ) ;
3031
3053
3032
3054
expect ( encryptSpy ) . not . toHaveBeenCalled ( ) ;
@@ -3040,7 +3062,7 @@ describe('KeyringController', () => {
3040
3062
{
3041
3063
skipVaultCreation : true ,
3042
3064
cacheEncryptionKey,
3043
- state : { vault : 'my vault' } ,
3065
+ state : { vault : freshVault } ,
3044
3066
} ,
3045
3067
async ( { controller, encryptor } ) => {
3046
3068
jest . spyOn ( encryptor , 'isVaultUpdated' ) . mockReturnValue ( false ) ;
@@ -3066,7 +3088,7 @@ describe('KeyringController', () => {
3066
3088
{
3067
3089
skipVaultCreation : true ,
3068
3090
cacheEncryptionKey,
3069
- state : { vault : 'my vault' } ,
3091
+ state : { vault : freshVault } ,
3070
3092
} ,
3071
3093
async ( { controller, encryptor } ) => {
3072
3094
jest . spyOn ( encryptor , 'isVaultUpdated' ) . mockReturnValue ( true ) ;
@@ -3119,14 +3141,19 @@ describe('KeyringController', () => {
3119
3141
it ( 'should throw error when using the wrong password' , async ( ) => {
3120
3142
await withController (
3121
3143
{
3122
- skipVaultCreation : true ,
3123
3144
cacheEncryptionKey,
3124
- state : { vault : 'my vault' } ,
3145
+ skipVaultCreation : true ,
3146
+ state : {
3147
+ vault : freshVault ,
3148
+ // @ts -expect-error we want to force the controller to have an
3149
+ // encryption salt equal to the one in the vault
3150
+ encryptionSalt : SALT ,
3151
+ } ,
3125
3152
} ,
3126
3153
async ( { controller } ) => {
3127
3154
await expect (
3128
3155
controller . submitPassword ( 'wrong password' ) ,
3129
- ) . rejects . toThrow ( 'Incorrect password.' ) ;
3156
+ ) . rejects . toThrow ( DECRYPTION_ERROR ) ;
3130
3157
} ,
3131
3158
) ;
3132
3159
} ) ;
@@ -3154,14 +3181,14 @@ describe('KeyringController', () => {
3154
3181
cacheEncryptionKey : true ,
3155
3182
skipVaultCreation : true ,
3156
3183
state : {
3157
- vault : JSON . stringify ( { data : '0x123' , salt : 'my salt' } ) ,
3184
+ vault : freshVault ,
3158
3185
// @ts -expect-error we want to force the controller to have an
3159
3186
// encryption salt equal to the one in the vault
3160
- encryptionSalt : 'my salt' ,
3187
+ encryptionSalt : SALT ,
3161
3188
} ,
3162
3189
} ,
3163
3190
async ( { controller, initialState, encryptor } ) => {
3164
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
3191
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
3165
3192
{
3166
3193
type : 'UnsupportedKeyring' ,
3167
3194
data : '0x1234' ,
@@ -3188,19 +3215,15 @@ describe('KeyringController', () => {
3188
3215
cacheEncryptionKey : true ,
3189
3216
skipVaultCreation : true ,
3190
3217
state : {
3191
- vault : JSON . stringify ( { data : '0x123' , salt : 'my salt' } ) ,
3218
+ vault : freshVault ,
3192
3219
// @ts -expect-error we want to force the controller to have an
3193
3220
// encryption salt equal to the one in the vault
3194
- encryptionSalt : 'my salt' ,
3221
+ encryptionSalt : SALT ,
3195
3222
} ,
3196
3223
} ,
3197
3224
async ( { controller, initialState, encryptor } ) => {
3198
3225
const encryptWithKeySpy = jest . spyOn ( encryptor , 'encryptWithKey' ) ;
3199
- jest
3200
- . spyOn ( encryptor , 'importKey' )
3201
- // @ts -expect-error we are assigning a mock value
3202
- . mockResolvedValue ( 'imported key' ) ;
3203
- jest . spyOn ( encryptor , 'decrypt' ) . mockResolvedValueOnce ( [
3226
+ jest . spyOn ( encryptor , 'decryptWithKey' ) . mockResolvedValueOnce ( [
3204
3227
{
3205
3228
type : KeyringTypes . hd ,
3206
3229
data : '0x123' ,
@@ -3213,18 +3236,21 @@ describe('KeyringController', () => {
3213
3236
) ;
3214
3237
3215
3238
expect ( controller . state . isUnlocked ) . toBe ( true ) ;
3216
- expect ( encryptWithKeySpy ) . toHaveBeenCalledWith ( 'imported key' , [
3217
- {
3218
- type : KeyringTypes . hd ,
3219
- data : {
3220
- accounts : [ '0x123' ] ,
3221
- } ,
3222
- metadata : {
3223
- id : expect . any ( String ) ,
3224
- name : '' ,
3239
+ expect ( encryptWithKeySpy ) . toHaveBeenCalledWith (
3240
+ JSON . parse ( MOCK_ENCRYPTION_KEY ) ,
3241
+ [
3242
+ {
3243
+ type : KeyringTypes . hd ,
3244
+ data : {
3245
+ accounts : [ '0x123' ] ,
3246
+ } ,
3247
+ metadata : {
3248
+ id : expect . any ( String ) ,
3249
+ name : '' ,
3250
+ } ,
3225
3251
} ,
3226
- } ,
3227
- ] ) ;
3252
+ ] ,
3253
+ ) ;
3228
3254
} ,
3229
3255
) ;
3230
3256
} ) ;
@@ -3233,7 +3259,7 @@ describe('KeyringController', () => {
3233
3259
await withController (
3234
3260
{ cacheEncryptionKey : true } ,
3235
3261
async ( { controller, initialState, encryptor } ) => {
3236
- jest . spyOn ( encryptor , 'decrypt ' ) . mockResolvedValueOnce ( [
3262
+ jest . spyOn ( encryptor , 'decryptWithKey ' ) . mockResolvedValueOnce ( [
3237
3263
{
3238
3264
foo : 'bar' ,
3239
3265
} ,
0 commit comments