Parsing Attestation Data

Attestation data is encoded into a single hex string and can be decoded if you know the schema used to encode the data. With attestations on Sign Protocol, the attestation object returned also includes the schema's structure, which is exactly what we need.

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

We will write a function that takes in a message and the list of attestations returned from our query. This function will find the specific attestation we are looking for and return it with the parsed data object.

You can use our NPM SDK's decodeOnChainData function to decode your attestation data rather than directly interfacing with viem's decodeAbiParameters as shown in this tutorial. For more information and an example, see here.

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;
}

Now, we can parse the attestations returned from our query to Sign Protocol's Indexing Service, perform additional checks, and utilize the data they contain. In our example, we can perform some action using Alice's attestation of Bob's signature, or display an error if no such valid attestation is found.

If you know the attestation or schema you are looking for, you can also directly request its data from the Sign Protocol Indexing Service. In our case, we were looking for an attestation that met certain criteria. To expand upon our example, if our application was responsible for creating Alice's attestation, it might make sense to store the returned ID in a database for quicker load times (querying will always take longer than direct access with an ID, even if marginally so).

Finished Tutorial

You can find the finished tutorial implementation here.

Last updated

Logo

Copyright Sign 2021-2024