-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (40 loc) · 1.21 KB
/
main.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
from flask import Flask, request
from flask_cors import CORS
import requests
import json
from app.Blockchain import Blockchain
import hashlib
server = 'https://e-voting-blockchain-website.herokuapp.com/api'
app = Flask(__name__)
CORS(app)
blockchain = Blockchain()
@app.route('/')
def index():
return 'Index'
@app.route('/get_blockchain')
def get_blockchain():
return blockchain.get_blockchain()
@app.route('/cast_vote', methods=['POST'])
def cast_vote():
voter_id = request.form['voter_id']
key = request.form['key']
key_hash = hashlib.sha256(key.encode()).hexdigest()
candidate_id = request.form['candidate_id']
payload = {'voter_id': voter_id, 'key_hash': key_hash}
r = requests.post(server+'/voter_check', data=payload)
response = r.json()
if response['status'] == 1:
blockchain.add_block(candidate_id)
return response
@app.route('/get_result')
def get_result():
chain = json.loads(blockchain.get_blockchain())
chain = chain[1:]
result = {}
for block in chain:
candidate_id = block['candidate_id']
if candidate_id in result:
result[candidate_id] += 1
else:
result[candidate_id] = 1
return result