1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00

Handling non-numeric input for chain id in network form (#11537)

This commit is contained in:
ryanml 2021-07-15 14:11:43 -07:00 committed by GitHub
parent b21ad53bdc
commit b21b139653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -358,14 +358,27 @@ export default class NetworkForm extends PureComponent {
}; };
validateChainIdOnChange = (selfRpcUrl, chainIdArg = '') => { validateChainIdOnChange = (selfRpcUrl, chainIdArg = '') => {
const { t } = this.context;
const { networksToRender } = this.props; const { networksToRender } = this.props;
const chainId = chainIdArg.trim(); const chainId = chainIdArg.trim();
let errorKey = ''; let errorKey = '';
let errorMessage = ''; let errorMessage = '';
let radix = 10; let radix = 10;
const hexChainId = chainId.startsWith('0x') let hexChainId = chainId;
? chainId
: `0x${decimalToHex(chainId)}`; if (!hexChainId.startsWith('0x')) {
try {
hexChainId = `0x${decimalToHex(hexChainId)}`;
} catch (err) {
this.setErrorTo('chainId', {
key: 'invalidHexNumber',
msg: t('invalidHexNumber'),
});
return;
}
}
const [matchingChainId] = networksToRender.filter( const [matchingChainId] = networksToRender.filter(
(e) => e.chainId === hexChainId && e.rpcUrl !== selfRpcUrl, (e) => e.chainId === hexChainId && e.rpcUrl !== selfRpcUrl,
); );
@ -375,26 +388,26 @@ export default class NetworkForm extends PureComponent {
return; return;
} else if (matchingChainId) { } else if (matchingChainId) {
errorKey = 'chainIdExistsErrorMsg'; errorKey = 'chainIdExistsErrorMsg';
errorMessage = this.context.t('chainIdExistsErrorMsg', [ errorMessage = t('chainIdExistsErrorMsg', [
matchingChainId.label ?? matchingChainId.labelKey, matchingChainId.label ?? matchingChainId.labelKey,
]); ]);
} else if (chainId.startsWith('0x')) { } else if (chainId.startsWith('0x')) {
radix = 16; radix = 16;
if (!/^0x[0-9a-f]+$/iu.test(chainId)) { if (!/^0x[0-9a-f]+$/iu.test(chainId)) {
errorKey = 'invalidHexNumber'; errorKey = 'invalidHexNumber';
errorMessage = this.context.t('invalidHexNumber'); errorMessage = t('invalidHexNumber');
} else if (!isPrefixedFormattedHexString(chainId)) { } else if (!isPrefixedFormattedHexString(chainId)) {
errorMessage = this.context.t('invalidHexNumberLeadingZeros'); errorMessage = t('invalidHexNumberLeadingZeros');
} }
} else if (!/^[0-9]+$/u.test(chainId)) { } else if (!/^[0-9]+$/u.test(chainId)) {
errorKey = 'invalidNumber'; errorKey = 'invalidNumber';
errorMessage = this.context.t('invalidNumber'); errorMessage = t('invalidNumber');
} else if (chainId.startsWith('0')) { } else if (chainId.startsWith('0')) {
errorKey = 'invalidNumberLeadingZeros'; errorKey = 'invalidNumberLeadingZeros';
errorMessage = this.context.t('invalidNumberLeadingZeros'); errorMessage = t('invalidNumberLeadingZeros');
} else if (!isSafeChainId(parseInt(chainId, radix))) { } else if (!isSafeChainId(parseInt(chainId, radix))) {
errorKey = 'invalidChainIdTooBig'; errorKey = 'invalidChainIdTooBig';
errorMessage = this.context.t('invalidChainIdTooBig'); errorMessage = t('invalidChainIdTooBig');
} }
this.setErrorTo('chainId', { this.setErrorTo('chainId', {