2021-02-04 19:15:23 +01:00
|
|
|
import log from 'loglevel';
|
2023-05-06 23:04:20 +02:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-06 23:04:20 +02:00
|
|
|
import withModalProps from '../../../../helpers/higher-order-components/with-modal-props';
|
|
|
|
import Box from '../../../ui/box';
|
2022-09-22 17:04:24 +02:00
|
|
|
import {
|
2023-05-06 23:04:20 +02:00
|
|
|
BUTTON_SIZES,
|
|
|
|
BUTTON_VARIANT,
|
|
|
|
BannerAlert,
|
|
|
|
Button,
|
2023-07-20 00:32:35 +02:00
|
|
|
Text,
|
2023-05-06 23:04:20 +02:00
|
|
|
} from '../../../component-library';
|
2023-07-20 00:32:35 +02:00
|
|
|
|
2023-05-06 23:04:20 +02:00
|
|
|
import AccountModalContainer from '../account-modal-container';
|
|
|
|
import { toChecksumHexAddress } from '../../../../../shared/modules/hexstring-utils';
|
2022-08-16 18:39:23 +02:00
|
|
|
import {
|
2023-04-03 17:31:04 +02:00
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventKeyType,
|
|
|
|
MetaMetricsEventName,
|
2022-08-16 18:39:23 +02:00
|
|
|
} from '../../../../../shared/constants/metametrics';
|
2023-05-06 23:04:20 +02:00
|
|
|
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(
|
|
|
|
{
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Keys,
|
|
|
|
event: MetaMetricsEventName.KeyExportRevealed,
|
2022-08-16 18:39:23 +02:00
|
|
|
properties: {
|
2023-04-03 17:31:04 +02:00
|
|
|
key_type: MetaMetricsEventKeyType.Pkey,
|
2022-08-16 18:39:23 +02:00
|
|
|
},
|
2023-05-06 23:04:20 +02:00
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
setPrivateKey(privateKeyRetrieved);
|
|
|
|
setShowWarning(false);
|
|
|
|
setShowHoldToReveal(true);
|
|
|
|
} catch (e) {
|
|
|
|
trackEvent(
|
|
|
|
{
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Keys,
|
|
|
|
event: MetaMetricsEventName.KeyExportFailed,
|
2022-08-16 18:39:23 +02:00
|
|
|
properties: {
|
2023-04-03 17:31:04 +02:00
|
|
|
key_type: MetaMetricsEventKeyType.Pkey,
|
2022-08-16 18:39:23 +02:00
|
|
|
reason: 'incorrect_password',
|
|
|
|
},
|
2023-05-06 23:04:20 +02:00
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
2022-08-16 18:39:23 +02:00
|
|
|
|
2023-05-06 23:04:20 +02:00
|
|
|
log.error(e);
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-11-27 16:06:31 +01:00
|
|
|
|
2023-05-06 23:04:20 +02:00
|
|
|
const { name, address } = selectedIdentity;
|
2019-11-27 16:06:31 +01:00
|
|
|
|
2023-05-06 23:04:20 +02:00
|
|
|
if (showHoldToReveal) {
|
2019-11-27 16:06:31 +01:00
|
|
|
return (
|
2023-05-06 23:04:20 +02:00
|
|
|
<AccountModalContainer
|
|
|
|
className="export-private-key-modal"
|
|
|
|
selectedIdentity={selectedIdentity}
|
|
|
|
showBackButton={previousModalState === 'ACCOUNT_DETAILS'}
|
|
|
|
backButtonAction={() => showAccountDetailModal()}
|
2022-04-04 17:07:01 +02:00
|
|
|
>
|
2023-05-06 23:04:20 +02:00
|
|
|
<HoldToRevealModal
|
|
|
|
onLongPressed={() => setShowHoldToReveal(false)}
|
|
|
|
willHide={false}
|
|
|
|
holdToRevealType="PrivateKey"
|
|
|
|
/>
|
|
|
|
</AccountModalContainer>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-11-27 16:06:31 +01:00
|
|
|
}
|
|
|
|
|
2023-05-06 23:04:20 +02:00
|
|
|
return (
|
|
|
|
<AccountModalContainer
|
|
|
|
className="export-private-key-modal"
|
|
|
|
selectedIdentity={selectedIdentity}
|
|
|
|
showBackButton={previousModalState === 'ACCOUNT_DETAILS'}
|
|
|
|
backButtonAction={() => showAccountDetailModal()}
|
|
|
|
>
|
|
|
|
<Text
|
|
|
|
as="span"
|
|
|
|
marginTop={2}
|
|
|
|
variant={TextVariant.bodyLgMedium}
|
|
|
|
fontWeight={FONT_WEIGHT.NORMAL}
|
|
|
|
>
|
|
|
|
{name}
|
|
|
|
</Text>
|
|
|
|
<Box
|
|
|
|
className="ellip-address-wrapper"
|
|
|
|
borderStyle={BorderStyle.solid}
|
|
|
|
borderColor={BorderColor.borderDefault}
|
|
|
|
borderWidth={1}
|
|
|
|
marginTop={2}
|
|
|
|
padding={[1, 2, 1, 2]}
|
|
|
|
>
|
|
|
|
{toChecksumHexAddress(address)}
|
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
className="export-private-key-modal__divider"
|
|
|
|
width={BLOCK_SIZES.FULL}
|
|
|
|
margin={[5, 0, 3, 0]}
|
|
|
|
/>
|
|
|
|
<Text
|
|
|
|
variant={TextVariant.bodyLgMedium}
|
|
|
|
margin={[4, 0, 4, 0]}
|
|
|
|
fontWeight={FONT_WEIGHT.NORMAL}
|
|
|
|
>
|
|
|
|
{t('showPrivateKeys')}
|
|
|
|
</Text>
|
|
|
|
{privateKey ? (
|
|
|
|
<PrivateKeyDisplay privateKey={privateKey} />
|
|
|
|
) : (
|
|
|
|
<PasswordInput setPassword={setPassword} />
|
|
|
|
)}
|
|
|
|
{showWarning && (
|
|
|
|
<Text color={Color.errorDefault} variant={TextVariant.bodySm}>
|
|
|
|
{warning}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
<BannerAlert
|
|
|
|
padding={[1, 3, 0, 3]}
|
|
|
|
marginLeft={5}
|
|
|
|
marginRight={5}
|
|
|
|
marginTop={4}
|
|
|
|
severity="danger"
|
|
|
|
>
|
|
|
|
{t('privateKeyWarning')}
|
|
|
|
</BannerAlert>
|
|
|
|
<Box
|
|
|
|
display={DISPLAY.FLEX}
|
|
|
|
flexDirection={FLEX_DIRECTION.ROW}
|
|
|
|
width={BLOCK_SIZES.FULL}
|
|
|
|
justifyContent={JustifyContent.spaceBetween}
|
|
|
|
marginTop={3}
|
|
|
|
padding={[5, 0, 5, 0]}
|
|
|
|
>
|
2019-11-27 16:06:31 +01:00
|
|
|
{!privateKey && (
|
|
|
|
<Button
|
2023-05-06 23:04:20 +02:00
|
|
|
type={BUTTON_VARIANT.SECONDARY}
|
|
|
|
size={BUTTON_SIZES.LG}
|
|
|
|
width={BLOCK_SIZES.HALF}
|
|
|
|
marginRight={4}
|
2022-08-16 18:39:23 +02:00
|
|
|
onClick={() => {
|
2023-05-06 23:04:20 +02:00
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Keys,
|
|
|
|
event: MetaMetricsEventName.KeyExportCanceled,
|
2022-08-16 18:39:23 +02:00
|
|
|
properties: {
|
2023-04-03 17:31:04 +02:00
|
|
|
key_type: MetaMetricsEventKeyType.Pkey,
|
2022-08-16 18:39:23 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
hideModal();
|
|
|
|
}}
|
2019-11-27 16:06:31 +01:00
|
|
|
>
|
2023-05-06 23:04:20 +02:00
|
|
|
{t('cancel')}
|
2019-11-27 16:06:31 +01:00
|
|
|
</Button>
|
|
|
|
)}
|
2020-11-03 00:41:28 +01:00
|
|
|
{privateKey ? (
|
|
|
|
<Button
|
2023-05-06 23:04:20 +02:00
|
|
|
type={BUTTON_VARIANT.PRIMARY}
|
|
|
|
size={BUTTON_SIZES.LG}
|
|
|
|
width={BLOCK_SIZES.FULL}
|
2022-08-16 18:39:23 +02:00
|
|
|
onClick={() => {
|
|
|
|
hideModal();
|
|
|
|
}}
|
2020-11-03 00:41:28 +01:00
|
|
|
>
|
2023-05-06 23:04:20 +02:00
|
|
|
{t('done')}
|
2020-11-03 00:41:28 +01:00
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<Button
|
2023-05-06 23:04:20 +02:00
|
|
|
type={BUTTON_VARIANT.PRIMARY}
|
|
|
|
size={BUTTON_SIZES.LG}
|
|
|
|
width={BLOCK_SIZES.HALF}
|
2022-08-16 18:39:23 +02:00
|
|
|
onClick={() => {
|
2023-05-06 23:04:20 +02:00
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Keys,
|
|
|
|
event: MetaMetricsEventName.KeyExportRequested,
|
2022-08-16 18:39:23 +02:00
|
|
|
properties: {
|
2023-04-03 17:31:04 +02:00
|
|
|
key_type: MetaMetricsEventKeyType.Pkey,
|
2022-08-16 18:39:23 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-05-06 23:04:20 +02:00
|
|
|
exportAccountAndGetPrivateKey(password, address);
|
2022-08-16 18:39:23 +02:00
|
|
|
}}
|
2023-05-06 23:04:20 +02:00
|
|
|
disabled={!password}
|
2020-11-03 00:41:28 +01:00
|
|
|
>
|
2023-05-06 23:04:20 +02:00
|
|
|
{t('confirm')}
|
2020-11-03 00:41:28 +01:00
|
|
|
</Button>
|
|
|
|
)}
|
2023-05-06 23:04:20 +02:00
|
|
|
</Box>
|
|
|
|
</AccountModalContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|