Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: As a user i want to get decoded attestation data via the subgraph #229

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Attestation @entity {
revoked: Boolean!
subject: Bytes!
attestationData: Bytes!
schemaString: String
decodedData:[String!]
}

type Module @entity {
Expand Down
94 changes: 90 additions & 4 deletions subgraph/src/attestation-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
AttestationRegistered as AttestationRegisteredEvent,
AttestationRegistry,
} from "../generated/AttestationRegistry/AttestationRegistry";
import { Attestation } from "../generated/schema";
import { BigInt } from "@graphprotocol/graph-ts";
import { Attestation, Schema } from "../generated/schema";
import { BigInt, ByteArray, Bytes, ethereum } from "@graphprotocol/graph-ts";

export function handleAttestationRegistered(event: AttestationRegisteredEvent): void {
const contract = AttestationRegistry.bind(event.address);
const attestationData = contract.getAttestation(event.params.attestationId);
const attestationRegistryContract = AttestationRegistry.bind(event.address);
const attestationData = attestationRegistryContract.getAttestation(event.params.attestationId);
const attestation = new Attestation(event.params.attestationId.toHex());

attestation.schemaId = attestationData.schemaId;
Expand All @@ -22,5 +22,91 @@ export function handleAttestationRegistered(event: AttestationRegisteredEvent):
attestation.subject = attestationData.subject;
attestation.attestationData = attestationData.attestationData;

// Get matching Schema
const schema = Schema.load(attestationData.schemaId.toHex());

if (schema) {
// Split Schema into a "type fieldName" array
const splitSchema = schema.schema.split(",");

// Keep only the Schema's types
const schemaTypes = splitSchema.map<string>((item) => item.trim().split(" ")[0]);

// Join the types in a single coma-separated string
const schemaString = schemaTypes.toString();

// Add this Schema string to the Attestation Entity
attestation.schemaString = schemaString;

const encodedData = attestationData.attestationData;

// Initiate the decoded data in case it's not decoded at all
attestation.decodedData = ["NOT DECODED"];

// Decode the encoded attestation data
let decoded = ethereum.decode("(" + schemaString + ")", Bytes.fromUint8Array(encodedData));

// If the decoding function didn't give anything, re-try with the encoded data as a tuple
if (!decoded) {
// Change attestation encoded data into an encoded Tuple
const tuplePrefix = ByteArray.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000020");
const encodedDataAsTuple = new Uint8Array(tuplePrefix.length + encodedData.length);
encodedDataAsTuple.set(tuplePrefix, 0);
encodedDataAsTuple.set(encodedData, tuplePrefix.length);

// Decode the tuple
decoded = ethereum.decode("(" + schemaString + ")", Bytes.fromUint8Array(encodedDataAsTuple));
}

// If the decode function went through, save it as an Array of Strings
if (decoded) {
const tempStringArray: string[] = [];

// Make the decoded data into a Tuple
const tupleValue = decoded.toTuple();

// Convert every field of the Tuple into a String
for (let i = 0; i < tupleValue.length; i++) {
tempStringArray.push(valueToString(tupleValue[i]));
}

// Add this decoded Array to the Attestation Entity
attestation.decodedData = tempStringArray;
}
}

attestation.save();
}

function valueToString(value: ethereum.Value): string {
switch (value.kind) {
case ethereum.ValueKind.ADDRESS:
return value.toAddress().toHexString();
case ethereum.ValueKind.FIXED_BYTES:
return value.toBytes().toHex();
case ethereum.ValueKind.BYTES:
return value.toString();
case ethereum.ValueKind.INT:
return value.toBigInt().toHexString();
case ethereum.ValueKind.UINT:
return value.toBigInt().toHexString();
case ethereum.ValueKind.BOOL:
return value.toBoolean().toString();
case ethereum.ValueKind.STRING:
return value.toString();
case ethereum.ValueKind.FIXED_ARRAY:
return value
.toArray()
.map<string>((item) => valueToString(item))
.toString();
case ethereum.ValueKind.ARRAY:
return value
.toArray()
.map<string>((item) => valueToString(item))
.toString();
case ethereum.ValueKind.TUPLE:
return "TUPLE NOT SUPPORTED";
default:
return "UNKNOWN TYPE";
}
}
Loading