Skip to content

Commit

Permalink
Merge pull request #59 from RCCodeBase/fileanchoringchanges
Browse files Browse the repository at this point in the history
feat: File anchoring changes
  • Loading branch information
prashant4dev authored Feb 23, 2025
2 parents 26955e9 + fb6cca6 commit ca66974
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dotenv": "^16.0.3",
"express": "^4.18.2",
"moment": "^2.30.1",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.1.7",
"pg": "^8.10.0",
"reflect-metadata": "^0.2.2",
Expand All @@ -30,6 +31,7 @@
"devDependencies": {
"@types/cors": "^2.8.14",
"@types/express": "^4.17.17",
"@types/multer": "^1.4.12",
"@types/node": "^22.7.3",
"@types/swagger-ui-express": "^4.1.3",
"@types/uuid": "^10.0.0",
Expand Down
28 changes: 17 additions & 11 deletions src/controller/credential_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Cred } from '../entity/Cred';
import { Schema } from '../entity/Schema';
import { dataSource } from '../dbconfig';
import { extractCredentialFields } from '../utils/CredentialUtils';
import { MulterRequest } from '../types/type';
const { CHAIN_SPACE_ID, CHAIN_SPACE_AUTH } = process.env;

export async function issueVC(req: express.Request, res: express.Response) {
Expand Down Expand Up @@ -274,19 +275,24 @@ export async function revokeCred(req: express.Request, res: express.Response) {
}
}




export async function documentHashOnChain(
req: express.Request,
req: MulterRequest,
res: express.Response
) {
try {
const data = req.body;
const api = Cord.ConfigService.get('api');
// 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')}`;
try {
// Ensure a file is uploaded
if (!req.file) {
return res.status(400).json({ err: "No file uploaded" });
}
const api = Cord.ConfigService.get('api');
// Compute SHA-256 hash of the file buffer
const hashFn = crypto.createHash("sha256");
hashFn.update(req.file.buffer);
const digest = `0x${hashFn.digest("hex")}`;

const docProof = await Vc.getCordProofForDigest(
digest as `0x${string}`,
Expand All @@ -312,8 +318,8 @@ export async function documentHashOnChain(
console.log(`✅ Statement element registered - ${statement1}`);

return res.status(200).json({ result: statement1 });
} catch (error) {
} catch (error:any) {
console.log('errr: ', error);
return res.status(400).json({ err: error });
return res.status(400).json({ err: error.message ? error.message : error });
}
}
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ import {
} from './controller/credential_controller';
import { generateDid, resolveDid } from './controller/did_controller';
import app from './server';
import multer from "multer";

const { PORT } = process.env;

const credentialRouter = express.Router({ mergeParams: true });
const schemaRouter = express.Router({ mergeParams: true });
const didRouter = express.Router({ mergeParams: true });
const upload = multer({
storage: multer.memoryStorage(),
limits: { fileSize: 3 * 1024 * 1024 }, // 3MB limit
});

credentialRouter.post('/', async (req, res) => {
return await issueVC(req, res);
Expand Down Expand Up @@ -50,14 +55,23 @@ app.use('/api/v1/schema', schemaRouter);
app.use('/api/v1/cred', credentialRouter);
app.use('/api/v1/did', didRouter);

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

app.get('/:id/did.json', async (req, res) => {
return await resolveDid(req, res);
});

app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
if (err instanceof multer.MulterError) {
if (err.code === "LIMIT_FILE_SIZE") {
return res.status(400).json({ err: "❌ File size exceeds 3MB limit!" });
}
}
next(err);
});

app.get('/*', async (req, res) => {
return res.json({
message: 'check https://docs.dhiway.com/api for details of the APIs',
Expand Down
6 changes: 6 additions & 0 deletions src/types/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Request } from "express";
import { File } from "multer";

export interface MulterRequest extends Request {
file?: File;
}

0 comments on commit ca66974

Please sign in to comment.