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'; import { EVENT, EVENT_NAMES, } from '../../../../../shared/constants/metametrics'; export default class ExportPrivateKeyModal extends Component { static contextTypes = { t: PropTypes.func, trackEvent: 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.context.trackEvent({ category: EVENT.CATEGORIES.KEYS, event: EVENT_NAMES.KEY_EXPORT_REVEALED, properties: { key_type: EVENT.KEY_TYPES.PKEY, }, }); this.setState({ privateKey, showWarning: false, }); }) .catch((e) => { this.context.trackEvent({ category: EVENT.CATEGORIES.KEYS, event: EVENT_NAMES.KEY_EXPORT_FAILED, properties: { key_type: EVENT.KEY_TYPES.PKEY, reason: 'incorrect_password', }, }); 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); this.context.trackEvent({ category: EVENT.CATEGORIES.KEYS, event: EVENT_NAMES.KEY_EXPORT_COPIED, properties: { key_type: EVENT.KEY_TYPES.PKEY, copy_method: 'clipboard', }, }); }} > {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)} ); } }