-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethereum.py
63 lines (52 loc) · 1.44 KB
/
ethereum.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
#!/usr/bin/env python3
import json
import sys
from web3.auto import w3
CONTRACT = "<INSERT CONTRACT ADDRESS HERE>"
ABI = json.loads('<INSERT ABI HERE>')
ACCOUNT = "<INSERT ACCOUNT ADDRESS HERE>"
PASSWORD = "<INSERT ACCOUNT PASSWORD HERE>"
UNLOCK_DURATION = 5 * 60
def unlockAccount():
w3.personal.unlockAccount(ACCOUNT, PASSWORD, UNLOCK_DURATION)
def storeHash(hash):
c = w3.eth.contract(address=CONTRACT, abi=ABI)
try:
tx_hash = c.functions.storeHash(hash).transact({"from": ACCOUNT})
return tx_hash.hex()[2:]
except:
return -1
def verifyHash(hash):
c = w3.eth.contract(address=CONTRACT, abi=ABI)
try:
return c.functions.getHash(hash).call()
except:
return -1
input = sys.stdin.read()
input = json.loads(input)
if "operation" not in data:
raise Exception("Operation was not specified.")
elif input["operation"] == "store":
unlockAccount()
operationResult = storeHash(input["block_hash"])
result = {}
if operationResult == -1:
result["success"] = False
else:
result["success"] = True
result["contract"] = CONTRACT
result["tx_hash"] = operationResult
print(json.dumps(result))
elif input["operation"] == "verify":
operationResult = verifyHash(input["block_hash"])
result = {}
if operationResult == -1:
result = {}
result["success"] = False
else:
result["success"] = True
result["contract"] = CONTRACT
result["timestamp"] = operationResult
print(json.dumps(result))
else:
raise Exception("Invalid operation specified.")