-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathcontract.py
279 lines (222 loc) · 9.77 KB
/
contract.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
"""
Module handling the source unit
"""
import re
from typing import Dict, List, Optional, Union, Tuple, Set, TYPE_CHECKING
import cbor2
from Crypto.Hash import keccak
from crytic_compile.utils.natspec import Natspec
if TYPE_CHECKING:
from crytic_compile.source_unit import SourceUnit
from crytic_compile.utils.naming import combine_filename_name
# pylint: disable=too-many-instance-attributes,too-many-public-methods
class Contract:
"""The Contract class represents a single compiled contract within a source unit
Attributes
----------
source_unit: SourceUnit
A pointer to the associated SourceUnit
contract_name: str
The contract's name
abi: Dict
The application binary interface (ABI) of the contract
init_bytecode: str
The initialization bytecode for the contract
runtime_bytecode: str
The runtime bytecode of the contract
srcmap_init: str
The initialization source mapping of the contract
srcmap_runtime: str
The runtime source mapping of the contract
natspec: Natspec
The NatSpec for the contract
function_hashes: Dict
The contract's function signatures and their associated hashes
events: Dict
The contract's event signatures and their associated hashes
placeholder_set: Set[str]
The set of library placeholders identified in the contract
"""
def __init__(self, source_unit: "SourceUnit", contract_name: str, abi: Dict, init_bytecode: str, runtime_bytecode: str, srcmap_init: str, srcmap_runtime: str, natspec: Natspec):
"""Initialize the Contract class"""
self._source_unit: SourceUnit = source_unit
self._contract_name: str = contract_name
self._abi: Dict = abi
self._init_bytecode: str = init_bytecode
self._runtime_bytecode: str = runtime_bytecode
self._srcmap_init: str = srcmap_init
self._srcmap_runtime: str = srcmap_runtime
self._natspec: Natspec = natspec
self._function_hashes: Dict = self._compute_function_hashes()
self._events: Dict = self._compute_topics_events()
self._placeholder_set: Set[str] = self._compute_placeholder_set()
# TODO: Maybe introduce metadata in a future PR
# region Getters
###################################################################################
###################################################################################
@property
def source_unit(self) -> SourceUnit:
"""Return the SourceUnit associated with this Contract object
Returns:
SourceUnit: Pointer to the associated SourceUnit
"""
return self._source_unit
@property
def contract_name(self) -> str:
"""Return the name of the contract
Returns:
str: Contract name
"""
return self._contract_name
@property
def abi(self) -> Dict:
"""Return the ABI of the contract
Returns:
Dict: ABI
"""
return self._abi
@property
def init_bytecode(self) -> SourceUnit:
"""Return the init bytecode of the contract
Returns:
str: Init bytecode
"""
return self._init_bytecode
@property
def runtime_bytecode(self) -> SourceUnit:
"""Return the runtime bytecode of the contract
Returns:
str: Runtime bytecode
"""
return self._runtime_bytecode
@property
def srcmap_init(self) -> str:
"""Return the init source mapping of the contract
Returns:
str: The initialization source mapping
"""
return self._srcmap_init
@property
def srcmap_runtime(self) -> str:
"""Return the runtime source mapping of the contract
Returns:
str: The runtime source mapping
"""
return self._srcmap_runtime
@property
def natspec(self) -> Natspec:
"""Returns the Natspec associated with the contract
Returns:
Natspec: Natspec of contract
"""
return self._natspec
@property
def function_hashes(self) -> Dict[str, int]:
"""Return a mapping of function signatures to keccak hashes within a contract
Returns:
Dict[str, int]: Mapping of function signature to keccak hash
"""
return self._function_hashes
@property
def events(self) -> Dict[str, Tuple[int, List[bool]]]:
"""Return a mapping of event signatures to keccak hashes within the contract
Returns:
Dict[str, Tuple[int, List[bool]]]: Mapping of event signature to keccak hash in addition to which input parameters are indexed
"""
return self._events
@property
def placeholder_set(self) -> Set(str):
"""Returns any library placeholders found in the contract
Returns:
Set(str): Set of library placeholders
"""
return self._placeholder_set
# endregion
###################################################################################
###################################################################################
# region Internal functions
###################################################################################
###################################################################################
def _compute_placeholder_set(self) -> Set[str]:
"""Returns all library placeholders within the init bytecode of the contract.
If there are different placeholders within the runtime bytecode of a contract, which is true for a compilation platform like Brownie,
then this function will not find those placeholders.
Returns:
Set[str]: This is the list of placeholders identified in the init bytecode of the contract
"""
# Use regex to find __PLACEHOLDER__ strings
init = re.findall(r"__(\$[0-9a-zA-Z]*\$|\w*)__", self.init_bytecode)
return set(init)
def _compute_function_hashes(self) -> Dict[str, int]:
"""Compute the function hashes
Returns:
Dict[str, int]: Returns a dictionary mapping the function signature to the keccak hash as a 256-bit integer
"""
function_hashes: Dict[str, int] = {}
# Iterate through each key in the ABI
for function in self._abi:
function_type = function.get("type", "N/A")
# If the object describes a function
if function_type == "function":
# Grab the name
try:
function_name = function["name"]
except KeyError:
raise KeyError
# Create a comma-delimited string containing all the input arguments
try:
function_args = ",".join([input["type"] for input in function["inputs"]])
except KeyError:
raise KeyError
# Format and hash the function signature
sig = f"{function_name}({function_args})"
sha3_result = keccak.new(digest_bits=256)
sha3_result.update(sig.encode("utf-8"))
# Update mapping
function_hashes[sig] = int("0x" + sha3_result.hexdigest()[:8], 16)
return function_hashes
def _compute_topics_events(self) -> Dict[str, Tuple[int, List[bool]]]:
"""Computes each event's signature, keccak hash, and which parameters are indexed
Returns:
Dict[str, Tuple[int, List[bool]]]: Returns a mapping from event signature to a tuple where the integer is the 256-bit keccak
hash and the list tells you which parameters are indexed
"""
events: Dict[str, Tuple[int, List[bool]]] = {}
# Iterate through each key in the ABI
for event in self._abi:
event_type = event.get("type", "N/A")
# If the object describes an event
if event_type == "event":
# Grab the name
try:
event_name = event["name"]
except KeyError:
raise KeyError
# Create a comma-delimited string containing all the input arguments
try:
event_args = ",".join([input["type"] for input in event["inputs"]])
except KeyError:
raise KeyError
# Figure out which input arguments are indexed
indexed = [input.get("indexed", False) for input in event["inputs"]]
# Format and hash the event signature
sig = f"{event_name}({event_args})"
sha3_result = keccak.new(digest_bits=256)
sha3_result.update(sig.encode("utf-8"))
# Update mapping
events[sig] = (int("0x" + sha3_result.hexdigest()[:8], 16), indexed)
return events
# endregion
###################################################################################
###################################################################################
# region Metadata
###################################################################################
###################################################################################
# TODO: Metadata parsing is broken. Needs to be fixed in a separate PR
def metadata_of(self, name: str) -> Dict[str, Union[str, bool]]:
return None
def remove_metadata(self) -> None:
return None
# endregion
###################################################################################
###################################################################################