Skip to content

Commit 371c358

Browse files
authored
✨ New Election context endpoint (#101)
1 parent 5165710 commit 371c358

File tree

3 files changed

+81
-49
lines changed

3 files changed

+81
-49
lines changed

app/api/v1/mediator/election.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from os.path import realpath, join
22
from typing import Any
3-
from electionguard.election import ElectionDescription, ElectionConstants
4-
from fastapi import APIRouter, HTTPException
5-
6-
3+
from electionguard.election import (
4+
ElectionDescription,
5+
ElectionConstants,
6+
make_ciphertext_election_context,
7+
)
8+
from electionguard.group import ElementModP
9+
from electionguard.serializable import read_json_object, write_json_object
10+
from fastapi import APIRouter, Body
11+
12+
from ..models import ElectionContextRequest
713
from ..tags import CONFIGURE_ELECTION
814

915
router = APIRouter()
@@ -22,17 +28,22 @@ def get_election_constants() -> Any:
2228
return constants.to_json_object()
2329

2430

25-
@router.get("/description", tags=[CONFIGURE_ELECTION])
26-
def get_default_election_description() -> Any:
31+
@router.post("/context", tags=[CONFIGURE_ELECTION])
32+
def build_election_context(request: ElectionContextRequest = Body(...)) -> Any:
2733
"""
28-
Return a default election description
34+
Build a CiphertextElectionContext for a given election
2935
"""
30-
with open(DESCRIPTION_FILE, READ) as description_file:
31-
result = description_file.read()
32-
description = ElectionDescription.from_json(result)
33-
if not description:
34-
raise HTTPException(
35-
status_code=500,
36-
detail="Default description not found",
37-
)
38-
return description
36+
description: ElectionDescription = ElectionDescription.from_json_object(
37+
request.description
38+
)
39+
elgamal_public_key: ElementModP = read_json_object(
40+
request.elgamal_public_key, ElementModP
41+
)
42+
number_of_guardians = request.number_of_guardians
43+
quorum = request.quorum
44+
45+
context = make_ciphertext_election_context(
46+
number_of_guardians, quorum, elgamal_public_key, description.crypto_hash()
47+
)
48+
49+
return write_json_object(context)

app/api/v1/models/election.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
from typing import Any
2+
from .base import Base
23

3-
__all__ = ["ElectionDescription", "CiphertextElectionContext"]
4+
__all__ = ["CiphertextElectionContext", "ElectionContextRequest", "ElectionDescription"]
45

56
ElectionDescription = Any
67

78
CiphertextElectionContext = Any
9+
10+
11+
class ElectionContextRequest(Base):
12+
"""
13+
A request to build an Election Context for a given election
14+
"""
15+
16+
description: ElectionDescription
17+
elgamal_public_key: str
18+
number_of_guardians: int
19+
quorum: int

tests/ElectionGuard Web Api.postman_collection.json

Lines changed: 41 additions & 32 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)