11from os .path import realpath , join
22from 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
713from ..tags import CONFIGURE_ELECTION
814
915router = 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 )
0 commit comments