From b36f212f72a08a8b323e33efbf903d557510b21b Mon Sep 17 00:00:00 2001 From: Keri Date: Thu, 8 Aug 2019 21:53:34 -0600 Subject: [PATCH] Don't allow empty string for hex values --- .../contracts/test_contract_call_interface.py | 23 +++++++++++++++++++ web3/_utils/abi.py | 7 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/core/contracts/test_contract_call_interface.py b/tests/core/contracts/test_contract_call_interface.py index af9269513c..16c06749b8 100644 --- a/tests/core/contracts/test_contract_call_interface.py +++ b/tests/core/contracts/test_contract_call_interface.py @@ -252,6 +252,29 @@ def test_call_get_byte_array(arrays_contract, call): assert result == expected_byte_arr +@pytest.mark.parametrize('args,expected', [([b''], [b'\x00']), (['0x'], [b'\x00'])]) +def test_set_byte_array(arrays_contract, call, transact, args, expected): + transact( + contract=arrays_contract, + contract_function='setByteValue', + func_args=[args] + ) + result = call(contract=arrays_contract, + contract_function='getByteValue') + + assert result == expected + + +@pytest.mark.parametrize('args', ([''], ['s'])) +def test_set_byte_array_with_invalid_args(arrays_contract, transact, args): + with pytest.raises(ValidationError): + transact( + contract=arrays_contract, + contract_function='setByteValue', + func_args=[args] + ) + + def test_call_get_byte_const_array(arrays_contract, call): result = call(contract=arrays_contract, contract_function='getByteConstValue') diff --git a/web3/_utils/abi.py b/web3/_utils/abi.py index 851fcd5dca..e30808eaa2 100644 --- a/web3/_utils/abi.py +++ b/web3/_utils/abi.py @@ -147,7 +147,12 @@ def validate_value(cls, value): class AcceptsHexStrMixin: def validate_value(self, value): - if is_text(value): + if value == '': + self.invalidate_value( + value, + msg='invalid hex string. To pass in an empty hex string, use "0x".', + ) + elif is_text(value): try: value = decode_hex(value) except binascii.Error: