@@ -21,8 +21,8 @@ def __init__(self, full_signature):
21
21
for sig_item in full_signature :
22
22
encode_types = [f ['type' ] for f in sig_item ['inputs' ]]
23
23
name = sig_item ['name' ]
24
- if '(' in name :
25
- name = name [:name .find ('(' )]
24
+ if b '(' in name :
25
+ name = name [:name .find (b '(' )]
26
26
if name in v :
27
27
i = 2
28
28
while name + utils .to_string (i ) in v :
@@ -31,9 +31,8 @@ def __init__(self, full_signature):
31
31
sys .stderr .write ("Warning: multiple methods with the same "
32
32
" name. Use %s to call %s with types %r"
33
33
% (name , sig_item ['name' ], encode_types ))
34
- sig = name + '(' + ',' .join (encode_types ) + ')'
35
- sig = sig .encode ('utf-8' )
36
- if sig_item ['type' ] == 'function' :
34
+ sig = name + b'(' + b',' .join (encode_types ) + b')'
35
+ if sig_item ['type' ] == b'function' :
37
36
prefix = big_endian_to_int (utils .sha3 (sig )[:4 ])
38
37
decode_types = [f ['type' ] for f in sig_item ['outputs' ]]
39
38
is_unknown_type = len (sig_item ['outputs' ]) and \
@@ -44,7 +43,7 @@ def __init__(self, full_signature):
44
43
"decode_types" : decode_types ,
45
44
"is_unknown_type" : is_unknown_type
46
45
}
47
- elif sig_item ['type' ] == 'event' :
46
+ elif sig_item ['type' ] == b 'event' :
48
47
prefix = big_endian_to_int (utils .sha3 (sig ))
49
48
indexed = [f ['indexed' ] for f in sig_item ['inputs' ]]
50
49
names = [f ['name' ] for f in sig_item ['inputs' ]]
@@ -121,30 +120,30 @@ def decint(n):
121
120
def encode_single (arg , base , sub ):
122
121
normal_args , len_args , var_args = b'' , b'' , b''
123
122
# Unsigned integers: uint<sz>
124
- if base == 'uint' :
123
+ if base == b 'uint' :
125
124
sub = int (sub )
126
125
i = decint (arg )
127
126
assert 0 <= i < 2 ** sub , "Value out of bounds: %r" % arg
128
127
normal_args = zpad (encode_int (i ), 32 )
129
128
# Signed integers: int<sz>
130
- elif base == 'int' :
129
+ elif base == b 'int' :
131
130
sub = int (sub )
132
131
i = decint (arg )
133
132
assert - 2 ** (sub - 1 ) <= i < 2 ** sub , "Value out of bounds: %r" % arg
134
133
normal_args = zpad (encode_int (i % 2 ** sub ), 32 )
135
134
# Unsigned reals: ureal<high>x<low>
136
- elif base == 'ureal' :
135
+ elif base == b 'ureal' :
137
136
high , low = [int (x ) for x in sub .split ('x' )]
138
137
assert 0 <= arg < 2 ** high , "Value out of bounds: %r" % arg
139
138
normal_args = zpad (encode_int (arg * 2 ** low ), 32 )
140
139
# Signed reals: real<high>x<low>
141
- elif base == 'real' :
140
+ elif base == b 'real' :
142
141
high , low = [int (x ) for x in sub .split ('x' )]
143
142
assert - 2 ** (high - 1 ) <= arg < 2 ** (high - 1 ), \
144
143
"Value out of bounds: %r" % arg
145
144
normal_args = zpad (encode_int ((arg % 2 ** high ) * 2 ** low ), 32 )
146
145
# Strings
147
- elif base == 'string' :
146
+ elif base == b 'string' :
148
147
if not is_string (arg ):
149
148
raise Exception ("Expecting string: %r" % arg )
150
149
# Fixed length: string<sz>
@@ -157,7 +156,7 @@ def encode_single(arg, base, sub):
157
156
len_args = zpad (encode_int (len (arg )), 32 )
158
157
var_args = arg
159
158
# Hashes: hash<sz>
160
- elif base == 'hash' :
159
+ elif base == b 'hash' :
161
160
assert int (sub ) and int (sub ) <= 32
162
161
if isinstance (arg , int ):
163
162
normal_args = zpad (encode_int (arg ), 32 )
@@ -168,8 +167,8 @@ def encode_single(arg, base, sub):
168
167
else :
169
168
raise Exception ("Could not parse hash: %r" % arg )
170
169
# Addresses: address (== hash160)
171
- elif base == 'address' :
172
- assert sub == ''
170
+ elif base == b 'address' :
171
+ assert sub == b ''
173
172
if isinstance (arg , int ):
174
173
normal_args = zpad (encode_int (arg ), 32 )
175
174
elif len (arg ) == 20 :
@@ -184,43 +183,43 @@ def encode_single(arg, base, sub):
184
183
def process_type (typ ):
185
184
# Crazy reg expression to separate out base type component (eg. uint),
186
185
# size (eg. 256, 128x128, none), array component (eg. [], [45], none)
187
- regexp = '([a-z]*)([0-9]*x?[0-9]*)((\[[0-9]*\])*)'
186
+ regexp = b '([a-z]*)([0-9]*x?[0-9]*)((\[[0-9]*\])*)'
188
187
base , sub , arr , _ = re .match (regexp , typ ).groups ()
189
- arrlist = re .findall ('\[[0-9]*\]' , arr )
190
- assert len ('' .join (arrlist )) == len (arr ), \
188
+ arrlist = re .findall (b '\[[0-9]*\]' , arr )
189
+ assert len (b '' .join (arrlist )) == len (arr ), \
191
190
"Unknown characters found in array declaration"
192
191
# Only outermost array can be var-sized
193
192
for a in arrlist [:- 1 ]:
194
193
assert len (a ) > 2 , "Inner arrays must have fixed size"
195
194
# Check validity of string type
196
- if base == 'string' :
197
- assert re .match ('^[0-9]*$' , sub ), \
195
+ if base == b 'string' :
196
+ assert re .match (b '^[0-9]*$' , sub ), \
198
197
"String type must have no suffix or numerical suffix"
199
198
assert len (sub ) or len (arrlist ) == 0 , \
200
199
"Cannot have an array of var-sized strings"
201
200
# Check validity of integer type
202
- elif base == 'uint' or base == 'int' :
203
- assert re .match ('^[0-9]+$' , sub ), \
201
+ elif base == b 'uint' or base == b 'int' :
202
+ assert re .match (b '^[0-9]+$' , sub ), \
204
203
"Integer type must have numerical suffix"
205
204
assert 8 <= int (sub ) <= 256 , \
206
205
"Integer size out of bounds"
207
206
assert int (sub ) % 8 == 0 , \
208
207
"Integer size must be multiple of 8"
209
208
# Check validity of real type
210
- elif base == 'ureal' or base == 'real' :
211
- assert re .match ('^[0-9]+x[0-9]+$' , sub ), \
209
+ elif base == b 'ureal' or base == b 'real' :
210
+ assert re .match (b '^[0-9]+x[0-9]+$' , sub ), \
212
211
"Real type must have suffix of form <high>x<low>, eg. 128x128"
213
- high , low = [int (x ) for x in sub .split ('x' )]
212
+ high , low = [int (x ) for x in sub .split (b 'x' )]
214
213
assert 8 <= (high + low ) <= 256 , \
215
214
"Real size out of bounds (max 32 bytes)"
216
215
assert high % 8 == 0 and low % 8 == 0 , \
217
216
"Real high/low sizes must be multiples of 8"
218
217
# Check validity of hash type
219
- elif base == 'hash' :
220
- assert re .match ('^[0-9]+$' , sub ), \
218
+ elif base == b 'hash' :
219
+ assert re .match (b '^[0-9]+$' , sub ), \
221
220
"Hash type must have numerical suffix"
222
221
# Check validity of address type
223
- elif base == 'address' :
222
+ elif base == b 'address' :
224
223
assert sub == '' , "Address cannot have suffix"
225
224
return base , sub , arrlist
226
225
@@ -232,7 +231,7 @@ def encode_any(arg, base, sub, arrlist):
232
231
return encode_single (arg , base , sub )
233
232
# Variable-sized arrays
234
233
if arrlist [- 1 ] == '[]' :
235
- if base == 'string' and sub == '' :
234
+ if base == b 'string' and sub == b '' :
236
235
raise Exception ('Array of dynamic-sized items not allowed: %r'
237
236
% arg )
238
237
o = b''
@@ -243,7 +242,7 @@ def encode_any(arg, base, sub, arrlist):
243
242
return zpad (encode_int (len (arg )), 32 ), b'' , o
244
243
# Fixed-sized arrays
245
244
else :
246
- if base == 'string' and sub == '' :
245
+ if base == b 'string' and sub == b '' :
247
246
raise Exception ('Array of dynamic-sized items not allowed' )
248
247
sz = int (arrlist [- 1 ][1 :- 1 ])
249
248
assert isinstance (arg , list ), "Expecting array: %r" % arg
@@ -273,11 +272,11 @@ def encode_abi(types, args):
273
272
274
273
def is_varsized (base , sub , arrlist ):
275
274
return (len (arrlist ) and arrlist [- 1 ] == '[]' ) or \
276
- (base == 'string' and sub == '' )
275
+ (base == b 'string' and sub == b '' )
277
276
278
277
279
278
def getlen (base , sub , arrlist ):
280
- if base == 'string' and not len (sub ):
279
+ if base == b 'string' and not len (sub ):
281
280
sz = 1
282
281
else :
283
282
sz = 32
@@ -288,22 +287,22 @@ def getlen(base, sub, arrlist):
288
287
289
288
290
289
def decode_single (data , base , sub ):
291
- if base == 'address' :
290
+ if base == b 'address' :
292
291
return encode_hex (data [12 :])
293
- elif base == 'string' or base == 'hash' :
292
+ elif base == b 'string' or base == b 'hash' :
294
293
return data [:int (sub )] if len (sub ) else data
295
- elif base == 'uint' :
294
+ elif base == b 'uint' :
296
295
return big_endian_to_int (data )
297
- elif base == 'int' :
296
+ elif base == b 'int' :
298
297
o = big_endian_to_int (data )
299
298
return (o - 2 ** int (sub )) if o >= 2 ** (int (sub ) - 1 ) else o
300
- elif base == 'ureal' :
301
- high , low = [int (x ) for x in sub .split ('x' )]
299
+ elif base == b 'ureal' :
300
+ high , low = [int (x ) for x in sub .split (b 'x' )]
302
301
return big_endian_to_int (data ) * 1.0 / 2 ** low
303
- elif base == 'real' :
304
- high , low = [int (x ) for x in sub .split ('x' )]
302
+ elif base == b 'real' :
303
+ high , low = [int (x ) for x in sub .split (b 'x' )]
305
304
return (big_endian_to_int (data ) * 1.0 / 2 ** low ) % 2 ** high
306
- elif base == 'bool' :
305
+ elif base == b 'bool' :
307
306
return bool (int (data .encode ('hex' ), 16 ))
308
307
309
308
0 commit comments