@mr-zwets/bchn-api-wrapper
Version:
a Typescript wrapper for interacting with the Bitcoin Cash Node (BCHN) API
50 lines (49 loc) • 1.73 kB
JavaScript
export function getRandomId() {
return Math.floor(Math.random() * 100000);
}
// A utility function to validate and construct the URL from the RpcClientConfig object
export function validateAndConstructUrl(config) {
let url;
if (isUrlConfig(config)) {
url = validateUrl(config.url);
}
else if (isHostConfig(config)) {
const { protocol, host, port } = config;
if (protocol !== 'http' && protocol !== 'https') {
throw new Error("Protocol should be 'http' or 'https'");
}
url = validateUrl(`${protocol}://${host}:${port}`);
}
else {
throw new Error('Invalid configuration: Either provide the url or protocol/host/port');
}
return url;
}
// A utility function to validate a URL
export function validateUrl(url) {
if (!url)
throw new Error('URL is required');
try {
new URL(url);
}
catch (err) {
throw new Error('Invalid URL format');
}
return url;
}
// Type guard to check if the config is RpcClientUrlConfig
function isUrlConfig(config) {
return 'url' in config;
}
// Type guard to check if the config is RpcClientHostConfig
function isHostConfig(config) {
return 'protocol' in config && 'hostname' in config && 'port' in config;
}
export var BchnNetworkPort;
(function (BchnNetworkPort) {
BchnNetworkPort[BchnNetworkPort["Mainnet"] = 8332] = "Mainnet";
BchnNetworkPort[BchnNetworkPort["Testnet"] = 18332] = "Testnet";
BchnNetworkPort[BchnNetworkPort["Testnet4"] = 28332] = "Testnet4";
BchnNetworkPort[BchnNetworkPort["Scalenet"] = 38332] = "Scalenet";
BchnNetworkPort[BchnNetworkPort["Regtest"] = 18443] = "Regtest";
})(BchnNetworkPort || (BchnNetworkPort = {}));