UNPKG

zkverifyjs

Version:

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

184 lines (183 loc) 9.9 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 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"); 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); 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() { 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); return this.createOptimisticVerifyBuilder(proofOptions); }, 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: * - Groth16: Requires `library` and `curve`. * - Plonky2: Requires `compressed` (boolean) and `hashFunction`. * - Risc0: Requires `version`. * - Ultraplonk / ProofOfSql: No specific options required. * * @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: * - Groth16: Requires `library` and `curve`. * - Plonky2: Requires `compressed` (boolean) and `hashFunction`. * - Risc0: Requires `version`. * - Ultraplonk / ProofOfSql: No specific options required. * * @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) { return new optimisticVerify_2.OptimisticVerificationBuilder(this.executeOptimisticVerify.bind(this), proofOptions); } /** * 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 {VerifyOptions} options - The options for the verification process, including: * - `proofOptions` {AllProofOptions}: Contains the proof type and associated options depending on the type: * - Groth16: Requires `library` and `curve`. * - Plonky2: Requires `compressed` (boolean) and `hashFunction`. * - Risc0: Requires `version`. * - Ultraplonk / ProofOfSql: No specific options required. * - `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. * * @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 {AllProofConfigs} proofOptions - The proof options, including: * - `proofType` {ProofType}: The type of proof to be verified. * - Depending on the `proofType`, the following additional properties may be required: * - Groth16: `library` and `curve` (as part of `Groth16Options`). * - Plonky2: `compressed` (boolean) and `hashFunction` (as part of `Plonky2Options`). * - Risc0: `version` (as part of `Risc0Options`). * - Ultraplonk / ProofOfSql: No specific options required. * * @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<{ success: boolean; message: string }>} * 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(proofOptions, 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, proofOptions, input); } } exports.VerificationManager = VerificationManager;