Skip to content

Commit 87a0493

Browse files
dmerniestic1987dmernies-iohk
authored andcommitted
Corrections by code review
1 parent b4e81a0 commit 87a0493

File tree

15 files changed

+103
-176
lines changed

15 files changed

+103
-176
lines changed

examples/explorer/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"rules": {
1717
"func-names": "error",
18-
"new-cap": "off",
18+
"new-cap": ["off", { "capIsNewExceptionPattern": "^Aesjs\.." }],
1919
"arrow-parens": "off",
2020
"consistent-return": "off",
2121
"comma-dangle": "off",

examples/wallet/app/Routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Settings from './pages/Settings';
99
import ChooseRestoreOrImport from './containers/ChooseRestoreOrImport';
1010
import Delegate from './pages/Delegate';
1111
import Index from './containers/Index';
12-
import InputKeys from './containers/InputKeys';
12+
import InputKeys from './pages/InputKeys';
1313
import UnlockWallet from './containers/UnlockWallet';
1414

1515
export default () => (

examples/wallet/app/actions/account.js

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import type {
1616
Delegation,
1717
Identifier,
1818
TransactionHash,
19-
Transaction,
20-
Balance,
21-
Counter
19+
Transaction
2220
} from '../models';
2321
import { updateNodeSettings } from './nodeSettings';
2422
import {
@@ -34,7 +32,7 @@ import {
3432
} from '../utils/nodeConnection';
3533
import { isValidMnemonic, createSeedFromMnemonic } from '../utils/mnemonic';
3634
import {
37-
saveAccountInfoInDEN,
35+
saveEncryptedAccountInfo,
3836
saveSpendingPassword,
3937
readAccountKeysFromDEN
4038
} from '../utils/storage';
@@ -46,8 +44,7 @@ export type SetKeysWithSpendingPasswordAction = {
4644
type: 'SET_SPENDING_PASSWORD'
4745
} & SpendingPassword;
4846
export const SET_KEYS = 'SET_KEYS';
49-
export const PRIVATE_KEY_ERROR = 'privateKeyError';
50-
export const ACCOUNT_STATE_ERROR = 'accountStateError';
47+
export const SET_SPENDING_PASSWORD = 'SET_SPENDING_PASSWORD';
5148

5249
export function setAccount(
5350
privateKey: string,
@@ -60,32 +57,49 @@ export function setAccount(
6057
};
6158
}
6259

63-
export function setAccountFromPrivateKey(
64-
privateKey: string
60+
export function setKeysWithSpendingPassword(
61+
spendingPassword: ?string = ''
6562
): Thunk<SetKeysAction> {
66-
return function setAccountFromPrivateKeyThunk(dispatch) {
67-
return getAccountFromPrivateKey(privateKey)
68-
.then(loadedPrivateKey => {
69-
dispatch({
70-
type: SET_KEYS,
71-
...loadedPrivateKey
72-
});
73-
return dispatch(updateAccountTransactionsAndState());
74-
})
75-
.catch(error => {
76-
console.error(error);
77-
throw new Error(PRIVATE_KEY_ERROR);
63+
return function setKeysWithSpendingPasswordThunk(dispatch) {
64+
const accountKeys = readAccountKeysFromDEN(spendingPassword);
65+
if (accountKeys) {
66+
const spendingPasswordKeys = {
67+
walletId: 'wallet01',
68+
spendingPassword
69+
};
70+
dispatch({
71+
type: SET_SPENDING_PASSWORD,
72+
...spendingPasswordKeys
7873
});
74+
75+
return getAccountFromPrivateKey(accountKeys.privateKey).then(keys =>
76+
curry(initializeKeysAndRedirect)(dispatch, keys, spendingPassword)
77+
);
78+
}
79+
throw new Error('Invalid password');
7980
};
8081
}
8182

82-
export function updateAccountTransactionsAndState(): Thunk<SetKeysAction> {
83-
return function updateAccountTransactionsAndStateThunk(dispatch) {
84-
return Promise.all([
85-
dispatch(updateAccountTransactions()),
86-
dispatch(updateNodeSettings()),
87-
dispatch(updateAccountState())
88-
]).then(() => dispatch(push(routes.WALLET)));
83+
export function setAccountFromPrivateKey(
84+
privateKey: string
85+
): Thunk<SetKeysAction> {
86+
return function setAccountFromPrivateKeyThunk(dispatch) {
87+
return getAccountFromPrivateKey(privateKey).then(loadedPrivateKey => {
88+
dispatch({
89+
type: SET_KEYS,
90+
...loadedPrivateKey
91+
});
92+
return Promise.all([
93+
dispatch(updateAccountTransactions()),
94+
dispatch(updateNodeSettings()),
95+
dispatch(updateAccountState())
96+
])
97+
.then(() => dispatch(push(routes.WALLET)))
98+
.catch(error => {
99+
console.log(error);
100+
dispatch(push(routes.INPUT_KEYS));
101+
});
102+
});
89103
};
90104
}
91105

@@ -108,9 +122,18 @@ const initializeKeysAndRedirect = (
108122
});
109123

110124
saveSpendingPassword(spendingPassword);
111-
saveAccountInfoInDEN(spendingPassword, keys);
125+
saveEncryptedAccountInfo(spendingPassword, keys);
112126

113-
return dispatch(updateAccountTransactionsAndState());
127+
return Promise.all([
128+
dispatch(updateAccountTransactions()),
129+
dispatch(updateNodeSettings()),
130+
dispatch(updateAccountState())
131+
])
132+
.then(() => dispatch(push(routes.WALLET)))
133+
.catch(error => {
134+
console.log(error);
135+
dispatch(push(routes.WALLET));
136+
});
114137
};
115138

116139
export function setAccountFromMnemonic(
@@ -155,7 +178,7 @@ export function updateAccountState(): Thunk<SetAccountStateAction> {
155178
// TODO: display a notification or something
156179
.catch(() => {
157180
console.error('there was an error fetching account info');
158-
return Promise.reject(new Error(ACCOUNT_STATE_ERROR));
181+
return Promise.reject();
159182
})
160183
);
161184
};
@@ -187,7 +210,7 @@ export function updateAccountTransactions(): Thunk<SetAccountStateAction> {
187210
// TODO: display a notification or something
188211
.catch(() => {
189212
console.error('there was an error fetching transactions');
190-
return Promise.reject(new Error(ACCOUNT_STATE_ERROR));
213+
return Promise.reject();
191214
})
192215
);
193216
};

examples/wallet/app/components/RestoreWalletFromMnemonic.js

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,27 @@ type Props = {
1111
setAccountFromMnemonic: SetAccountFromMnemonic
1212
};
1313

14-
// FIXME: this has no error handling, neither while parsing the address
15-
// nor when fetching the balance.
1614
export default ({ setAccountFromMnemonic }: Props) => {
1715
const checkIsValidMnemonicPhrase = function checkIsValidMnemonicPhrase() {
1816
setIsMnemonicValid(isValidMnemonic(newMnemonicPhrase));
1917
};
2018

2119
const handleSubmitMnemonic = function handleSubmitMnemonic(event) {
2220
event.preventDefault();
23-
if (mustCreateSpendingPassword) {
24-
if (
25-
isValidMnemonic(newMnemonicPhrase) &&
26-
checkValidSpendingPassword(password, confirmPassword)
27-
) {
28-
return Promise.all([
29-
setAccountFromMnemonic(
30-
newMnemonicPhrase,
31-
newMnemonicPassword,
32-
password
33-
)
34-
]);
35-
}
36-
}
3721
if (isValidMnemonic(newMnemonicPhrase)) {
38-
return Promise.all([
39-
setAccountFromMnemonic(newMnemonicPhrase, newMnemonicPassword, '')
40-
]);
22+
if (checkValidSpendingPassword(password, confirmPassword)) {
23+
return setAccountFromMnemonic(
24+
newMnemonicPhrase,
25+
newMnemonicPassword,
26+
password
27+
);
28+
}
29+
} else {
30+
setIsMnemonicValid(false);
4131
}
42-
43-
setIsMnemonicValid(false);
44-
};
45-
46-
const handleCheckCreateSpendingPassword = function handleCheckCreateSpendingPassword(
47-
evt
48-
) {
49-
setMustCreateSpendingPassword(evt.target.checked);
50-
setHiddenSpendingPassword(!evt.target.checked);
5132
};
5233

5334
const [isMnemonicValid, setIsMnemonicValid] = useState(true);
54-
const [hiddenSpendingPassword, setHiddenSpendingPassword] = useState(false);
55-
const [mustCreateSpendingPassword, setMustCreateSpendingPassword] = useState(
56-
true
57-
);
5835

5936
const [newMnemonicPhrase, setNewMnemonicPhrase] = useState('');
6037

@@ -118,21 +95,17 @@ export default ({ setAccountFromMnemonic }: Props) => {
11895
<Form.Control
11996
type="password"
12097
name="mnemonicPassword"
121-
placeholder="Secret password"
98+
placeholder="Secret password of wallet seed"
12299
value={newMnemonicPassword}
123100
onChange={event => setNewMnemonicPassword(event.target.value)}
124101
className="mt-3"
125102
/>
126-
<Form.Group controlId="formCreateSpendingPassword" className="mt-4">
127-
<Form.Check
128-
type="switch"
129-
label="Create a password to store your settings securely in an encrypted
130-
storage"
131-
onChange={event => handleCheckCreateSpendingPassword(event)}
132-
checked={mustCreateSpendingPassword}
133-
/>
134-
</Form.Group>
135-
<Form.Group hidden={hiddenSpendingPassword}>
103+
<Form.Text>
104+
This secret password is part of the BIP39 standard to generate safer
105+
wallet seeds.
106+
</Form.Text>
107+
<Form.Label className="mt-5">Unlock wallet (optional):</Form.Label>
108+
<Form.Group>
136109
<Form.Control
137110
type="password"
138111
id="password"
@@ -154,6 +127,10 @@ export default ({ setAccountFromMnemonic }: Props) => {
154127
onChange={event => setConfirmPassword(event.target.value)}
155128
className="mt-3"
156129
/>
130+
<Form.Text>
131+
This key allows you to unlock your wallet every time you start it
132+
and to keep your account data in a more secure way.
133+
</Form.Text>
157134
<Form.Label
158135
className="text-danger"
159136
hidden={arePasswordAndConfirmationEqual}

examples/wallet/app/components/RestoreWalletFromPrivateKey.js

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,13 @@ type Props = {
1313
// FIXME: this has no error handling, neither while parsing the address
1414
// nor when fetching the balance.
1515
export default ({ setAccount }: Props) => {
16-
const handleSubmitCreateSpending = function handleSubmitCreateSpending(
17-
event
18-
) {
16+
const handleSubmit = function handleSubmit(event) {
1917
event.preventDefault();
20-
if (mustCreateSpendingPassword) {
21-
if (checkValidSpendingPassword(password, confirmPassword)) {
22-
setAccount(newPrivateKey, password);
23-
}
24-
} else {
25-
setAccount(newPrivateKey, '');
18+
if (checkValidSpendingPassword(password, confirmPassword)) {
19+
setAccount(newPrivateKey, password);
2620
}
2721
};
2822

29-
const handleCheckCreateSpendingPassword = function handleCheckCreateSpendingPassword(
30-
evt
31-
) {
32-
setMustCreateSpendingPassword(evt.target.checked);
33-
setHiddenSpendingPassword(!evt.target.checked);
34-
};
35-
3623
const checkValidSpendingPassword = function checkValidSpendingPassword(
3724
pass,
3825
confirmation
@@ -54,10 +41,6 @@ export default ({ setAccount }: Props) => {
5441

5542
const [isValidPassword, setIsValidPassword] = useState(true);
5643
const [newPrivateKey, setNewPrivateKey] = useState('');
57-
const [mustCreateSpendingPassword, setMustCreateSpendingPassword] = useState(
58-
true
59-
);
60-
const [hiddenSpendingPassword, setHiddenSpendingPassword] = useState(false);
6144

6245
const [
6346
arePasswordAndConfirmationEqual,
@@ -70,7 +53,7 @@ export default ({ setAccount }: Props) => {
7053

7154
return (
7255
<Container>
73-
<Form onSubmit={handleSubmitCreateSpending} className="mt-5">
56+
<Form onSubmit={handleSubmit} className="mt-5">
7457
<Form.Group>
7558
<Form.Label>Private key:</Form.Label>
7659
<Form.Control
@@ -87,19 +70,7 @@ export default ({ setAccount }: Props) => {
8770
ed25519e_sk15psr45hyqnpwcl8xd4lv0m32prenhh8kcltgte2305h5jgynndxect9274j0am0qmmd0snjuadnm6xkgssnkn2njvkg8et8qg0vevsgnwvmpl
8871
</code>
8972
</Form.Text>
90-
<Form.Group
91-
controlId="formCreateSpendingPasswordCheck"
92-
className="mt-4"
93-
>
94-
<Form.Check
95-
type="switch"
96-
label="Create a password to store your settings securely in an encrypted
97-
storage"
98-
checked={mustCreateSpendingPassword}
99-
onChange={event => handleCheckCreateSpendingPassword(event)}
100-
/>
101-
</Form.Group>
102-
<Form.Group hidden={hiddenSpendingPassword}>
73+
<Form.Group>
10374
<Form.Control
10475
type="password"
10576
id="password"

examples/wallet/app/containers/InputKeys.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/wallet/app/containers/RestoreWalletFromMnemonic.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 RestoreWalletFromMnemonic from '../components/RestoreWalletFromMnemonic';
5-
import { setAccount } from '../actions/account';
5+
import { setAccountFromMnemonic } from '../actions/account';
66

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

1111
export default connect(

0 commit comments

Comments
 (0)