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

encode_abi failed in python3 due to non-bytestrings, sha3 also required ... #254

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in order to not introduce unicode in the first place, this should better be:
sig = name + b'(' + b','.join(encode_types) + b')'

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