zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
79 lines • 3.2 kB
JavaScript
import { ConnectionManager } from "./managers/connection/index.js";
import { VerificationManager } from "./managers/verification/index.js";
import { VerificationKeyRegistrationManager } from "./managers/register/index.js";
import { EventManager } from "./managers/events/index.js";
import { ExtrinsicManager } from "./managers/extrinsic/index.js";
import { DomainManager } from "./managers/domain/index.js";
import { SupportedNetwork, SupportedNetworkConfig } from "../config/index.js";
import { NetworkBuilder } from "./builders/network/index.js";
import { FormatManager } from "./managers/format/index.js";
import { RpcManager } from "./managers/rpc/index.js";
import { bindMethods } from "../utils/helpers/index.js";
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.connectionDetails;
}
/**
* 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;
}
}
}