-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmediator_api.py
More file actions
203 lines (161 loc) · 5.49 KB
/
mediator_api.py
File metadata and controls
203 lines (161 loc) · 5.49 KB
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
from typing import Dict, List, Optional
from fastapi.testclient import TestClient
from app.core.settings import ApiMode, Settings
from app.main import get_app
from . import api_utils
_api_client = TestClient(get_app(Settings(API_MODE=ApiMode.MEDIATOR)))
def combine_election_keys(key_name: str, election_public_keys: List[Dict]) -> Dict:
"""
Combine the public keys of all guardians into a single ElGamal public key
for use throughout the election
"""
request = {"key_name": key_name, "election_public_keys": election_public_keys}
return api_utils.send_post_request(_api_client, "key/ceremony/combine", request)
def get_election(election_id: str) -> Dict:
return api_utils.send_get_request(
_api_client, f"election?election_id={election_id}"
)
def submit_election(
election_id: str,
context: Dict,
manifest: Dict,
) -> Dict:
"""
Construct an encryption context for use throughout the election to encrypt and decrypt data
"""
request = {"election_id": election_id, "context": context, "manifest": manifest}
return api_utils.send_put_request(_api_client, "election", request)
def open_election(election_id: str) -> Dict:
return api_utils.send_post_request(
_api_client, f"election/open?election_id={election_id}"
)
def close_election(election_id: str) -> Dict:
return api_utils.send_post_request(
_api_client, f"election/close?election_id={election_id}"
)
def publish_election(election_id: str) -> Dict:
return api_utils.send_post_request(
_api_client, f"election/publish?election_id={election_id}"
)
def build_election_context(
manifest: Dict,
elgamal_public_key: str,
commitment_hash: str,
number_of_guardians: int,
quorum: int,
) -> Dict:
"""
Construct an encryption context for use throughout the election to encrypt and decrypt data
"""
request = {
"manifest": manifest,
"elgamal_public_key": elgamal_public_key,
"commitment_hash": commitment_hash,
"number_of_guardians": number_of_guardians,
"quorum": quorum,
}
return api_utils.send_post_request(_api_client, "election/context", request)
def get_manifest(manifest_hash: str) -> Dict:
return api_utils.send_get_request(
_api_client, f"manifest?manifest_hash={manifest_hash}"
)
def submit_manifest(
manifest: Dict,
) -> Dict:
request = {
"manifest": manifest,
}
return api_utils.send_put_request(_api_client, "manifest", request)
def validate_manifest(
manifest: Dict,
) -> Dict:
request = {
"manifest": manifest,
}
return api_utils.send_post_request(_api_client, "manifest/validate", request)
def cast_ballot(election_id: str, ballot: Dict, manifest: Dict, context: Dict) -> Dict:
request = {
"election_id": election_id,
"ballots": [ballot],
"manifest": manifest,
"context": context,
}
return api_utils.send_post_request(_api_client, "ballot/cast", request)
def spoil_ballot(election_id: str, ballot: Dict, manifest: Dict, context: Dict) -> Dict:
request = {
"election_id": election_id,
"ballots": [ballot],
"manifest": manifest,
"context": context,
}
return api_utils.send_post_request(_api_client, "ballot/spoil", request)
def submit_ballot(
election_id: str, ballot: Dict, manifest: Dict, context: Dict
) -> Dict:
request = {
"election_id": election_id,
"ballots": [ballot],
"manifest": manifest,
"context": context,
}
return api_utils.send_put_request(_api_client, "ballot/submit", request)
def validate_ballot(ballot: Dict, manifest: Dict, context: Dict) -> Dict:
request = {
"ballots": ballot,
"manifest": manifest,
"context": context,
}
return api_utils.send_post_request(_api_client, "ballot/validate", request)
def encrypt_ballots(
ballots: List[Dict],
seed_hash: str,
nonce: Optional[str],
manifest: Dict,
context: Dict,
) -> Dict:
request = {
"ballots": ballots,
"seed_hash": seed_hash,
"nonce": nonce,
"manifest": manifest,
"context": context,
}
return api_utils.send_post_request(_api_client, "ballot/encrypt", request)
def start_tally(ballots: List[Dict], description: Dict, context: Dict) -> Dict:
request = {
"ballots": ballots,
"description": description,
"context": context,
}
return api_utils.send_post_request(_api_client, "tally", request)
def append_tally(
ballots: List[Dict], encrypted_tally: Dict, description: Dict, context: Dict
) -> Dict:
request = {
"ballots": ballots,
"encrypted_tally": encrypted_tally,
"description": description,
"context": context,
}
return api_utils.send_post_request(_api_client, "tally/append", request)
def decrypt_tally(
encrypted_tally: Dict, shares: Dict[str, Dict], description: Dict, context: Dict
) -> Dict:
request = {
"encrypted_tally": encrypted_tally,
"shares": shares,
"description": description,
"context": context,
}
return api_utils.send_post_request(_api_client, "tally/decrypt", request)
def decrypt_ballots(
encrypted_ballots: List[Dict],
shares: Dict[str, List[Dict]],
context: Dict,
) -> Dict:
request = {
"encrypted_ballots": encrypted_ballots,
"shares": shares,
"context": context,
}
return api_utils.send_post_request(_api_client, "ballot/decrypt", request)