Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
22_23-J 16
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
22_23-J 16
22_23-J 16
Commits
3915b167
Commit
3915b167
authored
Apr 16, 2023
by
Kiridena I.T.K_IT19981840
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'NFT-endpoint-changes-2' into 'master'
Nft endpoint changes 2 See merge request
!27
parents
8100d282
6f793922
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
428 additions
and
96 deletions
+428
-96
ExpressBackend/route/patient-routes/nft-route.js
ExpressBackend/route/patient-routes/nft-route.js
+287
-95
ExpressBackend/services/getIssuerKeyPairFromDB.js
ExpressBackend/services/getIssuerKeyPairFromDB.js
+27
-0
ExpressBackend/services/manageDataForAsset.js
ExpressBackend/services/manageDataForAsset.js
+43
-0
ExpressBackend/services/paymentOperation.js
ExpressBackend/services/paymentOperation.js
+50
-0
ExpressBackend/services/trustlineCreator.js
ExpressBackend/services/trustlineCreator.js
+1
-1
ExpressBackend/services/updateNFTRequest.js
ExpressBackend/services/updateNFTRequest.js
+20
-0
No files found.
ExpressBackend/route/patient-routes/nft-route.js
View file @
3915b167
...
...
@@ -13,6 +13,11 @@ const RSAEncrypt = require("./../../services/rsaEncoder");
const
GenerateAESKey
=
require
(
"
./../../services/aesKeyGenerator
"
);
const
EncryptionService
=
require
(
"
./../../services/encryptionservice
"
);
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
;
...
...
@@ -320,112 +325,299 @@ router.post("/", async (req, res) => {
.
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
:
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
)
=>
{
// 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,
// });
// });
// }
// }
// );
//get issuer key pair from DB
const
issuerKeyPair
=
await
GetIssuerPair
(
decryptedPatientKey
,
"
patients
"
);
//create the trustline with the issuer account from patient
CreateTrustline
(
decryptedAssetCode
,
DecryptWithServerKey
(
issuerKeyPair
.
issuerKey
),
decryptedPatientSeed
)
.
then
((
result
)
=>
{
//Manage data operation from issuer account
DoManageData
(
DecryptWithServerKey
(
issuerKeyPair
.
issuerSeed
),
decryptedAssetCode
,
ipfsContentCID
)
.
then
((
success
)
=>
{
if
(
success
)
{
console
.
log
(
"
NFT manage data submitted from issuer
"
);
//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
(
"
Error when generating PDF :
"
+
errorWhenGeneratingPDF
"
NFT manage data not submitted from issuer
"
);
console
.
log
(
"
/patient/createnft - NFT creation and sharing failed
"
);
return
res
.
status
(
500
).
json
({
message
:
"
Error when generating PDF :
"
+
errorWhenGeneratingPDF
,
message
:
"
NFT manage data not submitted from issuer
"
,
});
}
})
.
catch
((
errorWhenSubmittingIssuerManageData
)
=>
{
console
.
log
(
"
Error when submitting issuer manage data :
"
+
errorWhenSubmittingIssuerManageData
);
console
.
log
(
"
/patient/createnft - NFT creation and sharing failed
"
);
return
res
.
status
(
500
).
json
({
message
:
"
Error when submitting issuer manage data :
"
+
errorWhenSubmittingIssuerManageData
,
});
}
}
);
});
})
.
catch
((
errorWhenCreatingPatientTrustLine
)
=>
{
console
.
log
(
"
Error when creating the patient - issuer trustline :
"
+
errorWhenCreatingPatientTrustLine
);
console
.
log
(
"
/patient/createnft - NFT creation and sharing failed
"
);
return
res
.
status
(
500
).
json
({
message
:
"
Error when creating the patient - issuer trustline :
"
+
errorWhenCreatingPatientTrustLine
,
});
});
//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
(
...
...
ExpressBackend/services/getIssuerKeyPairFromDB.js
0 → 100644
View file @
3915b167
const
MongoClient
=
require
(
"
mongodb
"
).
MongoClient
;
async
function
GetIssuerPair
(
publicKey
,
collectionName
)
{
const
uri
=
process
.
env
.
DATABASE_URL
;
const
client
=
new
MongoClient
(
uri
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
,
});
try
{
await
client
.
connect
();
const
collection
=
client
.
db
().
collection
(
collectionName
);
const
result
=
await
collection
.
findOne
({
publickey
:
publicKey
});
if
(
!
result
)
{
throw
new
Error
(
"
No record found for the provided public key
"
);
}
return
{
issuerKey
:
result
.
issuerpk
,
issuerSeed
:
result
.
issuersk
,
};
}
finally
{
await
client
.
close
();
}
}
module
.
exports
=
GetIssuerPair
;
ExpressBackend/services/manageDataForAsset.js
0 → 100644
View file @
3915b167
const
StellarSdk
=
require
(
"
stellar-sdk
"
);
require
(
"
dotenv
"
).
config
();
const
server
=
new
StellarSdk
.
Server
(
process
.
env
.
STELLARTESTNET
);
async
function
DoManageData
(
sourceSeed
,
assetCode
,
ipfsHash
)
{
try
{
// Load source account details
const
sourceKeypair
=
StellarSdk
.
Keypair
.
fromSecret
(
sourceSeed
);
const
account
=
await
server
.
loadAccount
(
sourceKeypair
.
publicKey
());
// Create a manage data operation with assetCode as name and ipfsHash as value
const
operation
=
StellarSdk
.
Operation
.
manageData
({
name
:
assetCode
,
value
:
ipfsHash
,
});
// Build the transaction
const
transaction
=
new
StellarSdk
.
TransactionBuilder
(
account
,
{
fee
:
StellarSdk
.
BASE_FEE
,
networkPassphrase
:
StellarSdk
.
Networks
.
TESTNET
,
})
.
addOperation
(
operation
)
.
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 NFT issuer manage data :
${
transactionResult
.
hash
}
`
);
return
true
;
}
catch
(
error
)
{
console
.
error
(
"
Error when submitting NFT manage data
"
,
error
);
return
false
;
}
}
module
.
exports
=
DoManageData
;
ExpressBackend/services/paymentOperation.js
0 → 100644
View file @
3915b167
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
;
ExpressBackend/services/trustlineCreator.js
View file @
3915b167
...
...
@@ -17,7 +17,7 @@ async function CreateTrustline(assetCode, issuerKey, sourceAccountSeed) {
.
addOperation
(
StellarSdk
.
Operation
.
changeTrust
({
asset
:
new
StellarSdk
.
Asset
(
assetCode
,
issuerKey
),
limit
:
"
1
"
,
limit
:
"
0.000000
1
"
,
})
)
.
setTimeout
(
30
)
...
...
ExpressBackend/services/updateNFTRequest.js
0 → 100644
View file @
3915b167
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
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment