2021-02-04 19:15:23 +01:00
|
|
|
import { 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.
|
|
|
|
*
|
|
|
|
* @param {number} chainId - The chain ID to check for safety.
|
|
|
|
* @returns {boolean} Whether the given chain ID is safe.
|
|
|
|
*/
|
|
|
|
export function isSafeChainId(chainId) {
|
|
|
|
return (
|
|
|
|
Number.isSafeInteger(chainId) && chainId > 0 && chainId <= MAX_SAFE_CHAIN_ID
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-01-21 00:37:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the given value is a 0x-prefixed, non-zero, non-zero-padded,
|
|
|
|
* hexadecimal string.
|
|
|
|
*
|
|
|
|
* @param {any} value - The value to check.
|
|
|
|
* @returns {boolean} True if the value is a correctly formatted hex string,
|
|
|
|
* false otherwise.
|
|
|
|
*/
|
|
|
|
export function isPrefixedFormattedHexString(value) {
|
|
|
|
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
|
|
|
}
|