import log from 'loglevel';
import React, { useContext, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import withModalProps from '../../../../helpers/higher-order-components/with-modal-props';
import Box from '../../../ui/box';
import {
BUTTON_SIZES,
BUTTON_VARIANT,
BannerAlert,
Button,
Text,
} from '../../../component-library';
import AccountModalContainer from '../account-modal-container';
import { toChecksumHexAddress } from '../../../../../shared/modules/hexstring-utils';
import {
MetaMetricsEventCategory,
MetaMetricsEventKeyType,
MetaMetricsEventName,
} from '../../../../../shared/constants/metametrics';
import HoldToRevealModal from '../hold-to-reveal-modal/hold-to-reveal-modal';
import { MetaMetricsContext } from '../../../../contexts/metametrics';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import {
BLOCK_SIZES,
BorderColor,
BorderStyle,
Color,
DISPLAY,
FLEX_DIRECTION,
FONT_WEIGHT,
JustifyContent,
TextVariant,
} from '../../../../helpers/constants/design-system';
import PrivateKeyDisplay from './private-key';
import PasswordInput from './password-input';
const ExportPrivateKeyModal = ({
clearAccountDetails,
hideWarning,
exportAccount,
selectedIdentity,
showAccountDetailModal,
hideModal,
warning = null,
previousModalState,
}) => {
const [password, setPassword] = useState('');
const [privateKey, setPrivateKey] = useState(null);
const [showWarning, setShowWarning] = useState(true);
const [showHoldToReveal, setShowHoldToReveal] = useState(false);
const trackEvent = useContext(MetaMetricsContext);
const t = useI18nContext();
useEffect(() => {
return () => {
clearAccountDetails();
hideWarning();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const exportAccountAndGetPrivateKey = async (passwordInput, address) => {
try {
const privateKeyRetrieved = await exportAccount(passwordInput, address);
trackEvent(
{
category: MetaMetricsEventCategory.Keys,
event: MetaMetricsEventName.KeyExportRevealed,
properties: {
key_type: MetaMetricsEventKeyType.Pkey,
},
},
{},
);
setPrivateKey(privateKeyRetrieved);
setShowWarning(false);
setShowHoldToReveal(true);
} catch (e) {
trackEvent(
{
category: MetaMetricsEventCategory.Keys,
event: MetaMetricsEventName.KeyExportFailed,
properties: {
key_type: MetaMetricsEventKeyType.Pkey,
reason: 'incorrect_password',
},
},
{},
);
log.error(e);
}
};
const { name, address } = selectedIdentity;
if (showHoldToReveal) {
return (
showAccountDetailModal()}
>
setShowHoldToReveal(false)}
willHide={false}
holdToRevealType="PrivateKey"
/>
);
}
return (
showAccountDetailModal()}
>
{name}
{toChecksumHexAddress(address)}
{t('showPrivateKeys')}
{privateKey ? (
) : (
)}
{showWarning && (
{warning}
)}
{t('privateKeyWarning')}
{!privateKey && (
)}
{privateKey ? (
) : (
)}
);
};
ExportPrivateKeyModal.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,
};
export default withModalProps(ExportPrivateKeyModal);