UNPKG

zkverifyjs

Version:

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

318 lines (317 loc) 18.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VerificationManager = void 0; const verify_1 = require("../../../api/verify"); const optimisticVerify_1 = require("../../../api/optimisticVerify"); const batchVerify_1 = require("../../../api/batchVerify"); const batchOptimisticVerify_1 = require("../../../api/batchOptimisticVerify"); const config_1 = require("../../../config"); const verify_2 = require("../../builders/verify"); const optimisticVerify_2 = require("../../builders/optimisticVerify"); const validator_1 = require("../../validator"); const events_1 = require("events"); const helpers_1 = require("../../../utils/helpers"); const batchVerify_2 = require("../../builders/batchVerify"); const batchOptimisticVerify_2 = require("../../builders/batchOptimisticVerify"); class VerificationManager { constructor(connectionManager) { this.connectionManager = connectionManager; } /** * Creates a builder map for different proof types that can be used for verification. * Each proof type returns a `VerificationBuilder` that allows you to chain methods for setting options * and finally executing the verification process. * * @param {string} [accountAddress] - The address of the account performing the verification. * @returns {ProofMethodMap} A map of proof types to their corresponding builder methods. */ verify(accountAddress) { const builderMethods = {}; for (const proofType in config_1.ProofType) { if (Object.prototype.hasOwnProperty.call(config_1.ProofType, proofType)) { Object.defineProperty(builderMethods, proofType, { value: (proofConfig) => { const proofOptions = { proofType: proofType, config: proofConfig ?? undefined, }; (0, validator_1.validateProofTypeOptions)(proofOptions, this.connectionManager.connectionDetails.runtimeSpec); return this.createVerifyBuilder(proofOptions, accountAddress); }, writable: false, configurable: false, enumerable: true, }); } } return builderMethods; } /** * Creates a builder map for different proof types that can be used for optimistic verification. * Each proof type returns an `OptimisticVerificationBuilder` that allows you to chain methods * and finally execute the optimistic verification process. * * @returns {OptimisticProofMethodMap} A map of proof types to their corresponding builder methods. */ optimisticVerify(accountAddress) { const builderMethods = {}; for (const proofType in config_1.ProofType) { if (Object.prototype.hasOwnProperty.call(config_1.ProofType, proofType)) { Object.defineProperty(builderMethods, proofType, { value: (proofConfig) => { const proofOptions = { proofType: proofType, config: proofConfig ?? undefined, }; (0, validator_1.validateProofTypeOptions)(proofOptions, this.connectionManager.connectionDetails.runtimeSpec); return this.createOptimisticVerifyBuilder(proofOptions, accountAddress); }, writable: false, configurable: false, enumerable: true, }); } } return builderMethods; } /** * Creates a builder map for different proof types that can be used for **batch verification**. * Each proof type returns a `BatchVerificationBuilder` allowing you to set options * and then submit multiple proofs in a single `batchAll` transaction. * * @param {string} [accountAddress] - The account address performing the batch verification. * @returns {BatchProofMethodMap} A map of proof types to their batch verification builder methods. */ batchVerify(accountAddress) { const builderMethods = {}; for (const proofType in config_1.ProofType) { if (Object.prototype.hasOwnProperty.call(config_1.ProofType, proofType)) { Object.defineProperty(builderMethods, proofType, { value: (proofConfig) => { const proofOptions = { proofType: proofType, config: proofConfig ?? undefined, }; (0, validator_1.validateProofTypeOptions)(proofOptions, this.connectionManager.connectionDetails.runtimeSpec); return this.createBatchVerifyBuilder(proofOptions, accountAddress); }, writable: false, configurable: false, enumerable: true, }); } } return builderMethods; } /** * Creates a builder map for different proof types that can be used for **optimistic batch verification**. * Each proof type returns a `BatchOptimisticVerificationBuilder` that builds and dry-runs a `batchAll` transaction * to ensure all proofs would pass without submitting to the chain. * * @returns {BatchOptimisticProofMethodMap} A map of proof types to their optimistic batch verification builders. */ batchOptimisticVerify(accountAddress) { const builderMethods = {}; for (const proofType in config_1.ProofType) { if (Object.prototype.hasOwnProperty.call(config_1.ProofType, proofType)) { Object.defineProperty(builderMethods, proofType, { value: (proofConfig) => { const proofOptions = { proofType: proofType, config: proofConfig ?? undefined, }; (0, validator_1.validateProofTypeOptions)(proofOptions, this.connectionManager.connectionDetails.runtimeSpec); return this.createBatchOptimisticVerifyBuilder(proofOptions, accountAddress); }, writable: false, configurable: false, enumerable: true, }); } } return builderMethods; } /** * Factory method to create a `VerificationBuilder` for the given proof type. * The builder allows for chaining options and executing the verification process. * * @param {AllProofConfigs} proofOptions - The proof options object containing the proof type and its specific options. * - Must include a valid `proofType` and associated options depending on the proof type * * @param {string} [accountAddress] - The account to use for verification. * - If a `string`, it represents the account address. * - If `undefined`, the first available account is used by default. * * @returns {VerificationBuilder} A new instance of `VerificationBuilder` configured with the provided proof options and account. * * @throws {Error} If the provided proof options are invalid or incomplete. * @private */ createVerifyBuilder(proofOptions, accountAddress) { return new verify_2.VerificationBuilder(this.executeVerify.bind(this), proofOptions, accountAddress); } /** * Factory method to create an `OptimisticVerificationBuilder` for the given proof type. * This builder allows for configuring and executing the optimistic verification process. * * @param {AllProofConfigs} proofOptions - The proof options object containing the proof type and its specific options. * - Must include a valid `proofType` and associated options depending on the proof type * * @param {string} [accountAddress] - Optional account address to sign and submit the transaction. * @returns {OptimisticVerificationBuilder} A new instance of `OptimisticVerificationBuilder` configured with the provided proof options. * * @throws {Error} If the provided proof options are invalid or incomplete. * @private */ createOptimisticVerifyBuilder(proofOptions, accountAddress) { return new optimisticVerify_2.OptimisticVerificationBuilder(this.executeOptimisticVerify.bind(this), proofOptions, accountAddress); } /** * Factory method to create a `BatchVerificationBuilder` for the given proof type. * This builder enables chaining configuration methods and executing a batch of on-chain verifications * using a single `batchAll` transaction. * * @param {ProofOptions} proofOptions - Configuration for the proof type and related parameters. * @param {string} [accountAddress] - Optional account address to sign and submit the transaction. * @returns {BatchVerificationBuilder} A builder for executing batch verification. * * @throws {Error} If the provided proof options are invalid. * @private */ createBatchVerifyBuilder(proofOptions, accountAddress) { return new batchVerify_2.BatchVerificationBuilder(this.executeBatchVerify.bind(this), proofOptions, accountAddress); } /** * Factory method to create a `BatchVerificationBuilder` for the given proof type. * This builder enables chaining configuration methods and executing a batch of on-chain verifications * using a single `batchAll` transaction. * * @param {ProofOptions} proofOptions - Configuration for the proof type and related parameters. * @param {string} [accountAddress] - Optional account address to sign and submit the transaction. * @returns {BatchVerificationBuilder} A builder for executing batch verification. * * @throws {Error} If the provided proof options are invalid. * @private */ createBatchOptimisticVerifyBuilder(proofOptions, accountAddress) { return new batchOptimisticVerify_2.BatchOptimisticVerificationBuilder(this.executeBatchOptimisticVerify.bind(this), proofOptions, accountAddress); } /** * Executes the verification process with the provided proof options and proof data or pre-built extrinsic. * This method is intended to be called by the `VerificationBuilder`. * * @param {OptimisticVerifyOptions} options - The options for the verification process, including: * - `proofOptions` {AllProofOptions}: Contains the proof type and associated options depending on the type. * - `accountAddress` {string} [optional]: The account address to use for the verification. * - `nonce` {number} [optional]: The nonce for the transaction, if applicable. * - `registeredVk` {boolean} [optional]: Whether to use a registered verification key. * - `domainId` {number} [optional]: The domain ID for domain-specific operations. * - `atBlock` {number | string} [optional]: The block at which to run the optimistic verification. * * @param {VerifyInput} input - The verification input, which must be one of the following: * - `proofData`: An array of proof parameters (proof, public signals, and verification key). * - `extrinsic`: A pre-built `SubmittableExtrinsic`. * - Ensure only one of these options is provided within the `VerifyInput`. * * @returns {Promise<{events: EventEmitter, transactionResult: Promise<VerifyTransactionInfo>}>} * A promise that resolves with an object containing: * - `events`: An `EventEmitter` instance for real-time verification events. * - `transactionResult`: A promise that resolves to the final transaction information once verification is complete. * * @throws {Error} If the verification fails or the proof options are invalid. * @private */ async executeVerify(options, input) { (0, helpers_1.checkReadOnly)(this.connectionManager.connectionDetails); const events = new events_1.EventEmitter(); const transactionResult = (0, verify_1.verify)(this.connectionManager.connectionDetails, options, events, input); return { events, transactionResult }; } /** * Executes the optimistic verification process using the provided proof options and input. * This method is intended to be called by the `OptimisticVerificationBuilder`. * * @param {OptimisticVerifyOptions} options - The options for the verification process, including: * - `proofOptions` {AllProofOptions}: Contains the proof type and associated options depending on the type. * - `accountAddress` {string} [optional]: The account address to use for the verification. * - `nonce` {number} [optional]: The nonce for the transaction, if applicable. * - `registeredVk` {boolean} [optional]: Whether to use a registered verification key. * - `domainId` {number} [optional]: The domain ID for domain-specific operations. * - `atBlock` {number | string} [optional]: The block at which to run the optimistic verification. * @param {VerifyInput} input - The verification input, which must be one of the following: * - `proofData`: An array of proof parameters (proof, public signals, and verification key). * - `extrinsic`: A pre-built `SubmittableExtrinsic`. * - Ensure only one of these options is provided within the `VerifyInput`. * * @returns {Promise<OptimisticVerifyResult>} * A promise that resolves to an object containing: * - `success`: A boolean indicating whether the optimistic verification was successful. * - `message`: A message providing additional details about the verification result. * * @throws {Error} If the session is in read-only mode. * @throws {Error} If not connected to a custom network. * @private */ async executeOptimisticVerify(options, input) { (0, helpers_1.checkReadOnly)(this.connectionManager.connectionDetails); if (!this.connectionManager.customNetwork) { throw new Error('Optimistic verification is only supported on custom networks.'); } return (0, optimisticVerify_1.optimisticVerify)(this.connectionManager.connectionDetails, options, input); } /** * Executes a full on-chain batch verification of multiple proofs. * Internally constructs a `batchAll` extrinsic and listens for success or error events. * * @param {OptimisticVerifyOptions} options - The options for the verification process, including: * - `proofOptions` {AllProofOptions}: Contains the proof type and associated options depending on the type. * - `accountAddress` {string} [optional]: The account address to use for the verification. * - `nonce` {number} [optional]: The nonce for the transaction, if applicable. * - `registeredVk` {boolean} [optional]: Whether to use a registered verification key. * - `domainId` {number} [optional]: The domain ID for domain-specific operations. * - `atBlock` {number | string} [optional]: The block at which to run the optimistic verification. * @param {VerifyInput[]} input - An array of verification input, which must be one of the following: * - `proofData`: An array of proof parameters (proof, public signals, and verification key). * - `extrinsic`: A pre-built `SubmittableExtrinsic`. * - Ensure only one of these options is provided within the `VerifyInput`. * @returns {Promise<{events: EventEmitter, transactionResult: Promise<BatchVerifyTransactionInfo>}>} * A promise resolving to: * - `events`: An EventEmitter for lifecycle events (broadcast, includedInBlock, finalized, etc). * - `transactionResult`: The final batch verification result after on-chain execution. * * @throws {Error} If verification is called while in read-only mode. */ async executeBatchVerify(options, input) { (0, helpers_1.checkReadOnly)(this.connectionManager.connectionDetails); const events = new events_1.EventEmitter(); const transactionResult = (0, batchVerify_1.batchVerify)(this.connectionManager.connectionDetails, options, events, input); return { events, transactionResult }; } /** * Executes a dry-run of a batch verification to simulate success or failure without submitting. * This method wraps `api.rpc.system.dryRun` around the `batchAll` extrinsic built from the proof list. * * @param {OptimisticVerifyOptions} options - The options for the verification process, including: * - `proofOptions` {AllProofOptions}: Contains the proof type and associated options depending on the type. * - `accountAddress` {string} [optional]: The account address to use for the verification. * - `nonce` {number} [optional]: The nonce for the transaction, if applicable. * - `registeredVk` {boolean} [optional]: Whether to use a registered verification key. * - `domainId` {number} [optional]: The domain ID for domain-specific operations. * - `atBlock` {number | string} [optional]: The block at which to run the optimistic verification. * @param {VerifyInput[]} input - An array of verification input, which must be one of the following: * - `proofData`: An array of proof parameters (proof, public signals, and verification key). * - `extrinsic`: A pre-built `SubmittableExtrinsic`. * - Ensure only one of these options is provided within the `VerifyInput`. * @returns {Promise<OptimisticVerifyResult>} A dry-run result summary. * * @throws {Error} If not connected to a custom network or called in read-only mode. */ async executeBatchOptimisticVerify(options, input) { (0, helpers_1.checkReadOnly)(this.connectionManager.connectionDetails); if (!this.connectionManager.customNetwork) { throw new Error('Optimistic batch verification is only supported on custom networks.'); } return (0, batchOptimisticVerify_1.batchOptimisticVerify)(this.connectionManager.connectionDetails, options, input); } } exports.VerificationManager = VerificationManager;