-
Notifications
You must be signed in to change notification settings - Fork 0
/
BACKEND.py
147 lines (139 loc) · 3.62 KB
/
BACKEND.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
from flask import *
import ipfshttpclient
from web3 import Web3
import json
app = Flask(__name__)
w3 = Web3(Web3.HTTPProvider('https://rpc-mumbai.maticvigil.com/'))
contract_abi = [
{
"inputs": [
{
"internalType": "string",
"name": "fingerprintHash_",
"type": "string"
}
],
"name": "collectFingerprintHash",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "photo_",
"type": "string"
}
],
"name": "collectPhoto",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "voterId_",
"type": "string"
}
],
"name": "collectVoterID",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getFingerprintHash",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getPhoto",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getVoterID",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
]
contract_address='0x98747c02E12B28772BBcB18972eef97B71769557'
contract = w3.eth.contract(address=contract_address, abi=contract_abi)
@app.route('/', methods=['GET'])
def home():
return render_template('home.html')
@app.route('/about', methods=['GET'])
def about():
return render_template('about.html')
@app.route('/login', methods=['GET'])
def login():
# dummy login code that logs in no matter what input is provided
return redirect('/data_collection')
fingerprint=""
voter_id=""
photo=""
@app.route('/data_collection', methods=['GET'])
def data_collection():
if request.method == 'POST':
fingerprint = request.form['Finger Print']
voter_id = request.form['Voter ID']
photo = request.form['Photo']
return render_template('data_collection.html')
client = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001')
election_day_1_hash = 'QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH'
json_data = client.cat(election_day_1_hash)
data = json.loads(json_data)
# Append new data to the JSON file
new_data = {"voter_id":voter_id,"fingerprint":fingerprint, "photo": photo}
data.append(new_data)
# Store the updated JSON file back into IPFS
new_json_data = json.dumps(data)
new_ipfs_hash = client.add_bytes(new_json_data)["Hash"]
print("Updated JSON file stored in IPFS with hash:", new_ipfs_hash)
main_data_cid = 'QmQwr95wEKmB9tmEVCmzth8tXFYkHATTjjiJGrYcfvWnz8'
json_data_main = client.cat(main_data_cid)
data_main=json.loads(json_data_main)
if new_data in data_main:
contract.functions.collectFingerprintHash(fingerprint)
contract.functions.collectVoterID(voter_id)
contract.functions.collectphoto(photo)
fingerprint_after_block = contract.functions.getPhoto()
voter_id_after_block = contract.functions.getFingerprintHash()
photo_after_block = contract.functions.getVoterID()
else:
print("not matched")
election_day_1_hash = 'QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH'
after_json_data = client.cat(election_day_1_hash)
after_data = json.loads(after_json_data)
after_block_json = {"voter_id":voter_id_after_block,"fingerprint":fingerprint_after_block, "photo": photo_after_block}
after_data.append(after_block_json)
print("data sucessfully transfered and stored ")
if __name__ == '__main__':
app.run()