// Copyright 2015-2018 Parity Technologies (UK) Ltd. // This file is part of Parity. // // SPDX-License-Identifier: BSD-3-Clause import React, { Component } from 'react'; import { addressShort, Card, Form as FetherForm } from 'fether-ui'; import { accounts$, withoutLoading } from '@parity/light.js'; import light from '@parity/light.js-react'; import { inject, observer } from 'mobx-react'; @light({ accounts: () => accounts$().pipe(withoutLoading()) }) @inject('createAccountStore') @observer class AccountImportOptions extends Component { state = { error: '', isLoading: false, phrase: '' }; 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(); const { createAccountStore, createAccountStore: { setPhrase } } = this.props; this.setState({ isLoading: true, phrase }); try { await setPhrase(phrase); if (this.hasExistingAddressForImport(createAccountStore.address)) { return; } this.handleNextStep(); } catch (error) { this.setState({ isLoading: false, error: 'The passphrase was not recognized. Please verify that you entered your passphrase correctly.' }); console.error(error); } }; handleChangeFile = async jsonString => { const { createAccountStore: { setJsonString } } = this.props; this.setState({ isLoading: true }); try { const json = JSON.parse(jsonString); if (this.hasExistingAddressForImport(`0x${json['address']}`)) { return; } await setJsonString(jsonString); this.handleNextStep(); } catch (error) { this.setState({ isLoading: false, error: 'Invalid file. Please check this is your actual Parity backup JSON keyfile and try again.' }); console.error(error); } }; hasExistingAddressForImport = addressForImport => { const { accounts } = this.props; const isExistingAddress = accounts .map(address => address && address.toLowerCase()) .includes(addressForImport.toLowerCase()); if (isExistingAddress) { this.setState({ isLoading: false, error: `Account ${addressShort(addressForImport)} already listed` }); } return isExistingAddress; }; render () { const { history, location: { pathname } } = this.props; const { error, phrase } = this.state; const currentStep = pathname.slice(-1); const jsonCard = (

Recover from JSON Keyfile

); const phraseCard = (

Recover from Seed Phrase

{this.renderButton()}
); return (
{jsonCard}
{phraseCard}

{error}

); } renderButton = () => { const { isLoading, json, phrase } = this.state; // If we are importing an existing account, the button goes to the next step return ( ); }; } export default AccountImportOptions;