Skip to content

Commit 1a0341f

Browse files
committed
Add newsfragment & update docs for #198 / #211
1 parent 670482c commit 1a0341f

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

docs/decoding.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,32 @@ The :py:meth:`eth_abi.decoding.BaseDecoder.decode` function provides an API for
2929
decoding binary values for ABI types into python values. It accepts a sequence of
3030
ABI type strings as the first argument and the binary data to be decoded, as a python
3131
``bytes`` or ``bytearray`` value, as the second argument.
32+
33+
Strict Mode
34+
-----------
35+
36+
By default, the decoder will raise an exception if the binary data to be decoded
37+
is not padded to the next 32-byte boundary. This behavior can be disabled for the
38+
``ByteStringDecoder`` (the default decoder for ``bytes`` and ``string`` types) by
39+
passing ``strict=False`` to the :py:meth:`eth_abi.abi.decode` method. Turning off
40+
strict mode will also ignore any trailing bytes beyond the data size specified. This
41+
means that if there is any padding, the validation for only empty bytes in the padding
42+
area is also ignored.
43+
44+
.. doctest::
45+
46+
>>> from eth_abi import abi
47+
48+
>>> # decode a bytes value without strict mode
49+
>>> hex_val = (
50+
... # offset to data is 32 bytes:
51+
... "0000000000000000000000000000000000000000000000000000000000000020"
52+
... # length of data is 1 byte:
53+
... "0000000000000000000000000000000000000000000000000000000000000001"
54+
... # b"\x01" with less than 32 bytes of padding
55+
... # and not strictly padded with only zero bytes:
56+
... "0100000000000001020300"
57+
... )
58+
>>> (decoded,) = abi.decode(['bytes'], bytes.fromhex(hex_val), strict=False)
59+
>>> decoded
60+
b'\x01'

eth_abi/codec.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ def decode(
139139
:param types: A list or tuple of string representations of the ABI types that
140140
will be used for decoding e.g. ``('uint256', 'bytes[]', '(int,int)')``
141141
:param data: The binary value to be decoded.
142-
:param strict: If ``False``, then the decoder will ignore properties such as
143-
the length of the data being a multiple of 32 bytes. ``False`` is how the
144-
Solidity ABI decoder currently works. However, ``True`` is the default for
145-
the eth-abi library.
146-
142+
:param strict: If ``False``, dynamic-type decoders will ignore validations such
143+
as making sure the data is padded to a multiple of 32 bytes or checking that
144+
padding bytes are zero / empty. ``False`` is how the Solidity ABI decoder
145+
currently works. However, ``True`` is the default for the eth-abi library.
147146
148147
:returns: A tuple of equivalent python values for the ABI values
149148
represented in ``data``.

newsfragments/198.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow turning off abi decoder "strict mode" when calling ``abi.decode()``.

0 commit comments

Comments
 (0)