zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
271 lines • 16.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VerificationManager = void 0;
const index_js_1 = require("../../../api/verify/index.js");
const index_js_2 = require("../../../api/optimisticVerify/index.js");
const index_js_3 = require("../../../api/batchVerify/index.js");
const index_js_4 = require("../../../api/batchOptimisticVerify/index.js");
const index_js_5 = require("../../../config/index.js");
const index_js_6 = require("../../builders/verify/index.js");
const index_js_7 = require("../../builders/optimisticVerify/index.js");
const index_js_8 = require("../../validator/index.js");
const events_1 = require("events");
const index_js_9 = require("../../../utils/helpers/index.js");
const index_js_10 = require("../../builders/batchVerify/index.js");
const index_js_11 = require("../../builders/batchOptimisticVerify/index.js");
class VerificationManager {
constructor(connectionManager) {
this.connectionManager = connectionManager;
}
/**
* Builds a frozen, per-proof-type method map. Used by verify/optimisticVerify/
* batchVerify/batchOptimisticVerify — each proof type entry validates options
* against the connected runtime, then hands off to the supplied factory.
*/
buildProofTypeMap(createBuilder, accountAddress) {
const builderMethods = {};
for (const proofType in index_js_5.ProofType) {
if (!Object.prototype.hasOwnProperty.call(index_js_5.ProofType, proofType))
continue;
Object.defineProperty(builderMethods, proofType, {
value: (proofConfig) => {
const validatedOptions = (0, index_js_8.validateProofTypeOptions)({
proofType: proofType,
config: proofConfig ?? undefined,
}, this.connectionManager.connectionDetails.runtimeSpec);
return createBuilder(validatedOptions, accountAddress);
},
writable: false,
configurable: false,
enumerable: true,
});
}
return builderMethods;
}
/**
* 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) {
return this.buildProofTypeMap((opts, addr) => this.createVerifyBuilder(opts, addr), accountAddress);
}
/**
* 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) {
return this.buildProofTypeMap((opts, addr) => this.createOptimisticVerifyBuilder(opts, addr), accountAddress);
}
/**
* 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) {
return this.buildProofTypeMap((opts, addr) => this.createBatchVerifyBuilder(opts, addr), accountAddress);
}
/**
* 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) {
return this.buildProofTypeMap((opts, addr) => this.createBatchOptimisticVerifyBuilder(opts, addr), accountAddress);
}
/**
* 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 index_js_6.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 index_js_7.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 index_js_10.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 index_js_11.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, index_js_9.checkReadOnly)(this.connectionManager.connectionDetails);
const events = new events_1.EventEmitter();
const transactionResult = (0, index_js_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, index_js_9.checkReadOnly)(this.connectionManager.connectionDetails);
if (!this.connectionManager.customNetwork) {
throw new Error('Optimistic verification is only supported on custom networks.');
}
return (0, index_js_2.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, index_js_9.checkReadOnly)(this.connectionManager.connectionDetails);
const events = new events_1.EventEmitter();
const transactionResult = (0, index_js_3.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, index_js_9.checkReadOnly)(this.connectionManager.connectionDetails);
if (!this.connectionManager.customNetwork) {
throw new Error('Optimistic batch verification is only supported on custom networks.');
}
return (0, index_js_4.batchOptimisticVerify)(this.connectionManager.connectionDetails, options, input);
}
}
exports.VerificationManager = VerificationManager;
//# sourceMappingURL=index.js.map