Skip to content

Commit d63b730

Browse files
committed
UnlockWalletPassword is used instead of SpendingPassword
1 parent be98350 commit d63b730

File tree

8 files changed

+55
-47
lines changed

8 files changed

+55
-47
lines changed

examples/wallet/app/actions/account.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
Thunk,
88
AccountKeys,
99
AccountState,
10-
SpendingPassword
10+
UnlockWalletPassword
1111
} from '../reducers/types';
1212
import type {
1313
Amount,
@@ -39,40 +39,40 @@ import {
3939
import routes from '../constants/routes.json';
4040

4141
export type SetKeysAction = { type: 'SET_KEYS' } & AccountKeys;
42-
export type SetKeysWithSpendingPasswordAction = {
43-
type: 'SET_SPENDING_PASSWORD'
44-
} & SpendingPassword;
42+
export type SetKeysWithUnlockWalletPasswordAction = {
43+
type: 'SET_UNLOCK_WALLET_PASSWORD'
44+
} & UnlockWalletPassword;
4545
export const SET_KEYS = 'SET_KEYS';
46-
export const SET_SPENDING_PASSWORD = 'SET_SPENDING_PASSWORD';
46+
export const SET_UNLOCK_WALLET_PASSWORD = 'SET_UNLOCK_WALLET_PASSWORD';
4747

4848
export function setAccount(
4949
privateKey: string,
50-
spendingPassword: ?string
50+
unlockWalletPassword: ?string
5151
): Thunk<SetKeysAction> {
5252
return function setAccountThunk(dispatch) {
5353
return getAccountFromPrivateKey(privateKey).then(keys =>
54-
curry(initializeKeysAndRedirect)(dispatch, keys, spendingPassword)
54+
curry(initializeKeysAndRedirect)(dispatch, keys, unlockWalletPassword)
5555
);
5656
};
5757
}
5858

5959
export function setKeysWithUnlockWalletPassword(
60-
spendingPassword: ?string = ''
60+
unlockWalletPassword: ?string = ''
6161
): Thunk<SetKeysAction> {
6262
return function setKeysWithUnlockWalletPasswordThunk(dispatch) {
63-
const accountKeys = readEncryptedAccountInfo(spendingPassword);
63+
const accountKeys = readEncryptedAccountInfo(unlockWalletPassword);
6464
if (accountKeys) {
65-
const spendingPasswordKeys = {
65+
const unlockWalletPasswordKeys = {
6666
walletId: 'wallet01',
67-
spendingPassword
67+
unlockWalletPassword
6868
};
6969
dispatch({
70-
type: SET_SPENDING_PASSWORD,
71-
...spendingPasswordKeys
70+
type: SET_UNLOCK_WALLET_PASSWORD,
71+
...unlockWalletPasswordKeys
7272
});
7373

7474
return getAccountFromPrivateKey(accountKeys.privateKey).then(keys =>
75-
curry(initializeKeysAndRedirect)(dispatch, keys, spendingPassword)
75+
curry(initializeKeysAndRedirect)(dispatch, keys, unlockWalletPassword)
7676
);
7777
}
7878
throw new Error('Invalid password');
@@ -113,14 +113,14 @@ export function setAccountFromPrivateKey(
113113
const initializeKeysAndRedirect = (
114114
dispatch: Dispatch,
115115
keys: AccountKeys,
116-
spendingPassword: ?string = ''
116+
unlockWalletPassword: ?string = ''
117117
) => {
118118
dispatch({
119119
type: SET_KEYS,
120120
...keys
121121
});
122122

123-
saveEncryptedAccountInfo(spendingPassword, keys);
123+
saveEncryptedAccountInfo(unlockWalletPassword, keys);
124124

125125
return Promise.all([
126126
dispatch(updateAccountTransactions()),
@@ -137,13 +137,13 @@ const initializeKeysAndRedirect = (
137137
export function setAccountFromMnemonic(
138138
mnemonicPhrase: string,
139139
mnemonicPassword: string,
140-
spendingPassword: ?string
140+
unlockWalletPassword: ?string
141141
): Thunk<SetKeysAction> {
142142
if (isValidMnemonic(mnemonicPhrase)) {
143143
const seed = createSeedFromMnemonic(mnemonicPhrase, mnemonicPassword);
144144
return function setAccountThunk(dispatch) {
145145
return getAccountFromSeed(seed).then(keys =>
146-
curry(initializeKeysAndRedirect)(dispatch, keys, spendingPassword)
146+
curry(initializeKeysAndRedirect)(dispatch, keys, unlockWalletPassword)
147147
);
148148
};
149149
}

examples/wallet/app/components/RestoreWalletFromMnemonic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default ({ setAccountFromMnemonic }: Props) => {
1919
const handleSubmitMnemonic = function handleSubmitMnemonic(event) {
2020
event.preventDefault();
2121
if (isValidMnemonic(newMnemonicPhrase)) {
22-
if (checkValidSpendingPassword(password, confirmPassword)) {
22+
if (checkValidUnlockWalletPassword(password, confirmPassword)) {
2323
return setAccountFromMnemonic(
2424
newMnemonicPhrase,
2525
newMnemonicPassword,
@@ -37,7 +37,7 @@ export default ({ setAccountFromMnemonic }: Props) => {
3737

3838
const [newMnemonicPassword, setNewMnemonicPassword] = useState('');
3939

40-
const checkValidSpendingPassword = function checkValidSpendingPassword(
40+
const checkValidUnlockWalletPassword = function checkValidUnlockWalletPassword(
4141
pass,
4242
confirmation
4343
) {

examples/wallet/app/components/RestoreWalletFromPrivateKey.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ type Props = {
1515
export default ({ setAccount }: Props) => {
1616
const handleSubmit = function handleSubmit(event) {
1717
event.preventDefault();
18-
if (checkValidSpendingPassword(password, confirmPassword)) {
18+
if (checkValidUnlockWalletPassword(password, confirmPassword)) {
1919
setAccount(newPrivateKey, password);
2020
}
2121
};
2222

23-
const checkValidSpendingPassword = function checkValidSpendingPassword(
23+
const checkValidUnlockWalletPassword = function checkValidUnlockWalletPassword(
2424
pass,
2525
confirmation
2626
) {

examples/wallet/app/pages/UnlockWallet.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ export default ({ setKeysWithUnlockWalletPassword }: Props) => {
1515
const handleSubmit = function handleSubmit(event) {
1616
event.preventDefault();
1717
try {
18-
return setKeysWithUnlockWalletPassword(spendingPassword);
18+
return setKeysWithUnlockWalletPassword(unlockWalletPassword);
1919
} catch (error) {
20-
setIsWrongSpendingPassword(true);
21-
setHiddenSpendingPassword(false);
20+
setIsWrongUnlockWalletPassword(true);
21+
setHiddenUnlockWalletPassword(false);
2222
}
2323
};
2424

25-
const [spendingPassword, setSpendingPassword] = useState('');
26-
const [isWrongSpendingPassword, setIsWrongSpendingPassword] = useState(false);
27-
const [hiddenSpendingPassword, setHiddenSpendingPassword] = useState(true);
25+
const [unlockWalletPassword, setUnlockWalletPassword] = useState('');
26+
const [isWrongSpendingPassword, setIsWrongUnlockWalletPassword] = useState(
27+
false
28+
);
29+
const [hiddenSpendingPassword, setHiddenUnlockWalletPassword] = useState(
30+
true
31+
);
2832

2933
return (
3034
<Container className={styles.container}>
@@ -35,12 +39,12 @@ export default ({ setKeysWithUnlockWalletPassword }: Props) => {
3539
</Form.Label>
3640
<Form.Control
3741
type="password"
38-
id="spendingPassword"
39-
name="spendingPassword"
42+
id="unlockWalletPassword"
43+
name="unlockWalletPassword"
4044
placeholder="Password"
41-
value={spendingPassword}
45+
value={unlockWalletPassword}
4246
isInvalid={isWrongSpendingPassword}
43-
onChange={event => setSpendingPassword(event.target.value)}
47+
onChange={event => setUnlockWalletPassword(event.target.value)}
4448
/>
4549
<Form.Label className="text-danger" hidden={hiddenSpendingPassword}>
4650
<code>Incorrect password</code>

examples/wallet/app/reducers/account.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22
import sortBy from 'lodash/sortBy';
33
import {
4-
SET_SPENDING_PASSWORD,
4+
SET_UNLOCK_WALLET_PASSWORD,
55
SET_KEYS,
66
SET_ACCOUNT_STATE,
77
SEND_TRANSACTION,
@@ -13,7 +13,7 @@ import type {
1313
SendTransactionAction,
1414
SetAccountStateAction,
1515
SetTransactionsAction,
16-
SetKeysWithSpendingPasswordAction,
16+
SetKeysWithUnlockWalletPasswordAction,
1717
SendStakeDelegation
1818
} from '../actions/account';
1919
import type { Account } from './types';
@@ -23,7 +23,7 @@ export default function account(
2323
state: Account,
2424
// eslint-disable-next-line flowtype/space-after-type-colon
2525
action:
26-
| SetKeysWithSpendingPasswordAction
26+
| SetKeysWithUnlockWalletPasswordAction
2727
| SetKeysAction
2828
| SetAccountStateAction
2929
| SendTransactionAction
@@ -34,10 +34,10 @@ export default function account(
3434
return { transactions: [] };
3535
}
3636
switch (action.type) {
37-
case SET_SPENDING_PASSWORD:
37+
case SET_UNLOCK_WALLET_PASSWORD:
3838
return Object.assign({}, state, {
3939
walletId: action.walletId,
40-
spendingPassword: action.spendingPassword
40+
unlockWalletPassword: action.unlockWalletPassword
4141
});
4242
case SET_KEYS:
4343
return Object.assign({}, state, {

examples/wallet/app/reducers/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ export type AccountKeys = {
3737
identifier: Identifier
3838
};
3939

40-
export type SpendingPassword = {
40+
export type UnlockWalletPassword = {
4141
walletId: string,
42-
spendingPassword: string
42+
unlockWalletPassword: string
4343
};
4444

4545
export type AccountState = {

examples/wallet/app/utils/decrypt.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ const blake2b = data => blakejs.blake2b(data, null, 32);
66
const SECRET = 'wallet_demo#';
77
const AES_SECRET = ':aes_secret#';
88

9-
export function aesEncrypt(spendingPassword: string, text: string): string {
10-
const aesPasswordText = spendingPassword.concat(SECRET).concat(AES_SECRET);
9+
export function aesEncrypt(unlockWalletPassword: string, text: string): string {
10+
const aesPasswordText = unlockWalletPassword
11+
.concat(SECRET)
12+
.concat(AES_SECRET);
1113
const aesKey = blake2b(aesPasswordText);
1214
const aesCtr = new aesjs.ModeOfOperation.ctr(aesKey, new aesjs.Counter(5));
1315
const encryptedBytes = aesCtr.encrypt(aesjs.utils.utf8.toBytes(text));
@@ -16,10 +18,12 @@ export function aesEncrypt(spendingPassword: string, text: string): string {
1618
}
1719

1820
export function aesDecrypt(
19-
spendingPassword: string,
21+
unlockWalletPassword: string,
2022
encryptedHex: string
2123
): string {
22-
const aesPasswordText = spendingPassword.concat(SECRET).concat(AES_SECRET);
24+
const aesPasswordText = unlockWalletPassword
25+
.concat(SECRET)
26+
.concat(AES_SECRET);
2327
const aesKey = blake2b(aesPasswordText);
2428
const aesCtr = new aesjs.ModeOfOperation.ctr(aesKey, new aesjs.Counter(5));
2529
const decryptedBytes = aesCtr.decrypt(aesjs.utils.hex.toBytes(encryptedHex));

examples/wallet/app/utils/storage.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ export function isUnlockWalletPasswordCreated(): boolean {
1111
}
1212

1313
export function saveEncryptedAccountInfo(
14-
spendingPassword: ?string,
14+
unlockWalletPassword: ?string,
1515
keys: AccountKeys
1616
): void {
1717
const plainTextAccountInfo = JSON.stringify(keys);
18-
const spedingPwd: string = spendingPassword || '';
18+
const spedingPwd: string = unlockWalletPassword || '';
1919
const encryptedTextAccountInfo = aesEncrypt(spedingPwd, plainTextAccountInfo);
2020
localStorage.setItem(WALLET_ENCRYPTED_KEYS, encryptedTextAccountInfo);
2121
}
2222

2323
// eslint-disable-next-line flowtype/space-after-type-colon
2424
export function readEncryptedAccountInfo(
25-
spendingPassword: ?string
25+
unlockWalletPassword: ?string
2626
): ?AccountKeys {
2727
const encryptedHex = localStorage.getItem(WALLET_ENCRYPTED_KEYS);
2828
try {
2929
if (encryptedHex && encryptedHex.length > 0) {
30-
const plainText = aesDecrypt(spendingPassword, encryptedHex);
30+
const plainText = aesDecrypt(unlockWalletPassword, encryptedHex);
3131
const accountKeys = JSON.parse(plainText);
3232
return accountKeys;
3333
}

0 commit comments

Comments
 (0)