-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacaron_utils.py
233 lines (207 loc) · 6.21 KB
/
macaron_utils.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
import time
from datetime import datetime
from web3 import Web3
known_addresses = {}
# Opcode Defs
opcodes = {
"STOP": 0x0,
"ADD": 0x1,
"MUL": 0x2,
"SUB": 0x3,
"SHA3": 0x20,
"MSTORE": 0x52,
"SLOAD": 0x54,
"SSTORE": 0x55,
"JUMP": 0x56,
"JUMPI": 0x57,
"JUMPDEST": 0x5B,
}
# AST data Defs
validAstTypes = [
"ParameterList",
"ExpressionStatement",
"VariableDeclaration",
"VariableDeclarationStatement",
"Return",
"Assignment",
"Identifier",
"BinaryOperation",
"Literal",
"MemberAccess",
"IndexAccess",
"FunctionCall",
"UnaryOperation",
"Continue",
"Break",
"Conditional",
"CustomInlineAssemblyChild",
]
invalidAstTypes = [
"PragmaDirective",
"ContractDefinition",
"EventDefinition",
"DoWhileStatement",
"WhileStatement",
"ForStatement",
"IfStatement",
"FunctionDefinition",
"PlaceholderStatement",
"InlineAssembly",
]
node_children_names = {
"arguments",
"baseExpression",
"body",
"components",
"condition",
"declarations",
"expression",
"externalReferences",
"falseBody",
"falseExpression",
"modifiers",
"parameters",
"statements",
"nodes",
"leftHandSide",
"rightHandSide",
"leftExpression",
"rightExpression",
"initializationExpression",
"initialValue",
"value",
"trueBody",
"trueExpression",
"indexExpression",
"loopExpression",
"returnParameters",
"subExpression",
"eventCall",
"_codeLength",
"_addr",
}
# Printing Defs
LINE_SPLIT_DELIMETER = "\n"
LINE_LIMIT = 36
color_normal = "\033[0m\033[97m" # WHITE
color_highlight = "\033[30m\033[103m" # BLACK on BRIGHT YELLOW
color_calldata = "\033[30m\033[105m" # BLACK on BRIGHT MAGENTA
color_warning = "\033[30m\033[106m" # BLACK on BRIGHT CYAN
color_note = "\033[30m\033[102m" # BLACK on BRIGHT GREEN
color_error = "\033[30m\033[101m" # BLACK on BRIGHT RED
color_reset = "\033[m" # RESET
def int_to_hex_addr(address):
address = Web3.toHex(address)[2:]
return address.zfill(65 - len(address))
def strip_cbor(bytecode):
assert len(bytecode) % 2 == 0
cbor_size = int(bytecode[len(bytecode) - 4 :], 16)
stripped_bytecode = bytecode[: len(bytecode) - 4 - cbor_size * 2 + 1]
cbor_object = bytecode[len(bytecode) - 4 - cbor_size * 2 :]
return (stripped_bytecode, cbor_object)
def format_calldata(calldata, value, function_db, api):
if calldata == "New Contract Creation":
return calldata
value_formatted = "%4f ETH" % ((value * 1.0) / 10 ** 18)
if value == 0:
value_formatted = "0 ETH"
if len(calldata) == 0:
return f"transfer({value_formatted})"
if value > 0:
value_part = f".value({value_formatted})"
else:
value_part = ""
selector = calldata[:4].hex()
if selector not in function_db:
return "#" + selector
function_data = function_db[selector]
signature_split = function_data["signature"].split("(")
function_name, rest = signature_split
arg_types = rest[:-1]
arg_types = [a for a in arg_types.split(",") if a]
lost = False
arg_format = []
pointer = 4
for i, t in enumerate(arg_types):
if t.endswith("]"):
# is an array
t, multiple_str = t[:-1].split("[")
if multiple_str == "":
multiple = -1
else:
multiple = int(multiple_str)
else:
multiple = 0
if t == "address":
format = lambda a: format_address(a[-40:], api)
elif t.startswith("uint"):
unix_timestamp = time.time()
def format(a):
a = int(a, 16)
if abs(a - unix_timestamp) < unix_timestamp / 10:
# within 10%, treat as timestamp
return datetime.utcfromtimestamp(a).strftime(
"%Y-%m-%d %H:%M:%S UTC"
)
return "%.3g" % a
elif t == "bool":
format = lambda a: "false" if int(a, 16) == 0 else "true"
elif t.startswith("bytes"):
length_str = t[5:]
if length_str:
length = int(length_str)
else:
length = 32
format = lambda a: a[: length * 2 + 1]
else:
lost = True
if lost:
value = f"<{t}>"
else:
if multiple == 0:
# convert arg
calldata_arg_hex = calldata[pointer : pointer + 32].hex()
value = format(calldata_arg_hex)
pointer += 32
elif multiple == -1:
# dynamically-sized arrays
values = []
data_pointer = 4 + int(calldata[pointer : pointer + 32].hex(), 16)
multiple = int(calldata[data_pointer : data_pointer + 32].hex(), 16)
data_pointer += 32
for i in range(multiple):
calldata_arg_hex = calldata[data_pointer : data_pointer + 32].hex()
values.append(format(calldata_arg_hex))
data_pointer += 32
value = "[" + ", ".join(values) + "]"
pointer += 32
else:
# statically-sized arrays
values = []
for i in range(multiple):
calldata_arg_hex = calldata[pointer : pointer + 32].hex()
values.append(format(calldata_arg_hex))
pointer += 32
value = "[" + ", ".join(values) + "]"
if len(function_data["param_names"]) > i:
variable = function_data["param_names"][i] + " = "
else:
variable = ""
arg_format.append(variable + value)
args = ", ".join(arg_format)
return f"{function_name}{value_part}({args})"
def format_address(a, api):
a = a.lower()
if a.startswith("0x"):
a = a[2:]
if a in known_addresses:
ret = known_addresses[a]
if " " in ret:
# braketize for readability
return "<" + ret + ">"
else:
return ret
else:
return api.toChecksumAddress(a)
def print_err(msg):
print(f"{color_error}Error: {msg}{color_normal}.")