Add Recover button to Accounts and warnings (#5645)

* Add Recover button to Accounts // Add Warnings

* Change to 11

* Add test net checks for empty recovery phrase

* Fix failing tests
This commit is contained in:
Nicolas Gotchac 2017-05-18 11:50:16 +02:00 committed by Jaco Greeff
parent 2ec51fc0ad
commit 8b1f0b7cf4
9 changed files with 284 additions and 150 deletions

View File

@ -39,21 +39,6 @@ const TYPES = [
), ),
key: 'fromNew' key: 'fromNew'
}, },
{
description: (
<FormattedMessage
id='createAccount.creationType.fromPhrase.description'
defaultMessage='Recover using a previously stored recovery phrase and new password'
/>
),
label: (
<FormattedMessage
id='createAccount.creationType.fromPhrase.label'
defaultMessage='Recovery phrase'
/>
),
key: 'fromPhrase'
},
{ {
description: ( description: (
<FormattedMessage <FormattedMessage

View File

@ -19,7 +19,7 @@ import React, { Component, PropTypes } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { Checkbox } from 'material-ui'; import { Checkbox } from 'material-ui';
import { Form, Input } from '~/ui'; import { Form, Input, Warning } from '~/ui';
import PasswordStrength from '~/ui/Form/PasswordStrength'; import PasswordStrength from '~/ui/Form/PasswordStrength';
import ChangeVault from '../ChangeVault'; import ChangeVault from '../ChangeVault';
@ -33,12 +33,24 @@ export default class RecoveryPhrase extends Component {
} }
render () { render () {
const { isWindowsPhrase, name, nameError, password, passwordRepeat, passwordRepeatError, passwordHint, phrase } = this.props.createStore; const { isWindowsPhrase, name, nameError, passPhraseError, password, passwordRepeat, passwordRepeatError, passwordHint, phrase } = this.props.createStore;
return ( return (
<div className={ styles.details }>
{ this.renderWarning() }
<Form> <Form>
<Input <Input
autoFocus autoFocus
error={
passPhraseError
? (
<FormattedMessage
id='createAccount.recoveryPhrase.passPhrase.error'
defaultMessage='enter a recovery phrase'
/>
)
: null
}
hint={ hint={
<FormattedMessage <FormattedMessage
id='createAccount.recoveryPhrase.phrase.hint' id='createAccount.recoveryPhrase.phrase.hint'
@ -145,9 +157,65 @@ export default class RecoveryPhrase extends Component {
onCheck={ this.onToggleWindowsPhrase } onCheck={ this.onToggleWindowsPhrase }
/> />
</Form> </Form>
</div>
); );
} }
renderWarning () {
const { isTest, phrase } = this.props.createStore;
if (!isTest && phrase.length === 0) {
return (
<Warning
warning={
<FormattedMessage
id='createAccount.recoveryPhrase.warning.emptyPhrase'
defaultMessage={ `The recovery phrase is empty.
This account can be recovered by anyone.
` }
/>
}
/>
);
}
if (phrase.length === 0) {
return (
<Warning
warning={
<FormattedMessage
id='createAccount.recoveryPhrase.warning.testnetEmptyPhrase'
defaultMessage={ `The recovery phrase is empty.
This account can be recovered by anyone.
Proceed with caution.
` }
/>
}
/>
);
}
const words = phrase.split(' ');
if (words.length < 11) {
return (
<Warning
warning={
<FormattedMessage
id='createAccount.recoveryPhrase.warning.shortPhrase'
defaultMessage={ `The recovery phrase is less than 11 words.
This account has not been generated by Parity and might be insecure.
Proceed with caution.
` }
/>
}
/>
);
}
return null;
}
onToggleWindowsPhrase = (event) => { onToggleWindowsPhrase = (event) => {
const { createStore } = this.props; const { createStore } = this.props;

View File

@ -63,8 +63,14 @@ const TITLES = {
), ),
import: ( import: (
<FormattedMessage <FormattedMessage
id='createAccount.title.importWallet' id='createAccount.title.importAccount'
defaultMessage='import wallet' defaultMessage='import account'
/>
),
restore: (
<FormattedMessage
id='createAccount.title.restoreAccount'
defaultMessage='restore account'
/> />
), ),
qr: ( qr: (
@ -76,25 +82,37 @@ const TITLES = {
}; };
const STAGE_NAMES = [TITLES.type, TITLES.create, TITLES.info]; const STAGE_NAMES = [TITLES.type, TITLES.create, TITLES.info];
const STAGE_IMPORT = [TITLES.type, TITLES.import, TITLES.info]; const STAGE_IMPORT = [TITLES.type, TITLES.import, TITLES.info];
const STAGE_RESTORE = [TITLES.restore, TITLES.info];
const STAGE_QR = [TITLES.type, TITLES.qr, TITLES.info]; const STAGE_QR = [TITLES.type, TITLES.qr, TITLES.info];
@observer @observer
class CreateAccount extends Component { class CreateAccount extends Component {
static contextTypes = { static contextTypes = {
api: PropTypes.object.isRequired api: PropTypes.object.isRequired
} };
static propTypes = { static propTypes = {
accounts: PropTypes.object.isRequired, accounts: PropTypes.object.isRequired,
isTest: PropTypes.bool.isRequired,
newError: PropTypes.func.isRequired, newError: PropTypes.func.isRequired,
onClose: PropTypes.func, onClose: PropTypes.func,
onUpdate: PropTypes.func onUpdate: PropTypes.func,
} restore: PropTypes.bool
};
createStore = new Store(this.context.api, this.props.accounts); static defaultProps = {
restore: false
};
createStore = new Store(this.context.api, this.props.accounts, this.props.isTest);
vaultStore = VaultStore.get(this.context.api); vaultStore = VaultStore.get(this.context.api);
componentWillMount () { componentWillMount () {
if (this.props.restore) {
this.createStore.setCreateType('fromPhrase');
this.createStore.nextStage();
}
return this.vaultStore.loadVaults(); return this.vaultStore.loadVaults();
} }
@ -107,6 +125,8 @@ class CreateAccount extends Component {
steps = STAGE_NAMES; steps = STAGE_NAMES;
} else if (createType === 'fromQr') { } else if (createType === 'fromQr') {
steps = STAGE_QR; steps = STAGE_QR;
} else if (createType === 'fromPhrase') {
steps = STAGE_RESTORE;
} }
return ( return (
@ -199,6 +219,7 @@ class CreateAccount extends Component {
} }
renderDialogActions () { renderDialogActions () {
const { restore } = this.props;
const { createType, canCreate, isBusy, stage } = this.createStore; const { createType, canCreate, isBusy, stage } = this.createStore;
const cancelBtn = ( const cancelBtn = (
@ -215,6 +236,22 @@ class CreateAccount extends Component {
/> />
); );
const backBtn = restore
? null
: (
<Button
icon={ <PrevIcon /> }
key='back'
label={
<FormattedMessage
id='createAccount.button.back'
defaultMessage='Back'
/>
}
onClick={ this.createStore.prevStage }
/>
);
switch (stage) { switch (stage) {
case STAGE_SELECT_TYPE: case STAGE_SELECT_TYPE:
return [ return [
@ -235,17 +272,7 @@ class CreateAccount extends Component {
case STAGE_CREATE: case STAGE_CREATE:
return [ return [
cancelBtn, cancelBtn,
<Button backBtn,
icon={ <PrevIcon /> }
key='back'
label={
<FormattedMessage
id='createAccount.button.back'
defaultMessage='Back'
/>
}
onClick={ this.createStore.prevStage }
/>,
<Button <Button
disabled={ !canCreate || isBusy } disabled={ !canCreate || isBusy }
icon={ <CheckIcon /> } icon={ <CheckIcon /> }
@ -335,6 +362,12 @@ class CreateAccount extends Component {
} }
} }
function mapStateToProps (state) {
const { isTest } = state.nodeStatus;
return { isTest };
}
function mapDispatchToProps (dispatch) { function mapDispatchToProps (dispatch) {
return bindActionCreators({ return bindActionCreators({
newError newError
@ -342,6 +375,6 @@ function mapDispatchToProps (dispatch) {
} }
export default connect( export default connect(
null, mapStateToProps,
mapDispatchToProps mapDispatchToProps
)(CreateAccount); )(CreateAccount);

View File

@ -54,7 +54,11 @@ function createRedux () {
dispatch: sinon.stub(), dispatch: sinon.stub(),
subscribe: sinon.stub(), subscribe: sinon.stub(),
getState: () => { getState: () => {
return {}; return {
nodeStatus: {
isTest: true
}
};
} }
}; };
} }

View File

@ -50,9 +50,10 @@ export default class Store {
@observable walletFileError = ERRORS.noFile; @observable walletFileError = ERRORS.noFile;
@observable walletJson = ''; @observable walletJson = '';
constructor (api, accounts, loadGeth = true) { constructor (api, accounts, isTest, loadGeth = true) {
this._api = api; this._api = api;
this.accounts = Object.assign({}, accounts); this.accounts = Object.assign({}, accounts);
this.isTest = isTest;
if (loadGeth) { if (loadGeth) {
this.loadAvailableGethAccounts(); this.loadAvailableGethAccounts();
@ -72,7 +73,7 @@ export default class Store {
return !(this.nameError || this.passwordRepeatError) && this.hasAddress; return !(this.nameError || this.passwordRepeatError) && this.hasAddress;
case 'fromPhrase': case 'fromPhrase':
return !(this.nameError || this.passwordRepeatError); return !(this.nameError || this.passwordRepeatError || this.passPhraseError);
case 'fromQr': case 'fromQr':
return this.qrAddressValid && !this.nameError; return this.qrAddressValid && !this.nameError;
@ -85,6 +86,10 @@ export default class Store {
} }
} }
@computed get passPhraseError () {
return !this.isTest && this.phrase.length === 0;
}
@computed get hasAddress () { @computed get hasAddress () {
return !!(this.address); return !!(this.address);
} }

View File

@ -37,7 +37,7 @@ function createStore (loadGeth) {
createVaultStore(); createVaultStore();
api = createApi(); api = createApi();
store = new Store(api, ACCOUNTS, loadGeth); store = new Store(api, ACCOUNTS, true, loadGeth);
return store; return store;
} }

View File

@ -77,7 +77,7 @@ class FirstRun extends Component {
visible: PropTypes.bool.isRequired visible: PropTypes.bool.isRequired
} }
createStore = new CreateStore(this.context.api, {}, false); createStore = new CreateStore(this.context.api, {}, true, false);
state = { state = {
stage: 0, stage: 0,

View File

@ -56,6 +56,7 @@ export PrintIcon from 'material-ui/svg-icons/action/print';
export RefreshIcon from 'material-ui/svg-icons/action/autorenew'; export RefreshIcon from 'material-ui/svg-icons/action/autorenew';
export ReorderIcon from 'material-ui/svg-icons/action/reorder'; export ReorderIcon from 'material-ui/svg-icons/action/reorder';
export ReplayIcon from 'material-ui/svg-icons/av/replay'; export ReplayIcon from 'material-ui/svg-icons/av/replay';
export RestoreIcon from 'material-ui/svg-icons/action/restore';
export SaveIcon from 'material-ui/svg-icons/content/save'; export SaveIcon from 'material-ui/svg-icons/content/save';
export SearchIcon from 'material-ui/svg-icons/action/search'; export SearchIcon from 'material-ui/svg-icons/action/search';
export SendIcon from 'material-ui/svg-icons/content/send'; export SendIcon from 'material-ui/svg-icons/content/send';

View File

@ -53,6 +53,7 @@ class Accounts extends Component {
newDialog: false, newDialog: false,
newWalletDialog: false, newWalletDialog: false,
newExportDialog: false, newExportDialog: false,
restoreDialog: false,
sortOrder: '', sortOrder: '',
searchValues: [], searchValues: [],
searchTokens: [], searchTokens: [],
@ -96,6 +97,7 @@ class Accounts extends Component {
return ( return (
<div> <div>
{ this.renderNewDialog() } { this.renderNewDialog() }
{ this.renderRestoreDialog() }
{ this.renderNewWalletDialog() } { this.renderNewWalletDialog() }
{ this.renderNewExportDialog() } { this.renderNewExportDialog() }
{ this.renderActionbar() } { this.renderActionbar() }
@ -284,6 +286,17 @@ class Accounts extends Component {
} }
onClick={ this.onNewWalletClick } onClick={ this.onNewWalletClick }
/>, />,
<Button
key='restoreAccount'
icon={ <AddIcon /> }
label={
<FormattedMessage
id='accounts.button.restoreAccount'
defaultMessage='restore'
/>
}
onClick={ this.onRestoreAccountClick }
/>,
<Button <Button
key='newExport' key='newExport'
icon={ <FileDownloadIcon /> } icon={ <FileDownloadIcon /> }
@ -336,7 +349,23 @@ class Accounts extends Component {
<CreateAccount <CreateAccount
accounts={ accounts } accounts={ accounts }
onClose={ this.onNewAccountClose } onClose={ this.onNewAccountClose }
onUpdate={ this.onNewAccountUpdate } />
);
}
renderRestoreDialog () {
const { accounts } = this.props;
const { restoreDialog } = this.state;
if (!restoreDialog) {
return null;
}
return (
<CreateAccount
accounts={ accounts }
onClose={ this.onRestoreAccountClose }
restore
/> />
); );
} }
@ -384,6 +413,12 @@ class Accounts extends Component {
}); });
} }
onRestoreAccountClick = () => {
this.setState({
restoreDialog: true
});
}
onNewWalletClick = () => { onNewWalletClick = () => {
this.setState({ this.setState({
newWalletDialog: true newWalletDialog: true
@ -402,6 +437,12 @@ class Accounts extends Component {
}); });
} }
onRestoreAccountClose = () => {
this.setState({
restoreDialog: false
});
}
onNewWalletClose = () => { onNewWalletClose = () => {
this.setState({ this.setState({
newWalletDialog: false newWalletDialog: false
@ -414,9 +455,6 @@ class Accounts extends Component {
}); });
} }
onNewAccountUpdate = () => {
}
onHardwareChange = () => { onHardwareChange = () => {
const { accountsInfo } = this.props; const { accountsInfo } = this.props;
const { wallets } = this.hwstore; const { wallets } = this.hwstore;