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: revoke changes issuer agent #62

Merged
merged 2 commits into from
Feb 25, 2025
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
112 changes: 93 additions & 19 deletions src/controller/credential_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand All @@ -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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be updateDocumentHashOnChain()

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}`);

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);
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
issueVC,
revokeCred,
revokeDocumentHashOnChain,
udpateDocumentHashonChain,
updateCred,
} from './controller/credential_controller';
import { generateDid, resolveDid } from './controller/did_controller';
Expand Down Expand Up @@ -56,6 +57,10 @@ docRouter.post("/revoke", async (req, res) => {
return await revokeDocumentHashOnChain(req, res);
});

docRouter.post("/update", async (req, res) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And try to use "put" for update call rather than "post"

return await udpateDocumentHashonChain(req, res);
});

app.use('/api/v1/schema', schemaRouter);
app.use('/api/v1/cred', credentialRouter);
app.use('/api/v1/did', didRouter);
Expand Down