Commit 42fac8fb authored by Kiridena I.T.K_IT19981840's avatar Kiridena I.T.K_IT19981840

Merge branch 'upload-encrypted-weekly-report-to-IPFS' into 'master'

IPFS recording of weekly practitioner records

See merge request !20
parents 1a13b9ee 05c2472c
......@@ -4,7 +4,9 @@ const DecryptWithServerKey = require("./../../services/decryptionservice");
const PractitionerPatientCount = require("../../model/practitioner/practitioner-patient");
const StellarSDK = require("stellar-sdk");
const { TimeoutInfinite } = require("stellar-sdk");
const IPFS = require("ipfs-infura");
const LIMIT = 64;
const EncryptionService = require("./../../services/encryptionservice");
router.post("/", async (req, res) => {
// const decryptedPatientPubKey = DecryptWithServerKey(req.body.patient);
......@@ -38,144 +40,145 @@ router.post("/", async (req, res) => {
const server = new StellarSDK.Server(process.env.STELLARTESTNET);
const account = await server.loadAccount(userPublicKey);
const fee = await server.fetchBaseFee();
let operations = [];
//check the length of the record string
if (records.length > LIMIT) {
for (let i = 0; i < records.length; i += LIMIT) {
// recordChunks.push(records.substring(i, i + LIMIT));
operations.push(
StellarSDK.Operation.manageData({
name: i.toString(),
value: records.substring(i, i + LIMIT),
})
);
}
} else {
//Build one manage data operation
operations.push(
StellarSDK.Operation.manageData({
name: "0",
value: records.toString(),
})
);
}
//encrypt the weekly records using practitioner BC secret key
const encryptedRec = EncryptionService.EncryptWithStellarSeed(
req.body.practitionerseed
);
//build the transaction
const transaction = new StellarSDK.TransactionBuilder(account, {
fee,
networkPassphrase: StellarSDK.Networks.TESTNET,
})
.addMemo(StellarSDK.Memo.text("Record " + currentCount.toString()))
.addOperation(
StellarSDK.Operation.manageData({
name: "Length",
value: operations.length.toString(),
})
)
.addOperation(
StellarSDK.Operation.manageData({
name: "Timestamp",
value: timestampSent,
})
)
.addOperation(
StellarSDK.Operation.manageData({
name: "Previous",
value: previousHash,
})
)
.addOperation(
StellarSDK.Operation.manageData({
name: "Count",
value: currentCount.toString(),
})
)
.setTimeout(30)
.build();
//loop through the operations array and append to the transaction
// for (let i = 0; i < operations.length; i++) {
// transaction
// .addOperation(operations[i])
// .setTimeout(TimeoutInfinite)
// .build();
// }
//create the IPFS instance
const ipfs = new IPFS({
host: process.env.IPFSHOST,
port: process.env.IPFSPORT,
protocol: process.env.IPFSPROTOCOL,
projectId: process.env.IPFSPROJECTID,
projectSecret: process.env.IPFSAPIKEY,
});
//sign the transaction
transaction.sign(userSourceKeyPair);
//add the weekly records to IPFS
ipfs
.add(encryptedRec)
.then(async (response) => {
let ipfsContentCID = response.toString();
//send the transaction to the blockchain
try {
const transactionResult = await server.submitTransaction(transaction);
console.log(
"Weekly record transaction added to blockchain : HASH : " +
transactionResult.hash
);
console.log(
"View weekly record transaction at : https://horizon-testnet.stellar.org/transactions/" +
transactionResult.hash
);
try {
//update the counter collection
const updateCounter = await PractitionerPatientCount.updateOne(
{
practitionerpubkey: req.body.practitionerpubkey,
patientpubkey: req.body.patient,
},
{
currenthash: transactionResult.hash,
currentcounter: currentCount,
}
);
//build the transaction
const transaction = new StellarSDK.TransactionBuilder(account, {
fee,
networkPassphrase: StellarSDK.Networks.TESTNET,
})
.addMemo(StellarSDK.Memo.text("Record " + currentCount.toString()))
.addOperation(
StellarSDK.Operation.manageData({
name: "Timestamp",
value: timestampSent,
})
)
.addOperation(
StellarSDK.Operation.manageData({
name: "Previous",
value: previousHash,
})
)
.addOperation(
StellarSDK.Operation.manageData({
name: "CID",
value: ipfsContentCID.toString(),
})
)
.setTimeout(30)
.build();
if (!updateCounter) {
console.log("Failed to update the hash : " + updateCounter);
//sign the transaction
transaction.sign(userSourceKeyPair);
//send the transaction to the blockchain
try {
const transactionResult = await server.submitTransaction(
transaction
);
console.log(
"/practitioner/records - adding practitioner records failed"
"Weekly record transaction added to blockchain : HASH : " +
transactionResult.hash
);
return res.status(500).json({
message: "Failed to update the hash : " + updateCounter,
});
} else {
console.log(
"Weekly record transaction saved to database : " + updateCounter
"View weekly record transaction at : https://horizon-testnet.stellar.org/transactions/" +
transactionResult.hash
);
try {
//update the counter collection
const updateCounter = await PractitionerPatientCount.updateOne(
{
practitionerpubkey: req.body.practitionerpubkey,
patientpubkey: req.body.patient,
},
{
currenthash: transactionResult.hash,
currentcounter: currentCount,
}
);
if (!updateCounter) {
console.log("Failed to update the hash : " + updateCounter);
console.log(
"/practitioner/records - adding practitioner records failed"
);
return res.status(500).json({
message: "Failed to update the hash : " + updateCounter,
});
} else {
console.log(
"Weekly record transaction saved to database : " +
updateCounter
);
res.status(200).json({
message: "Weekly record transaction saved to blockchain",
hash: transactionResult.hash,
});
console.log(
"/practitioner/records - adding practitioner records success"
);
}
} catch (errorWhenUpdatingCounter) {
console.log(
"Error when updating patient-practitioner count in DB : " +
errorWhenUpdatingCounter
);
res.status(500).json({
message:
"Error when updating patient-practitioner count in DB : " +
errorWhenUpdatingCounter,
});
console.log(
"/practitioner/records - adding practitioner records failed"
);
}
} catch (errorWhenSendingTransactionToBlockchain) {
console.log(
"Error when sending weekly records to blockchain : " +
errorWhenSendingTransactionToBlockchain
);
res.status(200).json({
message: "Weekly record transaction saved to blockchain",
hash: transactionResult.hash,
res.status(500).json({
message:
"Error when sending weekly records to blockchain : " +
errorWhenSendingTransactionToBlockchain,
});
console.log(
"/practitioner/records - adding practitioner records success"
"/practitioner/records - adding practitioner records failed"
);
}
} catch (errorWhenUpdatingCounter) {
})
.catch((errorWhenAddingToIPFS) => {
console.log(
"Error when updating patient-practitioner count in DB : " +
errorWhenUpdatingCounter
"Error when adding weekly record to IPFS : " + errorWhenAddingToIPFS
);
res.status(500).json({
message:
"Error when updating patient-practitioner count in DB : " +
errorWhenUpdatingCounter,
});
console.log(
"/practitioner/records - adding practitioner records failed"
);
}
} catch (errorWhenSendingTransactionToBlockchain) {
console.log(
"Error when sending weekly records to blockchain : " +
errorWhenSendingTransactionToBlockchain
);
res.status(500).json({
message:
"Error when sending weekly records to blockchain : " +
errorWhenSendingTransactionToBlockchain,
res.status(400).json({
message:
"Error when adding weekly record to IPFS : " +
errorWhenAddingToIPFS,
});
});
console.log(
"/practitioner/records - adding practitioner records failed"
);
}
}
} catch (errorWhenGettingCount) {
console.log(
......
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