Skip to content

Commit

Permalink
feat: As a developer, I want the SDK to decode all attestations on th…
Browse files Browse the repository at this point in the history
…e fly (#397)

Co-authored-by: Satyajeet Kolhapure <[email protected]>
Co-authored-by: Alain Nicolas <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2023
1 parent ea0d9f6 commit a0d64e5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
29 changes: 27 additions & 2 deletions sdk/src/dataMapper/AttestationDataMapper.ts
Original file line number Diff line number Diff line change
@@ -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<
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions sdk/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type Attestation = OnChainAttestation & {
id: string;
schemaString: string;
decodedData: string[];
decodedPayload: object;
};

export type OnChainAttestation = {
Expand Down
12 changes: 12 additions & 0 deletions sdk/src/utils/abiCoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
}
}
1 change: 1 addition & 0 deletions sdk/test/integration/Attestation.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});

Expand Down

0 comments on commit a0d64e5

Please sign in to comment.