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

Despite the documentation, method web3.eth.accounts.signTransaction does not allow passing chainId as String #3030

Closed
3sGgpQ8H opened this issue Aug 15, 2019 · 13 comments
Labels
2.x 2.0 related issues Bug Addressing a bug Stale Has not received enough activity

Comments

@3sGgpQ8H
Copy link

Description

Documentation for method web3.eth.accounts.signTransaction says that parameter chainId is of type String, though example in the documentation sets chainId like this:

chainId: 1

i.e. as number not as string.

Actually, when chainId is specified as string this method silently produces incorrect signature.

Expected behavior

Method web3.eth.accounts.signTransaction allows specifying chainId and either string or number and works properly in both cases.

Actual behavior

Method web3.eth.accounts.signTransaction works correctly only when chainId is specified as number, but when chainId is specified as string this method silently produces incorrect signature.

Steps to reproduce the behavior

  1. Pass chainId parameter to method web3.eth.accounts.signTransaction as string.
  2. Recover address from returned signed transaction.
  3. Compare recovered address with address used to sign the transaction.
  4. These addresses will be different.

Versions

  • web3.js: 1.0.0
@nivida nivida added 1.x 1.0 related issues Bug Addressing a bug labels Aug 16, 2019
@princesinha19
Copy link
Contributor

@3sGgpQ8H Which web3 version are you using?
I tried reproducing the bug as per steps, given by you. But It is working fine.

I am testing with web3 version 1.2.1.

@3sGgpQ8H
Copy link
Author

3sGgpQ8H commented Aug 19, 2019

1.0.0-beta.55

@princesinha19
Copy link
Contributor

It is working fine with the current version which is 1.2.1. Please check once.

@3sGgpQ8H
Copy link
Author

3sGgpQ8H commented Aug 19, 2019

I looked through the code, and it seems that the actual problem was in ethereumjs-tx called from packages/web3-eth/src/signers/TransactionSigner.js. This file is missing (or moved) in 1.2.1: https://github.com/ethereum/web3.js/blob/v1.2.1/packages/web3-eth/src/signers/TransactionSigner.js , that is probably the reason why you cannot reproduce the problem on 1.2.1. Though, this file is present in 2.x branch: https://github.com/ethereum/web3.js/blob/2.x/packages/web3-eth/src/signers/TransactionSigner.js and is very similar to the file used in 1.0.0-beta.55: https://github.com/ethereum/web3.js/blob/v1.0.0-beta.55/packages/web3-eth/src/signers/TransactionSigner.js

So, 1.2.1 looks very different from both, 1.0.0-beta.55 and 2.x branch, and the fact that problem is not reproducible in 1.2.1 does not necessary mean, that it is not reproducible in 2.x.

P.S. Yes, it seems that what is fixed in 1.2.1 is not fixed in 2.x:

1.2.1 implements transaction signing itself and properly converts chainId to number: https://github.com/ethereum/web3.js/blob/v1.2.1/packages/web3-eth-accounts/src/index.js#L187

2.x delegates to ethereumjs-tx: https://github.com/ethereum/web3.js/blob/2.x/packages/web3-eth-accounts/src/Accounts.js#L162 https://github.com/ethereum/web3.js/blob/2.x/packages/web3-eth/src/signers/TransactionSigner.js#L63

So the problem is probably still reproducible on 2.x branch.

@princesinha19
Copy link
Contributor

Yes sure. I will try to reproduce it using 2.0

@cgewecke cgewecke added 2.x 2.0 related issues and removed 1.x 1.0 related issues labels Oct 7, 2019
@cgewecke
Copy link
Collaborator

cgewecke commented Oct 7, 2019

NB: Per comment above have re-labelled.

In #3070 it's proposed to resolve some signing bugs by upgrading to ethereumjs-tx. Leaving a cross ref there so this problem isn't replicated in the new work.

@dmihal
Copy link

dmihal commented Apr 4, 2020

Just ran into this issue using Web3 + HDWalletProvider, both the most recent versions.

I'm not sure what the solution is, but there should at least be an error thrown if the chainId is passed as a string. Returning a transaction signed by an incorrect key is really strange behavior, and took quite a while of debugging to figure out.

@github-actions
Copy link

github-actions bot commented Jul 3, 2020

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment, otherwise this issue will be closed in 7 days

@github-actions github-actions bot added the Stale Has not received enough activity label Jul 3, 2020
@dmihal
Copy link

dmihal commented Jul 3, 2020

I believe this is still an issue and shouldn't be closed

@github-actions github-actions bot removed the Stale Has not received enough activity label Jul 4, 2020
@github-actions
Copy link

github-actions bot commented Aug 3, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Aug 3, 2020
@dmihal
Copy link

dmihal commented Aug 4, 2020

Bumping to keep this open

@ryanio
Copy link
Collaborator

ryanio commented Aug 4, 2020

Just ran into this issue using Web3 + HDWalletProvider, both the most recent versions.

I'm not sure what the solution is, but there should at least be an error thrown if the chainId is passed as a string. Returning a transaction signed by an incorrect key is really strange behavior, and took quite a while of debugging to figure out.

@dmihal hm that's weird, it looks like the src uses numberToHex which should handle strings fine. could you share some code or a snippet to reproduce?

@github-actions github-actions bot removed the Stale Has not received enough activity label Aug 5, 2020
@github-actions
Copy link

github-actions bot commented Oct 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

@github-actions github-actions bot added the Stale Has not received enough activity label Oct 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x 2.0 related issues Bug Addressing a bug Stale Has not received enough activity
Projects
None yet
Development

No branches or pull requests

7 participants