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

TypeError: Transaction had invalid fields: {'to': '0x0d8ce2a99bb6e3b7db580ed848240e4a0f9ae153'} #211

Open
woshidama323 opened this issue Nov 22, 2022 · 3 comments

Comments

@woshidama323
Copy link

woshidama323 commented Nov 22, 2022

  • Version: 5.31.1
  • Python: 3.9.6
  • OS: linux
  • pip freeze output
aiohttp==3.8.1
aiosignal==1.2.0
async-timeout==4.0.2
attrs==22.1.0
aws-cdk-lib==2.41.0
base58==2.1.1
bitarray==2.6.0
cached-property==1.5.2
cattrs==22.1.0
certifi==2021.5.30
charset-normalizer==2.1.1
constructs==10.1.97
contourpy==1.0.6
crytic-compile==0.2.3
cycler==0.11.0
cytoolz==0.12.0
docopt==0.6.2
ecdsa==0.18.0
eth-abi==2.2.0
eth-account==0.5.9
eth-bloom==1.0.4
eth-hash==0.3.3
eth-keyfile==0.5.1
eth-keys==0.3.4
eth-rlp==0.2.1
eth-typing==2.3.0
eth-utils==1.10.0
exceptiongroup==1.0.0rc9
fonttools==4.38.0
frozenlist==1.3.1
hexbytes==0.3.0
idna==3.4
ipfshttpclient==0.8.0a2
jsii==1.67.0
jsonschema==4.16.0
kiwisolver==1.4.4
lru-dict==1.1.8
matplotlib==3.6.1
multiaddr==0.0.9
multidict==6.0.2
mypy-extensions==0.4.3
netaddr==0.8.0
numpy==1.23.4
openstep-parser==1.5.3
packaging==21.3
parsimonious==0.8.1
pbxproj==3.2.3
Pillow==9.3.0
prettytable==3.3.0
protobuf==3.19.5
publication==0.0.3
py-ecc==6.0.0
py-evm==0.6.0a1
pycryptodome==3.15.0
pyethash==0.1.27
pyparsing==3.0.9
pyrsistent==0.18.1
pysha3==1.0.2
python-dateutil==2.8.2
requests==2.28.1
rlp==2.0.1
six==1.16.0
slither-analyzer==0.8.3
solc-select==0.2.1
sortedcontainers==2.4.0
toolz==0.12.0
trie==2.0.2
typeguard==2.13.3
typing-extensions==4.3.0
urllib3==1.26.12
varint==1.0.2
wcwidth==0.2.5
web3==5.31.1
websockets==9.1
yarl==1.8.1

What was wrong?

Please include any of the following that are applicable:

description:
when trying to sign with sign_transaction offline, I got such errors bellow:
TypeError: Transaction had invalid fields: {'to': '0x0d8ce2a99bb6e3b7db580ed848240e4a0f9ae153'}

Here is the whole output

>>> stx = {'gasPrice': 5000000000, 'nonce': 431, 'to': '0x0d8ce2a99bb6e3b7db580ed848240e4a0f9ae153', 'value': 0, 'data': '0xa9059cbb0000000000000000000000002ff22e41447be8acadbfab0c95968263da15dd1a000000000000000000000000000000000000000000000000c3e3df74074f3f51', 'gas': 200000, 'chainId': 56}
>>> 
>>> import web3
>>> from web3.auto import w3
>>> w3.eth.account.sign_transaction(transaction_dict, "<private key>")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'transaction_dict' is not defined
>>> w3.eth.account.sign_transaction(stx, "<private key>")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eth_utils/decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eth_account/account.py", line 748, in sign_transaction
    ) = sign_transaction_dict(account._key_obj, sanitized_transaction)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eth_account/_utils/signing.py", line 32, in sign_transaction_dict
    unsigned_transaction = serializable_unsigned_transaction_from_dict(transaction_dict)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eth_account/_utils/legacy_transactions.py", line 44, in serializable_unsigned_transaction_from_dict
    assert_valid_fields(transaction_dict)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eth_account/_utils/legacy_transactions.py", line 114, in assert_valid_fields
    raise TypeError("Transaction had invalid fields: %r" % invalid)
TypeError: Transaction had invalid fields: {'to': '0x0d8ce2a99bb6e3b7db580ed848240e4a0f9ae153'}

But if i change the to field with "0xdd8ce2a99bb6e3b7db580ed848240e4a0f9ae153" (here i changed 0x0 to 0xd)
It works well

Guess the web3.py cannot handle such 0x0 prefix address well ?

How can it be fixed?

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

Note: We prefer to use issues to track our work. If you think you've encountered a bug in web3py or
have a feature request, you're in the right place. If you have implementation or usage questions,
please refer to our documentation and/or join the conversation
on discord.

@kclowes
Copy link
Collaborator

kclowes commented Nov 28, 2022

Thanks for the report @woshidama323. We changed the private key you listed to <private key>, even though it doesn't look like the address associated with the PK has any assets. Don't use the address associated with that PK in the future.

It does look like this is a bug, but you can work around it by converting the to address to HexBytes before sending it:

>>> from hexbytes import HexBytes
>>> stx = {'gasPrice': 5000000000, 'nonce': 431, 'to': HexBytes(0x0d8ce2a99bb6e3b7db580ed848240e4a0f9ae153), ... }

I'm going to transfer this issue to the eth-account repo because I think that's where the actual issue is.

@kclowes kclowes transferred this issue from ethereum/web3.py Nov 28, 2022
@kclowes
Copy link
Collaborator

kclowes commented Nov 28, 2022

Adding on to this, it looks like the problem is actually that the address isn't checksummed. The error message could definitely stand to be improved here.

@heki-ru
Copy link

heki-ru commented Feb 4, 2024

|   File "/venv/lib/python3.11/site-packages/eth_account/signers/local.py", line 94, in sign_transaction
|     return self._publicapi.sign_transaction(transaction_dict, self.key)
|            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/venv/lib/python3.11/site-packages/eth_utils/decorators.py", line 29, in _wrapper
|     return self.method(obj, *args, **kwargs)
|            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/venv/lib/python3.11/site-packages/eth_account/account.py", line 772, in sign_transaction
|     ) = sign_transaction_dict(account._key_obj, sanitized_transaction)
|         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/venv/lib/python3.11/site-packages/eth_account/_utils/signing.py", line 32, in sign_transaction_dict
|     unsigned_transaction = serializable_unsigned_transaction_from_dict(transaction_dict)
|                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/venv/lib/python3.11/site-packages/eth_account/_utils/legacy_transactions.py", line 42, in serializable_unsigned_transaction_from_dict
|     return TypedTransaction.from_dict(transaction_dict)
|            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                     
|   File "/venv/lib/python3.11/site-packages/eth_account/_utils/typed_transactions.py", line 161, in from_dict
|     transaction=transaction.from_dict(dictionary),
|                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/venv/lib/python3.11/site-packages/eth_account/_utils/typed_transactions.py", line 492, in from_dict
|     cls.assert_valid_fields(dictionary)
|   File "/venv/lib/python3.11/site-packages/eth_account/_utils/typed_transactions.py", line 483, in assert_valid_fields
|     raise TypeError("Transaction had invalid fields: %r" % invalid)
| TypeError: Transaction had invalid fields: {'gas': HexBytes('0xd1d8')}
+------------------------------------

Occasionally received similar error today
web3.py 6.15.0
eth_account 0.10.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants