-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.py
196 lines (163 loc) · 7.58 KB
/
common.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
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
"""
Common models and utility functions
"""
import struct
import json
import socket
import config
import em_interface
import bb_interface
class RespError:
"""
represents an error response from the server
"""
def __init__(self, msg):
self.msg = msg
def __str__(self):
return "<RespError: {}>".format(self.msg)
@classmethod
def from_dictionary(cls, params):
msg = params['msg']
return cls(msg)
def to_dictionary(self):
return {'msg': self.msg}
def read_message(conn):
"""
Reads message from a socket
:param conn: the socket to read from
:return: length of the message OR None if message length cannot be read
"""
buf = _read_socket_buf(conn, 4)
if not buf:
print("Error: Unable to read message size")
return None
msg_size = struct.unpack("!i", buf)[0]
print("Length of the message is: {}".format(msg_size))
buf = _read_socket_buf(conn, msg_size)
if not buf:
print("Error: Unable to read message of length {}".format(msg_size))
return None
obj = json.loads(buf.decode("utf-8"), object_hook=_from_json)
return obj
def write_message(conn, obj):
"""
Writes a message to the socket
:param conn: the socket to write to
:param obj: any python object supported by pickle, it will be written to the socket
:return:
"""
msg = json.dumps(obj, default=_to_json).encode('utf-8')
msg_size = len(msg)
buf_to_write = struct.pack("!i", msg_size) + msg
conn.sendall(buf_to_write)
def get_public_key_from_em():
"""
Gets public keys from the EM
:return: public keys
"""
sock_to_em = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_to_em.connect(config.EM_ADDR)
write_message(sock_to_em, em_interface.ReqPublicKeys())
pub_keys = read_message(sock_to_em)
# socket will be closed by the EM server
return pub_keys
# --- Private ---
def _read_socket_buf(conn, n):
"""
Reads buffer from conn up to n bytes
:param conn: the socket to read from
:param n: number of bytes to read
:return: the buffer OR None
"""
buf = b''
while len(buf) < n:
new_data = conn.recv(n - len(buf))
if not new_data:
return None
buf += new_data
return buf
def _to_json(python_object):
"""
converts request and response objects used in the system to python serializable objects
:param python_object: object to convert to json serializable object
:return: json serializable representation of `python_object`
"""
if isinstance(python_object, RespError):
return {'__class__': 'RespError',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, em_interface.ReqPublicKeys):
return {'__class__': 'em_interface.ReqPublicKeys',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, em_interface.RespPublicKeys):
return {'__class__': 'em_interface.RespPublicKeys',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, em_interface.ReqBlindSign):
return {'__class__': 'em_interface.ReqBlindSign',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, em_interface.RespBlindSign):
return {'__class__': 'em_interface.RespBlindSign',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, em_interface.ReqDisplayResults):
return {'__class__': 'em_interface.ReqDisplayResults',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, bb_interface.ReqCastVote):
return {'__class__': 'bb_interface.ReqCastVote',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, bb_interface.RespZKPProvideCommitment):
return {'__class__': 'bb_interface.RespZKPProvideCommitment',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, bb_interface.ReqZKPChallenge):
return {'__class__': 'bb_interface.ReqZKPChallenge',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, bb_interface.RespZKPChallenge):
return {'__class__': 'bb_interface.RespZKPChallenge',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, bb_interface.ReqZKPVerify):
return {'__class__': 'bb_interface.ReqZKPVerify',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, bb_interface.RespCastVoteSuccess):
return {'__class__': 'bb_interface.RespCastVoteSuccess',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, bb_interface.RespVotingClosed):
return {'__class__': 'bb_interface.RespVotingClosed',
'__value__': python_object.to_dictionary()}
elif isinstance(python_object, bb_interface.ReqCloseVoting):
return {'__class__': 'bb_interface.ReqCloseVoting',
'__value__': python_object.to_dictionary()}
raise TypeError(repr(python_object) + ' is not JSON serializable')
def _from_json(json_object):
"""
converts json object to python object
:param json: object to convert from
:return: python object corresponding to json object
"""
if '__class__' in json_object:
if json_object['__class__'] == 'RespError':
return RespError.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'em_interface.ReqPublicKeys':
return em_interface.ReqPublicKeys.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'em_interface.RespPublicKeys':
return em_interface.RespPublicKeys.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'em_interface.ReqBlindSign':
return em_interface.ReqBlindSign.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'em_interface.RespBlindSign':
return em_interface.RespBlindSign.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'em_interface.ReqDisplayResults':
return em_interface.ReqDisplayResults.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'bb_interface.ReqCastVote':
return bb_interface.ReqCastVote.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'bb_interface.RespZKPProvideCommitment':
return bb_interface.RespZKPProvideCommitment.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'bb_interface.ReqZKPChallenge':
return bb_interface.ReqZKPChallenge.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'bb_interface.RespZKPChallenge':
return bb_interface.RespZKPChallenge.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'bb_interface.ReqZKPVerify':
return bb_interface.ReqZKPVerify.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'bb_interface.RespCastVoteSuccess':
return bb_interface.RespCastVoteSuccess.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'bb_interface.RespVotingClosed':
return bb_interface.RespVotingClosed.from_dictionary(json_object['__value__'])
elif json_object['__class__'] == 'bb_interface.ReqCloseVoting':
return bb_interface.ReqCloseVoting.from_dictionary(json_object['__value__'])
return json_object