UNPKG

@toruslabs/fetch-node-details

Version:
118 lines (115 loc) 4.31 kB
import _objectSpread from '@babel/runtime/helpers/objectSpread2'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { TORUS_SAPPHIRE_NETWORK, TORUS_LEGACY_NETWORK, BUILD_ENV, KEY_TYPE, SIG_TYPE, FND_SERVER_MAP, LEGACY_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, buildEnv = BUILD_ENV.PRODUCTION } = {}) { _defineProperty(this, "fndServerEndpoint", void 0); _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); _defineProperty(this, "buildEnv", 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.buildEnv = buildEnv; this._keyType = keyType; this._sigType = sigType; this.updated = false; if (fndServerEndpoint) { this.fndServerEndpoint = fndServerEndpoint; } else { this.fndServerEndpoint = `${FND_SERVER_MAP[buildEnv]}/node-details`; } 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) 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 LEGACY_METADATA_MAP[this.buildEnv]; } 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 };