caip-api
Version:
CAIP Blockchain API and JSON-RPC Config
48 lines • 1.83 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const caip_1 = require("caip");
const blockchain = __importStar(require("../"));
function getNamespaceProperty(chainId, property) {
const { namespace } = caip_1.ChainID.parse(chainId);
const res = blockchain[property][namespace];
if (!res) {
throw new Error(`Missing ${property} for chainId: ${chainId}`);
}
return res;
}
exports.getNamespaceProperty = getNamespaceProperty;
function getChainJsonRpcSchemas(chainId) {
return getNamespaceProperty(chainId, 'schemas');
}
exports.getChainJsonRpcSchemas = getChainJsonRpcSchemas;
function getChainConfig(chainId) {
const namespace = getNamespaceProperty(chainId, 'config');
const { reference } = caip_1.ChainID.parse(chainId);
const chain = namespace[reference];
if (!chain) {
throw new Error(`Missing chain for chainId: ${chainId}`);
}
return chain;
}
exports.getChainConfig = getChainConfig;
function getSupportedChains(targetNamespace) {
const chains = {};
Object.keys(blockchain.config).forEach((namespace) => {
if (typeof targetNamespace !== 'undefined' && namespace !== targetNamespace)
return;
Object.keys(blockchain.config[namespace]).forEach((reference) => {
const chainId = caip_1.ChainID.format({ namespace, reference });
chains[chainId] = getChainConfig(chainId);
});
});
return chains;
}
exports.getSupportedChains = getSupportedChains;
//# sourceMappingURL=utils.js.map