1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00
metamask-extension/ui/pages/confirmation/templates/add-ethereum-chain.js

424 lines
12 KiB
JavaScript
Raw Normal View History

2021-02-22 17:20:42 +01:00
import { ethErrors } from 'eth-rpc-errors';
import React from 'react';
import { infuraProjectId } from '../../../../shared/constants/network';
2021-02-22 17:20:42 +01:00
import {
Severity,
TypographyVariant,
TextAlign,
Display,
FlexDirection,
AlignItems,
JustifyContent,
BackgroundColor,
2021-02-22 17:20:42 +01:00
} from '../../../helpers/constants/design-system';
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes';
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
import fetchWithCache from '../../../../shared/lib/fetch-with-cache';
2023-03-08 17:33:27 +01:00
import { jsonRpcRequest } from '../../../../shared/modules/rpc.utils';
2021-02-22 17:20:42 +01:00
const UNRECOGNIZED_CHAIN = {
id: 'UNRECOGNIZED_CHAIN',
severity: Severity.Warning,
2021-02-22 17:20:42 +01:00
content: {
element: 'span',
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'unrecognizedChain',
},
},
},
};
const MISMATCHED_CHAIN_RECOMMENDATION = {
id: 'MISMATCHED_CHAIN_RECOMMENDATION',
severity: Severity.Warning,
2021-02-22 17:20:42 +01:00
content: {
element: 'span',
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'mismatchedChainRecommendation',
2021-02-22 17:20:42 +01:00
variables: [
{
element: 'a',
key: 'mismatchedChainLink',
props: {
href: ZENDESK_URLS.VERIFY_CUSTOM_NETWORK,
2021-02-22 17:20:42 +01:00
target: '__blank',
tabIndex: 0,
},
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'mismatchedChainLinkText',
},
},
},
],
},
},
},
};
const MISMATCHED_NETWORK_NAME = {
id: 'MISMATCHED_NETWORK_NAME',
severity: Severity.Warning,
content: {
element: 'span',
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'mismatchedNetworkName',
},
},
},
};
const MISMATCHED_NETWORK_SYMBOL = {
id: 'MISMATCHED_NETWORK_SYMBOL',
severity: Severity.Danger,
content: {
element: 'span',
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'mismatchedNetworkSymbol',
},
},
},
};
const MISMATCHED_NETWORK_RPC = {
id: 'MISMATCHED_NETWORK_RPC',
severity: Severity.Danger,
content: {
element: 'span',
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'mismatchedRpcUrl',
},
},
},
};
2023-03-08 17:33:27 +01:00
const MISMATCHED_NETWORK_RPC_CHAIN_ID = {
id: 'MISMATCHED_NETWORK_RPC_CHAIN_ID',
severity: Severity.Danger,
2023-03-08 17:33:27 +01:00
content: {
element: 'span',
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'mismatchedRpcChainId',
},
},
},
};
const ERROR_CONNECTING_TO_RPC = {
id: 'ERROR_CONNECTING_TO_RPC',
severity: Severity.Danger,
2023-03-08 17:33:27 +01:00
content: {
element: 'span',
children: {
element: 'MetaMaskTranslation',
props: {
translationKey: 'errorWhileConnectingToRPC',
},
},
},
};
2021-02-22 17:20:42 +01:00
async function getAlerts(pendingApproval) {
const alerts = [];
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),
);
const originIsMetaMask = pendingApproval.origin === 'metamask';
if (originIsMetaMask && Boolean(matchedChain)) {
return [];
}
2021-02-22 17:20:42 +01:00
if (matchedChain) {
if (
matchedChain.name.toLowerCase() !==
pendingApproval.requestData.chainName.toLowerCase()
) {
alerts.push(MISMATCHED_NETWORK_NAME);
}
if (
2021-02-22 17:20:42 +01:00
matchedChain.nativeCurrency?.symbol !== pendingApproval.requestData.ticker
) {
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)) {
alerts.push(MISMATCHED_NETWORK_RPC);
2021-02-22 17:20:42 +01:00
}
}
if (!matchedChain) {
alerts.push(UNRECOGNIZED_CHAIN);
}
if (alerts.length) {
alerts.push(MISMATCHED_CHAIN_RECOMMENDATION);
}
2021-02-22 17:20:42 +01:00
return alerts;
}
function getState(pendingApproval) {
if (parseInt(pendingApproval.requestData.chainId, 16) === 1) {
return { useWarningModal: true };
}
return {};
}
function getValues(pendingApproval, t, actions, history) {
const originIsMetaMask = pendingApproval.origin === 'metamask';
2023-03-08 17:33:27 +01:00
const customRpcUrl = pendingApproval.requestData.rpcUrl;
2021-02-22 17:20:42 +01:00
return {
content: [
{
hide: !originIsMetaMask,
element: 'Box',
key: 'network-box',
props: {
textAlign: TextAlign.Center,
display: Display.Flex,
justifyContent: JustifyContent.center,
marginTop: 4,
marginBottom: 2,
},
children: [
{
element: 'Chip',
key: 'network-chip',
props: {
label: pendingApproval.requestData.chainName,
backgroundColor: BackgroundColor.backgroundAlternative,
leftIconUrl: pendingApproval.requestData.imageUrl,
},
},
],
},
2023-03-08 17:33:27 +01:00
2021-02-22 17:20:42 +01:00
{
element: 'Typography',
key: 'title',
children: originIsMetaMask
? t('wantToAddThisNetwork')
: t('addEthereumChainConfirmationTitle'),
2021-02-22 17:20:42 +01:00
props: {
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: {
variant: TypographyVariant.H7,
2021-02-22 17:20:42 +01:00
align: 'center',
boxProps: {
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',
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"
href={ZENDESK_URLS.UNKNOWN_NETWORK}
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: {
href: ZENDESK_URLS.USER_GUIDE_CUSTOM_NETWORKS,
2021-02-22 17:20:42 +01:00
target: '__blank',
},
},
],
},
},
],
props: {
variant: TypographyVariant.H7,
2021-02-22 17:20:42 +01:00
boxProps: {
margin: originIsMetaMask ? [0, 8] : 0,
display: Display.Flex,
flexDirection: FlexDirection.Column,
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,
[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.rpcPrefs.blockExplorerUrl,
2021-02-22 17:20:42 +01:00
},
prefaceKeys: [
t('networkName'),
t('networkURL'),
t('chainId'),
t('currencySymbol'),
],
2021-02-22 17:20:42 +01:00
},
},
],
cancelText: t('cancel'),
submitText: t('approveButtonText'),
2023-03-08 17:33:27 +01:00
loadingText: t('addingCustomNetwork'),
onSubmit: async () => {
2023-03-08 17:33:27 +01:00
let endpointChainId;
try {
endpointChainId = await jsonRpcRequest(customRpcUrl, 'eth_chainId');
} catch (err) {
console.error(
`Request for method 'eth_chainId on ${customRpcUrl} failed`,
);
return [ERROR_CONNECTING_TO_RPC];
}
if (pendingApproval.requestData.chainId !== endpointChainId) {
console.error(
`Chain ID returned by RPC URL ${customRpcUrl} does not match ${endpointChainId}`,
);
return [MISMATCHED_NETWORK_RPC_CHAIN_ID];
}
await actions.resolvePendingApproval(
2021-02-22 17:20:42 +01:00
pendingApproval.id,
pendingApproval.requestData,
);
if (originIsMetaMask) {
const networkConfigurationId = await actions.upsertNetworkConfiguration(
{
...pendingApproval.requestData,
nickname: pendingApproval.requestData.chainName,
},
{
setActive: false,
source: pendingApproval.requestData.source,
},
);
await actions.setNewNetworkAdded({
networkConfigurationId,
nickname: pendingApproval.requestData.chainName,
});
history.push(DEFAULT_ROUTE);
}
2023-03-08 17:33:27 +01:00
return [];
},
2021-02-22 17:20:42 +01:00
onCancel: () =>
actions.rejectPendingApproval(
pendingApproval.id,
ethErrors.provider.userRejectedRequest().serialize(),
2021-02-22 17:20:42 +01:00
),
networkDisplay: !originIsMetaMask,
2021-02-22 17:20:42 +01:00
};
}
const addEthereumChain = {
getAlerts,
getValues,
getState,
2021-02-22 17:20:42 +01:00
};
export default addEthereumChain;