@hyperlane-xyz/sdk
Version:
The official SDK for the Hyperlane Network
33 lines • 1.25 kB
JavaScript
import { isNullish } from '@hyperlane-xyz/utils';
// Decimal-only canonical chain IDs. Rejects hex, prefixed values, and mixed IDs
// like cosmoshub-4 so those never alias to numeric domain lookups.
const NUMERIC_CHAIN_ID_REGEX = /^\d+$/;
export function tryNormalizeNumericChainId(chainId) {
if (typeof chainId === 'number') {
return Number.isSafeInteger(chainId) ? chainId : null;
}
if (!NUMERIC_CHAIN_ID_REGEX.test(chainId))
return null;
const numericChainId = Number(chainId);
if (!Number.isSafeInteger(numericChainId))
return null;
if (String(numericChainId) !== chainId)
return null;
return numericChainId;
}
export function areChainIdsEqual(left, right) {
if (isNullish(left) || isNullish(right))
return false;
if (left === right)
return true;
const leftNumeric = tryNormalizeNumericChainId(left);
const rightNumeric = tryNormalizeNumericChainId(right);
return leftNumeric !== null && leftNumeric === rightNumeric;
}
export function getEffectiveDomainId(metadata) {
if (!isNullish(metadata.domainId)) {
return metadata.domainId;
}
return tryNormalizeNumericChainId(metadata.chainId);
}
//# sourceMappingURL=chainIdUtils.js.map