From 15f8c3e2e35cebcad8486fc33c0987057b949b84 Mon Sep 17 00:00:00 2001 From: RCCodeBase Date: Tue, 25 Feb 2025 23:39:26 +0530 Subject: [PATCH 1/2] feat: revoke changes issuer agent --- src/controller/credential_controller.ts | 112 ++++++++++++++++++++---- src/index.ts | 5 ++ 2 files changed, 98 insertions(+), 19 deletions(-) diff --git a/src/controller/credential_controller.ts b/src/controller/credential_controller.ts index 8b68342..c9039b6 100644 --- a/src/controller/credential_controller.ts +++ b/src/controller/credential_controller.ts @@ -330,22 +330,40 @@ export async function revokeDocumentHashOnChain( res: express.Response ) { try { - const fileHash= req?.body.filehash; - if (!fileHash) { - return res.status(400).json({ err: 'No file uploaded' }); - } + const fileHash = req?.body.filehash; + const identifierReq = req?.body.identifier; + let statementUri = ``; const api = Cord.ConfigService.get('api'); - - const space = Cord.Identifier.uriToIdentifier(CHAIN_SPACE_ID); - const identifierencoded = await api.query.statement.identifierLookup( - fileHash as `0x${string}`, - space + if (fileHash) { + const space = Cord.Identifier.uriToIdentifier(CHAIN_SPACE_ID); + const identifierencoded = await api.query.statement.identifierLookup( + fileHash as `0x${string}`, + space + ); + const identifier = identifierencoded.toHuman(); + const digest = fileHash.replace(/^0x/, ''); + statementUri = `stmt:cord:${identifier}:${digest}`; + } else if (identifierReq) { + const statementDetails = await Cord.Statement.getDetailsfromChain( + identifierReq + ); + const digest = statementDetails?.digest.replace(/^0x/, ''); + statementUri = `${statementDetails?.uri}:${digest}`; + } else { + return res + .status(400) + .json({ err: 'File hash or identifier is required for revoke' }); + } + const statementStatus = await Cord.Statement.fetchStatementDetailsfromChain( + statementUri as `stmt:cord:${string}` ); - const identifier = identifierencoded.toHuman(); - const digest = fileHash.replace(/^0x/, ""); - const statmentid = `stmt:cord:${identifier}:${digest}` - const statement1 = await Cord.Statement.dispatchRevokeToChain( - statmentid as `stmt:cord:${string}`, + if (statementStatus?.revoked) { + return res + .status(400) + .json({ err: 'Document is already revoked on chain' }); + } + const revokeResponse = await Cord.Statement.dispatchRevokeToChain( + statementUri as `stmt:cord:${string}`, issuerDid.uri, authorIdentity, CHAIN_SPACE_AUTH as `auth:cord:${string}`, @@ -355,11 +373,67 @@ export async function revokeDocumentHashOnChain( }) ); - const statementStatus = await Cord.Statement.fetchStatementDetailsfromChain(statmentid as `stmt:cord:${string}`); - if(statementStatus?.revoked){ - return res.status(200).json({ result:{msg:'Successfully revoked'} }); - }else{ - return res.status(400).json({ err:'Document not revoked' }); + const statementStatusRevoked = + await Cord.Statement.fetchStatementDetailsfromChain( + statementUri as `stmt:cord:${string}` + ); + if (statementStatusRevoked?.revoked) { + return res.status(200).json({ result: { msg: 'Successfully revoked' } }); + } else { + return res.status(400).json({ err: 'Document not revoked' }); + } + } catch (error: any) { + console.log('errr: ', error); + return res.status(400).json({ err: error.message ? error.message : error }); + } +} + +export async function udpateDocumentHashonChain( + req: express.Request, + res: express.Response +) { + try { + const fileHash = req?.body.filehash; + const identifierReq = req?.body.identifier; + if (!req.params.id) { + return res.status(400).json({ err: 'Please enter correct id' }); + } + const api = Cord.ConfigService.get('api'); + if (!CHAIN_SPACE_ID) { + return res.status(400).json({ err: 'chain space id not' }); + } + const statementDetails = await Cord.Statement.getDetailsfromChain( + req.params.id + ); + if (statementDetails?.digest) { + const digest = statementDetails.digest.replace(/^0x/, ''); + const elementUri = `${statementDetails.uri}:digest`; + const updatedStatementEntry = Cord.Statement.buildFromUpdateProperties( + elementUri as `stmt:cord:${string}`, + statementDetails?.digest, + CHAIN_SPACE_ID as `space:cord:${string}`, + issuerDid.uri + ); + console.dir(updatedStatementEntry, { + depth: null, + colors: true, + }); + + const updatedStatement = await Cord.Statement.dispatchUpdateToChain( + updatedStatementEntry, + issuerDid.uri, + authorIdentity, + CHAIN_SPACE_AUTH as `auth:cord:${string}`, + async ({ data }) => ({ + signature: issuerKeysProperty.authentication.sign(data), + keyType: issuerKeysProperty.authentication.type, + }) + ); + console.log(`✅ Statement element registered - ${updatedStatement}`); + console.log('here', statementDetails); + return res.status(200).json({ result: { msg: 'Successfully update' } }); + } else { + return res.status(400).json({ err: 'Unable to find the digest' }); } } catch (error: any) { console.log('errr: ', error); diff --git a/src/index.ts b/src/index.ts index 8a42cdb..eabbc0c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { issueVC, revokeCred, revokeDocumentHashOnChain, + udpateDocumentHashonChain, updateCred, } from './controller/credential_controller'; import { generateDid, resolveDid } from './controller/did_controller'; @@ -56,6 +57,10 @@ docRouter.post("/revoke", async (req, res) => { return await revokeDocumentHashOnChain(req, res); }); +docRouter.post("/update", async (req, res) => { + return await udpateDocumentHashonChain(req, res); +}); + app.use('/api/v1/schema', schemaRouter); app.use('/api/v1/cred', credentialRouter); app.use('/api/v1/did', didRouter); From d1188506f71cfb544c2696e450cfa166ec95c242 Mon Sep 17 00:00:00 2001 From: RCCodeBase Date: Tue, 25 Feb 2025 23:43:30 +0530 Subject: [PATCH 2/2] fix: remove console log --- src/controller/credential_controller.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controller/credential_controller.ts b/src/controller/credential_controller.ts index c9039b6..bccb612 100644 --- a/src/controller/credential_controller.ts +++ b/src/controller/credential_controller.ts @@ -430,7 +430,7 @@ export async function udpateDocumentHashonChain( }) ); console.log(`✅ Statement element registered - ${updatedStatement}`); - console.log('here', statementDetails); + return res.status(200).json({ result: { msg: 'Successfully update' } }); } else { return res.status(400).json({ err: 'Unable to find the digest' });