-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (82 loc) · 2.85 KB
/
index.js
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
106
107
108
109
110
111
var express = require('express');
var morgan = require('morgan');
var path = require('path');
var bodyParser = require('body-parser');
var passwordHash = require('password-hash');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var request = require('request');
var fs = require('fs');
Web3 = require('web3')
solc = require('solc')
var app = express();
app.use( bodyParser.json() )
app.use(cookieParser());
app.use(morgan('combined'));
app.use("/", express.static("ui"));
var username;
var password;
app.post('/login', function(req, res) {
console.log(req.body);
username = req.body.username;
password = req.body.password;
var hashedPassword = passwordHash.generate(password);
console.log(hashedPassword);
if (username == "admin" && password == "password") {
res.status(200).send({ message: hashedPassword});
} else {
res.status(500).send({ message: 'error' });
}
});
app.post('/auth', function(req, res) {
var cookie_pass = req.cookies['auth'];
if (passwordHash.verify('password', cookie_pass)) {
res.status(200).send({ message: hashedPassword});
} else {
res.status(500).send({ message: 'error' });
}
});
app.get('/',function(req,res){
var cookie_pass = req.cookies['auth'];
if (passwordHash.verify('password', cookie_pass)) {
res.sendFile(path.join(__dirname, 'ui', 'app.html'));
} else {
console.log('ok');
}
});
app.get('/app', function(req, res){
var cookie_pass = req.cookies['auth'];
var cookie_otp = req.cookies['show'];
if (passwordHash.verify('password', cookie_pass) && cookie_otp != null) {
//res.sendFile(path.join(__dirname, 'ui', 'clist.html'));
res.redirect('/info');
} else if (cookie_otp == null && passwordHash.verify('password', cookie_pass)) {
res.sendFile(path.join(__dirname, 'ui', 'app.html'));
}
else {
res.redirect('/');
}
});
// app.post('/getaddress',function(req,res){
// });
app.get('/info', function(req, res){
var cookie_pass = req.cookies['auth'];
var cookie_otp = req.cookies['show'];
if (cookie_pass == null || cookie_pass == '' || cookie_otp == null || cookie_otp == '') {
res.redirect('/app');
} else {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
code = fs.readFileSync('Voting.sol').toString()
compiledCode = solc.compile(code)
abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface)
VotingContract = web3.eth.contract(abiDefinition)
byteCode = compiledCode.contracts[':Voting'].bytecode
deployedContract = VotingContract.new(['Sanat','Aniket','Mandar','Akshay'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
contractInstance = VotingContract.at(deployedContract.address)
res.sendFile(path.join(__dirname, 'ui', 'clist.html'));
}
});
var port = 8080;
app.listen(8080, function () {
console.log(`app listening on port ${port}!`);
});