@toruslabs/fetch-node-details
Version:
Fetches the node details for torus nodes
115 lines (111 loc) • 4.19 kB
JavaScript
'use strict';
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var constants = require('@toruslabs/constants');
var fndBase = require('@toruslabs/fnd-base');
var httpHelpers = require('@toruslabs/http-helpers');
var logger = require('loglevel');
const log = logger.getLogger("fnd");
class NodeDetailManager {
constructor({
network = constants.TORUS_SAPPHIRE_NETWORK.SAPPHIRE_MAINNET,
keyType = constants.KEY_TYPE.SECP256K1,
sigType = constants.SIG_TYPE.ECDSA_SECP256K1,
fndServerEndpoint,
enableLogging = false
} = {}) {
_defineProperty(this, "fndServerEndpoint", `${constants.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({}, constants.TORUS_LEGACY_NETWORK), constants.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 && !constants.MULTI_CLUSTER_NETWORKS.includes(this.network)) return this._nodeDetails;
try {
const {
nodeDetails
} = await httpHelpers.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 = fndBase.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(constants.TORUS_LEGACY_NETWORK).includes(this.network)) {
return constants.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;
}
}
module.exports = NodeDetailManager;