Commit 3b425031 authored by Ishini Kiridena's avatar Ishini Kiridena

Patient registration completed

parent 02330adc
STELLARTESTNET="https://horizon-testnet.stellar.org"
STELLARPUBNET="https://horizon.stellar.org"
DATABASE_URL="mongodb+srv://dbUser:1997Ishini%21@cluster0.lmazo.mongodb.net/research?retryWrites=true&w=majority"
SERVER_PORT=4000
\ No newline at end of file
SERVER_PORT=4000
STELLARPUBLICKEY="GB3U2F6MWAQ43O6J4AIUQXEMTK5XQZWURC3SF4D37DPBFRY4HCZGRBLG"
STELLARSEED="SBUXCZ7F7G5Z3QK3NKM52UWVJWFSLEVVGQOCIEWEJY4DOYMYVOMUTS4B"
AESKEY="QfTjWnZr4u7x!A%D"
\ No newline at end of file
const mongoose = require("mongoose");
const Patient = new mongoose.Schema({
username: String,
password: String,
email: String,
publickey: String,
genesishash: String,
currenthash: String,
timestamp: Number,
});
module.exports = mongoose.model("Patient", Patient);
const mongoose = require("mongoose");
//TODO: add the needed other details to the object
const regRequest = new mongoose.Schema({
email: String,
username: String,
password: String,
});
module.exports = mongoose.model("PatientRegistrationSchema", regRequest);
const mongoose = require("mongoose");
const accountCreate = new mongoose.Schema({
publickey: String,
amount: String,
timestamp: Number,
transactionhash: String,
});
module.exports = mongoose.model("AccountCreated", accountCreate);
This diff is collapsed.
......@@ -15,11 +15,14 @@
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"crypto-js": "^4.1.1",
"express": "^4.18.2",
"mongoose": "^6.8.0"
"mongoose": "^6.8.0",
"stellar-sdk": "^10.4.1"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/crypto-js": "^4.1.1",
"@types/express": "^4.17.15",
"@types/nodemon": "^1.19.2",
"dotenv": "^16.0.3",
......
const express = require("express");
const router = express.Router();
const RegisterRequestSchema = require("./../../model/patient/registration");
require("dotenv").config();
const StellarSDK = require("stellar-sdk");
const DecryptWithServerKey = require("./../../services/decryptionservice");
const EncryptionService = require("./../../services/encryptionservice");
const AccountCreateCollection = require("../../model/stellar/accountCreate");
const UserSchema = require("../../model/patient/patient");
router.post("/", async (req, res) => {
//decrypt user request details
const decryptedEmail = DecryptWithServerKey(req.body.email);
const decryptedPassword = DecryptWithServerKey(req.body.password);
const decryptedUserName = DecryptWithServerKey(req.body.email);
//catching the request
const request = new RegisterRequestSchema({
email: req.body.email,
username: req.body.username,
password: req.body.password,
});
console.log(request);
//get the server key pair
const serverSourceKeyPair = StellarSDK.Keypair.fromSecret(
process.env.STELLARSEED
);
const serverPubKey = serverSourceKeyPair.publicKey();
const server = new StellarSDK.Server(process.env.STELLARTESTNET);
//load server account
const account = await server.loadAccount(serverPubKey);
const fee = await server.fetchBaseFee();
//generate random keypair
const userKeyPair = StellarSDK.Keypair.random();
//build the account funding transaction
const accCreateTransaction = new StellarSDK.TransactionBuilder(account, {
fee,
networkPassphrase: StellarSDK.Networks.TESTNET,
})
.addMemo(StellarSDK.Memo.text("Create account"))
.addOperation(
StellarSDK.Operation.createAccount({
destination: userKeyPair.publicKey(),
startingBalance: "10",
})
)
.setTimeout(30)
.build();
//sign the transaction
accCreateTransaction.sign(serverSourceKeyPair);
//send account creation transaction to BC
try {
const accCreateTxnResult = await server.submitTransaction(
accCreateTransaction
);
console.log("New user account created: HASH : ", accCreateTxnResult.hash);
console.log(
"View account creation transaction at : https://horizon-testnet.stellar.org/transactions/" +
accCreateTxnResult.hash
);
//create the account creation details collection object
const createAccountObj = new AccountCreateCollection({
publickey: userKeyPair.publicKey(),
amount: "10XLM",
timestamp: Date.now(),
transactionhash: accCreateTxnResult.hash,
});
//save the account creation details to the database
try {
const saveInCollection = await createAccountObj.save();
console.log(
"Account creation details saved in the database " + saveInCollection
);
//encrypt user entered details with stellar seed
const encEmail = EncryptionService.EncryptWithStellarSeed(
request.email,
userKeyPair.secret()
);
const encUsername = EncryptionService.EncryptWithStellarSeed(
request.username,
userKeyPair.secret()
);
const encPassword = EncryptionService.EncryptWithStellarSeed(
request.password,
userKeyPair.secret()
);
//get the user keypair
const userSourceKeyPair = StellarSDK.Keypair.fromSecret(
userKeyPair.secret()
);
const userSourcePublicKey = userSourceKeyPair.publicKey();
//load user account
const userAccount = await server.loadAccount(userSourcePublicKey);
//build genesis transaction
const genesisTransaction = new StellarSDK.TransactionBuilder(
userAccount,
{
fee,
networkPassphrase: StellarSDK.Networks.TESTNET,
}
)
.addMemo(StellarSDK.Memo.text("Genesis"))
.addOperation(
StellarSDK.Operation.manageData({
name: "Email Address",
value: encEmail,
})
)
.addOperation(
StellarSDK.Operation.manageData({
name: "Username",
value: encUsername,
})
)
.addOperation(
StellarSDK.Operation.manageData({
name: "Previous",
value: "",
})
)
.setTimeout(30)
.build();
//sign the genesis transaction
genesisTransaction.sign(userSourceKeyPair);
//send the genesis transaction
try {
const genesisTxnResult = await server.submitTransaction(
genesisTransaction
);
console.log(
"Genesis transaction submitted: HASH : ",
genesisTxnResult.hash
);
console.log(
"View genesis transaction at : https://horizon-testnet.stellar.org/transactions/" +
genesisTxnResult.hash
);
//create the user collection object to be added to the DB
const userCollectionObject = new UserSchema({
username: req.body.username,
password: req.body.password,
email: req.body.email,
publickey: userKeyPair.publicKey(),
genesishash: genesisTxnResult.hash,
currenthash: genesisTxnResult.hash,
timestamp: Date.now(),
});
//save the user collection object to the database
try {
const saveUserInCollection = await userCollectionObject.save();
console.log("User details added to the DB " + saveUserInCollection);
console.log("Public key: " + userKeyPair.publicKey());
console.log("Secret: " + userKeyPair.secret());
res.status(200).json({
message: "User details added to the blockchain",
hash: genesisTxnResult.hash,
pubkey: EncryptionService.EncryptWithServerKey(
userKeyPair.publicKey()
),
seed: EncryptionService.EncryptWithServerKey(userKeyPair.secret()),
});
console.log(
"/patient/register - register patient details successful"
);
} catch (error) {
console.log(
"Error when adding user details to the blockchain : " + error
);
res.status(500).json({
message:
"Error when adding user details to the blockchain : " + error,
});
console.log(
"/patient/register - register patient details unsuccessful"
);
}
} catch (error) {
console.log(
"Error when sending genesis transaction to blockchain: " + error
);
res.status(500).json({
message:
"Error when sending genesis transaction to blockchain: " + error,
});
console.log(
"/patient/register - register patient details unsuccessful"
);
}
} catch (error) {
console.log(
"Error when sending account creation transaction details to DB : " +
error
);
res.status(500).json({
message:
"Error when sending account creation transaction details to DB : " +
error,
});
console.log("/patient/register - register patient details unsuccessful");
}
} catch (error) {
console.log(
"Error when sending account creation transaction to blockchain: " + error
);
res.status(500).json({
message:
"Error when sending account creation transaction to blockchain: " +
error,
});
console.log("/patient/register - register patient details unsuccessful");
}
});
module.exports = router;
......@@ -4,7 +4,7 @@ const app = express();
const mongoose = require("mongoose");
var cors = require("cors");
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true });
mongoose.connect(process.env.DATABASE_URL);
const db = mongoose.connection;
db.on("error", (error) => console.error(error));
db.once("open", (error) => console.log("Connected to DB.."));
......@@ -16,6 +16,10 @@ app.use(express.json());
const healthRoute = require("./route/serverHealth");
app.use("/health", healthRoute);
//patient routes
const patientRegister = require("./route/patient-routes/register-route");
app.use("/patient/register", patientRegister);
app.listen(process.env.SERVER_PORT, () =>
console.log("Server started on port " + process.env.SERVER_PORT)
);
const cryptojs = require("crypto-js");
require("dotenv").config();
function DecryptWithServerKey(encrypted) {
var key = cryptojs.enc.Utf8.parse(process.env.AESKEY);
var options = {
mode: cryptojs.mode.CBC,
padding: cryptojs.pad.Pkcs7,
iv: cryptojs.enc.Utf8.parse(process.env.AESKEY),
};
var decrypted = cryptojs.AES.decrypt(encrypted, key, options).toString(
cryptojs.enc.Utf8
);
return decrypted;
}
module.exports = DecryptWithServerKey;
const cryptojs = require("crypto-js");
require("dotenv").config();
function EncryptWithServerKey(originalTxt) {
var key = cryptojs.enc.Utf8.parse(process.env.AESKEY);
var options = {
mode: cryptojs.mode.CBC,
padding: cryptojs.pad.Pkcs7,
iv: cryptojs.enc.Utf8.parse(process.env.AESKEY),
};
var encryptedTxt = cryptojs.AES.encrypt(originalTxt, key, options).toString();
return encryptedTxt;
}
function EncryptWithStellarSeed(originalText, seed) {
var key = cryptojs.enc.Utf8.parse(seed);
var options = {
mode: cryptojs.mode.CBC,
padding: cryptojs.pad.Pkcs7,
iv: cryptojs.enc.Utf8.parse(seed),
};
var encryptedTxt = cryptojs.AES.encrypt(
originalText,
key,
options
).toString();
return encryptedTxt;
}
module.exports = { EncryptWithServerKey, EncryptWithStellarSeed };
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