zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
55 lines • 2.33 kB
JavaScript
import { handleTransaction } from "../../utils/transactions/index.js";
import { TransactionType, ZkVerifyEvents } from "../../enums.js";
import { format } from "../format/index.js";
import { createSubmitProofExtrinsic } from "../extrinsic/index.js";
import { getKeyringAccountIfAvailable } from "../../utils/helpers/index.js";
export const batchVerify = async (connection, options, emitter, input) => {
const {
api
} = connection;
const selectedAccount = getKeyringAccountIfAvailable(connection, options.accountAddress);
const calls = [];
let formatError = null;
for (let i = 0; i < input.length; i++) {
const item = input[i];
try {
if ('proofData' in item && item.proofData) {
const {
proof,
publicSignals,
vk
} = item.proofData;
const formatted = format(options.proofOptions, proof, publicSignals, vk, options.registeredVk ?? false);
const extrinsic = createSubmitProofExtrinsic(api, options.proofOptions.proofType, formatted, item.domainId ?? undefined);
calls.push(extrinsic);
} else if ('extrinsic' in item && item.extrinsic) {
calls.push(item.extrinsic);
} else {
throw new Error('Missing both proofData and extrinsic.');
}
} catch (err) {
formatError = new Error(`Failed to format proof at batch index ${i}: ${err.message}`);
break;
}
}
if (formatError) {
return Promise.reject(formatError);
}
if (calls.length === 0) {
const err = new Error('No valid proofs provided for batch verification.');
emitter.emit(ZkVerifyEvents.ErrorEvent, err);
emitter.removeAllListeners();
throw err;
}
const batchTransaction = api.tx.utility.batchAll(calls);
try {
const result = selectedAccount ? await handleTransaction(api, batchTransaction, selectedAccount, undefined, emitter, options, TransactionType.BatchVerify, input.length) : 'injector' in connection ? await handleTransaction(api, batchTransaction, connection.accountAddress, connection.injector.signer, emitter, options, TransactionType.BatchVerify, input.length) : (() => {
throw new Error('Unsupported connection type.');
})();
return result;
} catch (error) {
emitter.emit(ZkVerifyEvents.ErrorEvent, error);
emitter.removeAllListeners();
throw error;
}
};