1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/ui/components/app/signature-request-original/signature-request-original.component.js

347 lines
9.3 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
2021-04-16 17:05:13 +02:00
import { stripHexPrefix } from 'ethereumjs-util';
import classnames from 'classnames';
import { ObjectInspector } from 'react-inspector';
2020-11-03 00:41:28 +01:00
import {
ENVIRONMENT_TYPE_NOTIFICATION,
MESSAGE_TYPE,
} from '../../../../shared/constants/app';
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
import Identicon from '../../ui/identicon';
import AccountListItem from '../account-list-item';
import { conversionUtil } from '../../../helpers/utils/conversion-util';
import Button from '../../ui/button';
import SiteIcon from '../../ui/site-icon';
export default class SignatureRequestOriginal extends Component {
static contextTypes = {
t: PropTypes.func.isRequired,
metricsEvent: 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,
domainMetadata: PropTypes.object,
};
state = {
fromAccount: this.props.fromAccount,
};
componentDidMount = () => {
if (getEnvironmentType() === ENVIRONMENT_TYPE_NOTIFICATION) {
window.addEventListener('beforeunload', this._beforeUnload);
}
};
componentWillUnmount = () => {
this._removeBeforeUnload();
};
_beforeUnload = (event) => {
const { clearConfirmTransaction, cancel } = this.props;
const { metricsEvent } = this.context;
metricsEvent({
eventOpts: {
category: 'Transactions',
action: 'Sign Request',
name: 'Cancel Sig Request Via Notification Close',
},
});
clearConfirmTransaction();
cancel(event);
};
_removeBeforeUnload = () => {
if (getEnvironmentType() === ENVIRONMENT_TYPE_NOTIFICATION) {
window.removeEventListener('beforeunload', this._beforeUnload);
}
};
renderHeader = () => {
return (
<div className="request-signature__header">
<div className="request-signature__header-background" />
<div className="request-signature__header__text">
2020-11-03 00:41:28 +01:00
{this.context.t('sigRequest')}
</div>
<div className="request-signature__header__tip-container">
<div className="request-signature__header__tip" />
</div>
</div>
);
};
renderAccount = () => {
const { fromAccount } = this.state;
return (
<div className="request-signature__account">
<div className="request-signature__account-text">
2020-11-03 00:41:28 +01:00
{`${this.context.t('account')}:`}
</div>
<div className="request-signature__account-item">
2020-11-03 00:41:28 +01:00
<AccountListItem account={fromAccount} />
</div>
</div>
);
};
renderBalance = () => {
const { conversionRate } = this.props;
2020-11-03 00:41:28 +01:00
const {
fromAccount: { balance },
} = this.state;
const balanceInEther = conversionUtil(balance, {
fromNumericBase: 'hex',
toNumericBase: 'dec',
fromDenomination: 'WEI',
numberOfDecimals: 6,
conversionRate,
});
return (
<div className="request-signature__balance">
<div className="request-signature__balance-text">
2020-11-03 00:41:28 +01:00
{`${this.context.t('balance')}:`}
</div>
<div className="request-signature__balance-value">
2020-11-03 00:41:28 +01:00
{`${balanceInEther} ETH`}
</div>
</div>
);
};
renderRequestIcon = () => {
const { requesterAddress } = this.props;
return (
<div className="request-signature__request-icon">
2020-11-03 00:41:28 +01:00
<Identicon diameter={40} address={requesterAddress} />
</div>
);
};
renderAccountInfo = () => {
return (
<div className="request-signature__account-info">
2020-11-03 00:41:28 +01:00
{this.renderAccount()}
{this.renderRequestIcon()}
{this.renderBalance()}
</div>
);
};
renderOriginInfo = () => {
const { txData, domainMetadata } = this.props;
const { t } = this.context;
const originMetadata = txData.msgParams.origin
? domainMetadata?.[txData.msgParams.origin]
: null;
return (
<div className="request-signature__origin-row">
<div className="request-signature__origin-label">
{`${t('origin')}:`}
</div>
{originMetadata?.icon ? (
<SiteIcon
icon={originMetadata.icon}
2021-02-22 17:20:42 +01:00
name={originMetadata.hostname}
size={24}
/>
) : null}
<div className="request-signature__origin">
{txData.msgParams.origin}
</div>
</div>
);
};
msgHexToText = (hex) => {
try {
2021-04-16 17:05:13 +02:00
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 (
<div className="request-signature__typed-container">
2020-11-03 00:41:28 +01:00
{domain ? (
<div>
<h1>{t('domain')}</h1>
2020-11-03 00:41:28 +01:00
<ObjectInspector data={domain} expandLevel={1} name="domain" />
</div>
) : (
''
)}
{message ? (
<div>
<h1>{t('message')}</h1>
2020-11-03 00:41:28 +01:00
<ObjectInspector data={message} expandLevel={1} name="message" />
</div>
) : (
''
)}
</div>
);
};
renderBody = () => {
let rows;
let notice = `${this.context.t('youSign')}:`;
const { txData } = this.props;
2020-11-03 00:41:28 +01:00
const {
type,
msgParams: { data },
} = txData;
if (type === MESSAGE_TYPE.PERSONAL_SIGN) {
2020-11-03 00:41:28 +01:00
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 (
<div className="request-signature__body">
2020-11-03 00:41:28 +01:00
{this.renderAccountInfo()}
{this.renderOriginInfo()}
<div
className={classnames('request-signature__notice', {
'request-signature__warning': type === MESSAGE_TYPE.ETH_SIGN,
})}
>
2020-11-03 00:41:28 +01:00
{notice}
{type === MESSAGE_TYPE.ETH_SIGN ? (
<span
className="request-signature__help-link"
onClick={() => {
global.platform.openTab({
url:
'https://metamask.zendesk.com/hc/en-us/articles/360015488751',
});
2020-11-03 00:41:28 +01:00
}}
>
{this.context.t('learnMore')}
</span>
) : null}
</div>
<div className="request-signature__rows">
2020-11-03 00:41:28 +01:00
{rows.map(({ name, value }, index) => {
if (typeof value === 'boolean') {
// eslint-disable-next-line no-param-reassign
value = value.toString();
2020-11-03 00:41:28 +01:00
}
return (
<div
className="request-signature__row"
key={`request-signature-row-${index}`}
>
<div className="request-signature__row-title">{`${name}:`}</div>
<div className="request-signature__row-value">{value}</div>
</div>
);
2020-11-03 00:41:28 +01:00
})}
</div>
</div>
);
};
renderFooter = () => {
2020-11-03 00:41:28 +01:00
const {
cancel,
clearConfirmTransaction,
history,
mostRecentOverviewPage,
sign,
} = this.props;
return (
<div className="request-signature__footer">
<Button
type="default"
large
className="request-signature__footer__cancel-button"
2020-02-15 21:34:12 +01:00
onClick={async (event) => {
this._removeBeforeUnload();
await cancel(event);
this.context.metricsEvent({
eventOpts: {
category: 'Transactions',
action: 'Sign Request',
name: 'Cancel',
},
});
clearConfirmTransaction();
history.push(mostRecentOverviewPage);
}}
>
2020-11-03 00:41:28 +01:00
{this.context.t('cancel')}
2019-12-04 06:35:44 +01:00
</Button>
<Button
data-testid="request-signature__sign"
type="secondary"
large
className="request-signature__footer__sign-button"
2020-02-15 21:34:12 +01:00
onClick={async (event) => {
this._removeBeforeUnload();
await sign(event);
this.context.metricsEvent({
eventOpts: {
category: 'Transactions',
action: 'Sign Request',
name: 'Confirm',
},
});
clearConfirmTransaction();
history.push(mostRecentOverviewPage);
}}
>
2020-11-03 00:41:28 +01:00
{this.context.t('sign')}
</Button>
</div>
);
};
render = () => {
return (
<div className="request-signature__container">
2020-11-03 00:41:28 +01:00
{this.renderHeader()}
{this.renderBody()}
{this.renderFooter()}
</div>
);
};
}