-
Notifications
You must be signed in to change notification settings - Fork 3
/
electionPoll.py
144 lines (110 loc) · 5.25 KB
/
electionPoll.py
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
import BlindSig as bs
import websocket
import thread
import time
import os
import hashlib
from tkinter import *
import random
class poll:
def __init__(self, ws):
self.ws = ws
self.signer = bs.Signer()
self.publicKey = self.signer.getPublicKey()
self.n = self.publicKey['n']
self.e = self.publicKey['e']
def poll_response(self, poll_answer, eligble_answer):
if (poll_answer == 0): poll_answer = 2;
if (eligble_answer == 0): eligble_answer = "n";
if (eligble_answer == 1): eligble_answer = "y";
l = random.randint(1,self.n)
message = poll_answer
concat_message = str(message) + str(l)
voter = bs.Voter(self.n, eligble_answer)
message_hash = hashlib.sha256(concat_message).hexdigest()
message_hash = int(message_hash,16)
blindMessage = voter.blindMessage(message_hash, self.n, self.e)
self.ws.send("Blinded message: " + str(blindMessage))
signedBlindMessage = self.signer.signMessage(blindMessage, voter.getEligibility())
if signedBlindMessage == None:
self.ws.send("INELIGIBLE VOTER....VOTE NOT AUTHORIZED!")
else:
self.ws.send("Signed blinded message: " + str(signedBlindMessage))
signedMessage = voter.unwrapSignature(signedBlindMessage, self.n)
decodedMessage = str(message)
verificationStatus = bs.verifySignature(message, l ,signedMessage, self.e, self.n)
self.ws.send("Signature: " + str(signedMessage))
self.ws.send("Decoded message: " + str(decodedMessage))
self.ws.send("Hashed message: " + str(hashlib.sha256(str(message)+str(l)).hexdigest()))
self.ws.send("Verification status: " + str(verificationStatus))
class poll_machine:
def __init__(self):
self.ws = websocket.WebSocketApp("ws://localhost:8000",
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close)
self.p = poll(self.ws)
self.master = Tk()
self.master.configure(background='yellow')
self.var_poll = IntVar()
self.var_answer = IntVar()
self.question_poll = Label(self.master, text="Is Bitcoin cool?")
self.yesBox_poll = Radiobutton(self.master, text="Yes", variable=self.var_poll, value=1)
self.noBox_poll = Radiobutton(self.master, text="No", variable=self.var_poll, value=0)
self.question_eligible = Label(self.master, text="Are you eligible to vote?")
self.yesBox_eligible = Radiobutton(self.master, text="Yes", variable=self.var_answer, value=1)
self.noBox_eligible = Radiobutton(self.master, text="No", variable=self.var_answer, value=0)
self.submitButton = Button(self.master, text='Submit', command=self.make_vote)
self.pollLabel = Label(self.master, text="Welcome to the Poll Booth")
self.takePollButton = Button(self.master, text='Take Poll', command=self.reset_poll)
def on_message(self,ws, message):
pass
def on_error(self,ws, error):
print error
def on_close(self,ws):
print "### closed ###"
def on_open(self,ws):
self.master.wm_title("Election Poll Demo")
self.master.geometry('200x200')
self.pollLabel.grid(row=0, sticky=W, padx=10, pady=4)
self.takePollButton.grid(row=1, sticky=W, padx=62)
mainloop()
def make_vote(self):
self.p.poll_response(self.var_poll.get(),self.var_answer.get())
self.question_poll.grid_remove()
self.yesBox_poll.grid_remove()
self.noBox_poll.grid_remove()
self.question_eligible.grid_remove()
self.yesBox_eligible.grid_remove()
self.noBox_eligible.grid_remove()
self.submitButton.grid_remove()
if self.var_answer.get() == 0:
root = Tk()
root.wm_title("Unsuccessful Vote")
root.geometry('200x100')
label = Label(root, text="Please try again!").grid(row=0, sticky=W)
root.configure(background='red')
else:
root = Tk()
root.wm_title("Successful Vote")
root.geometry('200x100')
label = Label(root, text="Thanks for voting!").grid(row=0, sticky=W)
root.configure(background='green')
self.pollLabel.grid(row=0, sticky=W, padx=10, pady=4)
self.takePollButton.grid(row=1, sticky=W, padx=62)
def reset_poll(self):
self.question_poll.grid(row=0, sticky=W, padx=50, pady=4)
self.yesBox_poll.grid(row=1, sticky=W, padx=75)
self.noBox_poll.grid(row=2, sticky=W, padx=75)
self.question_eligible.grid(row=3, sticky=W, padx=20, pady=4)
self.yesBox_eligible.grid(row=4, sticky=W, padx=75)
self.noBox_eligible.grid(row=5, sticky=W, padx=75)
self.submitButton.grid(row=6, sticky=W, pady=4, padx=62)
self.pollLabel.grid_remove()
self.takePollButton.grid_remove()
def main(self):
#websocket.enableTrace(True)
self.ws.on_open = self.on_open
self.ws.run_forever()
pm = poll_machine()
pm.main()