UNPKG

@verax-attestation-registry/verax-sdk

Version:

Verax Attestation Registry SDK to interact with the subgraph and the contracts

64 lines 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeWithRetry = exports.encode = void 0; const viem_1 = require("viem"); const ENCODED_PARENTHESIS = "0x0000000000000000000000000000000000000000000000000000000000000020"; function encode(schema, values) { return (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)(schema), values); } exports.encode = encode; function decodeWithRetry(schema, attestationData) { const wrappedSchema = schema.startsWith("(") ? schema : `(${schema})`; let result = decodeWrapped(wrappedSchema, attestationData); if (!result.length && !attestationData.startsWith(ENCODED_PARENTHESIS)) { result = decodeWrapped(wrappedSchema, `${ENCODED_PARENTHESIS}${attestationData.substring(2)}`); } return result; } exports.decodeWithRetry = decodeWithRetry; function decodeWrapped(schema, attestationData) { try { const parsedParams = tryParse(schema); return (0, viem_1.decodeAbiParameters)(parsedParams, attestationData); } catch (e) { return []; } } function tryParse(schema) { const preparedSchema = schema.replaceAll("tuple(", "("); try { return (0, viem_1.parseAbiParameters)(preparedSchema); } catch (e) { if (e.shortMessage === "Invalid ABI parameter.") { try { return (0, viem_1.parseAbiParameters)(reverseSchema(preparedSchema)); } catch (e) { return []; } } return []; } } function reverseSchema(schema) { return schema.startsWith("(") ? `(${reverseWordsTwoByTwo(schema.substring(1, schema.length - 1))})` : reverseWordsTwoByTwo(schema); } function reverseWordsTwoByTwo(schema) { return schema .split(" ") .reduce((acc, word, i) => { if (i % 2 === 0) { acc.push(word); } else { acc.unshift(word); } return acc; }, []) .join(" "); } //# sourceMappingURL=abiCoder.js.map