Commit 6f793922 authored by Ishini Kiridena's avatar Ishini Kiridena

completed changes on the patient-practitioner NFT

parent 32f39bc7
......@@ -16,6 +16,8 @@ const AssetKey = require("./../../model/stellar/assetkeys");
const CreateTrustline = require("./../../services/trustlineCreator");
const GetIssuerPair = require("./../../services/getIssuerKeyPairFromDB");
const DoManageData = require("./../../services/manageDataForAsset");
const SendPayment = require("./../../services/paymentOperation");
const UpdateNFTRequestStatus = require("./../../services/updateNFTRequest");
let currentHash;
let genesisHash;
let isGenesisPassed;
......@@ -386,7 +388,7 @@ router.post("/", async (req, res) => {
.then((result) => {
//Manage data operation from issuer account
DoManageData(
issuerKeyPair.issuerSeed,
DecryptWithServerKey(issuerKeyPair.issuerSeed),
decryptedAssetCode,
ipfsContentCID
)
......@@ -394,7 +396,128 @@ router.post("/", async (req, res) => {
if (success) {
console.log("NFT manage data submitted from issuer ");
//TODO : payment operation to transfer asset from issuer to patient
//payment operation to transfer asset from issuer to patient
SendPayment(
decryptedAssetCode,
DecryptWithServerKey(issuerKeyPair.issuerSeed),
decryptedPatientKey,
DecryptWithServerKey(issuerKeyPair.issuerKey)
)
.then((success) => {
if (success) {
console.log(
"Payment operation from issuer to patient success"
);
//payment operation to transfer asset from patient to issuer
SendPayment(
decryptedAssetCode,
decryptedPatientSeed,
decryptedPractitionerKey,
DecryptWithServerKey(issuerKeyPair.issuerKey)
)
.then((success) => {
if (success) {
console.log(
"Payment operation from patient to practitioner success"
);
//Update the NFT req collection
UpdateNFTRequestStatus(decryptedAssetCode)
.then((success) => {
if (success) {
res.status(200).json({
message:
"NFT EHR issued successfully",
});
console.log(
"/patient/createnft - NFT creation and sharing success"
);
} else {
console.log(
"NFT Request update failed "
);
console.log(
"/patient/createnft - NFT creation and sharing failed"
);
return res.status(500).json({
message:
"NFT Request update failed ",
});
}
})
.catch((errorWhenUpdatingCollection) => {
console.log(
"Error updating NFT request status : ",
errorWhenUpdatingCollection
);
console.log(
"/patient/createnft - NFT creation and sharing failed"
);
return res.status(500).json({
message:
"Error updating NFT request status : ",
errorWhenUpdatingCollection,
});
});
} else {
console.log(
"NFT payment operation failed from patient to practitioner"
);
console.log(
"/patient/createnft - NFT creation and sharing failed"
);
return res.status(500).json({
message:
"NFT payment operation failed from patient to practitioner",
});
}
})
.catch(
(
errorWhenSendingAssetFromPatientToPractitioner
) => {
console.log(
"Error when sending NFT from patient to practitioner : ",
errorWhenSendingAssetFromPatientToPractitioner
);
console.log(
"/patient/createnft - NFT creation and sharing failed"
);
return res.status(500).json({
message:
"Error when sending NFT from patient to practitioner : ",
errorWhenSendingAssetFromPatientToPractitioner,
});
}
);
} else {
console.log(
"NFT payment operation failed from issuer to patient"
);
console.log(
"/patient/createnft - NFT creation and sharing failed"
);
return res.status(500).json({
message:
"NFT payment operation failed from issuer to patient",
});
}
})
.catch((errorWhenSendingAssetToPatient) => {
console.log(
"Error when submitting issuer to patient manage data : " +
errorWhenSendingAssetToPatient
);
console.log(
"/patient/createnft - NFT creation and sharing failed"
);
return res.status(500).json({
message:
"Error when submitting issuer to patient manage data : " +
errorWhenSendingAssetToPatient,
});
});
} else {
console.log(
"NFT manage data not submitted from issuer "
......@@ -437,64 +560,64 @@ router.post("/", async (req, res) => {
});
});
//load the patient account
const userSourceKeyPair =
StellarSDK.Keypair.fromSecret(decryptedPatientSeed);
const userPublicKey = userSourceKeyPair.publicKey();
const server = new StellarSDK.Server(process.env.STELLARTESTNET);
const account = await server.loadAccount(userPublicKey);
const fee = await server.fetchBaseFee();
const issueNFTTxn = new StellarSDK.TransactionBuilder(account, {
fee,
networkPassphrase: StellarSDK.Networks.TESTNET,
})
.addMemo(StellarSDK.Memo.text("Issue NFT"))
.addOperation(
StellarSDK.Operation.payment({
asset: new StellarSDK.Asset("NFTEHR", decryptedPatientKey),
amount: "1",
destination: decryptedPractitionerKey,
})
)
.addOperation(
StellarSDK.Operation.manageData({
name: "NFTEHR",
value: ipfsContentCID,
})
)
.setTimeout(30)
.build();
// //load the patient account
// const userSourceKeyPair =
// StellarSDK.Keypair.fromSecret(decryptedPatientSeed);
// const userPublicKey = userSourceKeyPair.publicKey();
// const server = new StellarSDK.Server(process.env.STELLARTESTNET);
// const account = await server.loadAccount(userPublicKey);
// const fee = await server.fetchBaseFee();
//sign transaction
issueNFTTxn.sign(userSourceKeyPair);
// const issueNFTTxn = new StellarSDK.TransactionBuilder(account, {
// fee,
// networkPassphrase: StellarSDK.Networks.TESTNET,
// })
// .addMemo(StellarSDK.Memo.text("Issue NFT"))
// .addOperation(
// StellarSDK.Operation.payment({
// asset: new StellarSDK.Asset("NFTEHR", decryptedPatientKey),
// amount: "1",
// destination: decryptedPractitionerKey,
// })
// )
// .addOperation(
// StellarSDK.Operation.manageData({
// name: "NFTEHR",
// value: ipfsContentCID,
// })
// )
// .setTimeout(30)
// .build();
//send transaction to blockchain
try {
const transactionResult = await server.submitTransaction(
issueNFTTxn
);
console.log("NFT issued : HASH : " + transactionResult.hash);
console.log(
"View NFT issued transaction at : https://horizon-testnet.stellar.org/transactions/" +
transactionResult.hash
);
res.status(200).json({
message: "NFT EHR issued successfully",
hash: transactionResult.hash,
});
console.log(
"/patient/createnft - NFT creation and sharing success"
);
} catch (errorWhenIssuingNFT) {
console.log("Error when issuing NFT : " + errorWhenIssuingNFT);
console.log(
"/patient/createnft - NFT creation and sharing failed"
);
return res.status(500).json({
message: "Error when issuing NFT : " + errorWhenIssuingNFT,
});
}
// //sign transaction
// issueNFTTxn.sign(userSourceKeyPair);
// //send transaction to blockchain
// try {
// const transactionResult = await server.submitTransaction(
// issueNFTTxn
// );
// console.log("NFT issued : HASH : " + transactionResult.hash);
// console.log(
// "View NFT issued transaction at : https://horizon-testnet.stellar.org/transactions/" +
// transactionResult.hash
// );
// res.status(200).json({
// message: "NFT EHR issued successfully",
// hash: transactionResult.hash,
// });
// console.log(
// "/patient/createnft - NFT creation and sharing success"
// );
// } catch (errorWhenIssuingNFT) {
// console.log("Error when issuing NFT : " + errorWhenIssuingNFT);
// console.log(
// "/patient/createnft - NFT creation and sharing failed"
// );
// return res.status(500).json({
// message: "Error when issuing NFT : " + errorWhenIssuingNFT,
// });
// }
})
.catch((errorWhenAddingToIPFS) => {
console.log(
......
......@@ -16,8 +16,8 @@ async function DoManageData(sourceSeed, assetCode, ipfsHash) {
// Build the transaction
const transaction = new StellarSdk.TransactionBuilder(account, {
fee,
networkPassphrase: StellarSdk.Networks.PUBLIC,
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(operation)
.setTimeout(30)
......
const StellarSdk = require("stellar-sdk");
require("dotenv").config();
const server = new StellarSdk.Server(process.env.STELLARTESTNET);
async function SendPayment(
assetCode,
sourceSeed,
destinationPublicKey,
issuerPk
) {
try {
// Load source account details
const sourceKeypair = StellarSdk.Keypair.fromSecret(sourceSeed);
const sourceAccount = await server.loadAccount(sourceKeypair.publicKey());
// Load destination account details
const destinationAccount = await server.loadAccount(destinationPublicKey);
// Create the payment operation
const paymentOperation = StellarSdk.Operation.payment({
destination: destinationPublicKey,
asset: new StellarSdk.Asset(assetCode, issuerPk),
amount: "0.0000001",
});
// Build the transaction
const transaction = new StellarSdk.TransactionBuilder(sourceAccount, {
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(paymentOperation)
.setTimeout(30)
.build();
// Sign the transaction with the source account keypair
transaction.sign(sourceKeypair);
// Submit the transaction to the Stellar network
const transactionResult = await server.submitTransaction(transaction);
// Log the transaction hash
console.log(`Transaction hash for payment : ${transactionResult.hash}`);
return true;
} catch (error) {
console.error(error);
return false;
}
}
module.exports = SendPayment;
const NFTRequest = require("./../model/stellar/nftRequests"); // assuming "nftRequest" is the name of the Mongoose model
async function UpdateNFTRequestStatus(assetcode) {
try {
const filter = { assetcode: assetcode };
const update = { status: "Completed" };
const options = { new: true }; // Return the updated document
const updatedRequest = await NFTRequest.findOneAndUpdate(
filter,
update,
options
);
return !!updatedRequest; // convert updatedRequest to boolean value
} catch (error) {
console.error(error);
return false;
}
}
module.exports = UpdateNFTRequestStatus;
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