zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
32 lines (31 loc) • 1.58 kB
JavaScript
import { EventEmitter } from 'events';
import { handleTransaction } from '../../utils/transactions';
import { getProofPallet, getProofProcessor, getSelectedAccount, } from '../../utils/helpers';
import { TransactionType, ZkVerifyEvents } from '../../enums';
export async function registerVk(connection, options, verificationKey) {
const { proofOptions, accountAddress } = options;
const emitter = new EventEmitter();
const processor = await getProofProcessor(proofOptions.proofType);
if (!processor) {
throw new Error(`Unsupported proof type: ${proofOptions.proofType}`);
}
if (verificationKey == null || verificationKey === '') {
throw new Error('verificationKey cannot be null, undefined, or an empty string');
}
const formattedVk = processor.formatVk(verificationKey, proofOptions);
const pallet = getProofPallet(proofOptions.proofType);
if (!pallet) {
throw new Error(`Unsupported proof type: ${proofOptions.proofType}`);
}
const selectedAccount = getSelectedAccount(connection, accountAddress);
const registerExtrinsic = connection.api.tx[pallet].registerVk(formattedVk);
const transactionResult = new Promise((resolve, reject) => {
handleTransaction(connection.api, registerExtrinsic, selectedAccount, undefined, emitter, options, TransactionType.VKRegistration)
.then(resolve)
.catch((error) => {
emitter.emit(ZkVerifyEvents.ErrorEvent, error);
reject(error);
});
});
return { events: emitter, transactionResult };
}