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

Issuing NFT completed

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