Skip to content

Commit

Permalink
Add DID and statement handler
Browse files Browse the repository at this point in the history
Signed-off-by: Adi Bhagavath <[email protected]>
  • Loading branch information
adi-a11y committed Feb 1, 2024
1 parent fdf40a9 commit 30d7e86
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#ffb733",
"activityBar.background": "#ffb733",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#008053",
"activityBarBadge.foreground": "#e7e7e7",
"commandCenter.border": "#15202b99",
"sash.hoverBorder": "#ffb733",
"statusBar.background": "#ffa500",
"statusBar.foreground": "#15202b",
"statusBarItem.hoverBackground": "#cc8400",
"statusBarItem.remoteBackground": "#ffa500",
"statusBarItem.remoteForeground": "#15202b",
"titleBar.activeBackground": "#ffa500",
"titleBar.activeForeground": "#15202b",
"titleBar.inactiveBackground": "#ffa50099",
"titleBar.inactiveForeground": "#15202b99"
},
"peacock.color": "orange"
}
41 changes: 41 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,44 @@ type Rating @entity {
entityType: String
ratingType: String
}

type Did @entity {
id: ID!

method: String
blockNumber: BigInt
identifier: String
submitter: String
signature: String

keyAgreementKeys: String
assertionKey: String
delegationKey: String
serviceDetails: [String]
}

type DidSubmitCall @entity {
id: ID!
blockNumber: BigInt

didUri: String
callIndex: String
txCounter: Int
submitter: String
signature: String
}

type Statement @entity {
id: ID!

method: String
callIndex: String
blockNumber: BigInt
submitter: String
signature: String

statement_id: String
authorization: String
schema_id: String
digest: String
}
12 changes: 12 additions & 0 deletions src/handlers/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { AnyCall, DispatchedCallData } from './types'
import { SubstrateExtrinsic } from "@subql/types";
import { Dispatcher, getBatchInterruptedIndex, getKVData } from './utils';
import { createScore } from './score';
import { indexDidCall } from './did';
import { createStatement } from './statement';

async function traverExtrinsic(extrinsic: Extrinsic, raw: SubstrateExtrinsic): Promise<Call[]> {
const list: any[] = []
Expand Down Expand Up @@ -57,6 +59,16 @@ async function traverExtrinsic(extrinsic: Extrinsic, raw: SubstrateExtrinsic): P
logger.info("Scoring call");
await createScore(raw, id as string);
}

if (call.section === 'did') {
logger.info("DID call")
await indexDidCall(raw, id as string, data.method)
}

if (call.section === "statement") {
logger.info(`${data.method}`);
await createStatement(raw, id as string, data.method);
}

if (depth < 1 && section === 'utility' && (method === 'batch' || method === 'batchAll')) {
const temp = args[0] as unknown as Vec<AnyCall>
Expand Down
64 changes: 64 additions & 0 deletions src/handlers/did.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { SubstrateExtrinsic } from "@subql/types";
import { Did, DidSubmitCall } from "../types";

export async function indexDidCall(
extrinsic: SubstrateExtrinsic,
id: string,
method: string
): Promise<void> {
const data = extrinsic.extrinsic.method;

if (method === "create") {
let arrayed = JSON.parse(JSON.stringify(data));
let serviceDetailsFromChain = arrayed.args.details.newServiceDetails;
let serviceDetails: string[] = [];

await serviceDetailsFromChain.forEach((item: any) => {
let serviceDetail = {
id: item.id,
serviceTypes: item.serviceTypes,
urls: item.urls,
};

serviceDetails.push(JSON.stringify(serviceDetail));
});

let did = new Did(id);
did.method = method;
did.blockNumber = extrinsic.block.block.header.number.toBigInt();
did.identifier = arrayed.args.details.did;
did.submitter = arrayed.args.details.submitter;
did.signature = JSON.stringify(arrayed.args.signature);

did.keyAgreementKeys = JSON.stringify(
arrayed.args.details.newKeyAgreementKeys
);
did.assertionKey = JSON.stringify(arrayed.args.details.newAssertionKey);
did.delegationKey = JSON.stringify(arrayed.args.details.newDelegationKey);
did.serviceDetails = serviceDetails;
await did.save();
}
if (method === "update") {
// ToDo:
// Develop a demo script within the GitHub repository at github.com/dhiway/cord.js which
// calls this method. Execute the script to capture the extrinsic information, analyze the obtained data,
// and complete the implementation.
}
if (method === "delete") {
// ToDo:
// Develop a demo script within the GitHub repository at github.com/dhiway/cord.js which
// calls this method. Execute the script to capture the extrinsic information, analyze the obtained data,
// and complete the implementation.
}
if (method === "submitDidCall") {
let arrayed = JSON.parse(JSON.stringify(data));
let didSubmitCall = new DidSubmitCall(id);
didSubmitCall.blockNumber = extrinsic.block.block.header.number.toBigInt();
didSubmitCall.didUri = arrayed.args.did_call.did;
didSubmitCall.callIndex = arrayed.args.did_call.call.callIndex;
didSubmitCall.txCounter = arrayed.args.did_call.txCounter;
didSubmitCall.submitter = arrayed.args.did_call.submitter;
didSubmitCall.signature = JSON.stringify(arrayed.args.signature);
await didSubmitCall.save();
}
}
64 changes: 64 additions & 0 deletions src/handlers/statement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { SubstrateExtrinsic } from "@subql/types";
import { Statement } from "../types";

export async function createStatement(
extrinsic: SubstrateExtrinsic,
id: string,
method: string
): Promise<void> {
const data = extrinsic.extrinsic.method;
let arrayed = JSON.parse(JSON.stringify(data));
if (method === "register") {
let arrayed = JSON.parse(JSON.stringify(data));
let statementId = JSON.parse(JSON.stringify(extrinsic.events))[0].event
.data[0];
let statement = new Statement(id);
statement.method = method;
statement.callIndex = arrayed.args.did_call.call.callIndex
statement.blockNumber = extrinsic.block.block.header.number.toBigInt();
statement.submitter = arrayed.args.did_call.submitter;
statement.signature = JSON.stringify(arrayed.args.signature);
statement.statement_id = statementId;
statement.authorization = arrayed.args.did_call.call.args.authorization;
statement.schema_id = arrayed.args.did_call.call.args.schema_id;
statement.digest = arrayed.args.did_call.call.args.digest;
await statement.save();
}
if (method === "update") {
let arrayed = JSON.parse(JSON.stringify(data));
let statement = new Statement(id);
statement.method = method;
statement.callIndex = arrayed.args.did_call.call.callIndex
statement.blockNumber = extrinsic.block.block.header.number.toBigInt();
statement.submitter = arrayed.args.did_call.submitter;
statement.signature = JSON.stringify(arrayed.args.signature);
statement.statement_id = arrayed.args.did_call.call.args.statement_id;
statement.digest = arrayed.args.did_call.call.args.digest;
statement.authorization = arrayed.args.did_call.call.args.authorization;
await statement.save();
}
if (method === "revoke") {
let arrayed = JSON.parse(JSON.stringify(data));
let statement = new Statement(id);
statement.method = method;
statement.callIndex = arrayed.args.did_call.call.callIndex
statement.blockNumber = extrinsic.block.block.header.number.toBigInt();
statement.submitter = arrayed.args.did_call.submitter;
statement.signature = JSON.stringify(arrayed.args.signature);
statement.statement_id = arrayed.args.did_call.call.args.statement_id;
statement.authorization = arrayed.args.did_call.call.args.authorization;
await statement.save();
}
if (method === "restore") {
let arrayed = JSON.parse(JSON.stringify(data));
let statement = new Statement(id);
statement.method = method;
statement.callIndex = arrayed.args.did_call.call.callIndex
statement.blockNumber = extrinsic.block.block.header.number.toBigInt();
statement.submitter = arrayed.args.did_call.submitter;
statement.signature = JSON.stringify(arrayed.args.signature);
statement.statement_id = arrayed.args.did_call.call.args.statement_id;
statement.authorization = arrayed.args.did_call.call.args.authorization;
await statement.save();
}
}

0 comments on commit 30d7e86

Please sign in to comment.