import log from 'loglevel'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { stripHexPrefix } from 'ethereumjs-util'; import copyToClipboard from 'copy-to-clipboard'; import Button from '../../../ui/button'; import AccountModalContainer from '../account-modal-container'; import { toChecksumHexAddress } from '../../../../../shared/modules/hexstring-utils'; export default class ExportPrivateKeyModal extends Component { static contextTypes = { t: PropTypes.func, }; static defaultProps = { warning: null, previousModalState: null, }; static propTypes = { exportAccount: PropTypes.func.isRequired, selectedIdentity: PropTypes.object.isRequired, warning: PropTypes.node, showAccountDetailModal: PropTypes.func.isRequired, hideModal: PropTypes.func.isRequired, hideWarning: PropTypes.func.isRequired, clearAccountDetails: PropTypes.func.isRequired, previousModalState: PropTypes.string, }; state = { password: '', privateKey: null, showWarning: true, }; componentWillUnmount() { this.props.clearAccountDetails(); this.props.hideWarning(); } exportAccountAndGetPrivateKey = (password, address) => { const { exportAccount } = this.props; exportAccount(password, address) .then((privateKey) => this.setState({ privateKey, showWarning: false, }), ) .catch((e) => log.error(e)); }; renderPasswordLabel(privateKey) { return ( {privateKey ? this.context.t('copyPrivateKey') : this.context.t('typePassword')} ); } renderPasswordInput(privateKey) { const plainKey = privateKey && stripHexPrefix(privateKey); if (!privateKey) { return ( this.setState({ password: event.target.value })} /> ); } return (
copyToClipboard(plainKey)} > {plainKey}
); } renderButtons(privateKey, address, hideModal) { return (
{!privateKey && ( )} {privateKey ? ( ) : ( )}
); } render() { const { selectedIdentity, warning, showAccountDetailModal, hideModal, previousModalState, } = this.props; const { name, address } = selectedIdentity; const { privateKey, showWarning } = this.state; return ( showAccountDetailModal()} > {name}
{toChecksumHexAddress(address)}
{this.context.t('showPrivateKeys')}
{this.renderPasswordLabel(privateKey)} {this.renderPasswordInput(privateKey)} {showWarning && warning ? ( {warning} ) : null}
{this.context.t('privateKeyWarning')}
{this.renderButtons(privateKey, address, hideModal)} ); } }