UNPKG

zkverifyjs

Version:

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

73 lines (72 loc) 3.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createExtrinsicFromHex = exports.createExtrinsicHex = exports.createSubmitProofExtrinsic = void 0; const util_1 = require("@polkadot/util"); const helpers_1 = require("../../utils/helpers"); /** * Creates a SubmittableExtrinsic using formatted proof details to enable submitting a proof. * * @param {ApiPromise} api - The Polkadot API instance. * @param {ProofType} proofType - The type of supported proof, used to select the correct pallet. * @param {FormattedProofData} params - Formatted Proof Parameters required by the extrinsic. * @param {number | null | undefined} domainId - The domain ID for the extrinsic (32-bit unsigned integer). * @returns {SubmittableExtrinsic<'promise'>} The generated SubmittableExtrinsic for submission. * @throws {Error} - Throws an error if the extrinsic creation fails. */ const createSubmitProofExtrinsic = (api, proofType, params, domainId = null) => { const pallet = (0, helpers_1.getProofPallet)(proofType); if (!pallet) { throw new Error(`Unsupported proof type: ${proofType}`); } try { return api.tx[pallet].submitProof(params.formattedVk, params.formattedProof, params.formattedPubs, domainId); } catch (error) { throw new Error(formatError(error, proofType, params)); } }; exports.createSubmitProofExtrinsic = createSubmitProofExtrinsic; /** * Generates the hex representation of a SubmittableExtrinsic using formatted proof details. * * @param {ApiPromise} api - The Polkadot API instance. * @param {ProofType} proofType - The type of supported proof, used to select the correct pallet. * @param {FormattedProofData} params - Formatted Proof Parameters required by the extrinsic. * @param {number | null | undefined} domainId - The domain ID for the extrinsic (32-bit unsigned integer). * @returns {string} Hex-encoded string of the SubmittableExtrinsic. * @throws {Error} - Throws an error if the extrinsic creation fails. */ const createExtrinsicHex = (api, proofType, params, domainId) => { const extrinsic = (0, exports.createSubmitProofExtrinsic)(api, proofType, params, domainId); return extrinsic.toHex(); }; exports.createExtrinsicHex = createExtrinsicHex; /** * Recreates an extrinsic from its hex-encoded representation. * * @param {ApiPromise} api - The Polkadot API instance. * @param {string} extrinsicHex - Hex-encoded string of the SubmittableExtrinsic. * @returns {SubmittableExtrinsic<'promise'>} The reconstructed SubmittableExtrinsic. * @throws {Error} - Throws an error if the reconstruction from hex fails. */ const createExtrinsicFromHex = (api, extrinsicHex) => { try { return api.createType('Extrinsic', (0, util_1.hexToU8a)(extrinsicHex)); } catch (error) { throw new Error(`Failed to reconstruct extrinsic from hex: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; exports.createExtrinsicFromHex = createExtrinsicFromHex; /** * Formats a detailed error message for extrinsic creation errors. * * @param {unknown} error - The original error object encountered. * @param {ProofType} proofType - The type of supported proof, used to select the correct pallet. * @param {unknown[]} params - Parameters used when creating the extrinsic. * @returns {string} A formatted error message string with details. */ const formatError = (error, proofType, params) => { const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred'; return `Error creating submittable extrinsic: ${proofType} Params: ${JSON.stringify(params, null, 2)} ${errorMessage}`; };