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 (