Commit 8100d282 authored by Kiridena I.T.K_IT19981840's avatar Kiridena I.T.K_IT19981840

Merge branch 'NFT-endpoint-changes' into 'master'

aes encoder, rsa encoder and key-code insert done

See merge request !26
parents 852d7861 1fd31c14
const mongoose = require("mongoose");
const AssetKey = new mongoose.Schema({
assetcode: String,
key: String,
});
module.exports = mongoose.model("AssetKey", AssetKey);
......@@ -9,6 +9,10 @@ const htmlpdf = require("node-htmlpdf");
const IPFS = require("ipfs-infura");
const fs = require("fs");
const request = require("request");
const RSAEncrypt = require("./../../services/rsaEncoder");
const GenerateAESKey = require("./../../services/aesKeyGenerator");
const EncryptionService = require("./../../services/encryptionservice");
const AssetKey = require("./../../model/stellar/assetkeys");
let currentHash;
let genesisHash;
let isGenesisPassed;
......@@ -19,6 +23,12 @@ router.post("/", async (req, res) => {
const decryptedPatientKey = req.body.patientkey;
const decryptedPatientSeed = req.body.patientseed;
const decryptedPractitionerKey = req.body.practitioner;
const decryptedPractitionerRsaKey = req.body.practitionerrsa;
const decryptedAssetCode = req.body.assetcode;
const b64DecodedRsa = Buffer.from(
decryptedPractitionerRsaKey,
"base64"
).toString("utf-8");
//get the current hash from DB
try {
......@@ -270,9 +280,43 @@ router.post("/", async (req, res) => {
projectId: process.env.IPFSPROJECTID,
projectSecret: process.env.IPFSAPIKEY,
});
//generate AES key for EHR encryption
const aesKey = GenerateAESKey();
//encrypt HTML string using generated AES key
const encryptedHTML = EncryptionService.EncryptWithStellarSeed(
htmlString,
aesKey
);
console.log(
"---------------------------------Encrypted EHR------------------------------"
);
console.log(encryptedHTML);
console.log(
"----------------------------------------------------------------------------"
);
//encode the AES key using practitioner RSA key
const encodedAesKey = RSAEncrypt(b64DecodedRsa, aesKey);
console.log("AES key (encoded) ", encodedAesKey);
//add AES key and Asset code to DB
const assetObj = new AssetKey({
assetcode: decryptedAssetCode,
key: encodedAesKey,
});
try {
const saveInAssetKeys = await assetObj.save();
console.log("Asset code and Key saved in DB : ", saveInAssetKeys);
//add the EHR content to IPFS and get the hash
ipfs
.add(htmlString.toString())
.add(encryptedHTML.toString())
.then(async (response) => {
let ipfsContentCID = response.toString();
//!Use this when getting EHR
......@@ -295,7 +339,8 @@ router.post("/", async (req, res) => {
console.log(error);
} else {
//create the PDF document for EHR
let path = `./patientehr/` + decryptedPatientKey + `-EHR.pdf`;
let path =
`./patientehr/` + decryptedPatientKey + `-EHR.pdf`;
let pdfOptions = {
path: path,
format: "A4",
......@@ -307,7 +352,8 @@ router.post("/", async (req, res) => {
})
.catch((errorWhenGeneratingPDF) => {
console.log(
"Error when generating PDF : " + errorWhenGeneratingPDF
"Error when generating PDF : " +
errorWhenGeneratingPDF
);
console.log(
"/patient/createnft - NFT creation and sharing failed"
......@@ -385,12 +431,27 @@ router.post("/", async (req, res) => {
console.log(
"Error when adding records to IPFS : " + errorWhenAddingToIPFS
);
console.log("/patient/createnft - NFT creation and sharing failed");
console.log(
"/patient/createnft - NFT creation and sharing failed"
);
return res.status(500).json({
message:
"Error when adding records to IPFS : " + errorWhenAddingToIPFS,
"Error when adding records to IPFS : " +
errorWhenAddingToIPFS,
});
});
} catch (errorWhenInsertingAssetCodeAndKey) {
console.log(
"Error when adding asset code to DB: " +
errorWhenInsertingAssetCodeAndKey
);
console.log("/patient/createnft - NFT creation and sharing failed");
return res.status(500).json({
message:
"Error when adding asset code to DB: " +
errorWhenInsertingAssetCodeAndKey,
});
}
}
}
} catch (errorWhenGettingCurrentHash) {
......
const crypto = require("crypto");
function GenerateAESKey() {
return crypto.randomBytes(32).toString("hex"); // 32 bytes = 256 bits
}
module.exports = GenerateAESKey;
const crypto = require("crypto");
function RSAEncrypt(publicKey, plainString) {
const encrypted = crypto.publicEncrypt(
{ key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING },
Buffer.from(plainString)
);
return encrypted.toString("base64");
}
module.exports = RSAEncrypt;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment