AccountImportOptions.js 5.89 KiB
Newer Older
YJ's avatar
YJ committed
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

YJ's avatar
YJ committed
import React, { Component } from 'react';
import { addressShort, Card, Form as FetherForm } from 'fether-ui';
YJ's avatar
YJ committed
import { inject, observer } from 'mobx-react';

import RequireHealthOverlay from '../../../RequireHealthOverlay';
import Scanner from '../../../Scanner';
Axel Chalon's avatar
Axel Chalon committed
import withAccountsInfo from '../../../utils/withAccountsInfo';
Axel Chalon's avatar
Axel Chalon committed
@withAccountsInfo
YJ's avatar
YJ committed
@inject('createAccountStore')
@observer
class AccountImportOptions extends Component {
  state = {
YJ's avatar
YJ committed
    isLoading: false,
    phrase: '',
    importingFromSigner: false
YJ's avatar
YJ committed
  };

  handleNextStep = async () => {
    const {
      history,
YJ's avatar
YJ committed
    } = this.props;
    const currentStep = pathname.slice(-1);
    history.push(`/accounts/new/${+currentStep + 1}`);
  };

  handlePhraseChange = ({ target: { value: phrase } }) => {
    this.setState({ phrase });
YJ's avatar
YJ committed
  };

  handleSubmitPhrase = async () => {
    const phrase = this.state.phrase.trim();
YJ's avatar
YJ committed
    const {
      createAccountStore: { setPhrase }
YJ's avatar
YJ committed
    } = this.props;

    this.setState({ isLoading: true, phrase });
YJ's avatar
YJ committed
    try {
      if (this.hasExistingAddressForImport(createAccountStore.address)) {
      this.setState({
        isLoading: false,
        error:
          'The passphrase was not recognized. Please verify that you entered your passphrase correctly.'
      });
  handleChangeFile = async jsonString => {
    const {
      createAccountStore: { setJsonString }
    } = this.props;
YJ's avatar
YJ committed

    this.setState({ isLoading: true });
    try {
      await setJsonString(jsonString);
      if (this.hasExistingAddressForImport(createAccountStore.address)) {
YJ's avatar
YJ committed
    } catch (error) {
      this.setState({
        error:
          'Invalid file. Please check this is your actual Parity backup JSON keyfile and try again.'
      });
YJ's avatar
YJ committed
    }
  };

  handleSignerImported = async ({ address, chainId: chainIdString }) => {
      createAccountStore: { importFromSigner }
    } = this.props;

    if (!address || !chainIdString) {
      this.setState({ error: 'Invalid QR code.' });
      return;
    }

    const chainId = parseInt(chainIdString);

    if (this.hasExistingAddressForImport(address, chainId)) {
Axel Chalon's avatar
Axel Chalon committed
      return;
    }

Axel Chalon's avatar
Axel Chalon committed
    await importFromSigner({ address, chainId });

    this.handleNextStep();
  };

  handleSignerImport = () => {
    this.setState({
      importingFromSigner: true
    });
Axel Chalon's avatar
Axel Chalon committed

  hasExistingAddressForImport = (addressForImport, chainId) => {
Axel Chalon's avatar
Axel Chalon committed
    const { accountsInfo } = this.props;
    const isExistingAddress = Object.keys(accountsInfo).some(
Axel Chalon's avatar
Axel Chalon committed
      key =>
        key.toLowerCase() === addressForImport.toLowerCase() &&
        (!accountsInfo[key].chainId ||
          !chainId ||
          accountsInfo[key].chainId === chainId)
        error: `Account ${addressShort(addressForImport)} already listed`
YJ's avatar
YJ committed
  render () {
    const {
      history,
      location: { pathname }
    } = this.props;
    const { error, importingFromSigner, phrase } = this.state;
YJ's avatar
YJ committed
    const currentStep = pathname.slice(-1);

YJ's avatar
YJ committed
    const jsonCard = (
      <Card>
        <div key='createAccount'>
          <div className='text -centered'>
            <p>Recover from JSON Keyfile</p>

            <FetherForm.InputFile
              label='JSON Backup Keyfile'
              onChangeFile={this.handleChangeFile}
              required
            />
          </div>
YJ's avatar
YJ committed
        </div>
      </Card>
    );

    const signerCard = (
      <Card>
        <div key='createAccount'>
          <div className='text -centered'>
            <p>Recover from Parity Signer</p>

            {importingFromSigner ? (
              <Scanner
                onScan={this.handleSignerImported}
                label='Scan Parity Signer account QR code'
              />
            ) : (
              <button
                className='button -footer'
                onClick={this.handleSignerImport}
              >
                Scan QR code
              </button>
            )}
          </div>
        </div>
      </Card>
YJ's avatar
YJ committed
    );
YJ's avatar
YJ committed

YJ's avatar
YJ committed
    const phraseCard = (
      <Card>
        <div key='importBackup'>
          <div className='text -centered'>
            <p>Recover from Seed Phrase</p>

            <FetherForm.Field
              as='textarea'
              label='Recovery phrase'
              onChange={this.handlePhraseChange}
              required
              phrase={phrase}
            />

            {this.renderButton()}
          </div>
YJ's avatar
YJ committed
        </div>
YJ's avatar
YJ committed
    );
YJ's avatar
YJ committed

    const spacer = <div style={{ height: '0.5rem' }} />;

YJ's avatar
YJ committed
    return (
      <RequireHealthOverlay require='node'>
        <div className='center-md'>
          {!importingFromSigner && jsonCard}
          {!importingFromSigner && phraseCard}
          <p>{error}</p>
          <nav className='form-nav -space-around'>
            {currentStep > 1 && (
              <button className='button -back' onClick={history.goBack}>
                Back
              </button>
            )}
          </nav>
        </div>
      </RequireHealthOverlay>
YJ's avatar
YJ committed
    );
  }

  renderButton = () => {
    const { isLoading, json, phrase } = this.state;

YJ's avatar
YJ committed
    // 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}
YJ's avatar
YJ committed
      >
        Next
      </button>
    );
  };
}

export default AccountImportOptions;