Skip to content

Commit f815bb4

Browse files
committed
Don't allow empty string for hex values
1 parent b899050 commit f815bb4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

tests/core/contracts/test_contract_call_interface.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,28 @@ def test_call_get_byte_array(arrays_contract, call):
252252
assert result == expected_byte_arr
253253

254254

255+
@pytest.mark.parametrize('args,expected', [([b''], [b'\x00']), (['0x'], [b'\x00'])])
256+
def test_set_byte_array(arrays_contract, call, transact, args, expected):
257+
transact(
258+
contract=arrays_contract,
259+
contract_function='setByteValue',
260+
func_args=[args]
261+
)
262+
result = call(contract=arrays_contract,
263+
contract_function='getByteValue')
264+
265+
assert result == expected
266+
267+
@pytest.mark.parametrize('args', [[''], ['s']])
268+
def test_set_byte_array_with_invalid_args(arrays_contract, transact, args):
269+
with pytest.raises(ValidationError):
270+
transact(
271+
contract=arrays_contract,
272+
contract_function='setByteValue',
273+
func_args=[args]
274+
)
275+
276+
255277
def test_call_get_byte_const_array(arrays_contract, call):
256278
result = call(contract=arrays_contract,
257279
contract_function='getByteConstValue')

web3/_utils/abi.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ def validate_value(cls, value):
147147

148148
class AcceptsHexStrMixin:
149149
def validate_value(self, value):
150-
if is_text(value):
150+
if value == '':
151+
self.invalidate_value(
152+
value,
153+
msg='invalid hex string. To pass in an empty hex string, use "0x".',
154+
)
155+
elif is_text(value):
151156
try:
152157
value = decode_hex(value)
153158
except binascii.Error:

0 commit comments

Comments
 (0)