Newer
Older
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause
import { addressShort, Card, Form as FetherForm } from 'fether-ui';
Luke Schoen
committed
import RequireHealthOverlay from '../../../RequireHealthOverlay';
import Scanner from '../../../Scanner';
import withAccountsInfo from '../../../utils/withAccountsInfo';
import i18n, { packageNS } from '../../../i18n';
@inject('createAccountStore')
@observer
class AccountImportOptions extends Component {
state = {
phrase: '',
importingFromSigner: false
};
handleNextStep = async () => {
const {
history,
location: { pathname }
} = this.props;
const currentStep = pathname.slice(-1);
history.push(`/accounts/new/${+currentStep + 1}`);
};
handlePhraseChange = ({ target: { value: phrase } }) => {
this.setState({ phrase });
handleSubmitPhrase = async () => {
const phrase = this.state.phrase.trim();
Luke Schoen
committed
createAccountStore,
createAccountStore: { setPhrase }
this.setState({ isLoading: true, phrase });
Luke Schoen
committed
await setPhrase(phrase);
Luke Schoen
committed
Luke Schoen
committed
if (this.hasExistingAddressForImport(createAccountStore.address)) {
Luke Schoen
committed
return;
}
this.handleNextStep();
Luke Schoen
committed
} catch (error) {
this.setState({
isLoading: false,
error: i18n.t(
`${packageNS}:account.import.phrase.error_msg_submit_phrase`
)
Luke Schoen
committed
console.error(error);
}
};
handleChangeFile = async jsonString => {
const {
createAccountStore,
createAccountStore: { setJsonString }
} = this.props;
this.setState({ isLoading: true });
Luke Schoen
committed
try {
await setJsonString(jsonString);
Luke Schoen
committed
if (this.hasExistingAddressForImport(createAccountStore.address)) {
Luke Schoen
committed
return;
}
this.handleNextStep();
isLoading: false,
error: i18n.t(`${packageNS}:account.import.error_msg_change_json_file`)
console.error(error);
handleSignerImported = async ({ address, chainId: chainIdString }) => {
createAccountStore: { importFromSigner }
this.setState({
error: i18n.t(
`${packageNS}:account.import.signer.error_msg_signer_imported`
)
});
const chainId = parseInt(chainIdString);
if (this.hasExistingAddressForImport(address, chainId)) {
this.handleNextStep();
};
handleSignerImport = () => {
this.setState({
importingFromSigner: true
});
hasExistingAddressForImport = (addressForImport, chainId) => {
const isExistingAddress = Object.keys(accountsInfo).some(
key =>
key.toLowerCase() === addressForImport.toLowerCase() &&
(!accountsInfo[key].chainId ||
!chainId ||
accountsInfo[key].chainId === chainId)
Luke Schoen
committed
if (isExistingAddress) {
this.setState({
isLoading: false,
error: i18n.t(
`${packageNS}:account.import.error_msg_existing_address`,
{
address: addressShort(addressForImport)
}
)
Luke Schoen
committed
});
}
return isExistingAddress;
render () {
const {
history,
location: { pathname }
} = this.props;
const { error, importingFromSigner, phrase } = this.state;
<Card>
<div key='createAccount'>
<div className='text -centered'>
<p>
{i18n.t(
`${packageNS}:account.import.json.label_msg_recover_json`
)}
</p>
i18n={i18n}
label={i18n.t(
`${packageNS}:account.import.json.label_recover_json`
)}
packageNS={packageNS}
</Card>
);
const signerCard = (
<Card>
<div key='createAccount'>
<div className='text -centered'>
<p>
{i18n.t(
`${packageNS}:account.import.signer.label_msg_recover_signer`
)}
</p>
{importingFromSigner ? (
<Scanner
onScan={this.handleSignerImported}
label={i18n.t(
`${packageNS}:account.import.signer.label_msg_recover_signer_scan`
)}
/>
) : (
<button
className='button -footer'
onClick={this.handleSignerImport}
>
{i18n.t(
`${packageNS}:account.import.signer.label_button_recover_signer_scan`
)}
</button>
)}
</div>
</div>
</Card>
<Card>
<div key='importBackup'>
<div className='text -centered'>
<p>
{i18n.t(
`${packageNS}:account.import.phrase.label_msg_recover_phrase`
)}
</p>
label={i18n.t(
`${packageNS}:account.import.phrase.label_recover_phrase`
)}
onChange={this.handlePhraseChange}
required
phrase={phrase}
/>
{this.renderButton()}
</div>
const spacer = <div style={{ height: '0.5rem' }} />;
Luke Schoen
committed
<RequireHealthOverlay require='node'>
<div className='center-md'>
{!importingFromSigner && jsonCard}
Luke Schoen
committed
{signerCard}
Luke Schoen
committed
{!importingFromSigner && phraseCard}
Luke Schoen
committed
<p className='error-import-account'>{error}</p>
{currentStep > 1 && (
<nav className='form-nav -space-around'>
Luke Schoen
committed
<button className='button -back' onClick={history.goBack}>
{i18n.t(`${packageNS}:navigation.back`)}
Luke Schoen
committed
</button>
Luke Schoen
committed
</nav>
)}
Luke Schoen
committed
</div>
</RequireHealthOverlay>
const { isLoading, json, phrase } = this.state;
// If we are importing an existing account, the button goes to the next step
return (
<button
className='button'
disabled={(!json && !phrase) || isLoading}
onClick={this.handleSubmitPhrase}
{i18n.t(`${packageNS}:navigation.next`)}