AccountRewritePhrase.js 4.48 KiB
Newer Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
Amaury Martiny's avatar
Amaury Martiny committed
// SPDX-License-Identifier: BSD-3-Clause
YJ's avatar
YJ committed
import React, { Component } from "react";
import { AccountCard, Card, Form as FetherForm } from "fether-ui";
YJ's avatar
YJ committed
import { inject, observer } from "mobx-react";
YJ's avatar
YJ committed
@inject("createAccountStore")
@observer
class AccountRewritePhrase extends Component {
  state = {
    isFileValid: false,
    json: null,
YJ's avatar
YJ committed
    value: ""
  handleChange = ({ target: { value } }) => {
    this.setState({ value });
  };
  handleChangeFile = ({ target: { result } }) => {
    try {
      const json = JSON.parse(result);

      const isFileValid =
YJ's avatar
YJ committed
        json.address.length === 32 &&
        typeof json.meta === "object" &&
        json.crpyto &&
        json.crypto.cipher === "aes-128-ctr";

      this.setState({
        isFileValid,
        json
      });
    } catch (error) {
      this.setState({
        isFileValid: false,
        json: null
      });
      console.error(error);
    }
  };

    const {
      history,
      createAccountStore: { isImport, isJSON, setJSON, setPhrase }
    } = this.props;
    const currentStep = pathname.slice(-1);
    const { json, value } = this.state;

    if (isJSON) {
      this.setState({ isLoading: true });
YJ's avatar
YJ committed
      console.log("setting address ", json.address);
      await setJSON(json);
    }

    // If we're importing, derive address from recovery phrase when we submit
    if (isImport && !isJSON) {
      this.setState({ isLoading: true });
      await setPhrase(value);
    }
    history.push(`/accounts/new/${+currentStep + 1}`);
  };

  toggleImportMethod = () => {
    const { createAccountStore } = this.props;
    createAccountStore.setIsJSON(!createAccountStore.isJSON);
  };

YJ's avatar
YJ committed
  render() {
      createAccountStore: { address, isImport, isJSON, name },
      history,
      location: { pathname }
    } = this.props;
    const { value } = this.state;
    const currentStep = pathname.slice(-1);
YJ's avatar
YJ committed
      <div key="createAccount">
YJ's avatar
YJ committed
        <div className="text -centered">
            isJSON ? (
              <div>
                <p> Drop your JSON keyfile below </p>
YJ's avatar
YJ committed
                <button onClick={this.toggleImportMethod} className="button">
                  Use Seed Phrase
                </button>
              </div>
            ) : (
              <div>
                <p> Type your Recovery phrase </p>
YJ's avatar
YJ committed
                <button onClick={this.toggleImportMethod} className="button">
                  Use JSON Keyfile
                </button>
              </div>
            )
          ) : (
            <p>
              Type your secret phrase to confirm that you wrote it down
              correctly:
            </p>
          )}
        </div>

        {isJSON ? (
          <FetherForm.InputFile
YJ's avatar
YJ committed
            label="JSON Backup Keyfile"
            onChangeFile={this.handleChangeFile}
            required
            value={value}
          />
        ) : (
          <FetherForm.Field
YJ's avatar
YJ committed
            as="textarea"
            label="Recovery phrase"
            onChange={this.handleChange}
            required
            value={value}
          />
        )}

YJ's avatar
YJ committed
        <nav className="form-nav -space-around">
YJ's avatar
YJ committed
            <button className="button -cancel" onClick={history.goBack}>
              Back
            </button>
          )}
          {this.renderButton()}
        </nav>
      </div>
    ];

    return isImport ? (
      <Card>{body}</Card>
    ) : (
      <AccountCard
        address={address}
YJ's avatar
YJ committed
        name={address && !name ? "(no name)" : name}
Amaury Martiny's avatar
Amaury Martiny committed

  renderButton = () => {
    const {
      createAccountStore: { isImport, phrase }
    } = this.props;
    const { isLoading, json, value } = this.state;
Amaury Martiny's avatar
Amaury Martiny committed

    // If we are creating a new account, the button just checks the phrase has
    // been correctly written by the user.
    if (!isImport) {
      return (
        <button
YJ's avatar
YJ committed
          className="button"
          onClick={this.handleNextStep}
        >
          Next
        </button>
      );
Amaury Martiny's avatar
Amaury Martiny committed
    }

    // If we are importing an existing account, the button goes to the next step
Amaury Martiny's avatar
Amaury Martiny committed
    return (
YJ's avatar
YJ committed
        className="button"
        disabled={(!value.length && !json) || isLoading}
        onClick={this.handleNextStep}
Amaury Martiny's avatar
Amaury Martiny committed
        Next
      </button>
    );
  };
export default AccountRewritePhrase;