forked from NilFoundation/zkllvm-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_real_values_and_storage_proof.py
156 lines (123 loc) · 4.28 KB
/
get_real_values_and_storage_proof.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
import json
import sys
from web3 import Web3, WebsocketProvider
from web3.middleware import geth_poa_middleware
from eth_utils import (
keccak,
to_hex,
to_bytes,
)
import rlp
from rlp.sedes import (
Binary,
big_endian_int,
)
from trie import (
HexaryTrie,
)
from web3._utils.encoding import (
pad_bytes,
)
from pprint import pprint
# Ethereum node URL
node_url = "wss://eth-mainnet.g.alchemy.com/v2/p7sadjYi_zcgAhMKGvcS-aaksaWkdzdN"
# Contract address and storage slot
#contract_address = "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc"
contract_address = "0xd19d4B5d358258f05D7B411E21A1460D11B0876F"
storage_slot = 0 # Replace with your storage slot number
# Block number (optional, set to 'latest' by default)
block_number = 17973376
# Connect to Ethereum node
w3 = Web3(WebsocketProvider(node_url))
def format_proof_nodes(proof):
trie_proof = []
for rlp_node in proof:
trie_proof.append(rlp.decode(bytes(rlp_node)))
return trie_proof
def verify_eth_get_proof(proof, root):
trie_root = Binary.fixed_length(32, allow_empty=True)
hash32 = Binary.fixed_length(32)
for storage_proof in proof.storageProof:
trie_key = keccak(pad_bytes(b'\x00', 32, storage_proof.key))
root = proof.storageHash
if storage_proof.value == b'\x00':
rlp_value = b''
else:
rlp_value = rlp.encode(storage_proof.value)
assert rlp_value == HexaryTrie.get_from_proof(
root, trie_key, format_proof_nodes(storage_proof.proof)
), f"Failed to verify storage proof {storage_proof.key}"
return True
def get_storage_proof(contract_address, storage_slot, block_number="latest"):
contract = w3.eth.contract(address=contract_address, abi=[]) # Use your contract's ABI
# Get state root of the specified block
block = w3.eth.get_block(block_number)
state_root = block['stateRoot']
proof = w3.eth.get_proof(contract_address, [storage_slot], block.number)
is_valid_proof = verify_eth_get_proof(proof, state_root)
assert(is_valid_proof)
return proof
def bytes_to_cpp_hardcode(buf):
return '{' + ','.join("0x{:x}".format(x) for x in buf) + '}'
def byte_values_to_array_of_ints(val, padding_to):
#returns something like " {'array': [ {'int': '0xf9'}, {'int': '0xe1}, ... ]
arr = []
i = 0
total = len(bytes(val))
for b in range(0, padding_to):
if i < total:
arr.append({'int': hex(int(bytes(val)[i]))})
else:
arr.append({'int': 0})
i +=1
return {'array': arr}
def int_values_to_array_of_ints(val, padding_to):
arr = []
i = 0
total = len(val)
for v in range(0, padding_to):
if i < total:
arr.append({'int': hex(int(val[i]))})
else:
arr.append({'int': 0})
i += 1
return {'array': arr}
if __name__ == "__main__":
proof = get_storage_proof(contract_address, storage_slot, block_number)
block = w3.eth.get_block(block_number)
state_root = block['stateRoot']
print("Block {}".format(block_number))
print("Contract: {}".format(contract_address))
print("Slot: {}".format(storage_slot))
value = proof.storageProof[0].value
print("Value: {}".format(value.hex()))
# part of data passed to inputs
print("Proof data:")
print(" StorageHash: {}".format(proof.storageHash.hex()))
trie_key = keccak(pad_bytes(b'\x00', 32, proof.storageProof[0].key))
print(" TrieKey: {}".format(trie_key.hex()))
#proof_dec = proof.storageProof[0].proof[0]
print(" Proof:")
for p in proof.storageProof[0].proof:
print(" {}".format(p.hex()))
proofvs = []
proofls = []
i = 0
for p in proof.storageProof[0].proof:
proofls.append(0)
for b in bytes(p):
proofls[i] += 1
proofvs.append(b)
i += 1
proof_vals = byte_values_to_array_of_ints(proofvs, 4096)
proof_lengths = int_values_to_array_of_ints(proofls, 32)
res = [
byte_values_to_array_of_ints(proof.storageHash, 32),
byte_values_to_array_of_ints(trie_key, 32),
proof_lengths,
proof_vals
]
output_pi = "src/main-input.json"
with open(output_pi, 'w') as f:
sys.stdout = f
print(json.dumps(res, indent=2))