import React from 'react'; import { generateKeystoreFileInfo, KeystoreFile } from 'utils/keystore'; import Modal from 'components/ui/Modal'; import Input from './Input'; import translate, { translateRaw } from 'translations'; import { MINIMUM_PASSWORD_LENGTH } from 'config/data'; import { isValidPrivKey } from 'libs/validators'; import './index.scss'; interface Props { isOpen: boolean; privateKey?: string; handleClose(): void; } interface State { privateKey: string; password: string; isPrivateKeyVisible: boolean; isPasswordVisible: boolean; keystoreFile: KeystoreFile | null; hasError: boolean; } const initialState: State = { privateKey: '', password: '', isPrivateKeyVisible: false, isPasswordVisible: false, keystoreFile: null, hasError: false }; export default class GenerateKeystoreModal extends React.Component { public state: State = initialState; constructor(props: Props) { super(props); if (props.privateKey) { this.state = { ...this.state, privateKey: props.privateKey }; } } public componentWillReceiveProps(nextProps) { if (nextProps.privateKey !== this.props.privateKey) { this.setState({ privateKey: nextProps.privateKey }); } } public render() { const { privateKey, password, isPrivateKeyVisible, isPasswordVisible, keystoreFile, hasError } = this.state; const isPrivateKeyValid = isValidPrivKey(privateKey); const isPasswordValid = password.length >= MINIMUM_PASSWORD_LENGTH; return (
{!keystoreFile ? ( ) : hasError ? (

Keystore generation failed or was invalid. In order to prevent loss of funds, we cannot provide you with a keystore file that may be corrupted. Refresh the page or use a different browser, and try again.

) : ( {translate('Download Keystore File')} )}
); } private togglePrivateKey = () => { this.setState({ isPrivateKeyVisible: !this.state.isPrivateKeyVisible }); }; private togglePassword = () => { this.setState({ isPasswordVisible: !this.state.isPasswordVisible }); }; private handleInput = (e: React.FormEvent) => { const { name, value } = e.currentTarget; let { keystoreFile } = this.state; if (name === 'privateKey') { keystoreFile = null; } this.setState({ [name as any]: value, keystoreFile }); }; private handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!this.state.keystoreFile) { this.generateKeystoreFileInfo(); } }; private generateKeystoreFileInfo = async () => { try { const { privateKey, password } = this.state; const keystoreFile = await generateKeystoreFileInfo(privateKey, password); if (keystoreFile) { this.setState({ keystoreFile }); } } catch (err) { this.setState({ hasError: true }); } }; private handleClose = () => { this.setState({ ...initialState, privateKey: this.props.privateKey || initialState.privateKey }); this.props.handleClose(); }; }