-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
108 lines (97 loc) · 2.67 KB
/
main.js
File metadata and controls
108 lines (97 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
let mongoose = require("mongoose");
var express = require("express");
var app = express();
var cors = require("cors");
let connectToMongo = require("./src/database/index");
let moduleutils = require("./src/database/model");
let createCollection = moduleutils.createCollection;
let UserId = null;
function setUserId(userId) {
UserId = userId;
createCollection(UserId);
}
global.getUserId = () => {
return UserId;
};
const port = process.env.PORT || 8080; // here we cant use 5000 because at that react app will be running
let BlockChain = require("./src/blockchain/blockChain");
let blockChain = new BlockChain();
app.use(cors());
app.use(express.json());
connectToMongo(() => {});
global.fetchChain = async (callback) => {
try {
let BlockChainModel = mongoose.model(UserId);
const chain = await BlockChainModel.find({});
callback(chain);
} catch (err) {
console.log(err.message);
res.status(500).send("Internal server error");
}
};
let checkValidchain = (chain) => {
if (!blockChain.isValidChain(chain)) {
console.error("Invalid chain");
return false;
}
return true;
};
let match_credentials = (sender, receiver, amount) => {
if (!blockChain.isValidChain(blockChain.chain)) {
throw new Error("Invalid chain");
}
try {
for (let i = 0; i < blockChain.chain.length; i++) {
let current_block_transition = blockChain.chain[i].transition;
if (
current_block_transition[0].sender === sender &&
current_block_transition[0].recipient === receiver &&
current_block_transition[0].amount === amount
) {
return true;
}
}
return false;
} catch (e) {
console.error(e);
return false;
}
};
let getChain = async (callback) => {
fetchChain(callback);
};
let add_A_Block = (callback, sender, receiver, amount) => {
if (sender === "" || receiver === "" || amount === null) {
console.error("Worng Transcation Credentials");
return;
}
blockChain.addNewBlock(sender, receiver, amount);
setTimeout(() => {
fetchChain(callback);
}, 2000);
};
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.get("/chain", async (req, res) => {
try {
let BlockChainModel = mongoose.model(UserId);
const chain = await BlockChainModel.find({});
res.json(chain);
} catch (err) {
console.log(err.message);
res.status(500).send("Internal server error");
}
});
app.listen(port, (error) => {
if (error) throw error;
console.log(`server started on the ${port}`);
});
let blockchain_db = {
setUserId: setUserId,
checkValidchain: checkValidchain,
match_credentials: match_credentials,
add_A_Block: add_A_Block,
getChain: getChain,
};
module.exports = blockchain_db;