2021-02-22 17:20:42 +01:00
|
|
|
import { ethErrors } from 'eth-rpc-errors';
|
2022-06-30 18:19:07 +02:00
|
|
|
import React from 'react';
|
2022-07-14 11:43:56 +02:00
|
|
|
import { infuraProjectId } from '../../../../shared/constants/network';
|
2021-02-22 17:20:42 +01:00
|
|
|
import {
|
|
|
|
SEVERITIES,
|
2023-02-02 21:15:26 +01:00
|
|
|
TypographyVariant,
|
2022-06-30 18:19:07 +02:00
|
|
|
TEXT_ALIGN,
|
|
|
|
DISPLAY,
|
2022-08-10 20:37:35 +02:00
|
|
|
FLEX_DIRECTION,
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
|
|
|
JustifyContent,
|
|
|
|
BackgroundColor,
|
2021-02-22 17:20:42 +01:00
|
|
|
} from '../../../helpers/constants/design-system';
|
2022-07-27 22:19:05 +02:00
|
|
|
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes';
|
2022-08-23 16:19:31 +02:00
|
|
|
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
|
2022-09-16 21:05:21 +02:00
|
|
|
import fetchWithCache from '../../../../shared/lib/fetch-with-cache';
|
2021-02-22 17:20:42 +01:00
|
|
|
|
|
|
|
const UNRECOGNIZED_CHAIN = {
|
|
|
|
id: 'UNRECOGNIZED_CHAIN',
|
|
|
|
severity: SEVERITIES.WARNING,
|
|
|
|
content: {
|
|
|
|
element: 'span',
|
|
|
|
children: {
|
|
|
|
element: 'MetaMaskTranslation',
|
|
|
|
props: {
|
|
|
|
translationKey: 'unrecognizedChain',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-07-18 17:59:38 +02:00
|
|
|
const MISMATCHED_CHAIN_RECOMMENDATION = {
|
|
|
|
id: 'MISMATCHED_CHAIN_RECOMMENDATION',
|
2021-02-22 17:20:42 +01:00
|
|
|
content: {
|
|
|
|
element: 'span',
|
|
|
|
children: {
|
|
|
|
element: 'MetaMaskTranslation',
|
|
|
|
props: {
|
2022-07-18 17:59:38 +02:00
|
|
|
translationKey: 'mismatchedChainRecommendation',
|
2021-02-22 17:20:42 +01:00
|
|
|
variables: [
|
|
|
|
{
|
|
|
|
element: 'a',
|
|
|
|
key: 'mismatchedChainLink',
|
|
|
|
props: {
|
2022-08-23 16:19:31 +02:00
|
|
|
href: ZENDESK_URLS.VERIFY_CUSTOM_NETWORK,
|
2021-02-22 17:20:42 +01:00
|
|
|
target: '__blank',
|
|
|
|
tabIndex: 0,
|
|
|
|
},
|
|
|
|
children: {
|
|
|
|
element: 'MetaMaskTranslation',
|
|
|
|
props: {
|
|
|
|
translationKey: 'mismatchedChainLinkText',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-07-18 17:59:38 +02:00
|
|
|
const MISMATCHED_NETWORK_NAME = {
|
|
|
|
id: 'MISMATCHED_NETWORK_NAME',
|
|
|
|
severity: SEVERITIES.WARNING,
|
|
|
|
content: {
|
|
|
|
element: 'span',
|
|
|
|
children: {
|
|
|
|
element: 'MetaMaskTranslation',
|
|
|
|
props: {
|
|
|
|
translationKey: 'mismatchedNetworkName',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const MISMATCHED_NETWORK_SYMBOL = {
|
|
|
|
id: 'MISMATCHED_NETWORK_SYMBOL',
|
|
|
|
severity: SEVERITIES.DANGER,
|
|
|
|
content: {
|
|
|
|
element: 'span',
|
|
|
|
children: {
|
|
|
|
element: 'MetaMaskTranslation',
|
|
|
|
props: {
|
|
|
|
translationKey: 'mismatchedNetworkSymbol',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const MISMATCHED_NETWORK_RPC = {
|
|
|
|
id: 'MISMATCHED_NETWORK_RPC',
|
|
|
|
severity: SEVERITIES.DANGER,
|
|
|
|
content: {
|
|
|
|
element: 'span',
|
|
|
|
children: {
|
|
|
|
element: 'MetaMaskTranslation',
|
|
|
|
props: {
|
|
|
|
translationKey: 'mismatchedRpcUrl',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-02-22 17:20:42 +01:00
|
|
|
async function getAlerts(pendingApproval) {
|
|
|
|
const alerts = [];
|
2022-05-24 18:30:46 +02:00
|
|
|
const safeChainsList =
|
|
|
|
(await fetchWithCache('https://chainid.network/chains.json')) || [];
|
2021-02-22 17:20:42 +01:00
|
|
|
const matchedChain = safeChainsList.find(
|
|
|
|
(chain) =>
|
|
|
|
chain.chainId === parseInt(pendingApproval.requestData.chainId, 16),
|
|
|
|
);
|
|
|
|
|
2022-06-30 18:19:07 +02:00
|
|
|
const originIsMetaMask = pendingApproval.origin === 'metamask';
|
2022-07-18 17:59:38 +02:00
|
|
|
if (originIsMetaMask && Boolean(matchedChain)) {
|
2022-06-30 18:19:07 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:20:42 +01:00
|
|
|
if (matchedChain) {
|
|
|
|
if (
|
|
|
|
matchedChain.name.toLowerCase() !==
|
2022-07-18 17:59:38 +02:00
|
|
|
pendingApproval.requestData.chainName.toLowerCase()
|
|
|
|
) {
|
|
|
|
alerts.push(MISMATCHED_NETWORK_NAME);
|
|
|
|
}
|
|
|
|
if (
|
2021-02-22 17:20:42 +01:00
|
|
|
matchedChain.nativeCurrency?.symbol !== pendingApproval.requestData.ticker
|
|
|
|
) {
|
2022-07-18 17:59:38 +02:00
|
|
|
alerts.push(MISMATCHED_NETWORK_SYMBOL);
|
2021-02-22 17:20:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const { origin } = new URL(pendingApproval.requestData.rpcUrl);
|
|
|
|
if (!matchedChain.rpc.map((rpc) => new URL(rpc).origin).includes(origin)) {
|
2022-07-18 17:59:38 +02:00
|
|
|
alerts.push(MISMATCHED_NETWORK_RPC);
|
2021-02-22 17:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matchedChain) {
|
|
|
|
alerts.push(UNRECOGNIZED_CHAIN);
|
|
|
|
}
|
2022-07-18 17:59:38 +02:00
|
|
|
|
|
|
|
if (alerts.length) {
|
|
|
|
alerts.push(MISMATCHED_CHAIN_RECOMMENDATION);
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:20:42 +01:00
|
|
|
return alerts;
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:14:27 +01:00
|
|
|
function getState(pendingApproval) {
|
|
|
|
if (parseInt(pendingApproval.requestData.chainId, 16) === 1) {
|
|
|
|
return { useWarningModal: true };
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-27 22:19:05 +02:00
|
|
|
function getValues(pendingApproval, t, actions, history) {
|
2022-06-30 18:19:07 +02:00
|
|
|
const originIsMetaMask = pendingApproval.origin === 'metamask';
|
|
|
|
|
2021-02-22 17:20:42 +01:00
|
|
|
return {
|
|
|
|
content: [
|
2022-06-30 18:19:07 +02:00
|
|
|
{
|
|
|
|
hide: !originIsMetaMask,
|
|
|
|
element: 'Box',
|
|
|
|
key: 'network-box',
|
|
|
|
props: {
|
|
|
|
textAlign: TEXT_ALIGN.CENTER,
|
|
|
|
display: DISPLAY.FLEX,
|
2023-02-02 21:15:26 +01:00
|
|
|
justifyContent: JustifyContent.center,
|
2022-06-30 18:19:07 +02:00
|
|
|
marginTop: 4,
|
|
|
|
marginBottom: 2,
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
element: 'Chip',
|
|
|
|
key: 'network-chip',
|
|
|
|
props: {
|
|
|
|
label: pendingApproval.requestData.chainName,
|
2023-02-02 21:15:26 +01:00
|
|
|
backgroundColor: BackgroundColor.backgroundAlternative,
|
2022-06-30 18:19:07 +02:00
|
|
|
leftIconUrl: pendingApproval.requestData.imageUrl,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-02-22 17:20:42 +01:00
|
|
|
{
|
|
|
|
element: 'Typography',
|
|
|
|
key: 'title',
|
2022-06-30 18:19:07 +02:00
|
|
|
children: originIsMetaMask
|
|
|
|
? t('wantToAddThisNetwork')
|
|
|
|
: t('addEthereumChainConfirmationTitle'),
|
2021-02-22 17:20:42 +01:00
|
|
|
props: {
|
2023-02-02 21:15:26 +01:00
|
|
|
variant: TypographyVariant.H3,
|
2021-02-22 17:20:42 +01:00
|
|
|
align: 'center',
|
|
|
|
fontWeight: 'bold',
|
|
|
|
boxProps: {
|
|
|
|
margin: [0, 0, 4],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
element: 'Typography',
|
|
|
|
key: 'description',
|
|
|
|
children: t('addEthereumChainConfirmationDescription'),
|
|
|
|
props: {
|
2023-02-02 21:15:26 +01:00
|
|
|
variant: TypographyVariant.H7,
|
2021-02-22 17:20:42 +01:00
|
|
|
align: 'center',
|
|
|
|
boxProps: {
|
2022-06-30 18:19:07 +02:00
|
|
|
margin: originIsMetaMask ? [0, 8, 4] : [0, 0, 4],
|
2021-02-22 17:20:42 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
element: 'Typography',
|
|
|
|
key: 'only-add-networks-you-trust',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
element: 'b',
|
|
|
|
key: 'bolded-text',
|
2022-06-30 18:19:07 +02:00
|
|
|
props: {
|
|
|
|
style: { display: originIsMetaMask && '-webkit-box' },
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
`${t('addEthereumChainConfirmationRisks')} `,
|
|
|
|
{
|
|
|
|
hide: !originIsMetaMask,
|
|
|
|
element: 'Tooltip',
|
|
|
|
key: 'tooltip-info',
|
|
|
|
props: {
|
|
|
|
position: 'bottom',
|
|
|
|
interactive: true,
|
|
|
|
trigger: 'mouseenter',
|
|
|
|
html: (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
width: '180px',
|
|
|
|
margin: '16px',
|
|
|
|
textAlign: 'left',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('someNetworksMayPoseSecurity')}{' '}
|
|
|
|
<a
|
|
|
|
key="zendesk_page_link"
|
2022-08-23 16:19:31 +02:00
|
|
|
href={ZENDESK_URLS.UNKNOWN_NETWORK}
|
2022-06-30 18:19:07 +02:00
|
|
|
rel="noreferrer"
|
|
|
|
target="_blank"
|
|
|
|
style={{ color: 'var(--color-primary-default)' }}
|
|
|
|
>
|
|
|
|
{t('learnMoreUpperCase')}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
element: 'i',
|
|
|
|
key: 'info-circle',
|
|
|
|
props: {
|
|
|
|
className: 'fas fa-info-circle',
|
|
|
|
style: {
|
|
|
|
marginLeft: '4px',
|
|
|
|
color: 'var(--color-icon-default)',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2021-02-22 17:20:42 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
element: 'MetaMaskTranslation',
|
|
|
|
key: 'learn-about-risks',
|
|
|
|
props: {
|
|
|
|
translationKey: 'addEthereumChainConfirmationRisksLearnMore',
|
|
|
|
variables: [
|
|
|
|
{
|
|
|
|
element: 'a',
|
|
|
|
children: t('addEthereumChainConfirmationRisksLearnMoreLink'),
|
|
|
|
key: 'addEthereumChainConfirmationRisksLearnMoreLink',
|
|
|
|
props: {
|
2022-08-23 16:19:31 +02:00
|
|
|
href: ZENDESK_URLS.USER_GUIDE_CUSTOM_NETWORKS,
|
2021-02-22 17:20:42 +01:00
|
|
|
target: '__blank',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
props: {
|
2023-02-02 21:15:26 +01:00
|
|
|
variant: TypographyVariant.H7,
|
2021-02-22 17:20:42 +01:00
|
|
|
boxProps: {
|
2022-06-30 18:19:07 +02:00
|
|
|
margin: originIsMetaMask ? [0, 8] : 0,
|
2022-08-10 20:37:35 +02:00
|
|
|
display: DISPLAY.FLEX,
|
|
|
|
flexDirection: FLEX_DIRECTION.COLUMN,
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems: AlignItems.center,
|
2021-02-22 17:20:42 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
element: 'TruncatedDefinitionList',
|
|
|
|
key: 'network-details',
|
|
|
|
props: {
|
|
|
|
title: t('networkDetails'),
|
|
|
|
tooltips: {
|
|
|
|
[t('networkName')]: t('networkNameDefinition'),
|
|
|
|
[t('networkURL')]: t('networkURLDefinition'),
|
|
|
|
[t('chainId')]: t('chainIdDefinition'),
|
|
|
|
[t('currencySymbol')]: t('currencySymbolDefinition'),
|
|
|
|
[t('blockExplorerUrl')]: t('blockExplorerUrlDefinition'),
|
|
|
|
},
|
|
|
|
dictionary: {
|
|
|
|
[t('networkName')]: pendingApproval.requestData.chainName,
|
2022-07-14 11:43:56 +02:00
|
|
|
[t('networkURL')]: pendingApproval.requestData.rpcUrl?.includes(
|
|
|
|
`/v3/${infuraProjectId}`,
|
|
|
|
)
|
|
|
|
? pendingApproval.requestData.rpcUrl.replace(
|
|
|
|
`/v3/${infuraProjectId}`,
|
|
|
|
'',
|
|
|
|
)
|
|
|
|
: pendingApproval.requestData.rpcUrl,
|
2021-02-22 17:20:42 +01:00
|
|
|
[t('chainId')]: parseInt(pendingApproval.requestData.chainId, 16),
|
|
|
|
[t('currencySymbol')]: pendingApproval.requestData.ticker,
|
2022-07-31 20:26:40 +02:00
|
|
|
[t('blockExplorerUrl')]:
|
|
|
|
pendingApproval.requestData.blockExplorerUrl,
|
2021-02-22 17:20:42 +01:00
|
|
|
},
|
2022-07-18 17:59:38 +02:00
|
|
|
prefaceKeys: [
|
|
|
|
t('networkName'),
|
|
|
|
t('networkURL'),
|
|
|
|
t('chainId'),
|
|
|
|
t('currencySymbol'),
|
|
|
|
],
|
2021-02-22 17:20:42 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
cancelText: t('cancel'),
|
2022-12-01 16:46:06 +01:00
|
|
|
submitText: t('approveButtonText'),
|
|
|
|
onSubmit: async () => {
|
2022-07-27 22:19:05 +02:00
|
|
|
await actions.resolvePendingApproval(
|
2021-02-22 17:20:42 +01:00
|
|
|
pendingApproval.id,
|
|
|
|
pendingApproval.requestData,
|
2022-07-27 22:19:05 +02:00
|
|
|
);
|
|
|
|
if (originIsMetaMask) {
|
|
|
|
actions.addCustomNetwork(pendingApproval.requestData);
|
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
}
|
|
|
|
},
|
2021-02-22 17:20:42 +01:00
|
|
|
onCancel: () =>
|
|
|
|
actions.rejectPendingApproval(
|
|
|
|
pendingApproval.id,
|
2022-03-10 18:55:25 +01:00
|
|
|
ethErrors.provider.userRejectedRequest().serialize(),
|
2021-02-22 17:20:42 +01:00
|
|
|
),
|
2022-06-30 18:19:07 +02:00
|
|
|
networkDisplay: !originIsMetaMask,
|
2021-02-22 17:20:42 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const addEthereumChain = {
|
|
|
|
getAlerts,
|
|
|
|
getValues,
|
2022-12-03 04:14:27 +01:00
|
|
|
getState,
|
2021-02-22 17:20:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default addEthereumChain;
|