diff --git a/src/controller/credential_controller.ts b/src/controller/credential_controller.ts index 291a409..0d610d1 100644 --- a/src/controller/credential_controller.ts +++ b/src/controller/credential_controller.ts @@ -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, @@ -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 }); + } +} diff --git a/src/index.ts b/src/index.ts index 2b6c3fe..37c5edd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { createConnection } from 'typeorm'; import { dbConfig } from './dbconfig'; import { addDelegateAsRegistryDelegate } from './init'; import { + documentHashOnChain, getCredById, issueVC, revokeCred, @@ -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);