zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
63 lines (62 loc) • 3.08 kB
JavaScript
import { RegisterKeyBuilder } from '../../builders/register';
import { ProofType } from '../../../config';
import { registerVk } from '../../../api/register';
import { checkReadOnly } from '../../../utils/helpers';
import { validateProofTypeOptions } from '../../validator';
export class VerificationKeyRegistrationManager {
constructor(connectionManager) {
this.connectionManager = connectionManager;
}
/**
* Creates a builder map for different proof types that can be used for registering verification keys.
* Each proof type returns a `RegisterKeyBuilder` that allows you to chain methods for setting options
* and finally executing the registration process.
*
* @returns {RegisterKeyMethodMap} A map of proof types to their corresponding builder methods.
*/
registerVerificationKey(accountAddress) {
const builderMethods = {};
for (const proofType in ProofType) {
if (Object.prototype.hasOwnProperty.call(ProofType, proofType)) {
Object.defineProperty(builderMethods, proofType, {
value: (proofConfig) => {
const proofOptions = {
proofType: proofType,
config: proofConfig || {},
};
validateProofTypeOptions(proofOptions);
return this.createRegisterKeyBuilder(proofOptions, accountAddress);
},
writable: false,
configurable: false,
enumerable: true,
});
}
}
return builderMethods;
}
/**
* Factory method to create a `RegisterKeyBuilder` for the given proof type.
* The builder allows for chaining options and finally executing the key registration process.
*
* @returns {RegisterKeyBuilder} A new instance of `RegisterKeyBuilder`.
* @private
*/
createRegisterKeyBuilder(proofOptions, accountAddress) {
return new RegisterKeyBuilder(this.executeRegisterVerificationKey.bind(this), proofOptions, accountAddress);
}
/**
* Executes the verification key registration process with the provided options and verification key.
* This method is intended to be called by the `RegisterKeyBuilder`.
*
* @param {VerifyOptions} options - The options for the key registration process, including proof type and other optional settings.
* @param {unknown} verificationKey - The verification key to be registered.
* @returns {Promise<{events: EventEmitter, transactionResult: Promise<VKRegistrationTransactionInfo>}>}
* A promise that resolves with an object containing an `EventEmitter` for real-time events and the final transaction result.
* @private
*/
async executeRegisterVerificationKey(options, verificationKey) {
checkReadOnly(this.connectionManager.connectionDetails);
return registerVk(this.connectionManager.connectionDetails, options, verificationKey);
}
}