Skip to content

Commit

Permalink
Merge pull request #30 from Vikastc/pack-update
Browse files Browse the repository at this point in the history
misc: Updated to latest cord: 0.9.3-1rc2
  • Loading branch information
Vikastc authored Feb 14, 2024
2 parents f987a73 + b58507d commit 7abc8e1
Show file tree
Hide file tree
Showing 9 changed files with 1,010 additions and 4,547 deletions.
8 changes: 4 additions & 4 deletions Dockerfile.for-ci
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM node:18.5-slim as builder
FROM node:20.1-slim as builder

WORKDIR /app

COPY package.json package-lock.json /app/
RUN npm ci
COPY package.json yarn.lock /app/
RUN yarn

COPY . .
RUN npm run build
RUN yarn build
2 changes: 1 addition & 1 deletion Dockerfile.mac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18.5-slim
FROM node:20.1-slim

WORKDIR /app

Expand Down
3,646 changes: 0 additions & 3,646 deletions package-lock.json

This file was deleted.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
"author": "",
"license": "ISC",
"dependencies": {
"@cord.network/sdk": "^0.9.2",
"@cord.network/sdk": "0.9.3-1rc4",
"@cord.network/vc-export": "0.9.3-1rc4",
"body-parser": "^1.20.2",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"moment": "^2.30.1",
"nodemon": "^2.0.21",
"pg": "^8.10.0",
"reflect-metadata": "^0.1.13",
Expand All @@ -26,7 +28,7 @@
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/node": "^18.15.11",
"@types/node": "^20.11.0",
"@types/swagger-ui-express": "^4.1.3",
"@types/uuid": "^9.0.1",
"typescript": "^4.9.5"
Expand Down
262 changes: 143 additions & 119 deletions src/controller/credential_controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';

import * as Vc from '@cord.network/vc-export';
import * as Cord from '@cord.network/sdk';

import {
Expand All @@ -14,33 +14,57 @@ import {

import { getConnection } from 'typeorm';
import { Cred } from '../entity/Cred';
import { Schema } from '../entity/Schema';
const { CHAIN_SPACE_ID, CHAIN_SPACE_AUTH } = process.env;

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

if (!authorIdentity) {
await addDelegateAsRegistryDelegate();
}

try {
const newCredContent = req.body;
newCredContent.issuanceDate = new Date().toISOString();
const serializedCred = Cord.Utils.Crypto.encodeObjectAsStr(newCredContent);
const credHash = Cord.Utils.Crypto.hashStr(serializedCred);

const statementEntry = Cord.Statement.buildFromProperties(
credHash,
CHAIN_SPACE_ID as `space:cord:${string}`,
const schema = await getConnection()
.getRepository(Schema)
.findOne({ identifier: req.params.id });

const parsedSchema = JSON.parse(schema?.cordSchema as string);

const newCredContent = await Vc.buildVcFromContent(
parsedSchema.schema,
data.properties,
issuerDid,
issuerDid.uri,
req.params.id as `schema:cord:${string}`
{
spaceUri: CHAIN_SPACE_ID as `space:cord:${string}`,
schemaUri: schema?.identifier,
}
);

console.dir(statementEntry, {
const vc: any = await Vc.addProof(
newCredContent,
async (data) => ({
signature: await issuerKeysProperty.assertionMethod.sign(data),
keyType: issuerKeysProperty.assertionMethod.type,
keyUri: `${issuerDid.uri}${
issuerDid.assertionMethod![0].id
}` as Cord.DidResourceUri,
}),
issuerDid,
{
spaceUri: CHAIN_SPACE_ID as `space:cord:${string}`,
schemaUri: schema?.identifier,
needSDR: true,
}
);
console.dir(vc, {
depth: null,
colors: true,
});

const statement = await Cord.Statement.dispatchRegisterToChain(
statementEntry,
vc.proof[1],
issuerDid.uri,
authorIdentity,
CHAIN_SPACE_AUTH as `auth:cord:${string}`,
Expand All @@ -57,9 +81,9 @@ export async function issueVD(req: express.Request, res: express.Response) {
cred.identifier = statement;
cred.active = true;
cred.fromDid = issuerDid.uri;
cred.credHash = credHash;
cred.credHash = newCredContent.credentialHash;
cred.newCredContent = newCredContent;
cred.credentialEntry = statementEntry;
cred.vc = vc.proof;

if (statement) {
await getConnection().manager.save(cred);
Expand Down Expand Up @@ -118,108 +142,108 @@ export async function getCredById(req: express.Request, res: express.Response) {
}
}

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

if (!data.property || typeof data.property !== 'object') {
return res.status(400).json({
error: '"property" is a required field and should be an object',
});
}

try {
const cred = await getConnection()
.getRepository(Cred)
.findOne({ identifier: req.params.id });

if (!cred) {
return res.status(400).json({ error: 'Cred not found' });
}

console.log(`\n❄️ Statement Updation `);
let updateCredContent = cred.newCredContent;
updateCredContent.issuanceDate = new Date().toISOString();
updateCredContent.property = data.property;
const serializedUpCred =
Cord.Utils.Crypto.encodeObjectAsStr(updateCredContent);
const upCredHash = Cord.Utils.Crypto.hashStr(serializedUpCred);

const updatedStatementEntry = Cord.Statement.buildFromUpdateProperties(
cred.credentialEntry.elementUri,
upCredHash,
CHAIN_SPACE_ID as `space:cord:${string}`,
delegateDid.uri
);

console.dir(updatedStatementEntry, {
depth: null,
colors: true,
});

const updatedStatement = await Cord.Statement.dispatchUpdateToChain(
updatedStatementEntry,
delegateDid.uri,
authorIdentity,
delegateSpaceAuth as Cord.AuthorizationUri,
async ({ data }) => ({
signature: delegateKeysProperty.authentication.sign(data),
keyType: delegateKeysProperty.authentication.type,
})
);
console.log(`✅ Statement element registered - ${updatedStatement}`);

if (updatedStatement) {
cred.identifier = updatedStatement;
cred.credHash = upCredHash;
cred.newCredContent = updateCredContent;
cred.credentialEntry = updatedStatementEntry;

await getConnection().manager.save(cred);

console.log('\n✅ Statement updated!');

return res.status(200).json({
result: 'Updated successufully',
identifier: cred.identifier,
});
}
return res.status(400).json({ error: 'Document not updated' });
} catch (error) {
console.log('error: ', error);
throw new Error('Error in updating document');
}
}

export async function revokeCred(req: express.Request, res: express.Response) {
try {
const cred = await getConnection()
.getRepository(Cred)
.findOne({ identifier: req.params.id });

if (!cred) {
return res.status(400).json({ error: 'Invalid identifier' });
}

await Cord.Statement.dispatchRevokeToChain(
cred.credentialEntry.elementUri,
delegateDid.uri,
authorIdentity,
delegateSpaceAuth as Cord.AuthorizationUri,
async ({ data }) => ({
signature: delegateKeysProperty.authentication.sign(data),
keyType: delegateKeysProperty.authentication.type,
})
);

cred.active = false;

await getConnection().manager.save(cred);

console.log(`✅ Statement revoked!`);

return res.status(200).json({ result: 'Statement revoked Successfully' });
} catch (error) {
console.log('err: ', error);
return res.status(400).json({ err: error });
}
}
// export async function updateCred(req: express.Request, res: express.Response) {
// const data = req.body;

// if (!data.property || typeof data.property !== 'object') {
// return res.status(400).json({
// error: '"property" is a required field and should be an object',
// });
// }

// try {
// const cred = await getConnection()
// .getRepository(Cred)
// .findOne({ identifier: req.params.id });

// if (!cred) {
// return res.status(400).json({ error: 'Cred not found' });
// }

// console.log(`\n❄️ Statement Updation `);
// let updateCredContent = cred.newCredContent;
// updateCredContent.issuanceDate = new Date().toISOString();
// updateCredContent.property = data.property;
// const serializedUpCred =
// Cord.Utils.Crypto.encodeObjectAsStr(updateCredContent);
// const upCredHash = Cord.Utils.Crypto.hashStr(serializedUpCred);

// const updatedStatementEntry = Cord.Statement.buildFromUpdateProperties(
// cred.credentialEntry.elementUri,
// upCredHash,
// CHAIN_SPACE_ID as `space:cord:${string}`,
// delegateDid.uri
// );

// console.dir(updatedStatementEntry, {
// depth: null,
// colors: true,
// });

// const updatedStatement = await Cord.Statement.dispatchUpdateToChain(
// updatedStatementEntry,
// delegateDid.uri,
// authorIdentity,
// delegateSpaceAuth as Cord.AuthorizationUri,
// async ({ data }) => ({
// signature: delegateKeysProperty.authentication.sign(data),
// keyType: delegateKeysProperty.authentication.type,
// })
// );
// console.log(`✅ Statement element registered - ${updatedStatement}`);

// if (updatedStatement) {
// cred.identifier = updatedStatement;
// cred.credHash = upCredHash;
// cred.newCredContent = updateCredContent;
// cred.credentialEntry = updatedStatementEntry;

// await getConnection().manager.save(cred);

// console.log('\n✅ Statement updated!');

// return res.status(200).json({
// result: 'Updated successufully',
// identifier: cred.identifier,
// });
// }
// return res.status(400).json({ error: 'Document not updated' });
// } catch (error) {
// console.log('error: ', error);
// throw new Error('Error in updating document');
// }
// }

// export async function revokeCred(req: express.Request, res: express.Response) {
// try {
// const cred = await getConnection()
// .getRepository(Cred)
// .findOne({ identifier: req.params.id });

// if (!cred) {
// return res.status(400).json({ error: 'Invalid identifier' });
// }

// await Cord.Statement.dispatchRevokeToChain(
// cred.credentialEntry.elementUri,
// delegateDid.uri,
// authorIdentity,
// delegateSpaceAuth as Cord.AuthorizationUri,
// async ({ data }) => ({
// signature: delegateKeysProperty.authentication.sign(data),
// keyType: delegateKeysProperty.authentication.type,
// })
// );

// cred.active = false;

// await getConnection().manager.save(cred);

// console.log(`✅ Statement revoked!`);

// return res.status(200).json({ result: 'Statement revoked Successfully' });
// } catch (error) {
// console.log('err: ', error);
// return res.status(400).json({ err: error });
// }
// }
2 changes: 1 addition & 1 deletion src/entity/Cred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Cred {
newCredContent?: any;

@Column('simple-json', { nullable: true, default: null })
credentialEntry?: any;
vc?: any;

@CreateDateColumn({ name: 'created_at' })
createdAt?: Date;
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { addDelegateAsRegistryDelegate } from './init';
import {
getCredById,
issueVD,
revokeCred,
updateCred,
// revokeCred,
// updateCred,
} from './controller/credential_controller';

const app = express();
Expand All @@ -31,13 +31,13 @@ credentialRouter.get('/:id', async (req, res) => {
return await getCredById(req, res);
});

credentialRouter.put('/update/:id', async (req, res) => {
return await updateCred(req, res);
});
// credentialRouter.put('/update/:id', async (req, res) => {
// return await updateCred(req, res);
// });

credentialRouter.post('/revoke/:id', async (req, res) => {
return await revokeCred(req, res);
});
// credentialRouter.post('/revoke/:id', async (req, res) => {
// return await revokeCred(req, res);
// });

schemaRouter.post('/', async (req, res) => {
return await createSchema(req, res);
Expand Down
Loading

0 comments on commit 7abc8e1

Please sign in to comment.