UNPKG

zkverifyjs

Version:

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

76 lines (75 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSubmittableExtrinsicFromHex = exports.createExtrinsicHex = exports.createSubmitProofExtrinsic = void 0; 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; /** * Reconstructs a signable SubmittableExtrinsic from a hex-encoded string. * * Uses `api.tx(hex)` to restore full submittable behavior (e.g., `.signAsync`). * Note: The original signature and metadata are not preserved. * * @param api - Polkadot API instance. * @param extrinsicHex - Hex-encoded extrinsic string. * @returns A SubmittableExtrinsic<'promise'>. * @throws Error if decoding or reconstruction fails. */ const createSubmittableExtrinsicFromHex = (api, extrinsicHex) => { try { return api.tx(extrinsicHex); } catch (error) { const details = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Invalid extrinsic: Could not decode or reconstruct from the provided hex string. (${details})`); } }; exports.createSubmittableExtrinsicFromHex = createSubmittableExtrinsicFromHex; /** * 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}`; };