Commit 047439a7 authored by Ishini Kiridena's avatar Ishini Kiridena

Issuing NFT completed

parent f521cdff
...@@ -272,59 +272,124 @@ router.post("/", async (req, res) => { ...@@ -272,59 +272,124 @@ router.post("/", async (req, res) => {
}); });
//add the EHR content to IPFS and get the hash //add the EHR content to IPFS and get the hash
ipfs ipfs
.add(htmlString) .add(htmlString.toString())
.then((response) => { .then(async (response) => {
let ipfsContentCID = response.toString(); let ipfsContentCID = response.toString();
//!Use this when getting EHR //!Use this when getting EHR
// request.post( request.post(
// { {
// url: "https://ipfs.infura.io:5001/api/v0/cat", url: "https://ipfs.infura.io:5001/api/v0/cat",
// qs: { qs: {
// arg: hash, arg: ipfsContentCID,
// }, },
// headers: { headers: {
// Authorization: Authorization:
// "Basic " + "Basic " +
// Buffer.from( Buffer.from(
// process.env.IPFSPROJECTID + ":" + process.env.IPFSAPIKEY process.env.IPFSPROJECTID + ":" + process.env.IPFSAPIKEY
// ).toString("base64"), ).toString("base64"),
// }, },
// }, },
// (error, response, body) => { (error, response, body) => {
// if (error) { if (error) {
// console.log(error); console.log(error);
// } else { } else {
// //create the PDF document for EHR //create the PDF document for EHR
// let path = `./patientehr/` + decryptedPatientKey + `-EHR.pdf`; let path = `./patientehr/` + decryptedPatientKey + `-EHR.pdf`;
// let pdfOptions = { let pdfOptions = {
// path: path, path: path,
// format: "A4", format: "A4",
// }; };
// htmlpdf htmlpdf
// .create(htmlString, pdfOptions) .create(htmlString, pdfOptions)
// .then((buff) => { .then((buff) => {
// console.log("PDF created"); console.log("PDF created");
// }) })
// .catch((errorWhenGeneratingPDF) => { .catch((errorWhenGeneratingPDF) => {
// console.log( console.log(
// "Error when generating PDF : " + errorWhenGeneratingPDF "Error when generating PDF : " + errorWhenGeneratingPDF
// ); );
// console.log( console.log(
// "/patient/createnft - NFT creation and sharing failed" "/patient/createnft - NFT creation and sharing failed"
// ); );
// return res.status(500).json({ return res.status(500).json({
// message: message:
// "Error when generating PDF : " + "Error when generating PDF : " +
// errorWhenGeneratingPDF, errorWhenGeneratingPDF,
// }); });
// }); });
// } }
// } }
// ); );
//TODO: create NFT request
//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();
//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) => { .catch((errorWhenAddingToIPFS) => {
console.log(errorWhenAddingToIPFS); console.log(
"Error when adding records to IPFS : " + errorWhenAddingToIPFS
);
console.log("/patient/createnft - NFT creation and sharing failed");
return res.status(500).json({
message:
"Error when adding records to IPFS : " + errorWhenAddingToIPFS,
});
}); });
} }
} }
......
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