-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
326 lines (246 loc) · 9.2 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var fs = require('fs');
var express = require('express');
var Web3 = require('web3');
var solc = require('solc');
var util = require('util');
var bodyparser = require('body-parser');
const utf8ToHex = Web3.utils.utf8ToHex;
const hexToUtf8 = Web3.utils.hexToUtf8;
// (function (_accounts) {
// accounts = _accounts;
// });
let gas = 1000000;
let votingAbi = undefined;
let votingBin = undefined;
let identityAbi = undefined;
let identityBin = undefined;
var accounts = undefined;
let web3 = undefined;
var votingContracts = [];
var identityContracts = {};
function setUp() {
let source = fs.readFileSync('./contracts/contracts/Voting.sol', 'UTF-8');
let compiled = solc.compile(source);
votingBin = compiled.contracts[':Voting'].bytecode;
identityBin = compiled.contracts[':IdentityStore'].bytecode;
util.log(`>>>>> setup - Bytecode: ${votingBin}`);
util.log(`>>>>> setup - ABI: ${compiled.contracts[':Voting'].interface}`);
votingAbi = JSON.parse(compiled.contracts[':Voting'].interface);
identityAbi = JSON.parse(compiled.contracts[':IdentityStore'].interface);
web3 = new Web3(Web3.givenProvider || "ws://localhost:7545");
web3.eth.getAccounts().then((_accounts) => {
accounts = _accounts.map(account => account.toLowerCase());
util.log('accounts: ' + accounts)
});
util.log('>>>>> setup - Completed !!! ');
}
function initApi(response) {
let data = {
from: undefined,
};
web3.eth.getAccounts()
.then(accounts => {
util.log(`>>>>> initApi - Accounts: ${accounts}`);
data.from = accounts[0];
data.accounts = accounts;
response.json(data);
}).catch(function (ex) {
console.log("******error\n");
console.log(ex);
});
}
function createIdentities(req, res) {
let contract = new web3.eth.Contract(identityAbi);
let data = {
address: undefined
};
let addresses = req.body.addresses.map(addr => addr.toLowerCase());
let name = utf8ToHex(req.body.name);
util.log(name);
util.log(addresses);
util.log(`Creating Identity Contract - Unlocking ${accounts[0]} account`);
web3.eth.personal.unlockAccount(accounts[0], 'contract-creator')
.then(result => {
util.log('>>>>> Ready to deploy Identity contract');
contract.deploy({
data: '0x' + identityBin,
arguments: [addresses, name]
})
.send({
from: accounts[0],
gas: gas
})
.on('receipt', receipt => {
util.log(`Identity contract successfully deployed @ address: ${receipt.contractAddress}`);
data.address = receipt.contractAddress;
identityContracts[req.body.name] = receipt.contractAddress;
res.json(data);
});
}, error => {
util.log(`***** Dealer account unlock error - ${error}`);
});
}
function getIdentities(req, res){
let data = {
identities : identityContracts
}
res.json(data);
}
function createVoting(request, response) {
let contract = new web3.eth.Contract(votingAbi);
let data = {
_candidates: request.body.candidates.map(utf8ToHex),
_idStoreAddress: request.body.groupAddress,
_votesPerVoter: request.body.votesPerVoter,
from: accounts[0]
};
util.log(`>>>>> create Voting contract - Unlocking ${accounts[0]} account`);
web3.eth.personal.unlockAccount(accounts[0], 'contract-creator')
.then(result => {
util.log(`>>>>> create Voting contract - Is contract creator account unlocked ? ${result}`);
util.log('>>>>> create Voting - Ready to deploy Voting contract');
contract.deploy({
data: '0x' + votingBin,
arguments: [
request.body.candidates.map(utf8ToHex),
request.body.groupAddress,
request.body.votesPerVoter
]
})
.send({
from: accounts[0],
gas: gas
})
.on('receipt', receipt => {
util.log(`>>>>> create Voting contract - Contract sucessfully deployed @ address: ${receipt.contractAddress}`);
data.address = receipt.contractAddress;
votingContracts.push(receipt.contractAddress.toLowerCase());
response.json(data);
});
}, error => {
util.log(`***** create Voting contract - Dealer account unlock error - ${error}`);
});
}
function getVotings(req,res){
let data = {
votings : votingContracts
}
res.json(data);
}
function getVoting(request, response) {
let address = request.params.address.toLowerCase();
let contract = new web3.eth.Contract(votingAbi);
// util.log(`>>>>> getContractApi - Contract index: ${index}`);
// util.log(`>>>>> getContractApi - Contract address: ${votingContracts[index]}`);
contract.options.address = address;
web3.eth.personal.unlockAccount(accounts[0], '')
.then(result => {
// util.log(`>>>>> getContractApi - Is FROM account unlocked ? ${result}`);
contract.methods.getCandidates().call().then(candidates => {
// util.log(`>>>>> getContractApi - candidateListLength ? ${candidateListLength}`);
var proms = [];
let votes = [];
for (var j=0; j<candidates.length; j++) {
votes.push(0);
}
for (var i = 0; i < candidates.length; i++) {
let index = i;
proms.push(contract.methods.totalVotesFor(candidates[i]).call().then(votesCount => {
votes[index] = votesCount;
})
)
}
Promise.all(proms).then(() => {
data = {
candidates: candidates.map(hexToUtf8),
votes: votes
};
// util.log(`>>>>> getContractApi - candidates ? ${candidates}`);
response.json(data);
})
})
}, error => {
util.log(`***** getContractApi error - ${error}`);
response.status(404);
response.end();
});
}
function voteForCandidate(request, response){
let contact_address = request.params.address.toLowerCase();
let voter_address = request.body.from;
let candidate_to_vote = request.body.candidate;
let contract = new web3.eth.Contract(votingAbi);
contract.options.address = contact_address;
util.log(`***** postVoteApi voting from- ${request.body} `);
util.log(`***** postVoteApi voting from- ${request.body.from} `);
util.log(`***** postVoteApi voting from- ${request.body.candidate} `);
web3.eth.personal.unlockAccount(voter_address, '')
.then(result => {
util.log(`***** postVoteApi voting for- ${candidate_to_vote} , ${utf8ToHex(candidate_to_vote)}`);
contract.methods.voteForCandidate(utf8ToHex(candidate_to_vote)).send({from: voter_address}).then(function(receipt){
util.log(`***** postVoteApi voted- ${receipt}`);
});
response.status(200);
response.end();
}, error => {
util.log(`***** postVoteApi error - ${error}`);
response.status(404);
response.end();
});
}
/*
------------------------------------------- MAIN -------------------------
*/
setUp();
var app = express();
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.use(bodyparser.json());
app.use(function (req, res, next) {
util.log(`Request => url: ${req.url}, method: ${req.method}`);
next();
});
app.use(express.static('./static'));
app.get('/init', function (req, res) {
initApi(res);
});
app.get('/voting/:address', function (req, res) {
getVoting(req, res);
});
app.post('/voting', function (req, res) {
createVoting(req, res);
});
app.get('/voting', function (req, res) {
getVotings(req, res);
});
app.post('/voting/:address', function (req, res) {
voteForCandidate(req, res);
});
app.post('/identities', function (req, res) {
let name = req.body.name;
if (name in identityContracts) {
util.log(name + "already exists");
res.status(409);
res.end();
return;
}
util.log("creating " + name);
createIdentities(req, res);
});
app.get('/identities', function (req, res) {
getIdentities(req, res);
});
app.listen(8080);
util.log('-> Express server @localhost:8080');