forked from xujinzheng0610/transactServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchainSetup.py
316 lines (241 loc) · 10.2 KB
/
blockchainSetup.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from web3 import Web3
import json
from eth_typing import (
Address,
BlockNumber,
ChecksumAddress,
HexStr,
)
import hashlib
web3 = Web3(Web3.HTTPProvider("http://localhost:8545"))
accounts = web3.eth.accounts
inspectorAddress = web3.eth.accounts[0]
with open("./blockchain/build/contracts/Project.json") as project:
info_json = json.load(project)
abi = info_json["abi"]
projectContractAddress = '0xDa74afD9737F5f8B607251e5b418b9f2A18F6F94'
projectContract = web3.eth.contract(abi=abi, address=projectContractAddress)
with open("./blockchain/build/contracts/Registration.json") as regist:
info_json = json.load(regist)
abi = info_json["abi"]
registrationContractAddress = '0x241Ec9bDdbd63846554FA14285343671bD37e1de'
registrationContract = web3.eth.contract(abi=abi, address=registrationContractAddress)
with open("./blockchain/build/contracts/Donation.json") as donation:
info_json = json.load(donation)
abi = info_json["abi"]
donationContractAddress = '0x10061ad50958537851BEd1d87acb8eF439123c4F'
donationContract = web3.eth.contract(abi=abi, address=donationContractAddress)
def make_donation(amount, pid, donor):
# To protect user privacy, we will use sha256 to hash the donor' eth address
hash = encrypt_string(donor)
txn = donationContract.functions.makeDonation(amount, pid,hash).transact({'from': donor})
receipt = web3.eth.waitForTransactionReceipt(txn)
print(receipt)
return receipt.transactionHash.hex()
def confirmMoney(amount, pid, charity):
txn = donationContract.functions.confirmMoney(amount, pid).transact({'from': charity})
receipt = web3.eth.waitForTransactionReceipt(txn)
print(receipt)
return receipt.transactionHash.hex()
def registerInspector(address):
print(address)
# print(type(address))
# print(accounts[0])
# print(type(accounts[0]))
txn = registrationContract.functions.registerInspector(address, "inspector").transact({'from': accounts[0]})
receipt = web3.eth.waitForTransactionReceipt(txn)
# print("111")
# print(txn)
# print(type(txn))
# print("222")
# print(receipt)
print(receipt.transactionHash)
print(type(receipt.transactionHash))
# print(receipt['from'])
# print(receipt['to'])
return receipt.transactionHash.hex()
def registerDonor(address, name):
txn = registrationContract.functions.registerDonor(address,name).transact({'from':address})
receipt = web3.eth.waitForTransactionReceipt(txn)
print(receipt)
return receipt.transactionHash.hex()
def approveDonor(donor,inspector):
hash = encrypt_string(donor)
txn = registrationContract.functions.approveDonor(donor, hash).transact({'from':inspector})
receipt = web3.eth.waitForTransactionReceipt(txn)
print(receipt)
return receipt.transactionHash.hex()
def rejectDonor(donor, inspector):
hash = encrypt_string(donor)
txn = registrationContract.functions.rejectDonor(donor,hash).transact({'from': inspector})
receipt = web3.eth.waitForTransactionReceipt(txn)
print(receipt)
return receipt.transactionHash.hex()
def updateDonor(donor, name):
txn = registrationContract.functions.updateDonor(donor, name).transact({'from': donor})
receipt = web3.eth.waitForTransactionReceipt(txn)
print(receipt)
return receipt.transactionHash.hex()
def getDonorDetails(donor):
txn = registrationContract.functions.getOrganizationName(donor).call({'from': donor})
return txn
def registerOrganization(charity, name):
txn = registrationContract.functions.registerOrganization(charity, name).transact({'from': charity})
receipt = web3.eth.waitForTransactionReceipt(txn)
print(receipt)
return receipt.transactionHash.hex()
def approveOrganization(charity, inspector):
txn = registrationContract.functions.approveOrganization(charity).transact({'from': inspector})
receipt = web3.eth.waitForTransactionReceipt(txn)
print(receipt.transactionHash.hex())
return receipt.transactionHash.hex()
def rejectOrganization(charity, inspector):
txn = registrationContract.functions.rejectOrganization(charity).transact({'from': inspector})
receipt = web3.eth.waitForTransactionReceipt(txn)
return receipt.transactionHash.hex()
def updateOrganization(charity, name):
txn = registrationContract.functions.updateOrganization(charity, name).transact({'from': charity})
receipt = web3.eth.waitForTransactionReceipt(txn)
return receipt.transactionHash.hex()
def deleteOrganization(charity):
txn = registrationContract.functions.deleteOrganization(charity).transact({'from': charity})
receipt = web3.eth.waitForTransactionReceipt(txn)
return receipt.transactionHash.hex()
def approvedOrganization(charity):
txn = registrationContract.functions.approvedOrganization(charity).call({'from': charity})
return txn
def getOrganizationName(charity):
txn = registrationContract.functions.getOrganizationName(charity).call({'from': charity})
return txn
def confirmReceiveMoney(donation, charity):
txn = donationContract.functions.confirmReceiveMoney(donation).transact({'from': charity})
receipt = web3.eth.waitForTransactionReceipt(txn)
return receipt.transactionHash.hex()
def registerProject(charity, beneficiaryGainedRatio):
numProjects = registrationContract.functions.numProjects().call()
txn = registrationContract.functions.registerProject(beneficiaryGainedRatio).transact({'from': charity})
receipt = web3.eth.waitForTransactionReceipt(txn)
return receipt.transactionHash.hex(), numProjects
def approveProject(inspector, projectId):
int_id = int(projectId)
txn = registrationContract.functions.approveProject(int_id).transact({'from': inspector})
receipt = web3.eth.waitForTransactionReceipt(txn)
return receipt.transactionHash.hex()
def rejectProject(inspector, projectId):
int_id = int(projectId)
txn = registrationContract.functions.rejectProject(int_id).transact({'from': inspector})
receipt = web3.eth.waitForTransactionReceipt(txn)
return receipt.transactionHash.hex()
def stopProject(inspector, projectId):
int_id = int(projectId)
txn = registrationContract.functions.stopProject(int_id).transact({'from': inspector})
receipt = web3.eth.waitForTransactionReceipt(txn)
return receipt.transactionHash.hex()
def checkDonorApproval(txn_hash,donor):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = registrationContract.events.DonorApproval().processReceipt(receipt)
# print(logs)
if(logs[0]['args']['donor']==encrypt_string(donor)):
# print(logs[0]['args']['donor'])
return True
return False
except Exception as ex:
print(ex)
return False
def checkDonorReject(txn_hash,donor):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = registrationContract.events.DonorReject().processReceipt(receipt)
# print(logs)
if(logs[0]['args']['donor']==encrypt_string(donor)):
# print(logs[0]['args']['donor'])
return True
return False
except Exception as ex:
print(ex)
return False
def checkCharityApproval(txn_hash, charity):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = registrationContract.events.OrganizationApproval().processReceipt(receipt)
if(logs[0]['args']['organization']==charity):
return True
return False
except Exception as ex:
print(ex)
return False
def checkCharityReject(txn_hash, charity):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = registrationContract.events.OrganizationReject().processReceipt(receipt)
if(logs[0]['args']['organization']==charity):
return True
return False
except Exception as ex:
print(ex)
return False
def checkProjectApproval(txn_hash, project_solidity_id):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = registrationContract.events.ApprovalProject().processReceipt(receipt)
print(logs)
if(logs [0]['args']['projectId']== project_solidity_id):
return True
else:
return False
except Exception as ex:
print(ex)
return False
def checkProjectStop(txn_hash,project_solidity_id):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = registrationContract.events.StopProject().processReceipt(receipt)
print(logs)
if(logs [0]['args']['projectId']== project_solidity_id):
return True
else:
return False
except Exception as ex:
print(ex)
return False
def checkProjectReject(txn_hash,project_solidity_id):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = registrationContract.events.RejectProject().processReceipt(receipt)
print(logs)
if(logs [0]['args']['projectId']== project_solidity_id):
return True
else:
return False
except Exception as ex:
print(ex)
return False
def checkDonation(txn_hash,donor_address):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = donationContract.events.MadeDonation().processReceipt(receipt)
# To protect user privacy, we will use sha256 to hash the donor' eth address
# Here we will hash the donor's eth address to check whether the hashed values are same
if(logs [0]['args']['donor']== encrypt_string(donor_address)):
return True
else:
return False
except Exception as ex:
print(ex)
return False
def checkConfirmation(txn_hash,project_solidity_id,amount):
try:
receipt = web3.eth.getTransactionReceipt(txn_hash)
logs = donationContract.events.MadeConfirmation().processReceipt(receipt)
if(logs [0]['args']['projectId']== project_solidity_id and logs [0]['args']['amount']== int(amount)):
return True
else:
return False
except Exception as ex:
print(ex)
return False
def encrypt_string(hash_string):
sha_signature = \
hashlib.sha256(hash_string.encode()).hexdigest()
return sha_signature