1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Support hex and number net_version responses

Hex and number responses from the `net_version` request are now
accepted. While most chains return decimal strings for this request,
some chains were returning hex responses instead and failing our
validation for this request (which was added in v10.30.0). This
resolves that problem.

Support for number responses was added because it likely worked in
older versions as well, so support is maintained to avoid similar
problems.

Fixes #19151
This commit is contained in:
Mark Stacey 2023-05-15 17:52:07 -02:30
parent 2897607288
commit 77ef06ac57

View File

@ -10,10 +10,11 @@ import {
import EthQuery from 'eth-query';
import { RestrictedControllerMessenger } from '@metamask/base-controller';
import { v4 as uuid } from 'uuid';
import { Hex, isPlainObject } from '@metamask/utils';
import { Hex, isPlainObject, isStrictHexString } from '@metamask/utils';
import { errorCodes } from 'eth-rpc-errors';
import { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import { PollingBlockTracker } from 'eth-block-tracker';
import { hexToDecimal } from '../../../../shared/modules/conversion.utils';
import {
INFURA_PROVIDER_TYPES,
INFURA_BLOCKED_KEY,
@ -301,16 +302,22 @@ function isErrorWithCode(error: unknown): error is { code: string | number } {
}
/**
* Asserts that the given value is a network ID, i.e., that it is a decimal
* number represented as a string.
* Convert the given value into a valid network ID. The ID is accepted
* as either a number, a decimal string, or a 0x-prefixed hex string.
*
* @param value - The value to check.
* @param value - The network ID to convert, in an unknown format.
* @returns A valid network ID (as a decimal string)
* @throws If the given value cannot be safely parsed.
*/
function assertNetworkId(value: any): asserts value is NetworkId {
assert(
/^\d+$/u.test(value) && !Number.isNaN(Number(value)),
'value is not a number',
);
function convertNetworkId(value: unknown): NetworkId {
if (typeof value === 'number' && !Number.isNaN(value)) {
return `${value}`;
} else if (isStrictHexString(value)) {
return hexToDecimal(value) as NetworkId;
} else if (typeof value === 'string' && /^\d+$/u.test(value)) {
return value as NetworkId;
}
throw new Error(`Cannot parse as a valid network ID: '${value}'`);
}
/**
@ -619,8 +626,7 @@ export class NetworkController extends EventEmitter {
this.#determineEIP1559Compatibility(provider),
]);
const possibleNetworkId = results[0];
assertNetworkId(possibleNetworkId);
networkId = possibleNetworkId;
networkId = convertNetworkId(possibleNetworkId);
supportsEIP1559 = results[1];
networkStatus = NetworkStatus.Available;
} catch (error) {