-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoterValidation.js
171 lines (131 loc) · 5.53 KB
/
VoterValidation.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
const SHA256 = require('crypto-js/sha256');
class Block{
constructor(index, region, section, voterID, timestamp, data, previousHash = ''){
this.index = index;
this.region = region;
this.section = section;
this.voterID = voterID;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = this.calculateHash();
///--
this.hashvoter = this.calculateVoters();
}
calculateHash(){
//return SHA256(this.index + this.previousHash + this.region + this.section + this.voterID + this.timestamp + JSON.stringify(this.data)).toString();
return SHA256(this.index + this.previousHash + this.region + this.section + this.voterID + this.timestamp + JSON.stringify(this.data) + this.hashvoter).toString();
}
////-> New
calculateVoters(){
return SHA256(this.region + this.section + this.voterID).toString();
}
}
class Blockchain{
constructor(){
this.chain = [this.createGenesisBlock()]
}
createGenesisBlock(){
return new Block(0, 0, 0, 0, "10/07/2018", "Genesis Block","0");
}
getLatestBlock(){
return this.chain[this.chain.length -1];
}
addBlock(newBlock){
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
}
////--> New
addBlockVoter(newVoter){
newVoter.hashvoter = newVoter.calculateVoters();
this.chain.push(newVoter);
}
isChainValid(){
for(let i = 1; i < this.chain.length; i++){
const currentBlock = this.chain[i];
const previousBlock = this.chain[i -1];
if(currentBlock.hash !== currentBlock.calculateHash()){
return "- No. because index " + i + " is not valid.\r\nBlock was not created."
}
if(currentBlock.previousHash !== previousBlock.hash){
return "- No. because index " + i + " is not valid.\r\nBlock was not created."
}
}
return "- Block is valid. \r\nA new Block has been created."
}
// isVoterValid(){
// for(let i = 1; i< this.chain.length; i++){
// const HashVoters = this.chain[i];
// //const HashVotersII = this.filter( Block => Voters.includes(Block.hashvoter));
// console.log('Valid voter: ' + HashVoters.calculateVoters());
// //console.log('Hash Voter: ' + HashVoters.filter( HashVoters.hashvoter , blockvoter.Voters.includes(HashVoters.hashvoter)));
// console.log(JSON.stringify(blockvoter, null,4));
// }
// }
}
//////--- New
class Voters{
constructor(region, section, voterID){
this.region = region;
this.section = section;
this.voterID = voterID;
this.hash = this.checkingVoters();
}
checkingVoters(){
return SHA256(this.region + this.section + this.voterID).toString();
}
}
/////-- Build a class for Voters only.
class BlockchainVoters{
constructor(){
this.chainV = [this.createGenesisVoter()]
}
createGenesisVoter(){
return new Voters("ANY", "ANY","0");
}
/////////-- New
checkVoter(VoterBlock){
VoterBlock.hash = VoterBlock.checkingVoters();
this.chainV.push(VoterBlock);
}
// getLatestVoter(){
// return this.chainV[this.chainV.length -1];
// }
// addBlockVoter(newVoter){
// newVoter.previousHash = this.getLatestBlock().hash;
// newVoter.hash = newVoter.calculateHash();
// this.chain.push(newVoter);
// }
}
let voteblock = new Blockchain();
voteblock.addBlock(new Block(1, "CWB", "090", "075139147", "10/07/2018 - 08:10", {candidate: '01'}));
voteblock.addBlock(new Block(2, "RNP", "087", "052437139", "10/07/2018 - 09:30", {candidate: '02'}));
voteblock.addBlock(new Block(3, "CWB", "087", "000139973", "10/07/2018 - 09:35", {candidate: '01'}));
//voteblock.addBlockVoter(new Block("CWB","090","075139147"));
//voteblock.chain[2].data = {candidate: "01"}
console.log(JSON.stringify(voteblock, null, 4));
console.log('Is blockchain is valid?\r\n' + voteblock.isChainValid());
//////-- New
let blockvoter = new BlockchainVoters();
blockvoter.checkVoter(new Voters("CWB","090","075139147"));
blockvoter.checkVoter(new Voters("RNP","087","052437139"));
//console.log(voteblock.addBlockVoter());
// function isVoterValid(){
// for(let i = 1; i< voteblock.chain.length; i++){
// const HashVoters = voteblock.chain[i];
// const HashVotersII = blockvoter.chainV[i];
// //const HashVotersII = this.filter( Block => Voters.includes(Block.hashvoter));
// console.log('Valid voter: ' + HashVoters.calculateVoters());
// //console.log('Hash Voter: ' , HashVoters.filter( HashVoters.hashvoter => blockvoter.Voters.includes(HashVoters.hashvoter)));
// //console.log(JSON.stringify(blockvoter, null,4));
// if(HashVoters.hashvoter !== HashVotersII.checkingVoters()){
// return "- No. because index " + i + " is not valid.\r\nBlock was not created."
// }
// // if(currentBlock.previousHash !== previousBlock.hash){
// // return "- No. because index " + i + " is not valid.\r\nBlock was not created."
// // }
// }
// }
// isVoterValid();
//console.log(JSON.stringify(blockvoter, null,4));