zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
39 lines (38 loc) • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.establishConnection = void 0;
const api_1 = require("@polkadot/api");
const helpers_1 = require("../../utils/helpers");
const config_1 = require("../../config");
/**
* 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 provider = new api_1.WsProvider(websocket);
const api = await api_1.ApiPromise.create({
provider,
types: config_1.zkvTypes,
rpc: config_1.zkvRpc,
});
await (0, helpers_1.waitForNodeToSync)(api);
return { api, provider };
}
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;