diff --git a/sdk/package.json b/sdk/package.json index fe8626b5..bf1bc589 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@verax-attestation-registry/verax-sdk", - "version": "0.0.12", + "version": "0.0.13", "description": "Verax Attestation Registry SDK to interact with the subgraph and the contracts", "keywords": [ "linea-attestation-registry", diff --git a/sdk/src/dataMapper/AttestationDataMapper.ts b/sdk/src/dataMapper/AttestationDataMapper.ts index 68de8440..92bd49d4 100644 --- a/sdk/src/dataMapper/AttestationDataMapper.ts +++ b/sdk/src/dataMapper/AttestationDataMapper.ts @@ -1,11 +1,11 @@ import BaseDataMapper from "./BaseDataMapper"; import { abiAttestationRegistry } from "../abi/AttestationRegistry"; import { Attestation, AttestationPayload, AttestationWithDecodeObject, Schema } from "../types"; -import { Attestation_filter, Attestation_orderBy } from "../../.graphclient"; +import { Attestation_filter, Attestation_orderBy, OrderDirection } from "../../.graphclient"; import { Constants } from "../utils/constants"; import { handleSimulationError } from "../utils/simulationErrorHandler"; import { Address } from "viem"; -import { encode } from "../utils/abiCoder"; +import { decodeWithRetry, encode } from "../utils/abiCoder"; import { executeTransaction } from "../utils/transactionSender"; export default class AttestationDataMapper extends BaseDataMapper< @@ -31,6 +31,31 @@ export default class AttestationDataMapper extends BaseDataMapper< decodedData }`; + override async findOneById(id: string) { + const attestation = await super.findOneById(id); + if (attestation !== undefined) { + const schema = (await this.veraxSdk.schema.getSchema(attestation.schemaId)) as Schema; + attestation.decodedPayload = decodeWithRetry(schema.schema, attestation.attestationData as `0x${string}`); + } + return attestation; + } + + override async findBy( + first?: number, + skip?: number, + where?: Attestation_filter, + orderBy?: Attestation_orderBy, + orderDirection?: OrderDirection, + ) { + const attestations = await super.findBy(first, skip, where, orderBy, orderDirection); + attestations.forEach(async (attestation) => { + const schema = (await this.veraxSdk.schema.getSchema(attestation.schemaId)) as Schema; + attestation.decodedPayload = decodeWithRetry(schema.schema, attestation.attestationData as `0x${string}`); + }); + + return attestations; + } + async getRelatedAttestations(id: string) { return this.findBy( undefined, diff --git a/sdk/src/types/index.d.ts b/sdk/src/types/index.d.ts index f8b50715..ac1b32f6 100644 --- a/sdk/src/types/index.d.ts +++ b/sdk/src/types/index.d.ts @@ -25,6 +25,7 @@ export type Attestation = OnChainAttestation & { id: string; schemaString: string; decodedData: string[]; + decodedPayload: object; }; export type OnChainAttestation = { diff --git a/sdk/src/utils/abiCoder.ts b/sdk/src/utils/abiCoder.ts index 1c47ed02..d626697d 100644 --- a/sdk/src/utils/abiCoder.ts +++ b/sdk/src/utils/abiCoder.ts @@ -7,3 +7,15 @@ export function encode(schema: string, values: unknown[]): `0x${string}` { export function decode(schema: string, attestationData: `0x${string}`): readonly unknown[] { return decodeAbiParameters(parseAbiParameters(schema), attestationData); } + +export function decodeWithRetry(schema: string, attestationData: `0x${string}`): readonly unknown[] { + try { + return decodeAbiParameters(parseAbiParameters(schema), attestationData); + } catch (e) { + try { + return decodeAbiParameters(parseAbiParameters(`(${schema})`), attestationData); + } catch (e) { + return []; + } + } +} diff --git a/sdk/test/integration/Attestation.integration.test.ts b/sdk/test/integration/Attestation.integration.test.ts index c62c9301..74e81c7f 100644 --- a/sdk/test/integration/Attestation.integration.test.ts +++ b/sdk/test/integration/Attestation.integration.test.ts @@ -28,6 +28,7 @@ describe("AttestationDataMapper", () => { expect(result?.attestationData).toEqual("0x0000000000000000000000000000000000000000000000000000000000000004"); expect(result?.schemaString).toEqual("uint8"); expect(result?.decodedData).toEqual(["0x4"]); + expect(result?.decodedPayload).toEqual([4]); }); });