UNPKG

zkverifyjs

Version:

Submit proofs to zkVerify and query proof state with ease using our npm package.

43 lines 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.establishConnection = void 0; const api_1 = require("@polkadot/api"); const index_js_1 = require("../../utils/helpers/index.js"); const index_js_2 = require("../../config/index.js"); /** * Establishes a connection to the zkVerify blockchain by initializing the API and provider. * * @param config - NetworkConfig object containing details such as websocket and rpc urls. * @returns {Promise<EstablishedConnection>} The initialized API and provider. * @throws Will throw an error if the connection fails or if the provided configuration is invalid. */ const establishConnection = async (config) => { const { host, websocket } = config; if (!websocket || websocket.trim() === '') { throw new Error(`WebSocket URL is required for network: ${host}`); } try { const wsOpts = config.wsProvider; const provider = wsOpts !== undefined ? new api_1.WsProvider(websocket, wsOpts.autoConnectMs, undefined, wsOpts.timeout) : new api_1.WsProvider(websocket); const runtimeSpec = await (0, index_js_1.fetchRuntimeVersionFromProvider)(provider); const api = await api_1.ApiPromise.create({ provider, types: (0, index_js_2.getZkvTypes)(runtimeSpec), rpc: index_js_2.zkvRpc, }); await (0, index_js_1.waitForNodeToSync)(api, { timeoutMs: config.syncTimeoutMs }); return { api, provider, runtimeSpec }; } catch (error) { if (error instanceof Error) { throw new Error(`Failed to establish connection to ${host}: ${error.message}`); } else { throw new Error('Failed to establish connection due to an unknown error.'); } } }; exports.establishConnection = establishConnection; //# sourceMappingURL=index.js.map