Skip to content

Commit

Permalink
feat: sample for document hash anchor API implementaition for Sunbird…
Browse files Browse the repository at this point in the history
… integration

Signed-off-by: vikastc <[email protected]>
  • Loading branch information
Vikastc committed Mar 19, 2024
1 parent 14b88ce commit c8d71c9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/controller/credential_controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import * as Vc from '@cord.network/vc-export';
import * as Cord from '@cord.network/sdk';
import crypto from 'crypto';

import {
issuerDid,
Expand Down Expand Up @@ -254,3 +255,45 @@ export async function revokeCred(req: express.Request, res: express.Response) {
return res.status(400).json({ err: error });
}
}

export async function documentHashOnChain(
req: express.Request,
res: express.Response
) {
try {
const data = req.body;

// const content: any = fs.readFileSync('./package.json');
const content = JSON.stringify(data);

const hashFn = crypto.createHash('sha256');
hashFn.update(content);
let digest = `0x${hashFn.digest('hex')}`;

const docProof = await Vc.getCordProofForDigest(
digest as `0x${string}`,
issuerDid,
{
spaceUri: CHAIN_SPACE_ID as `space:cord:${string}`,
}
);

const statement1 = await Cord.Statement.dispatchRegisterToChain(
docProof,
issuerDid.uri,
authorIdentity,
CHAIN_SPACE_AUTH as `auth:cord:${string}`,
async ({ data }) => ({
signature: issuerKeysProperty.authentication.sign(data),
keyType: issuerKeysProperty.authentication.type,
})
);

console.dir(docProof, { colors: true, depth: null });
console.log(`✅ Statement element registered - ${statement1}`);

return res.status(200).json({ result: statement1 });
} catch (error) {
return res.status(400).json({ err: error });
}
}
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 { createConnection } from 'typeorm';
import { dbConfig } from './dbconfig';
import { addDelegateAsRegistryDelegate } from './init';
import {
documentHashOnChain,
getCredById,
issueVC,
revokeCred,
Expand Down Expand Up @@ -55,6 +56,10 @@ app.use('/docs', swaggerUi.serve, swaggerUi.setup(openApiDocumentation));
app.use('/api/v1/schema', schemaRouter);
app.use('/api/v1/cred', credentialRouter);

app.post('/api/v1/docHash', async (req, res) => {
return await documentHashOnChain(req, res);
});

async function main() {
try {
await createConnection(dbConfig);
Expand Down

0 comments on commit c8d71c9

Please sign in to comment.