relay/src/util.ts

38 lines
834 B
TypeScript
Raw Normal View History

2022-07-04 23:17:58 +00:00
import { chainNetworks } from "./rpc/index.js";
2022-06-27 17:53:00 +00:00
export function errorExit(msg: string): void {
console.error(msg);
2022-06-27 17:53:00 +00:00
process.exit(1);
}
2022-07-04 23:17:58 +00:00
export function maybeMapChainId(chain: string): string | boolean {
if (chain in chainNetworks) {
return chainNetworks[chain];
}
if (
[parseInt(chain, 16).toString(), parseInt(chain, 10).toString()].includes(
chain.toLowerCase()
)
) {
return chain;
}
return false;
}
export function reverseMapChainId(chainId: string): string | boolean {
let vals = Object.values(chainNetworks);
if (!vals.includes(chainId)) {
return false;
}
return Object.keys(chainNetworks)[vals.indexOf(chainId)];
}
export function isIp(ip: string) {
return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
ip
);
}