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