1
1
// @flow
2
2
import type { AccountKeys } from '../reducers/types' ;
3
- import type { Address , Identifier , PrivateKey } from '../models' ;
4
3
import {
5
4
computeBlake2bHexWithSecret ,
6
- isBlake2HashHexWithSecretOk
5
+ isBlake2HashHexWithSecretOk ,
6
+ aesEncrypt ,
7
+ aesDecrypt
7
8
} from './decrypt' ;
8
9
9
10
const WALLET_SPEDING_PASSWORD_KEY = 'wallet.spending.pwd' ;
11
+ const WALLET_ENCRYPTED_KEYS = 'wallet.encrypted.account.info' ;
10
12
11
13
export function saveSpendingPassword ( spendingPassword : string ) : void {
12
14
const spendingHash = computeBlake2bHexWithSecret ( spendingPassword ) ;
@@ -26,18 +28,25 @@ export function isSpedingPasswordCreated(): boolean {
26
28
return false ;
27
29
}
28
30
29
- export function saveAccountInfoInLocalStorage ( keys : AccountKeys ) : void {
30
- Object . keys ( keys ) . forEach ( key => localStorage . setItem ( key , keys [ key ] ) ) ;
31
+ export function saveAccountInfoInDEN (
32
+ spendingPassword : ?string ,
33
+ keys : AccountKeys
34
+ ) : void {
35
+ const plainTextAccountInfo = JSON . stringify ( keys ) ;
36
+ const spedingPwd : string = ! spendingPassword ? '' : spendingPassword ;
37
+ const encryptedTextAccountInfo = aesEncrypt ( spedingPwd , plainTextAccountInfo ) ;
38
+ localStorage . setItem ( WALLET_ENCRYPTED_KEYS , encryptedTextAccountInfo ) ;
31
39
}
32
40
33
41
// eslint-disable-next-line flowtype/space-after-type-colon
34
- export function readAccountKeysFromLocalStorage ( ) : ?AccountKeys {
35
- if ( localStorage . getItem ( 'privateKey' ) ) {
36
- return {
37
- address : ( localStorage . getItem ( 'address' ) : Address ) ,
38
- identifier : ( localStorage . getItem ( 'identifier' ) : Identifier ) ,
39
- privateKey : ( localStorage . getItem ( 'privateKey' ) : PrivateKey )
40
- } ;
42
+ export function readAccountKeysFromDEN (
43
+ spendingPassword : ?string
44
+ ) : ?AccountKeys {
45
+ const encryptedHex = localStorage . getItem ( WALLET_ENCRYPTED_KEYS ) ;
46
+ if ( encryptedHex && encryptedHex . length > 0 ) {
47
+ const plainText = aesDecrypt ( spendingPassword , encryptedHex ) ;
48
+ const accountKeys = JSON . parse ( plainText ) ;
49
+ return accountKeys ;
41
50
}
42
51
return undefined ;
43
52
}
0 commit comments