import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { stripHexPrefix } from 'ethereumjs-util';
import classnames from 'classnames';
import { ObjectInspector } from 'react-inspector';
import LedgerInstructionField from '../ledger-instruction-field';
import { MESSAGE_TYPE } from '../../../../shared/constants/app';
import { EVENT } from '../../../../shared/constants/metametrics';
import { getURLHostName } from '../../../helpers/utils/util';
import Identicon from '../../ui/identicon';
import AccountListItem from '../account-list-item';
import { conversionUtil } from '../../../../shared/modules/conversion.utils';
import Button from '../../ui/button';
import SiteIcon from '../../ui/site-icon';
export default class SignatureRequestOriginal 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,
cancel: PropTypes.func.isRequired,
clearConfirmTransaction: PropTypes.func.isRequired,
conversionRate: PropTypes.number,
history: PropTypes.object.isRequired,
mostRecentOverviewPage: PropTypes.string.isRequired,
requesterAddress: PropTypes.string,
sign: PropTypes.func.isRequired,
txData: PropTypes.object.isRequired,
subjectMetadata: PropTypes.object,
hardwareWalletRequiresConnection: PropTypes.bool,
isLedgerWallet: PropTypes.bool,
nativeCurrency: PropTypes.string.isRequired,
messagesCount: PropTypes.number,
showRejectTransactionsConfirmationModal: PropTypes.func.isRequired,
cancelAll: PropTypes.func.isRequired,
};
state = {
fromAccount: this.props.fromAccount,
};
renderHeader = () => {
return (
{this.context.t('sigRequest')}
);
};
renderAccount = () => {
const { fromAccount } = this.state;
return (
{`${this.context.t('account')}:`}
);
};
renderBalance = () => {
const { conversionRate, nativeCurrency } = this.props;
const {
fromAccount: { balance },
} = this.state;
const balanceInBaseAsset = conversionUtil(balance, {
fromNumericBase: 'hex',
toNumericBase: 'dec',
fromDenomination: 'WEI',
numberOfDecimals: 6,
conversionRate,
});
return (
{`${this.context.t('balance')}:`}
{`${balanceInBaseAsset} ${nativeCurrency}`}
);
};
renderRequestIcon = () => {
const { requesterAddress } = this.props;
return (
);
};
renderAccountInfo = () => {
return (
{this.renderAccount()}
{this.renderRequestIcon()}
{this.renderBalance()}
);
};
renderOriginInfo = () => {
const { txData, subjectMetadata } = this.props;
const { t } = this.context;
const targetSubjectMetadata = txData.msgParams.origin
? subjectMetadata?.[txData.msgParams.origin]
: null;
return (
{`${t('origin')}:`}
{targetSubjectMetadata?.iconUrl ? (
) : null}
{txData.msgParams.origin}
);
};
msgHexToText = (hex) => {
try {
const stripped = stripHexPrefix(hex);
const buff = Buffer.from(stripped, 'hex');
return buff.length === 32 ? hex : buff.toString('utf8');
} catch (e) {
return hex;
}
};
renderTypedData = (data) => {
const { t } = this.context;
const { domain, message } = JSON.parse(data);
return (
{domain ? (
{t('domain')}
) : (
''
)}
{message ? (
{t('message')}
) : (
''
)}
);
};
renderBody = () => {
let rows;
let notice = `${this.context.t('youSign')}:`;
const { txData } = this.props;
const {
type,
msgParams: { data },
} = txData;
if (type === MESSAGE_TYPE.PERSONAL_SIGN) {
rows = [
{ name: this.context.t('message'), value: this.msgHexToText(data) },
];
} else if (type === MESSAGE_TYPE.ETH_SIGN_TYPED_DATA) {
rows = data;
} else if (type === MESSAGE_TYPE.ETH_SIGN) {
rows = [{ name: this.context.t('message'), value: data }];
notice = this.context.t('signNotice');
}
return (
{this.renderAccountInfo()}
{this.renderOriginInfo()}
{notice}
{type === MESSAGE_TYPE.ETH_SIGN ? (
{
global.platform.openTab({
url:
'https://consensys.net/blog/metamask/the-seal-of-approval-know-what-youre-consenting-to-with-permissions-and-approvals-in-metamask/',
});
}}
>
{this.context.t('learnMoreUpperCase')}
) : null}
{rows.map(({ name, value }, index) => {
if (typeof value === 'boolean') {
// eslint-disable-next-line no-param-reassign
value = value.toString();
}
return (
);
})}
);
};
renderFooter = () => {
const {
cancel,
clearConfirmTransaction,
history,
mostRecentOverviewPage,
sign,
txData: { type },
hardwareWalletRequiresConnection,
} = this.props;
const { trackEvent, t } = this.context;
return (
);
};
handleCancelAll = () => {
const {
cancelAll,
clearConfirmTransaction,
history,
mostRecentOverviewPage,
showRejectTransactionsConfirmationModal,
messagesCount,
} = this.props;
const unapprovedTxCount = messagesCount;
showRejectTransactionsConfirmationModal({
unapprovedTxCount,
onSubmit: async () => {
await cancelAll();
clearConfirmTransaction();
history.push(mostRecentOverviewPage);
},
});
};
render = () => {
const { messagesCount } = this.props;
const { t } = this.context;
const rejectNText = t('rejectTxsN', [messagesCount]);
return (
{this.renderHeader()}
{this.renderBody()}
{this.props.isLedgerWallet ? (
) : null}
{this.renderFooter()}
{messagesCount > 1 ? (
) : null}
);
};
}