relay/src/util.ts

40 lines
894 B
TypeScript
Raw Normal View History

2022-07-05 20:14:39 +00:00
import * as chainNetworks from "./networks.json";
type networks = { [net: string]: string };
2022-07-04 23:17:58 +00:00
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) {
2022-07-05 20:14:39 +00:00
return (chainNetworks as networks)[chain];
2022-07-04 23:17:58 +00:00
}
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
);
}