Skip to content

Commit e9c8dad

Browse files
authored
Merge pull request #62 from RCCodeBase/issueragentrevokechanges
feat: revoke changes issuer agent
2 parents 9ed93e9 + d118850 commit e9c8dad

File tree

2 files changed

+98
-19
lines changed

2 files changed

+98
-19
lines changed

src/controller/credential_controller.ts

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -330,22 +330,40 @@ export async function revokeDocumentHashOnChain(
330330
res: express.Response
331331
) {
332332
try {
333-
const fileHash= req?.body.filehash;
334-
if (!fileHash) {
335-
return res.status(400).json({ err: 'No file uploaded' });
336-
}
333+
const fileHash = req?.body.filehash;
334+
const identifierReq = req?.body.identifier;
335+
let statementUri = ``;
337336
const api = Cord.ConfigService.get('api');
338-
339-
const space = Cord.Identifier.uriToIdentifier(CHAIN_SPACE_ID);
340-
const identifierencoded = await api.query.statement.identifierLookup(
341-
fileHash as `0x${string}`,
342-
space
337+
if (fileHash) {
338+
const space = Cord.Identifier.uriToIdentifier(CHAIN_SPACE_ID);
339+
const identifierencoded = await api.query.statement.identifierLookup(
340+
fileHash as `0x${string}`,
341+
space
342+
);
343+
const identifier = identifierencoded.toHuman();
344+
const digest = fileHash.replace(/^0x/, '');
345+
statementUri = `stmt:cord:${identifier}:${digest}`;
346+
} else if (identifierReq) {
347+
const statementDetails = await Cord.Statement.getDetailsfromChain(
348+
identifierReq
349+
);
350+
const digest = statementDetails?.digest.replace(/^0x/, '');
351+
statementUri = `${statementDetails?.uri}:${digest}`;
352+
} else {
353+
return res
354+
.status(400)
355+
.json({ err: 'File hash or identifier is required for revoke' });
356+
}
357+
const statementStatus = await Cord.Statement.fetchStatementDetailsfromChain(
358+
statementUri as `stmt:cord:${string}`
343359
);
344-
const identifier = identifierencoded.toHuman();
345-
const digest = fileHash.replace(/^0x/, "");
346-
const statmentid = `stmt:cord:${identifier}:${digest}`
347-
const statement1 = await Cord.Statement.dispatchRevokeToChain(
348-
statmentid as `stmt:cord:${string}`,
360+
if (statementStatus?.revoked) {
361+
return res
362+
.status(400)
363+
.json({ err: 'Document is already revoked on chain' });
364+
}
365+
const revokeResponse = await Cord.Statement.dispatchRevokeToChain(
366+
statementUri as `stmt:cord:${string}`,
349367
issuerDid.uri,
350368
authorIdentity,
351369
CHAIN_SPACE_AUTH as `auth:cord:${string}`,
@@ -355,11 +373,67 @@ export async function revokeDocumentHashOnChain(
355373
})
356374
);
357375

358-
const statementStatus = await Cord.Statement.fetchStatementDetailsfromChain(statmentid as `stmt:cord:${string}`);
359-
if(statementStatus?.revoked){
360-
return res.status(200).json({ result:{msg:'Successfully revoked'} });
361-
}else{
362-
return res.status(400).json({ err:'Document not revoked' });
376+
const statementStatusRevoked =
377+
await Cord.Statement.fetchStatementDetailsfromChain(
378+
statementUri as `stmt:cord:${string}`
379+
);
380+
if (statementStatusRevoked?.revoked) {
381+
return res.status(200).json({ result: { msg: 'Successfully revoked' } });
382+
} else {
383+
return res.status(400).json({ err: 'Document not revoked' });
384+
}
385+
} catch (error: any) {
386+
console.log('errr: ', error);
387+
return res.status(400).json({ err: error.message ? error.message : error });
388+
}
389+
}
390+
391+
export async function udpateDocumentHashonChain(
392+
req: express.Request,
393+
res: express.Response
394+
) {
395+
try {
396+
const fileHash = req?.body.filehash;
397+
const identifierReq = req?.body.identifier;
398+
if (!req.params.id) {
399+
return res.status(400).json({ err: 'Please enter correct id' });
400+
}
401+
const api = Cord.ConfigService.get('api');
402+
if (!CHAIN_SPACE_ID) {
403+
return res.status(400).json({ err: 'chain space id not' });
404+
}
405+
const statementDetails = await Cord.Statement.getDetailsfromChain(
406+
req.params.id
407+
);
408+
if (statementDetails?.digest) {
409+
const digest = statementDetails.digest.replace(/^0x/, '');
410+
const elementUri = `${statementDetails.uri}:digest`;
411+
const updatedStatementEntry = Cord.Statement.buildFromUpdateProperties(
412+
elementUri as `stmt:cord:${string}`,
413+
statementDetails?.digest,
414+
CHAIN_SPACE_ID as `space:cord:${string}`,
415+
issuerDid.uri
416+
);
417+
console.dir(updatedStatementEntry, {
418+
depth: null,
419+
colors: true,
420+
});
421+
422+
const updatedStatement = await Cord.Statement.dispatchUpdateToChain(
423+
updatedStatementEntry,
424+
issuerDid.uri,
425+
authorIdentity,
426+
CHAIN_SPACE_AUTH as `auth:cord:${string}`,
427+
async ({ data }) => ({
428+
signature: issuerKeysProperty.authentication.sign(data),
429+
keyType: issuerKeysProperty.authentication.type,
430+
})
431+
);
432+
console.log(`✅ Statement element registered - ${updatedStatement}`);
433+
434+
return res.status(200).json({ result: { msg: 'Successfully update' } });
435+
} else {
436+
return res.status(400).json({ err: 'Unable to find the digest' });
363437
}
364438
} catch (error: any) {
365439
console.log('errr: ', error);

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
issueVC,
99
revokeCred,
1010
revokeDocumentHashOnChain,
11+
udpateDocumentHashonChain,
1112
updateCred,
1213
} from './controller/credential_controller';
1314
import { generateDid, resolveDid } from './controller/did_controller';
@@ -56,6 +57,10 @@ docRouter.post("/revoke", async (req, res) => {
5657
return await revokeDocumentHashOnChain(req, res);
5758
});
5859

60+
docRouter.post("/update", async (req, res) => {
61+
return await udpateDocumentHashonChain(req, res);
62+
});
63+
5964
app.use('/api/v1/schema', schemaRouter);
6065
app.use('/api/v1/cred', credentialRouter);
6166
app.use('/api/v1/did', didRouter);

0 commit comments

Comments
 (0)