Skip to content

Parsing Attestation Data

Attestation data is encoded into a single hex string, which you can decode if you know the schema used to encode it. Sign Protocol returns the schema's structure with each attestation object, so you have everything needed to decode it.

Warning

NOTE: If you selected a data storage option that is not directly on-chain (such as Arweave/IPFS), your schema uses Hybrid Attestations. The SDK handles this for you, but when retrieving a hybrid attestation you will receive an encoded Content ID (CID) as data.

The following function takes a message and the list of attestations returned from the query, finds the matching attestation, and returns it with the parsed data object.

Info

You can use the NPM SDK's decodeOnChainData function to decode attestation data instead of viem's decodeAbiParameters as shown here. The SDK's Querying page explains it and includes an example.

import { decodeAbiParameters } from "viem";

function findAttestation(message: string, attestations: any[]) {
  // Iterate through the list of attestations
  for (const att of attestations) {
    if (!att.data) continue;

    let parsedData: any = {};

    // Parse the data.
    if (att.mode === "onchain") {
      // Looking for nested items in the on-chain schema
      try {
        const data = decodeAbiParameters(
          [att.dataLocation === "onchain" ? { components: att.schema.data, type: "tuple" } : { type: "string" }],
          att.data
        );
        parsedData = data[0];
      } catch (error) {
        // Looking for a regular schema format if the nested parse fails
        try {
          const data = decodeAbiParameters(
            att.dataLocation === "onchain" ? att.schema.data : [{ type: "string" }],
            att.data
          );
          const obj: any = {};
          data.forEach((item: any, i: number) => {
            obj[att.schema.data[i].name] = item;
          });
          parsedData = obj;
        } catch (error) {
          continue;
        }
      }
    } else {
      // Try parsing as a string (off-chain attestation)
      try {
        parsedData = JSON.parse(att.data);
      } catch (error) {
        console.log(error);
        continue;
      }
    }

    // Return the correct attestation and its parsed data.
    if(parsedData?.contractDetails === message) {
      return { parsedData, attestation: att };
    }
  }

  // Did not find the attestation we are looking for.
  return undefined;
}

With this function, you can parse the attestations the Indexing Service returns, run additional checks, and use their data. In this example, that means acting on Alice's attestation of Bob's signature, or displaying an error if no valid attestation is found.

If you know the attestation or schema you are looking for, you can request its data directly from the Indexing Service instead of searching by criteria. For example, if your application created Alice's attestation, you could store the returned ID in a database for quicker load times (a query always takes longer than a direct lookup by ID, if only slightly).

Finished tutorial

The finished tutorial implementation is here.