2022-09-14 16:55:31 +02:00
|
|
|
import { CHAIN_IDS, MAX_SAFE_CHAIN_ID } from '../constants/network';
|
2021-01-21 00:37:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the given number primitive chain ID is safe.
|
|
|
|
* Because some cryptographic libraries we use expect the chain ID to be a
|
|
|
|
* number primitive, it must not exceed a certain size.
|
|
|
|
*
|
2023-03-31 18:52:56 +02:00
|
|
|
* @param chainId - The chain ID to check for safety.
|
|
|
|
* @returns Whether the given chain ID is safe.
|
2021-01-21 00:37:18 +01:00
|
|
|
*/
|
2023-03-31 18:52:56 +02:00
|
|
|
export function isSafeChainId(chainId: unknown): boolean {
|
|
|
|
return isSafeInteger(chainId) && chainId > 0 && chainId <= MAX_SAFE_CHAIN_ID;
|
2021-01-21 00:37:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the given value is a 0x-prefixed, non-zero, non-zero-padded,
|
|
|
|
* hexadecimal string.
|
|
|
|
*
|
2023-03-31 18:52:56 +02:00
|
|
|
* @param value - The value to check.
|
|
|
|
* @returns True if the value is a correctly formatted hex string,
|
2021-01-21 00:37:18 +01:00
|
|
|
* false otherwise.
|
|
|
|
*/
|
2023-03-31 18:52:56 +02:00
|
|
|
export function isPrefixedFormattedHexString(value: unknown) {
|
2021-01-21 00:37:18 +01:00
|
|
|
if (typeof value !== 'string') {
|
2021-02-04 19:15:23 +01:00
|
|
|
return false;
|
2021-01-21 00:37:18 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return /^0x[1-9a-f]+[0-9a-f]*$/iu.test(value);
|
2021-01-21 00:37:18 +01:00
|
|
|
}
|
2022-04-13 18:23:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if token detection is enabled for certain networks
|
|
|
|
*
|
|
|
|
* @param chainId - ChainID of network
|
|
|
|
* @returns Whether the current network supports token detection
|
|
|
|
*/
|
2023-03-31 18:52:56 +02:00
|
|
|
export function isTokenDetectionEnabledForNetwork(chainId: string | undefined) {
|
2022-04-13 18:23:41 +02:00
|
|
|
switch (chainId) {
|
2022-09-14 16:55:31 +02:00
|
|
|
case CHAIN_IDS.MAINNET:
|
|
|
|
case CHAIN_IDS.BSC:
|
|
|
|
case CHAIN_IDS.POLYGON:
|
|
|
|
case CHAIN_IDS.AVALANCHE:
|
2022-04-13 18:23:41 +02:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 18:52:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Like {@link Number.isSafeInteger}, but types the input as a `number` if it is
|
|
|
|
* indeed a safe integer.
|
|
|
|
*
|
|
|
|
* @param value - The value to check.
|
|
|
|
* @returns True if the value is a safe integer, false otherwise.
|
|
|
|
*/
|
|
|
|
function isSafeInteger(value: unknown): value is number {
|
|
|
|
return Number.isSafeInteger(value);
|
|
|
|
}
|