Skip to content

Commit 52c1e29

Browse files
committed
Unlock wallet screen is added to unlock with spending password
1 parent aaf6601 commit 52c1e29

File tree

8 files changed

+122
-3
lines changed

8 files changed

+122
-3
lines changed

examples/wallet/app/Routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import ChooseRestoreOrImport from './containers/ChooseRestoreOrImport';
1010
import Delegate from './pages/Delegate';
1111
import Index from './containers/Index';
1212
import InputKeys from './containers/InputKeys';
13+
import UnlockWallet from './containers/UnlockWallet';
1314

1415
export default () => (
1516
<App>
@@ -19,6 +20,7 @@ export default () => (
1920
<Route path={routes.STAKING} component={Delegate} />
2021
<Route path={routes.SETTINGS} component={Settings} />
2122
<Route path={routes.INPUT_KEYS} component={InputKeys} />
23+
<Route path={routes.UNLOCK_WALLET} component={UnlockWallet} />
2224
<Route
2325
path={routes.CHOOSE_RESTORE_OR_IMPORT}
2426
component={ChooseRestoreOrImport}

examples/wallet/app/actions/account.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import type {
66
AppState,
77
Thunk,
88
AccountKeys,
9-
AccountState
9+
AccountState,
10+
SpendingPassword
1011
} from '../reducers/types';
1112
import type {
1213
Amount,
@@ -35,7 +36,11 @@ import { saveAccountInfoInDEN, saveSpendingPassword } from '../utils/storage';
3536
import routes from '../constants/routes.json';
3637

3738
export type SetKeysAction = { type: 'SET_KEYS' } & AccountKeys;
39+
export type SetSpendingPasswordAction = {
40+
type: 'SET_SPENDING_PASSWORD'
41+
} & SpendingPassword;
3842
export const SET_KEYS = 'SET_KEYS';
43+
export const SET_SPENDING_PASSWORD = 'SET_SPENDING_PASSWORD';
3944

4045
export function setAccount(
4146
privateKey: string,
@@ -48,6 +53,32 @@ export function setAccount(
4853
};
4954
}
5055

56+
export function setSpendingPassword(privateKey: string): Thunk<SetKeysAction> {
57+
return function setSpendingPasswordThunk(dispatch) {
58+
return getAccountFromPrivateKey(privateKey)
59+
.then(loadedPrivateKey => {
60+
dispatch({
61+
type: SET_KEYS,
62+
...loadedPrivateKey
63+
});
64+
return Promise.all([
65+
dispatch(updateAccountTransactions()),
66+
dispatch(updateNodeSettings()),
67+
dispatch(updateAccountState())
68+
])
69+
.then(() => dispatch(push(routes.WALLET)))
70+
.catch(error => {
71+
console.log(error);
72+
dispatch(push(routes.INPUT_KEYS));
73+
});
74+
})
75+
.catch(error => {
76+
// TODO: SHOW A MESSAGE SHOWING ERROR
77+
console.log(error);
78+
});
79+
};
80+
}
81+
5182
export function setAccountFromPrivateKey(
5283
privateKey: string
5384
): Thunk<SetKeysAction> {

examples/wallet/app/actions/router.js

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

8-
import { readAccountKeysFromDEN } from '../utils/storage';
8+
import {
9+
isSpedingPasswordCreated,
10+
readAccountKeysFromDEN
11+
} from '../utils/storage';
912

1013
// eslint-disable-next-line import/prefer-default-export
1114
export const redirectToFirstAppPage = () => {
1215
return (dispatch: Dispatch, getState: GetState) => {
16+
if (isSpedingPasswordCreated()) return dispatch(push(routes.UNLOCK_WALLET));
17+
1318
const accountKeys = readAccountKeysFromDEN('manteca');
1419
if (accountKeys !== undefined) {
1520
return dispatch(setAccountFromPrivateKey(accountKeys.privateKey));

examples/wallet/app/constants/routes.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"INPUT_KEYS": "/input_keys",
66
"SEND": "/send",
77
"STAKING": "/staking",
8-
"SETTINGS": "/settings"
8+
"SETTINGS": "/settings",
9+
"UNLOCK_WALLET": "/unlock"
910
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @flow
2+
import { bindActionCreators } from 'redux';
3+
import { connect } from 'react-redux';
4+
import UnlockWallet from '../pages/UnlockWallet';
5+
6+
function mapDispatchToProps(dispatch) {
7+
return bindActionCreators({}, dispatch);
8+
}
9+
10+
export default connect(
11+
undefined,
12+
mapDispatchToProps
13+
)(UnlockWallet);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// @flow
2+
import React, { useState } from 'react';
3+
import Form from 'react-bootstrap/Form';
4+
import Button from 'react-bootstrap/Button';
5+
import Container from 'react-bootstrap/Container';
6+
import Row from 'react-bootstrap/Row';
7+
import { isValidSpendingPassword } from '../utils/storage';
8+
9+
export default () => {
10+
const handleSubmit = function handleSubmit(event) {
11+
event.preventDefault();
12+
if (isValidSpendingPassword(spendingPassword)) {
13+
setIsWrongSpendingPassword(false);
14+
setHiddenSpendingPassword(true);
15+
console.log('**** OK OK OK LA CLAVES ES OK');
16+
return;
17+
}
18+
setIsWrongSpendingPassword(true);
19+
setHiddenSpendingPassword(false);
20+
};
21+
22+
const [spendingPassword, setSpendingPassword] = useState('');
23+
const [isWrongSpendingPassword, setIsWrongSpendingPassword] = useState(false);
24+
const [hiddenSpendingPassword, setHiddenSpendingPassword] = useState(true);
25+
26+
return (
27+
<Container>
28+
<Form onSubmit={handleSubmit} className="mt-5">
29+
<Form.Group>
30+
<Form.Label>
31+
Wellcome! Please insert you password to unlock wallet
32+
</Form.Label>
33+
<Form.Control
34+
type="password"
35+
id="spendingPassword"
36+
name="spendingPassword"
37+
placeholder="Password"
38+
value={spendingPassword}
39+
isInvalid={isWrongSpendingPassword}
40+
onChange={event => setSpendingPassword(event.target.value)}
41+
/>
42+
<Form.Label className="text-danger" hidden={hiddenSpendingPassword}>
43+
<code>Incorrect password</code>
44+
</Form.Label>
45+
</Form.Group>
46+
<Row className="justify-content-center">
47+
<Button variant="primary" type="submit">
48+
Unlock wallet
49+
</Button>
50+
</Row>
51+
</Form>
52+
</Container>
53+
);
54+
};

examples/wallet/app/reducers/account.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22
import sortBy from 'lodash/sortBy';
33
import {
4+
SET_SPENDING_PASSWORD,
45
SET_KEYS,
56
SET_ACCOUNT_STATE,
67
SEND_TRANSACTION,
@@ -12,6 +13,7 @@ import type {
1213
SendTransactionAction,
1314
SetAccountStateAction,
1415
SetTransactionsAction,
16+
SetSpendingPasswordAction,
1517
SendStakeDelegation
1618
} from '../actions/account';
1719
import type { Account } from './types';
@@ -21,6 +23,7 @@ export default function account(
2123
state: Account,
2224
// eslint-disable-next-line flowtype/space-after-type-colon
2325
action:
26+
| SetSpendingPasswordAction
2427
| SetKeysAction
2528
| SetAccountStateAction
2629
| SendTransactionAction
@@ -31,6 +34,11 @@ export default function account(
3134
return { transactions: [] };
3235
}
3336
switch (action.type) {
37+
case SET_SPENDING_PASSWORD:
38+
return Object.assign({}, state, {
39+
walletId: action.walletId,
40+
spendingPassword: action.spendingPassword
41+
});
3442
case SET_KEYS:
3543
return Object.assign({}, state, {
3644
address: action.address,

examples/wallet/app/reducers/types.js

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

40+
export type SpendingPassword = {
41+
walletId: ?string,
42+
spendingPassword: ?string
43+
};
44+
4045
export type AccountState = {
4146
balance: Balance,
4247
counter: Counter,

0 commit comments

Comments
 (0)