Skip to content

Commit 201703f

Browse files
committed
Saving encrypted wallet function is added and ivoked with hardcoded password
1 parent 6911b90 commit 201703f

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

examples/wallet/app/actions/account.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
getTransactions
3131
} from '../utils/nodeConnection';
3232
import { isValidMnemonic, createSeedFromMnemonic } from '../utils/mnemonic';
33-
import { saveAccountInfoInLocalStorage } from '../utils/storage';
33+
import { saveAccountInfoInDEN, saveSpendingPassword } from '../utils/storage';
3434

3535
import routes from '../constants/routes.json';
3636

@@ -91,7 +91,8 @@ const initializeKeysAndRedirect = (
9191
...keys
9292
});
9393
if (saveAccount) {
94-
saveAccountInfoInLocalStorage(keys);
94+
saveSpendingPassword('manteca');
95+
saveAccountInfoInDEN('manteca', keys);
9596
}
9697

9798
return Promise.all([

examples/wallet/app/actions/router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import type { Address } from '../models';
55
import routes from '../constants/routes';
66
import { setAccountFromPrivateKey } from './account';
77

8-
import { readAccountKeysFromLocalStorage } from '../utils/storage';
8+
import { readAccountKeysFromDEN } from '../utils/storage';
99

1010
// eslint-disable-next-line import/prefer-default-export
1111
export const redirectToFirstAppPage = () => {
1212
return (dispatch: Dispatch, getState: GetState) => {
13-
const accountKeys = readAccountKeysFromLocalStorage();
13+
const accountKeys = readAccountKeysFromDEN('manteca');
1414
if (accountKeys !== undefined) {
1515
return dispatch(setAccountFromPrivateKey(accountKeys.privateKey));
1616
}

examples/wallet/app/utils/storage.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// @flow
22
import type { AccountKeys } from '../reducers/types';
3-
import type { Address, Identifier, PrivateKey } from '../models';
43
import {
54
computeBlake2bHexWithSecret,
6-
isBlake2HashHexWithSecretOk
5+
isBlake2HashHexWithSecretOk,
6+
aesEncrypt,
7+
aesDecrypt
78
} from './decrypt';
89

910
const WALLET_SPEDING_PASSWORD_KEY = 'wallet.spending.pwd';
11+
const WALLET_ENCRYPTED_KEYS = 'wallet.encrypted.account.info';
1012

1113
export function saveSpendingPassword(spendingPassword: string): void {
1214
const spendingHash = computeBlake2bHexWithSecret(spendingPassword);
@@ -26,18 +28,25 @@ export function isSpedingPasswordCreated(): boolean {
2628
return false;
2729
}
2830

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);
3139
}
3240

3341
// 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;
4150
}
4251
return undefined;
4352
}

0 commit comments

Comments
 (0)