From 30d7e869b421e56a1c371fd4d519f6f2ef64e0c5 Mon Sep 17 00:00:00 2001 From: Adi Bhagavath Date: Thu, 1 Feb 2024 14:19:00 +0530 Subject: [PATCH] Add DID and statement handler Signed-off-by: Adi Bhagavath --- .vscode/settings.json | 22 ++++++++++++++ schema.graphql | 41 +++++++++++++++++++++++++ src/handlers/call.ts | 12 ++++++++ src/handlers/did.ts | 64 +++++++++++++++++++++++++++++++++++++++ src/handlers/statement.ts | 64 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 203 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 src/handlers/did.ts create mode 100644 src/handlers/statement.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e4cb847 --- /dev/null +++ b/.vscode/settings.json @@ -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" +} \ No newline at end of file diff --git a/schema.graphql b/schema.graphql index 2aeaa8a..d4462ff 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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 +} \ No newline at end of file diff --git a/src/handlers/call.ts b/src/handlers/call.ts index 5b5826f..84586b6 100644 --- a/src/handlers/call.ts +++ b/src/handlers/call.ts @@ -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 { const list: any[] = [] @@ -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 diff --git a/src/handlers/did.ts b/src/handlers/did.ts new file mode 100644 index 0000000..f39bcb4 --- /dev/null +++ b/src/handlers/did.ts @@ -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 { + 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(); + } +} \ No newline at end of file diff --git a/src/handlers/statement.ts b/src/handlers/statement.ts new file mode 100644 index 0000000..64c8596 --- /dev/null +++ b/src/handlers/statement.ts @@ -0,0 +1,64 @@ +import { SubstrateExtrinsic } from "@subql/types"; +import { Statement } from "../types"; + +export async function createStatement( + extrinsic: SubstrateExtrinsic, + id: string, + method: string +): Promise { + 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(); + } +} \ No newline at end of file