Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow empty string for hex values #1415

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/core/contracts/test_contract_call_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 6 additions & 1 deletion web3/_utils/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down