import React, { Component } from 'react';
import PropTypes from 'prop-types';
import copyToClipboard from 'copy-to-clipboard';
import classnames from 'classnames';
import AccountListItem from '../../components/app/account-list-item';
import Identicon from '../../components/ui/identicon';
import Tooltip from '../../components/ui/tooltip';
import Copy from '../../components/ui/icon/copy-icon.component';
import { PageContainerFooter } from '../../components/ui/page-container';
import { EVENT } from '../../../shared/constants/metametrics';
import { SECOND } from '../../../shared/constants/time';
import { Numeric } from '../../../shared/modules/Numeric';
import { EtherDenomination } from '../../../shared/constants/common';
export default class ConfirmDecryptMessage extends Component {
static contextTypes = {
t: PropTypes.func.isRequired,
trackEvent: PropTypes.func.isRequired,
};
static propTypes = {
fromAccount: PropTypes.shape({
address: PropTypes.string.isRequired,
balance: PropTypes.string,
name: PropTypes.string,
}).isRequired,
clearConfirmTransaction: PropTypes.func.isRequired,
cancelDecryptMessage: PropTypes.func.isRequired,
decryptMessage: PropTypes.func.isRequired,
decryptMessageInline: PropTypes.func.isRequired,
conversionRate: PropTypes.number,
history: PropTypes.object.isRequired,
mostRecentOverviewPage: PropTypes.string.isRequired,
requesterAddress: PropTypes.string,
txData: PropTypes.object,
subjectMetadata: PropTypes.object,
nativeCurrency: PropTypes.string.isRequired,
};
state = {
fromAccount: this.props.fromAccount,
copyToClipboardPressed: false,
hasCopied: false,
};
copyMessage = () => {
copyToClipboard(this.state.rawMessage);
this.context.trackEvent({
category: EVENT.CATEGORIES.MESSAGES,
event: 'Copy',
properties: {
action: 'Decrypt Message Copy',
legacy_event: true,
},
});
this.setState({ hasCopied: true });
setTimeout(() => this.setState({ hasCopied: false }), SECOND * 3);
};
renderHeader = () => {
return (
{this.context.t('decryptRequest')}
);
};
renderAccount = () => {
const { fromAccount } = this.state;
const { t } = this.context;
return (
);
};
renderBalance = () => {
const { conversionRate, nativeCurrency } = this.props;
const {
fromAccount: { balance },
} = this.state;
const { t } = this.context;
const nativeCurrencyBalance = new Numeric(
balance,
16,
EtherDenomination.WEI,
)
.applyConversionRate(conversionRate)
.round(6)
.toBase(10);
return (
{`${t('balance')}:`}
{`${nativeCurrencyBalance} ${nativeCurrency}`}
);
};
renderRequestIcon = () => {
const { requesterAddress } = this.props;
return (
);
};
renderAccountInfo = () => {
return (
{this.renderAccount()}
{this.renderRequestIcon()}
{this.renderBalance()}
);
};
renderBody = () => {
const { decryptMessageInline, subjectMetadata, txData } = this.props;
const { t } = this.context;
const targetSubjectMetadata = subjectMetadata[txData.msgParams.origin];
const name = targetSubjectMetadata?.name || txData.msgParams.origin;
const notice = t('decryptMessageNotice', [txData.msgParams.origin]);
const {
hasCopied,
hasDecrypted,
hasError,
rawMessage,
errorMessage,
copyToClipboardPressed,
} = this.state;
return (
{this.renderAccountInfo()}
{targetSubjectMetadata?.iconUrl ? (
) : (
{name.charAt(0).toUpperCase()}
)}
{notice}
{!hasDecrypted && !hasError ? txData.msgParams.data : rawMessage}
{hasError ? errorMessage : ''}
{
decryptMessageInline(txData, event).then((result) => {
if (result.error) {
this.setState({
hasError: true,
errorMessage: this.context.t('decryptInlineError', [
result.error,
]),
});
} else {
this.setState({
hasDecrypted: true,
rawMessage: result.rawData,
});
}
});
}}
>
{hasDecrypted ? (
this.copyMessage()}
onMouseDown={() => this.setState({ copyToClipboardPressed: true })}
onMouseUp={() => this.setState({ copyToClipboardPressed: false })}
>
{t('decryptCopy')}
) : (
)}
);
};
renderFooter = () => {
const {
cancelDecryptMessage,
clearConfirmTransaction,
decryptMessage,
history,
mostRecentOverviewPage,
txData,
} = this.props;
const { trackEvent, t } = this.context;
return (
{
await cancelDecryptMessage(txData, event);
trackEvent({
category: EVENT.CATEGORIES.MESSAGES,
event: 'Cancel',
properties: {
action: 'Decrypt Message Request',
legacy_event: true,
},
});
clearConfirmTransaction();
history.push(mostRecentOverviewPage);
}}
onSubmit={async (event) => {
await decryptMessage(txData, event);
trackEvent({
category: EVENT.CATEGORIES.MESSAGES,
event: 'Confirm',
properties: {
action: 'Decrypt Message Request',
legacy_event: true,
},
});
clearConfirmTransaction();
history.push(mostRecentOverviewPage);
}}
/>
);
};
render = () => {
return (
{this.renderHeader()}
{this.renderBody()}
{this.renderFooter()}
);
};
}