1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-04 23:14:56 +01:00
metamask-extension/ui/pages/confirm-encryption-public-key/confirm-encryption-public-key.component.js

257 lines
7.2 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import AccountListItem from '../../components/app/account-list-item';
import Button from '../../components/ui/button';
import Identicon from '../../components/ui/identicon';
import { ENVIRONMENT_TYPE_NOTIFICATION } from '../../../shared/constants/app';
import { getEnvironmentType } from '../../../app/scripts/lib/util';
import { conversionUtil } from '../../helpers/utils/conversion-util';
export default class ConfirmEncryptionPublicKey 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,
clearConfirmTransaction: PropTypes.func.isRequired,
cancelEncryptionPublicKey: PropTypes.func.isRequired,
encryptionPublicKey: PropTypes.func.isRequired,
conversionRate: PropTypes.number,
history: PropTypes.object.isRequired,
requesterAddress: PropTypes.string,
txData: PropTypes.object,
domainMetadata: PropTypes.object,
mostRecentOverviewPage: PropTypes.string.isRequired,
};
state = {
fromAccount: this.props.fromAccount,
};
componentDidMount = () => {
2020-11-03 00:41:28 +01:00
if (
getEnvironmentType(window.location.href) === ENVIRONMENT_TYPE_NOTIFICATION
) {
window.addEventListener('beforeunload', this._beforeUnload);
}
};
componentWillUnmount = () => {
this._removeBeforeUnload();
};
_beforeUnload = async (event) => {
const {
clearConfirmTransaction,
cancelEncryptionPublicKey,
txData,
} = this.props;
const { metricsEvent } = this.context;
await cancelEncryptionPublicKey(txData, event);
metricsEvent({
eventOpts: {
category: 'Messages',
action: 'Encryption public key Request',
name: 'Cancel Via Notification Close',
},
});
clearConfirmTransaction();
};
_removeBeforeUnload = () => {
2020-11-03 00:41:28 +01:00
if (
getEnvironmentType(window.location.href) === ENVIRONMENT_TYPE_NOTIFICATION
) {
window.removeEventListener('beforeunload', this._beforeUnload);
}
};
renderHeader = () => {
return (
<div className="request-encryption-public-key__header">
<div className="request-encryption-public-key__header-background" />
<div className="request-encryption-public-key__header__text">
2020-11-03 00:41:28 +01:00
{this.context.t('encryptionPublicKeyRequest')}
</div>
<div className="request-encryption-public-key__header__tip-container">
<div className="request-encryption-public-key__header__tip" />
</div>
</div>
);
};
renderAccount = () => {
const { fromAccount } = this.state;
const { t } = this.context;
return (
<div className="request-encryption-public-key__account">
<div className="request-encryption-public-key__account-text">
2020-11-03 00:41:28 +01:00
{`${t('account')}:`}
</div>
<div className="request-encryption-public-key__account-item">
2020-11-03 00:41:28 +01:00
<AccountListItem account={fromAccount} />
</div>
</div>
);
};
renderBalance = () => {
const { conversionRate } = this.props;
const { t } = this.context;
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-encryption-public-key__balance">
<div className="request-encryption-public-key__balance-text">
2020-11-03 00:41:28 +01:00
{`${t('balance')}:`}
</div>
<div className="request-encryption-public-key__balance-value">
2020-11-03 00:41:28 +01:00
{`${balanceInEther} ETH`}
</div>
</div>
);
};
renderRequestIcon = () => {
const { requesterAddress } = this.props;
return (
<div className="request-encryption-public-key__request-icon">
2020-11-03 00:41:28 +01:00
<Identicon diameter={40} address={requesterAddress} />
</div>
);
};
renderAccountInfo = () => {
return (
<div className="request-encryption-public-key__account-info">
2020-11-03 00:41:28 +01:00
{this.renderAccount()}
{this.renderRequestIcon()}
{this.renderBalance()}
</div>
);
};
renderBody = () => {
const { domainMetadata, txData } = this.props;
const { t } = this.context;
const originMetadata = domainMetadata[txData.origin];
const notice = t('encryptionPublicKeyNotice', [txData.origin]);
2021-02-22 17:20:42 +01:00
const name = originMetadata?.hostname || txData.origin;
return (
<div className="request-encryption-public-key__body">
2020-11-03 00:41:28 +01:00
{this.renderAccountInfo()}
<div className="request-encryption-public-key__visual">
<section>
{originMetadata?.icon ? (
<img
className="request-encryption-public-key__visual-identicon"
src={originMetadata.icon}
alt=""
/>
) : (
<i className="request-encryption-public-key__visual-identicon--default">
{name.charAt(0).toUpperCase()}
</i>
)}
2020-11-03 00:41:28 +01:00
<div className="request-encryption-public-key__notice">
{notice}
</div>
</section>
</div>
</div>
);
};
renderFooter = () => {
const {
cancelEncryptionPublicKey,
clearConfirmTransaction,
encryptionPublicKey,
history,
mostRecentOverviewPage,
txData,
} = this.props;
const { t, metricsEvent } = this.context;
return (
<div className="request-encryption-public-key__footer">
<Button
type="default"
large
className="request-encryption-public-key__footer__cancel-button"
onClick={async (event) => {
this._removeBeforeUnload();
await cancelEncryptionPublicKey(txData, event);
metricsEvent({
eventOpts: {
category: 'Messages',
action: 'Encryption public key Request',
name: 'Cancel',
},
});
clearConfirmTransaction();
history.push(mostRecentOverviewPage);
}}
>
2020-11-03 00:41:28 +01:00
{this.context.t('cancel')}
</Button>
<Button
type="secondary"
large
className="request-encryption-public-key__footer__sign-button"
onClick={async (event) => {
this._removeBeforeUnload();
await encryptionPublicKey(txData, event);
this.context.metricsEvent({
eventOpts: {
category: 'Messages',
action: 'Encryption public key Request',
name: 'Confirm',
},
});
clearConfirmTransaction();
history.push(mostRecentOverviewPage);
}}
>
2020-11-03 00:41:28 +01:00
{t('provide')}
</Button>
</div>
);
};
render = () => {
return (
<div className="request-encryption-public-key__container">
2020-11-03 00:41:28 +01:00
{this.renderHeader()}
{this.renderBody()}
{this.renderFooter()}
</div>
);
};
}