2022-07-19 22:24:53 +00:00
|
|
|
import * as chainNetworks from "./networks.json" assert {type: "json"};
|
|
|
|
import {Buffer} from "buffer";
|
|
|
|
import {blake2b} from "libskynet";
|
2022-07-05 20:14:39 +00:00
|
|
|
|
|
|
|
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 {
|
2022-07-19 22:24:53 +00:00
|
|
|
console.error(msg);
|
|
|
|
process.exit(1);
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|
2022-07-04 23:17:58 +00:00
|
|
|
|
|
|
|
export function maybeMapChainId(chain: string): string | boolean {
|
2022-07-19 22:24:53 +00:00
|
|
|
if (chain in chainNetworks) {
|
|
|
|
return (chainNetworks as networks)[chain];
|
|
|
|
}
|
2022-07-04 23:17:58 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
if (
|
|
|
|
[parseInt(chain, 16).toString(), parseInt(chain, 10).toString()].includes(
|
|
|
|
chain.toLowerCase()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return chain;
|
|
|
|
}
|
2022-07-04 23:17:58 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
return false;
|
2022-07-04 23:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function reverseMapChainId(chainId: string): string | boolean {
|
2022-07-19 22:24:53 +00:00
|
|
|
let vals = Object.values(chainNetworks);
|
|
|
|
if (!vals.includes(chainId)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-04 23:17:58 +00:00
|
|
|
|
2022-07-19 22:24:53 +00:00
|
|
|
return Object.keys(chainNetworks)[vals.indexOf(chainId)];
|
2022-07-04 23:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isIp(ip: string) {
|
2022-07-19 22:24:53 +00:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hashDataKey(dataKey: string): Uint8Array {
|
|
|
|
return blake2b(encodeUtf8String(dataKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeUtf8String(str: string): Uint8Array {
|
|
|
|
const byteArray = stringToUint8ArrayUtf8(str);
|
|
|
|
const encoded = new Uint8Array(8 + byteArray.length);
|
|
|
|
encoded.set(encodeNumber(byteArray.length));
|
|
|
|
encoded.set(byteArray, 8);
|
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stringToUint8ArrayUtf8(str: string): Uint8Array {
|
|
|
|
return Uint8Array.from(Buffer.from(str, "utf-8"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeNumber(num: number): Uint8Array {
|
|
|
|
const encoded = new Uint8Array(8);
|
|
|
|
for (let index = 0; index < encoded.length; index++) {
|
|
|
|
encoded[index] = num & 0xff;
|
|
|
|
num = num >> 8;
|
|
|
|
}
|
|
|
|
return encoded;
|
2022-07-04 23:17:58 +00:00
|
|
|
}
|