UNPKG

zkverifyjs

Version:

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

91 lines (90 loc) 3.44 kB
import { ConnectionManager } from './managers/connection'; import { VerificationManager } from './managers/verification'; import { VerificationKeyRegistrationManager } from './managers/register'; import { EventManager } from './managers/events'; import { ExtrinsicManager } from './managers/extrinsic'; import { DomainManager } from './managers/domain'; import { SupportedNetwork, SupportedNetworkConfig } from '../config'; import { NetworkBuilder } from './builders/network'; import { FormatManager } from './managers/format'; import { RpcManager } from './managers/rpc'; import { bindMethods } from '../utils/helpers'; export class zkVerifySession { constructor(connectionManager) { this.connectionManager = connectionManager; const managers = [ new VerificationManager(connectionManager), new VerificationKeyRegistrationManager(connectionManager), new EventManager(connectionManager), new ExtrinsicManager(connectionManager), new DomainManager(connectionManager), new FormatManager(), new RpcManager(connectionManager), connectionManager, ]; managers.forEach((manager) => bindMethods(this, manager)); } /** * Starts a session for the specified network. * @returns {SupportedNetworkMap} A map of supported networks. */ static start() { const map = {}; for (const [key, config] of Object.entries(SupportedNetworkConfig)) { const network = key; if (network === SupportedNetwork.Custom) { map[network] = (partialConfig) => new NetworkBuilder(zkVerifySession._startSession.bind(zkVerifySession), { ...partialConfig, host: SupportedNetwork.Custom, }); } else { map[network] = () => new NetworkBuilder(zkVerifySession._startSession.bind(zkVerifySession), config); } } return map; } /** * Getter for the Polkadot.js API instance. * @returns {ApiPromise} The API instance. */ get api() { return this.connectionManager.api; } /** * Getter for the WebSocket provider. * @returns {WsProvider} The WebSocket provider. */ get provider() { return this.connectionManager.provider; } /** * Getter for connection details. * @returns {AccountConnection | WalletConnection | EstablishedConnection} The connection details. */ get connection() { return this.connectionManager; } /** * Checks if the session is in read-only mode. * @returns {boolean} True if read-only, otherwise false. */ get readOnly() { return this.connectionManager.readOnly; } /** * Initializes a new zkVerifySession instance. * @param {zkVerifySessionOptions} options - The session configuration options. * @returns {Promise<zkVerifySession>} A promise resolving to the zkVerifySession instance. */ static async _startSession(options) { try { const connectionManager = await ConnectionManager.createSession(options); return new zkVerifySession(connectionManager); } catch (error) { console.debug(`❌ Failed to start session for network: ${options.networkConfig.host}`, error); throw error; } } }