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

Commit 0e817cb

Browse files
author
Jan Xie
committed
fix dynamic bytes decoding in abi
1 parent 8fdd2e3 commit 0e817cb

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

ethereum/abi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,12 @@ def decode_single(typ, data):
373373
return encode_hex(data[12:])
374374
elif base == 'hash':
375375
return data[32-int(sub):]
376-
elif base == 'string' or base == 'bytes' or base == 'hash':
377-
return data[:int(sub)] if len(sub) else data
376+
elif base == 'string' or base == 'bytes':
377+
if len(sub):
378+
return data[:int(sub)]
379+
else:
380+
l = big_endian_to_int(data[0:32])
381+
return data[32:][:l]
378382
elif base == 'uint':
379383
return big_endian_to_int(data)
380384
elif base == 'int':

ethereum/tests/test_abi.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ def test_abi_decode_single_hash():
3434
typ = ['hash', '8', []]
3535
assert b'\x01'*8 == abi.decode_single(typ, abi.encode_single(typ, b'\x01'*8))
3636

37+
def test_abi_decode_single_bytes():
38+
typ = ['bytes', '8', []]
39+
assert (b'\x01\x02' + b'\x00'*6) == abi.decode_single(typ, abi.encode_single(typ, '\x01\x02'))
40+
41+
typ = ['bytes', '', []]
42+
assert b'\x01\x02' == abi.decode_single(typ, abi.encode_single(typ, '\x01\x02'))
43+
3744
# SETUP TESTS IN GLOBAL NAME SPACE
3845
def gen_func(filename, testname, testdata):
3946
return lambda: do_test_state(filename, testname, testdata)

0 commit comments

Comments
 (0)