mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
fix chainId validation on network form (#16452)
This commit is contained in:
parent
de0f1ec51b
commit
68684bfd11
@ -232,6 +232,8 @@ const NetworksForm = ({
|
|||||||
const formChainId = chainArg.trim();
|
const formChainId = chainArg.trim();
|
||||||
let errorKey = '';
|
let errorKey = '';
|
||||||
let errorMessage = '';
|
let errorMessage = '';
|
||||||
|
let warningKey = '';
|
||||||
|
let warningMessage = '';
|
||||||
let radix = 10;
|
let radix = 10;
|
||||||
let hexChainId = formChainId;
|
let hexChainId = formChainId;
|
||||||
|
|
||||||
@ -240,8 +242,10 @@ const NetworksForm = ({
|
|||||||
hexChainId = `0x${decimalToHex(hexChainId)}`;
|
hexChainId = `0x${decimalToHex(hexChainId)}`;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {
|
return {
|
||||||
|
error: {
|
||||||
key: 'invalidHexNumber',
|
key: 'invalidHexNumber',
|
||||||
msg: t('invalidHexNumber'),
|
msg: t('invalidHexNumber'),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,8 +257,8 @@ const NetworksForm = ({
|
|||||||
if (formChainId === '') {
|
if (formChainId === '') {
|
||||||
return null;
|
return null;
|
||||||
} else if (matchingChainId) {
|
} else if (matchingChainId) {
|
||||||
errorKey = 'chainIdExistsErrorMsg';
|
warningKey = 'chainIdExistsErrorMsg';
|
||||||
errorMessage = t('chainIdExistsErrorMsg', [
|
warningMessage = t('chainIdExistsErrorMsg', [
|
||||||
matchingChainId.label ?? matchingChainId.labelKey,
|
matchingChainId.label ?? matchingChainId.labelKey,
|
||||||
]);
|
]);
|
||||||
} else if (formChainId.startsWith('0x')) {
|
} else if (formChainId.startsWith('0x')) {
|
||||||
@ -316,8 +320,18 @@ const NetworksForm = ({
|
|||||||
}
|
}
|
||||||
if (errorKey) {
|
if (errorKey) {
|
||||||
return {
|
return {
|
||||||
|
error: {
|
||||||
key: errorKey,
|
key: errorKey,
|
||||||
msg: errorMessage,
|
msg: errorMessage,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (warningKey) {
|
||||||
|
return {
|
||||||
|
warning: {
|
||||||
|
key: warningKey,
|
||||||
|
msg: warningMessage,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -447,7 +461,8 @@ const NetworksForm = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
async function validate() {
|
async function validate() {
|
||||||
const chainIdError = await validateChainId(chainId);
|
const { error: chainIdError, warning: chainIdWarning } =
|
||||||
|
(await validateChainId(chainId)) || {};
|
||||||
const tickerWarning = await validateTickerSymbol(chainId, ticker);
|
const tickerWarning = await validateTickerSymbol(chainId, ticker);
|
||||||
const blockExplorerError = validateBlockExplorerURL(blockExplorerUrl);
|
const blockExplorerError = validateBlockExplorerURL(blockExplorerUrl);
|
||||||
const rpcUrlError = validateRPCUrl(rpcUrl);
|
const rpcUrlError = validateRPCUrl(rpcUrl);
|
||||||
@ -455,10 +470,11 @@ const NetworksForm = ({
|
|||||||
...errors,
|
...errors,
|
||||||
blockExplorerUrl: blockExplorerError,
|
blockExplorerUrl: blockExplorerError,
|
||||||
rpcUrl: rpcUrlError,
|
rpcUrl: rpcUrlError,
|
||||||
|
chainId: chainIdError,
|
||||||
});
|
});
|
||||||
setWarnings({
|
setWarnings({
|
||||||
...warnings,
|
...warnings,
|
||||||
chainId: chainIdError,
|
chainId: chainIdWarning,
|
||||||
ticker: tickerWarning,
|
ticker: tickerWarning,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -632,6 +648,7 @@ const NetworksForm = ({
|
|||||||
/>
|
/>
|
||||||
<FormField
|
<FormField
|
||||||
warning={warnings.chainId?.msg || ''}
|
warning={warnings.chainId?.msg || ''}
|
||||||
|
error={errors.chainId?.msg || ''}
|
||||||
onChange={(value) => {
|
onChange={(value) => {
|
||||||
setIsEditing(true);
|
setIsEditing(true);
|
||||||
setChainId(value);
|
setChainId(value);
|
||||||
|
@ -78,7 +78,13 @@ describe('NetworkForm Component', () => {
|
|||||||
encodedQueryParams: true,
|
encodedQueryParams: true,
|
||||||
})
|
})
|
||||||
.post('/')
|
.post('/')
|
||||||
.reply(200, { jsonrpc: '2.0', id: '1643927040523', result: '0x38' });
|
.reply(200, { jsonrpc: '2.0', result: '0x38' });
|
||||||
|
|
||||||
|
nock('https://rpc.flashbots.net:443', {
|
||||||
|
encodedQueryParams: true,
|
||||||
|
})
|
||||||
|
.post('/')
|
||||||
|
.reply(200, { jsonrpc: '2.0', result: '0x1' });
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@ -190,17 +196,30 @@ describe('NetworkForm Component', () => {
|
|||||||
renderComponent(propNewNetwork);
|
renderComponent(propNewNetwork);
|
||||||
const chainIdField = screen.getByRole('textbox', { name: 'Chain ID' });
|
const chainIdField = screen.getByRole('textbox', { name: 'Chain ID' });
|
||||||
const rpcUrlField = screen.getByRole('textbox', { name: 'New RPC URL' });
|
const rpcUrlField = screen.getByRole('textbox', { name: 'New RPC URL' });
|
||||||
|
const currencySymbolField = screen.getByRole('textbox', {
|
||||||
|
name: 'Currency symbol',
|
||||||
|
});
|
||||||
|
|
||||||
fireEvent.change(chainIdField, {
|
fireEvent.change(chainIdField, {
|
||||||
target: { value: '1' },
|
target: { value: '1' },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
fireEvent.change(currencySymbolField, {
|
||||||
|
target: { value: 'test' },
|
||||||
|
});
|
||||||
|
|
||||||
|
fireEvent.change(rpcUrlField, {
|
||||||
|
target: { value: 'https://rpc.flashbots.net' },
|
||||||
|
});
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
await screen.findByText(
|
await screen.findByText(
|
||||||
'This Chain ID is currently used by the mainnet network.',
|
'This Chain ID is currently used by the mainnet network.',
|
||||||
),
|
),
|
||||||
).toBeInTheDocument();
|
).toBeInTheDocument();
|
||||||
|
|
||||||
|
expect(screen.getByText('Save')).not.toBeDisabled();
|
||||||
|
|
||||||
fireEvent.change(rpcUrlField, {
|
fireEvent.change(rpcUrlField, {
|
||||||
target: { value: 'https://bsc-dataseed.binance.org/' },
|
target: { value: 'https://bsc-dataseed.binance.org/' },
|
||||||
});
|
});
|
||||||
@ -209,6 +228,8 @@ describe('NetworkForm Component', () => {
|
|||||||
'The RPC URL you have entered returned a different chain ID (56). Please update the Chain ID to match the RPC URL of the network you are trying to add.';
|
'The RPC URL you have entered returned a different chain ID (56). Please update the Chain ID to match the RPC URL of the network you are trying to add.';
|
||||||
expect(await screen.findByText(expectedWarning)).toBeInTheDocument();
|
expect(await screen.findByText(expectedWarning)).toBeInTheDocument();
|
||||||
|
|
||||||
|
expect(screen.getByText('Save')).toBeDisabled();
|
||||||
|
|
||||||
fireEvent.change(chainIdField, {
|
fireEvent.change(chainIdField, {
|
||||||
target: { value: 'a' },
|
target: { value: 'a' },
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user