@toruslabs/fetch-node-details
Version:
Fetches the node details for torus nodes
113 lines (110 loc) • 4.17 kB
JavaScript
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { FND_SERVER, TORUS_SAPPHIRE_NETWORK, TORUS_LEGACY_NETWORK, KEY_TYPE, SIG_TYPE, MULTI_CLUSTER_NETWORKS, METADATA_MAP } from '@toruslabs/constants';
import { fetchLocalConfig } from '@toruslabs/fnd-base';
import { get } from '@toruslabs/http-helpers';
import logger from 'loglevel';
const log = logger.getLogger("fnd");
class NodeDetailManager {
constructor({
network = TORUS_SAPPHIRE_NETWORK.SAPPHIRE_MAINNET,
keyType = KEY_TYPE.SECP256K1,
sigType = SIG_TYPE.ECDSA_SECP256K1,
fndServerEndpoint,
enableLogging = false
} = {}) {
_defineProperty(this, "fndServerEndpoint", `${FND_SERVER}/node-details`);
_defineProperty(this, "_currentEpoch", "1");
_defineProperty(this, "_keyType", void 0);
_defineProperty(this, "_sigType", void 0);
_defineProperty(this, "_torusNodeEndpoints", []);
_defineProperty(this, "_torusNodeRSSEndpoints", []);
_defineProperty(this, "_torusNodeSSSEndpoints", []);
_defineProperty(this, "_torusNodeTSSEndpoints", []);
_defineProperty(this, "_torusNodePub", []);
_defineProperty(this, "_torusIndexes", []);
_defineProperty(this, "updated", void 0);
_defineProperty(this, "network", void 0);
if (network && !Object.values(_objectSpread(_objectSpread({}, TORUS_LEGACY_NETWORK), TORUS_SAPPHIRE_NETWORK)).includes(network)) {
throw new Error("Invalid network");
}
this.network = network;
this._keyType = keyType;
this._sigType = sigType;
this.updated = false;
if (fndServerEndpoint) {
this.fndServerEndpoint = fndServerEndpoint;
}
if (enableLogging) {
log.enableAll();
} else {
log.disableAll();
}
}
get _nodeDetails() {
return {
currentEpoch: this._currentEpoch,
torusNodeEndpoints: this._torusNodeEndpoints,
torusNodeSSSEndpoints: this._torusNodeSSSEndpoints,
torusNodeRSSEndpoints: this._torusNodeRSSEndpoints,
torusNodeTSSEndpoints: this._torusNodeTSSEndpoints,
torusNodePub: this._torusNodePub,
torusIndexes: this._torusIndexes,
updated: this.updated
};
}
async getNodeDetails({
verifier,
verifierId
}) {
try {
if (this.updated && !MULTI_CLUSTER_NETWORKS.includes(this.network)) return this._nodeDetails;
try {
const {
nodeDetails
} = await get(`${this.fndServerEndpoint}?network=${this.network}&verifier=${verifier}&verifierId=${verifierId}&keyType=${this._keyType}&sigType=${this._sigType}`);
this.setNodeDetails(nodeDetails);
return this._nodeDetails;
} catch (error) {
log.error("Failed to fetch node details from server, using local.", error);
}
const nodeDetails = fetchLocalConfig(this.network, this._keyType, this._sigType);
if (!nodeDetails) throw new Error("Failed to fetch node details");
this.setNodeDetails(nodeDetails);
return this._nodeDetails;
} catch (error) {
log.error("Failed to fetch node details", error);
throw error;
}
}
async getMetadataUrl() {
if (Object.values(TORUS_LEGACY_NETWORK).includes(this.network)) {
return METADATA_MAP[this.network];
}
const nodeDetails = await this.getNodeDetails({
verifier: "test-verifier",
verifierId: "test-verifier-id"
});
return nodeDetails.torusNodeEndpoints[0].replace("/sss/jrpc", "/metadata");
}
setNodeDetails(nodeDetails) {
const {
currentEpoch,
torusNodeEndpoints,
torusNodeSSSEndpoints,
torusNodeRSSEndpoints,
torusNodeTSSEndpoints,
torusNodePub,
torusIndexes
} = nodeDetails;
this._torusNodeEndpoints = torusNodeEndpoints;
this._torusNodeSSSEndpoints = torusNodeSSSEndpoints || [];
this._torusNodeRSSEndpoints = torusNodeRSSEndpoints || [];
this._torusNodeTSSEndpoints = torusNodeTSSEndpoints || [];
this._torusNodePub = torusNodePub;
this._torusIndexes = torusIndexes;
this._currentEpoch = currentEpoch;
this.updated = true;
}
}
export { NodeDetailManager as default };