ethereum / eth-account

Account abstraction library for web3.py
http://eth-account.readthedocs.io/
MIT License
275 stars 159 forks source link

Error prone parsing of `bool` in `encode_typed_data/encoding_and_hashing.py:encode_field` #303

Open Philogy opened 1 month ago

Philogy commented 1 month ago

What happened?

The encode_data method which is the underlying method for EIP712 encoding uses encode_field to encode its fields into the underlying python type. In the case of bool it just uses the python bool method to convert values to booleans, this however leads to very counterintuitive results, with strings like "False", "false", or "0" all getting parsed to True

Code that produced the error

from eth_account._utils.encode_typed_data.encoding_and_hashing import encode_data

if __name__ == '__main__':
    comp = encode_data(
        'Wow',
        {
            'Wow': [
                {'name': 'nice', 'type': 'bool'}
            ]
        },
        {
            'nice': '0' # can substitute for 'False' for same result
        }
    )

    # outputs: aa6fc6f6e625657d3af464fe79175e37e32237d2b4af802593e53cf78ab9a20900000000000000000000000000000000000000000000000000
00000000000001
    print(f'comp.hex(): {comp.hex()}')

Full error output

No response

Fill this section in if you know how this could or should be fixed

eth-account Version

0.13.4

Python Version

3.12.4

Operating System

osx

Output from pip freeze

annotated-types==0.7.0
bitarray==2.9.2
ckzg==2.0.1
cytoolz==1.0.0
eth-abi==5.1.0
eth-account==0.13.4
eth-hash==0.7.0
eth-keyfile==0.8.1
eth-keys==0.5.1
eth-rlp==2.1.0
eth-typing==5.0.0
eth-utils==5.0.0
hexbytes==1.2.1
parsimonious==0.10.0
pycryptodome==3.21.0
pydantic==2.9.2
pydantic-core==2.23.4
regex==2024.9.11
rlp==4.0.1
toolz==1.0.0
typing-extensions==4.12.2