Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
encode_abi failed in python3 due to non-bytestrings, sha3 also requir…
Browse files Browse the repository at this point in the history
…ed utf-8 string to be encoded before hashing
  • Loading branch information
0xc1c4da committed Apr 30, 2015
1 parent b7a551c commit 18e7d3a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions ethereum/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, full_signature):
" name. Use %s to call %s with types %r"
% (name, sig_item['name'], encode_types))
sig = name + '(' + ','.join(encode_types) + ')'
sig = sig.encode('utf-8')
if sig_item['type'] == 'function':
prefix = big_endian_to_int(utils.sha3(sig)[:4])
decode_types = [f['type'] for f in sig_item['outputs']]
Expand Down Expand Up @@ -118,7 +119,7 @@ def decint(n):

# Encodes a base type
def encode_single(arg, base, sub):
normal_args, len_args, var_args = '', '', ''
normal_args, len_args, var_args = b'', b'', b''
# Unsigned integers: uint<sz>
if base == 'uint':
sub = int(sub)
Expand Down Expand Up @@ -234,31 +235,31 @@ def encode_any(arg, base, sub, arrlist):
if base == 'string' and sub == '':
raise Exception('Array of dynamic-sized items not allowed: %r'
% arg)
o = ''
o = b''
assert isinstance(arg, list), "Expecting array: %r" % arg
for a in arg:
_, n, _ = encode_any(a, base, sub, arrlist[:-1])
o += n
return zpad(encode_int(len(arg)), 32), '', o
return zpad(encode_int(len(arg)), 32), b'', o
# Fixed-sized arrays
else:
if base == 'string' and sub == '':
raise Exception('Array of dynamic-sized items not allowed')
sz = int(arrlist[-1][1:-1])
assert isinstance(arg, list), "Expecting array: %r" % arg
assert sz == len(arg), "Wrong number of elements in array: %r" % arg
o = ''
o = b''
for a in arg:
_, n, _ = encode_any(a, base, sub, arrlist[:-1])
o += n
return '', o, ''
return b'', o, b''


# Encodes ABI data given a prefix, a list of types, and a list of arguments
def encode_abi(types, args):
len_args = ''
normal_args = ''
var_args = ''
len_args = b''
normal_args = b''
var_args = b''
if len(types) != len(args):
raise Exception("Wrong number of arguments!")
for typ, arg in zip(types, args):
Expand Down

0 comments on commit 18e7d3a

Please sign in to comment.