Skip to content

Commit be98350

Browse files
committed
Hash is removed from the key that is saved from in localStorate. Now the functions to verify the keys to unlock the wallet with the encrypted record
1 parent 2333f15 commit be98350

File tree

6 files changed

+22
-57
lines changed

6 files changed

+22
-57
lines changed

examples/wallet/app/actions/account.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ import {
3333
import { isValidMnemonic, createSeedFromMnemonic } from '../utils/mnemonic';
3434
import {
3535
saveEncryptedAccountInfo,
36-
saveSpendingPassword,
37-
readAccountKeysFromDEN
36+
readEncryptedAccountInfo
3837
} from '../utils/storage';
3938

4039
import routes from '../constants/routes.json';
@@ -57,11 +56,11 @@ export function setAccount(
5756
};
5857
}
5958

60-
export function setKeysWithSpendingPassword(
59+
export function setKeysWithUnlockWalletPassword(
6160
spendingPassword: ?string = ''
6261
): Thunk<SetKeysAction> {
63-
return function setKeysWithSpendingPasswordThunk(dispatch) {
64-
const accountKeys = readAccountKeysFromDEN(spendingPassword);
62+
return function setKeysWithUnlockWalletPasswordThunk(dispatch) {
63+
const accountKeys = readEncryptedAccountInfo(spendingPassword);
6564
if (accountKeys) {
6665
const spendingPasswordKeys = {
6766
walletId: 'wallet01',
@@ -121,7 +120,6 @@ const initializeKeysAndRedirect = (
121120
...keys
122121
});
123122

124-
saveSpendingPassword(spendingPassword);
125123
saveEncryptedAccountInfo(spendingPassword, keys);
126124

127125
return Promise.all([

examples/wallet/app/actions/router.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import type { Dispatch, GetState } from '../reducers/types';
44
import type { Address } from '../models';
55
import routes from '../constants/routes';
66

7-
import { isSpedingPasswordCreated } from '../utils/storage';
7+
import { isUnlockWalletPasswordCreated } from '../utils/storage';
88

99
// eslint-disable-next-line import/prefer-default-export
1010
export const redirectToFirstAppPage = () => {
1111
return (dispatch: Dispatch, getState: GetState) => {
12-
if (isSpedingPasswordCreated()) return dispatch(push(routes.UNLOCK_WALLET));
12+
if (isUnlockWalletPasswordCreated())
13+
return dispatch(push(routes.UNLOCK_WALLET));
1314
const {
1415
account: { address }
1516
}: { account: { address: Address } } = getState();

examples/wallet/app/containers/UnlockWallet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import { bindActionCreators } from 'redux';
33
import { connect } from 'react-redux';
44
import UnlockWallet from '../pages/UnlockWallet';
5-
import { setKeysWithSpendingPassword } from '../actions/account';
5+
import { setKeysWithUnlockWalletPassword } from '../actions/account';
66

77
function mapDispatchToProps(dispatch) {
8-
return bindActionCreators({ setKeysWithSpendingPassword }, dispatch);
8+
return bindActionCreators({ setKeysWithUnlockWalletPassword }, dispatch);
99
}
1010

1111
export default connect(

examples/wallet/app/pages/UnlockWallet.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ import Button from 'react-bootstrap/Button';
55
import Container from 'react-bootstrap/Container';
66
import Row from 'react-bootstrap/Row';
77
import styles from './UnlockWallet.scss';
8-
import { isValidSpendingPassword } from '../utils/storage';
9-
import typeof { setKeysWithSpendingPassword as SetKeysWithSpendingPassword } from '../actions/account';
8+
import typeof { setKeysWithUnlockWalletPassword as SetKeysWithSpendingPassword } from '../actions/account';
109

1110
type Props = {
12-
setKeysWithSpendingPassword: SetKeysWithSpendingPassword
11+
setKeysWithUnlockWalletPassword: SetKeysWithSpendingPassword
1312
};
1413

15-
export default ({ setKeysWithSpendingPassword }: Props) => {
14+
export default ({ setKeysWithUnlockWalletPassword }: Props) => {
1615
const handleSubmit = function handleSubmit(event) {
1716
event.preventDefault();
18-
if (isValidSpendingPassword(spendingPassword)) {
19-
setIsWrongSpendingPassword(false);
20-
setHiddenSpendingPassword(true);
21-
return setKeysWithSpendingPassword(spendingPassword);
17+
try {
18+
return setKeysWithUnlockWalletPassword(spendingPassword);
19+
} catch (error) {
20+
setIsWrongSpendingPassword(true);
21+
setHiddenSpendingPassword(false);
2222
}
23-
setIsWrongSpendingPassword(true);
24-
setHiddenSpendingPassword(false);
2523
};
2624

2725
const [spendingPassword, setSpendingPassword] = useState('');

examples/wallet/app/utils/decrypt.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,9 @@ import aesjs from 'aes-js';
33
import blakejs from 'blakejs';
44

55
const blake2b = data => blakejs.blake2b(data, null, 32);
6-
const blake2bHex = data => blakejs.blake2bHex(data, null, 32);
76
const SECRET = 'wallet_demo#';
87
const AES_SECRET = ':aes_secret#';
98

10-
export function computeBlake2bHexWithSecret(spendingPassword: string): string {
11-
const fullSpendingPassword = spendingPassword.concat(SECRET);
12-
return blake2bHex(fullSpendingPassword);
13-
}
14-
15-
export function isBlake2HashHexWithSecretOk(
16-
spendingPassword: string,
17-
blake2bHashHex: string
18-
): boolean {
19-
const fullSpendingPassword = spendingPassword.concat(SECRET);
20-
return blake2bHex(fullSpendingPassword) === blake2bHashHex;
21-
}
22-
239
export function aesEncrypt(spendingPassword: string, text: string): string {
2410
const aesPasswordText = spendingPassword.concat(SECRET).concat(AES_SECRET);
2511
const aesKey = blake2b(aesPasswordText);

examples/wallet/app/utils/storage.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
// @flow
22
import type { AccountKeys } from '../reducers/types';
3-
import {
4-
computeBlake2bHexWithSecret,
5-
isBlake2HashHexWithSecretOk,
6-
aesEncrypt,
7-
aesDecrypt
8-
} from './decrypt';
3+
import { aesEncrypt, aesDecrypt } from './decrypt';
94

10-
const WALLET_SPEDING_PASSWORD_KEY = 'wallet.spending.pwd';
115
const WALLET_ENCRYPTED_KEYS = 'wallet.encrypted.account.info';
126

13-
export function saveSpendingPassword(spendingPassword: ?string = ''): void {
14-
const spendingHash = computeBlake2bHexWithSecret(spendingPassword);
15-
localStorage.setItem(WALLET_SPEDING_PASSWORD_KEY, spendingHash);
16-
}
17-
18-
export function isValidSpendingPassword(spendingPassword: string): boolean {
19-
const savedSpendingHash = localStorage.getItem(WALLET_SPEDING_PASSWORD_KEY);
20-
if (!savedSpendingHash)
21-
throw new Error('There is not a spending password created');
22-
return isBlake2HashHexWithSecretOk(spendingPassword, savedSpendingHash);
23-
}
24-
25-
export function isSpedingPasswordCreated(): boolean {
26-
const savedSpendingHash = localStorage.getItem(WALLET_SPEDING_PASSWORD_KEY);
27-
if (savedSpendingHash) return true;
7+
export function isUnlockWalletPasswordCreated(): boolean {
8+
const encryptedAccountInfo = localStorage.getItem(WALLET_ENCRYPTED_KEYS);
9+
if (encryptedAccountInfo) return true;
2810
return false;
2911
}
3012

@@ -39,7 +21,7 @@ export function saveEncryptedAccountInfo(
3921
}
4022

4123
// eslint-disable-next-line flowtype/space-after-type-colon
42-
export function readAccountKeysFromDEN(
24+
export function readEncryptedAccountInfo(
4325
spendingPassword: ?string
4426
): ?AccountKeys {
4527
const encryptedHex = localStorage.getItem(WALLET_ENCRYPTED_KEYS);

0 commit comments

Comments
 (0)