@snowballmoney/chain-agnostic-utils
Version:
Chain agnostic utilities for cross-chain applications
146 lines • 4.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CAIP2Manager = void 0;
const namespaces_1 = require("./constants/namespaces");
const metadata_1 = require("./constants/metadata");
const networks_1 = require("./constants/networks");
class CAIP2Manager {
constructor() {
this.initialized = false;
this.networks = new Map();
}
static getInstance() {
if (!CAIP2Manager.instance) {
CAIP2Manager.instance = new CAIP2Manager();
}
return CAIP2Manager.instance;
}
/**
* Initialize the manager with options
* This should be called before using the manager
*/
init(options = {}) {
if (this.initialized) {
throw new Error('CAIP2Manager is already initialized');
}
// Load default networks if not explicitly disabled
if (options.defaultNetworks !== false) {
Object.entries(metadata_1.NETWORK_METADATA).forEach(([key, value]) => {
this.networks.set(key, value);
});
}
// Add custom networks
if (options.networks) {
Object.entries(options.networks).forEach(([key, value]) => {
if (this.isValid(key)) {
this.networks.set(key, value);
}
else {
console.warn(`Invalid CAIP-2 identifier skipped: ${key}`);
}
});
}
this.initialized = true;
}
/**
* Get built-in networks object
*/
static get NETWORKS() {
return networks_1.NETWORKS;
}
/**
* Get built-in namespaces
*/
static get NAMESPACES() {
return namespaces_1.NAMESPACES;
}
/**
* Parse CAIP2 identifier into namespace and reference
*/
parse(chainId) {
const [namespace, reference] = chainId.split(':');
if (!namespace || !reference) {
throw new Error('Invalid CAIP-2 identifier');
}
return { namespace, reference };
}
/**
* Format namespace and reference into CAIP2 identifier
*/
format(namespace, reference) {
return `${namespace}:${reference}`;
}
/**
* Validate CAIP2 identifier
*/
isValid(chainId) {
try {
const { namespace } = this.parse(chainId);
return Object.values(namespaces_1.NAMESPACES).includes(namespace);
}
catch (_a) {
return false;
}
}
/**
* Get network metadata
*/
getNetwork(caip2Id) {
this.checkInitialized();
const metadata = this.networks.get(caip2Id);
if (!metadata) {
throw new Error(`No metadata found for network: ${caip2Id}`);
}
return metadata;
}
/**
* Add or update network
*/
addNetwork(caip2Id, metadata, merge = true) {
this.checkInitialized();
if (!this.isValid(caip2Id)) {
throw new Error(`Invalid CAIP-2 identifier: ${caip2Id}`);
}
let metadataToSet = metadata;
if (merge && this.networks.has(caip2Id)) {
metadataToSet = Object.assign(Object.assign({}, this.networks.get(caip2Id)), metadata);
}
this.networks.set(caip2Id, metadataToSet);
}
/**
* Remove custom network
*/
removeNetwork(caip2Id) {
this.checkInitialized();
return this.networks.delete(caip2Id);
}
/**
* Get all registered networks
*/
getAllNetworks() {
this.checkInitialized();
return new Map(this.networks);
}
/**
* Check if network exists
*/
hasNetwork(caip2Id) {
this.checkInitialized();
return this.networks.has(caip2Id);
}
/**
* Reset to initial state
*/
reset() {
this.networks.clear();
this.initialized = false;
}
checkInitialized() {
if (!this.initialized) {
throw new Error('CAIP2Manager is not initialized. Call init() first.');
}
}
}
exports.CAIP2Manager = CAIP2Manager;
CAIP2Manager.instance = null;
//# sourceMappingURL=CAIP2Manager.js.map