@@ -68,6 +68,11 @@ const uint8ArraySeed = new Uint8Array(
6868const privateKey =
6969 '1e4e6a4c0c077f4ae8ddfbf372918e61dd0fb4a4cfa592cb16e7546d505e68fc' ;
7070const password = 'password123' ;
71+ const password2 = 'password456' ;
72+ const mockEncryptionKey2 = JSON . stringify ( {
73+ password : 'password2' ,
74+ salt : 'salt2' ,
75+ } ) ;
7176
7277const commonConfig = { chain : Chain . Goerli , hardfork : Hardfork . Berlin } ;
7378
@@ -795,6 +800,117 @@ describe('KeyringController', () => {
795800 ) ;
796801 } ) ;
797802
803+ describe ( 'envelope encryption' , ( ) => {
804+ it ( 'should create new vault with encryption key' , async ( ) => {
805+ await withController (
806+ { cacheEncryptionKey : true , skipVaultCreation : true } ,
807+ async ( { controller } ) => {
808+ await controller . createNewVaultAndKeychain (
809+ password ,
810+ MOCK_ENCRYPTION_KEY ,
811+ ) ;
812+
813+ expect ( controller . state . encryptionKey ) . toBeDefined ( ) ;
814+ // expect(controller.state.encryptionSalt).toBeDefined();
815+ expect ( controller . state . encryptedEncryptionKey ) . toBeDefined ( ) ;
816+ } ,
817+ ) ;
818+ } ) ;
819+
820+ it ( 'should unlock with password' , async ( ) => {
821+ await withController (
822+ { cacheEncryptionKey : true , skipVaultCreation : true } ,
823+ async ( { controller } ) => {
824+ await controller . createNewVaultAndKeychain (
825+ password ,
826+ MOCK_ENCRYPTION_KEY ,
827+ ) ;
828+
829+ expect ( controller . isUnlocked ( ) ) . toBe ( true ) ;
830+ expect ( controller . state . isUnlocked ) . toBe ( true ) ;
831+
832+ await controller . setLocked ( ) ;
833+
834+ expect ( controller . isUnlocked ( ) ) . toBe ( false ) ;
835+ expect ( controller . state . isUnlocked ) . toBe ( false ) ;
836+
837+ await controller . submitPassword ( password ) ;
838+
839+ expect ( controller . isUnlocked ( ) ) . toBe ( true ) ;
840+ expect ( controller . state . isUnlocked ) . toBe ( true ) ;
841+ } ,
842+ ) ;
843+ } ) ;
844+
845+ it ( 'should lock and unlock after change password' , async ( ) => {
846+ await withController (
847+ { cacheEncryptionKey : true , skipVaultCreation : true } ,
848+ async ( { controller } ) => {
849+ await controller . createNewVaultAndKeychain (
850+ password ,
851+ MOCK_ENCRYPTION_KEY ,
852+ ) ;
853+
854+ await controller . changePassword ( password2 ) ;
855+
856+ expect ( controller . isUnlocked ( ) ) . toBe ( true ) ;
857+ expect ( controller . state . isUnlocked ) . toBe ( true ) ;
858+
859+ await controller . setLocked ( ) ;
860+
861+ expect ( controller . isUnlocked ( ) ) . toBe ( false ) ;
862+ expect ( controller . state . isUnlocked ) . toBe ( false ) ;
863+
864+ await controller . submitPassword ( password2 ) ;
865+
866+ expect ( controller . isUnlocked ( ) ) . toBe ( true ) ;
867+ expect ( controller . state . isUnlocked ) . toBe ( true ) ;
868+ } ,
869+ ) ;
870+ } ) ;
871+
872+ it ( 'should lock and unlock after change password and key' , async ( ) => {
873+ await withController (
874+ { cacheEncryptionKey : true , skipVaultCreation : true } ,
875+ async ( { controller } ) => {
876+ await controller . createNewVaultAndKeychain (
877+ password ,
878+ MOCK_ENCRYPTION_KEY ,
879+ ) ;
880+
881+ await controller . changePasswordAndEncryptionKey (
882+ password2 ,
883+ mockEncryptionKey2 ,
884+ ) ;
885+
886+ expect ( controller . isUnlocked ( ) ) . toBe ( true ) ;
887+ expect ( controller . state . isUnlocked ) . toBe ( true ) ;
888+
889+ await controller . setLocked ( ) ;
890+
891+ expect ( controller . isUnlocked ( ) ) . toBe ( false ) ;
892+ expect ( controller . state . isUnlocked ) . toBe ( false ) ;
893+
894+ await controller . submitPassword ( password2 ) ;
895+
896+ expect ( controller . isUnlocked ( ) ) . toBe ( true ) ;
897+ expect ( controller . state . isUnlocked ) . toBe ( true ) ;
898+ } ,
899+ ) ;
900+ } ) ;
901+
902+ it ( 'should throw error if creating new vault with encryption key and cacheEncryptionKey is false' , async ( ) => {
903+ await withController (
904+ { cacheEncryptionKey : false , skipVaultCreation : true } ,
905+ async ( { controller } ) => {
906+ await expect (
907+ controller . createNewVaultAndKeychain ( password , MOCK_ENCRYPTION_KEY ) ,
908+ ) . rejects . toThrow ( KeyringControllerError . CacheEncryptionKeyDisabled ) ;
909+ } ,
910+ ) ;
911+ } ) ;
912+ } ) ;
913+
798914 describe ( 'setLocked' , ( ) => {
799915 it ( 'should set locked correctly' , async ( ) => {
800916 await withController ( async ( { controller } ) => {
0 commit comments