Commit 852d7861 authored by Kiridena I.T.K_IT19981840's avatar Kiridena I.T.K_IT19981840

Merge branch 'asset-code-generation-on-request' into 'master'

asset code generation moved to request done

See merge request !25
parents 47e650dd b39f5116
...@@ -8,7 +8,6 @@ const Patient = new mongoose.Schema({ ...@@ -8,7 +8,6 @@ const Patient = new mongoose.Schema({
rsa: String, rsa: String,
genesishash: String, genesishash: String,
currenthash: String, currenthash: String,
keysequence: String,
issuerpk: String, issuerpk: String,
issuersk: String, issuersk: String,
timestamp: String, timestamp: String,
......
...@@ -289,104 +289,75 @@ router.post("/", async (req, res) => { ...@@ -289,104 +289,75 @@ router.post("/", async (req, res) => {
const b64RSApk = btoa(RSAPK); const b64RSApk = btoa(RSAPK);
const b64RSAsk = btoa(RSASK); const b64RSAsk = btoa(RSASK);
var newAssetCode = ""; //generate new account
try {
const result = await GenerateKeyPairAndCreateAccount();
console.log("Issuer Public Key:", result.publicKey);
console.log("Issuer Private Key:", result.privateKey);
//generate NFT sequence NO as asset ID //create the user collection object to be added to the DB
GenerateRandomAssetCode() const userCollectionObject = new UserSchema({
.then(async (assetCode) => { username: decryptedUserName,
newAssetCode = assetCode; password: req.body.password,
console.log("New asset code generated : ", newAssetCode); email: decryptedEmail,
publickey: userKeyPair.publicKey(),
//generate new account rsa: b64RSApk,
try { genesishash: genesisTxnResult.hash,
const result = await GenerateKeyPairAndCreateAccount(); currenthash: genesisTxnResult.hash,
console.log("Issuer Public Key:", result.publicKey); timestamp: timestamp,
console.log("Issuer Private Key:", result.privateKey); issuerpk: EncryptionService.EncryptWithServerKey(result.publicKey),
issuersk: EncryptionService.EncryptWithServerKey(result.privateKey),
//create the user collection object to be added to the DB });
const userCollectionObject = new UserSchema({
username: decryptedUserName,
password: req.body.password,
email: decryptedEmail,
publickey: userKeyPair.publicKey(),
rsa: b64RSApk,
genesishash: genesisTxnResult.hash,
currenthash: genesisTxnResult.hash,
timestamp: timestamp,
keysequence: newAssetCode,
issuerpk: EncryptionService.EncryptWithServerKey(
result.publicKey
),
issuersk: EncryptionService.EncryptWithServerKey(
result.privateKey
),
});
//save the user collection object to the database //save the user collection object to the database
try { try {
const saveUserInCollection = await userCollectionObject.save(); const saveUserInCollection = await userCollectionObject.save();
console.log( console.log("User details added to the DB " + saveUserInCollection);
"User details added to the DB " + saveUserInCollection console.log("Public key: " + userKeyPair.publicKey());
); console.log("Secret: " + userKeyPair.secret());
console.log("Public key: " + userKeyPair.publicKey()); res.status(200).json({
console.log("Secret: " + userKeyPair.secret()); message: "User details added to the blockchain",
res.status(200).json({ hash: genesisTxnResult.hash,
message: "User details added to the blockchain", pubkey: EncryptionService.EncryptWithServerKey(
hash: genesisTxnResult.hash, userKeyPair.publicKey()
pubkey: EncryptionService.EncryptWithServerKey( ),
userKeyPair.publicKey() seed: EncryptionService.EncryptWithServerKey(
), userKeyPair.secret()
seed: EncryptionService.EncryptWithServerKey( ),
userKeyPair.secret() rsapk: EncryptionService.EncryptWithServerKey(b64RSApk),
), rsask: EncryptionService.EncryptWithServerKey(b64RSAsk),
rsapk: EncryptionService.EncryptWithServerKey(b64RSApk), });
rsask: EncryptionService.EncryptWithServerKey(b64RSAsk), console.log(
}); "/patient/register - register patient details successful"
console.log( );
"/patient/register - register patient details successful" } catch (errorWhenAddingUserToDB) {
);
} catch (errorWhenAddingUserToDB) {
console.log(
"Error when adding user details to the database : " +
errorWhenAddingUserToDB
);
res.status(500).json({
message:
"Error when adding user details to the database : " +
errorWhenAddingUserToDB,
});
console.log(
"/patient/register - register patient details unsuccessful"
);
}
} catch (errWhenGeneratingIssuerAccount) {
console.log(
"Error when generating issuer account : " +
errWhenGeneratingIssuerAccount
);
res.status(500).json({
message:
"Error when generating issuer account : " +
errWhenGeneratingIssuerAccount,
});
console.log(
"/patient/register - register patient details unsuccessful"
);
}
})
.catch((errWhenGeneratingAssetCode) => {
console.log( console.log(
"Error when generating asset code : " + errWhenGeneratingAssetCode "Error when adding user details to the database : " +
errorWhenAddingUserToDB
); );
res.status(500).json({ res.status(500).json({
message: message:
"Error when generating asset code : " + "Error when adding user details to the database : " +
errWhenGeneratingAssetCode, errorWhenAddingUserToDB,
}); });
console.log( console.log(
"/patient/register - register patient details unsuccessful" "/patient/register - register patient details unsuccessful"
); );
}
} catch (errWhenGeneratingIssuerAccount) {
console.log(
"Error when generating issuer account : " +
errWhenGeneratingIssuerAccount
);
res.status(500).json({
message:
"Error when generating issuer account : " +
errWhenGeneratingIssuerAccount,
}); });
console.log(
"/patient/register - register patient details unsuccessful"
);
}
} catch (error) { } catch (error) {
console.log( console.log(
"Error when sending genesis transaction to blockchain: " + error "Error when sending genesis transaction to blockchain: " + error
......
const express = require("express"); const express = require("express");
const router = express.Router(); const router = express.Router();
const GetIssuerAndAssetCode = require("./../../services/assetCodeGetterFromDB"); const GetIssuer = require("./../../services/assetCodeGetterFromDB");
const DecryptWithServerKey = require("./../../services/decryptionservice"); const DecryptWithServerKey = require("./../../services/decryptionservice");
const StellarSdk = require("stellar-sdk"); const StellarSdk = require("stellar-sdk");
const CreateTrustline = require("./../../services/trustlineCreator"); const CreateTrustline = require("./../../services/trustlineCreator");
const NFTRequest = require("./../../model/stellar/nftRequests"); const NFTRequest = require("./../../model/stellar/nftRequests");
const GenerateRandomAssetCode = require("./../../services/assetCodeGenerator");
require("dotenv").config(); require("dotenv").config();
router.post("/", async (req, res) => { router.post("/", async (req, res) => {
...@@ -13,52 +14,65 @@ router.post("/", async (req, res) => { ...@@ -13,52 +14,65 @@ router.post("/", async (req, res) => {
const patientKey = req.body.patient; const patientKey = req.body.patient;
const practitionerRSA = req.body.practitionerrsa; const practitionerRSA = req.body.practitionerrsa;
//take the current asset code and the issuer key for the patient from the data base //take the issuer key for the patient from the data base
const assetAndIssuer = await GetIssuerAndAssetCode("patients", patientKey); const issuerRes = await GetIssuer("patients", patientKey);
console.log("AC", assetAndIssuer.assetCode); //Generate the asset code
console.log("IP", DecryptWithServerKey(assetAndIssuer.issuerKey)); GenerateRandomAssetCode()
.then((assetCode) => {
console.log("AC", assetCode);
console.log("IP", DecryptWithServerKey(issuerRes.issuerKey));
const issuerPK = DecryptWithServerKey(issuerRes.issuerKey);
const assetCode = assetAndIssuer.assetCode; CreateTrustline(assetCode, issuerPK, practitionerSK)
const issuerPK = DecryptWithServerKey(assetAndIssuer.issuerKey); .then(async (result) => {
console.log("Trust line created successfully!");
CreateTrustline(assetCode, issuerPK, practitionerSK) //add request to DB
.then(async (result) => { const nftRequestObj = new NFTRequest({
console.log("Trust line created successfully!"); assetcode: assetCode,
//add request to DB issuerpk: issuerPK,
const nftRequestObj = new NFTRequest({ requesterpk: practitionerPK,
assetcode: assetCode, requesterrsa: practitionerRSA,
issuerpk: issuerPK, distributorpk: patientKey,
requesterpk: practitionerPK, status: "Pending",
requesterrsa: practitionerRSA, });
distributorpk: patientKey, try {
status: "Pending", const saveRequest = await nftRequestObj.save();
}); console.log("NFT request saved to database : " + saveRequest);
try { res.status(200).json({
const saveRequest = await nftRequestObj.save(); message: "NFT request saved to database",
console.log("NFT request saved to database : " + saveRequest); });
res.status(200).json({ console.log("/practitioner/requestnft - requesting NFT success");
message: "NFT request saved to database", } catch (errorWhenAddingRequestToDB) {
}); console.log(
console.log("/practitioner/requestnft - requesting NFT success"); "Error when adding NFT request to DB : ",
} catch (errorWhenAddingRequestToDB) { errorWhenAddingRequestToDB
console.log( );
"Error when adding NFT request to DB : ", console.log("/practitioner/requestnft - requesting NFT failed");
errorWhenAddingRequestToDB return res.status(500).json({
); message: "Error when adding NFT request to DB : ",
console.log("/practitioner/requestnft - requesting NFT failed"); errorWhenAddingRequestToDB,
return res.status(500).json({ });
message: "Error when adding NFT request to DB : ", }
errorWhenAddingRequestToDB, })
.catch((error) => {
console.log("Failed to create the trust line : ", error);
console.log("/practitioner/requestnft - requesting NFT failed");
return res.status(500).json({
message: "Failed to create the trust line : ",
error,
});
}); });
}
}) })
.catch((error) => { .catch((errWhenGeneratingAssetCode) => {
console.log("Failed to create the trust line : ", error); console.log(
"Failed to generate asset code : ",
errWhenGeneratingAssetCode
);
console.log("/practitioner/requestnft - requesting NFT failed"); console.log("/practitioner/requestnft - requesting NFT failed");
return res.status(500).json({ return res.status(500).json({
message: "Failed to create the trust line : ", message: "Failed to generate asset code : ",
error, errWhenGeneratingAssetCode,
}); });
}); });
}); });
......
const MongoClient = require("mongodb").MongoClient; const MongoClient = require("mongodb").MongoClient;
async function GetIssuerAndAssetCode(collectionName, publicKey) { async function GetIssuer(collectionName, publicKey) {
const uri = process.env.DATABASE_URL; const uri = process.env.DATABASE_URL;
const client = new MongoClient(uri, { const client = new MongoClient(uri, {
useNewUrlParser: true, useNewUrlParser: true,
...@@ -19,10 +19,9 @@ async function GetIssuerAndAssetCode(collectionName, publicKey) { ...@@ -19,10 +19,9 @@ async function GetIssuerAndAssetCode(collectionName, publicKey) {
return { return {
issuerKey: result.issuerpk, issuerKey: result.issuerpk,
assetCode: result.keysequence,
}; };
} finally { } finally {
await client.close(); await client.close();
} }
} }
module.exports = GetIssuerAndAssetCode; module.exports = GetIssuer;
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