[beta] Backport #5645 (Recover button) (#5654)

* Backport #5645

* Update property
This commit is contained in:
Jaco Greeff 2017-05-18 12:44:40 +02:00 committed by Gav Wood
parent 0a5c6a9ac7
commit 4b857bf4b0
9 changed files with 296 additions and 155 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.store; const { isWindowsPhrase, name, nameError, passPhraseError, password, passwordRepeat, passwordRepeatError, passwordHint, phrase } = this.props.store;
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.store;
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 { store } = this.props; const { store } = this.props;

View File

@ -62,37 +62,63 @@ 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'
/> />
) )
}; };
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];
@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
};
store = new Store(this.context.api, this.props.accounts); static defaultProps = {
restore: false
};
store = 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.store.setCreateType('fromPhrase');
this.store.nextStage();
}
return this.vaultStore.loadVaults(); return this.vaultStore.loadVaults();
} }
render () { render () {
const { isBusy, createType, stage } = this.store; const { isBusy, createType, stage } = this.store;
let steps = STAGE_IMPORT;
if (createType === 'fromNew') {
steps = STAGE_NAMES;
} else if (createType === 'fromPhrase') {
steps = STAGE_RESTORE;
}
return ( return (
<Portal <Portal
buttons={ this.renderDialogActions() } buttons={ this.renderDialogActions() }
@ -100,11 +126,7 @@ class CreateAccount extends Component {
activeStep={ stage } activeStep={ stage }
onClose={ this.onClose } onClose={ this.onClose }
open open
steps={ steps={ steps }
createType === 'fromNew'
? STAGE_NAMES
: STAGE_IMPORT
}
> >
<ModalBox icon={ <TypeIcon store={ this.store } /> }> <ModalBox icon={ <TypeIcon store={ this.store } /> }>
{ this.renderPage() } { this.renderPage() }
@ -179,6 +201,7 @@ class CreateAccount extends Component {
renderDialogActions () { renderDialogActions () {
const { createType, canCreate, isBusy, stage } = this.store; const { createType, canCreate, isBusy, stage } = this.store;
const { restore } = this.props;
const cancelBtn = ( const cancelBtn = (
<Button <Button
@ -194,6 +217,22 @@ class CreateAccount extends Component {
/> />
); );
const backBtn = restore
? null
: (
<Button
icon={ <PrevIcon /> }
key='back'
label={
<FormattedMessage
id='createAccount.button.back'
defaultMessage='Back'
/>
}
onClick={ this.store.prevStage }
/>
);
switch (stage) { switch (stage) {
case STAGE_SELECT_TYPE: case STAGE_SELECT_TYPE:
return [ return [
@ -214,17 +253,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.store.prevStage }
/>,
<Button <Button
disabled={ !canCreate || isBusy } disabled={ !canCreate || isBusy }
icon={ <CheckIcon /> } icon={ <CheckIcon /> }
@ -314,6 +343,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
@ -321,6 +356,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

@ -49,9 +49,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();
@ -71,7 +72,7 @@ export default class Store {
return !(this.nameError || this.passwordRepeatError); return !(this.nameError || this.passwordRepeatError);
case 'fromPhrase': case 'fromPhrase':
return !(this.nameError || this.passwordRepeatError); return !(this.nameError || this.passwordRepeatError || this.passPhraseError);
case 'fromRaw': case 'fromRaw':
return !(this.nameError || this.passwordRepeatError || this.rawKeyError); return !(this.nameError || this.passwordRepeatError || this.rawKeyError);
@ -81,6 +82,14 @@ export default class Store {
} }
} }
@computed get passPhraseError () {
return !this.isTest && this.phrase.length === 0;
}
@computed get hasAddress () {
return !!(this.address);
}
@computed get passwordRepeatError () { @computed get passwordRepeatError () {
return this.password === this.passwordRepeat return this.password === this.passwordRepeat
? null ? null

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

@ -54,6 +54,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 SendIcon from 'material-ui/svg-icons/content/send'; export SendIcon from 'material-ui/svg-icons/content/send';
export SettingsIcon from 'material-ui/svg-icons/action/settings'; export SettingsIcon from 'material-ui/svg-icons/action/settings';

View File

@ -53,6 +53,8 @@ class Accounts extends Component {
addressBook: false, addressBook: false,
newDialog: false, newDialog: false,
newWalletDialog: false, newWalletDialog: false,
newExportDialog: false,
restoreDialog: false,
sortOrder: '', sortOrder: '',
searchValues: [], searchValues: [],
searchTokens: [], searchTokens: [],
@ -96,6 +98,7 @@ class Accounts extends Component {
return ( return (
<div> <div>
{ this.renderNewDialog() } { this.renderNewDialog() }
{ this.renderRestoreDialog() }
{ this.renderNewWalletDialog() } { this.renderNewWalletDialog() }
{ this.renderActionbar() } { this.renderActionbar() }
@ -287,6 +290,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 }
/>,
<ActionbarExport <ActionbarExport
key='exportAccounts' key='exportAccounts'
content={ accounts } content={ accounts }
@ -333,7 +347,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
/> />
); );
} }
@ -367,6 +397,12 @@ class Accounts extends Component {
}); });
} }
onRestoreAccountClick = () => {
this.setState({
restoreDialog: true
});
}
onNewWalletClick = () => { onNewWalletClick = () => {
this.setState({ this.setState({
newWalletDialog: true newWalletDialog: true
@ -379,15 +415,18 @@ class Accounts extends Component {
}); });
} }
onRestoreAccountClose = () => {
this.setState({
restoreDialog: false
});
}
onNewWalletClose = () => { onNewWalletClose = () => {
this.setState({ this.setState({
newWalletDialog: false newWalletDialog: false
}); });
} }
onNewAccountUpdate = () => {
}
onHardwareChange = () => { onHardwareChange = () => {
const { accountsInfo } = this.props; const { accountsInfo } = this.props;
const { wallets } = this.hwstore; const { wallets } = this.hwstore;