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

Request: ensure '0x' prefixing for all data fields #1082

Closed
asmello opened this issue Sep 27, 2017 · 5 comments · Fixed by #3317
Closed

Request: ensure '0x' prefixing for all data fields #1082

asmello opened this issue Sep 27, 2017 · 5 comments · Fixed by #3317
Assignees
Labels
1.x 1.0 related issues 2.x 2.0 related issues Bug Addressing a bug

Comments

@asmello
Copy link

asmello commented Sep 27, 2017

Ok, so this started as an issue I filled to Parity, but it turns out the fix should be made in web3.js instead.

There's a very subtle and hard to trace bug caused (for example) when you submit the contract bytecode data without the 0x prefix. Sample code:

let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

let compiledCode = solc.compile(code);
let abiDefinition = JSON.parse(compiledCode.contracts[':Voting'].interface);
let VotingContract = web3.eth.contract(abiDefinition);
let byteCode = compiledCode.contracts[':Voting'].bytecode;

let deployedContract = VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000});

The last line fails because on the line before solc yields hex-encoded data without the 0x prefix. Both Geth and TestingRPC implementations are flexible enough to accept that payload as is, even though it doesn't strictly conform to the standard, but when I tested this code against a Parity backend I got a very unhelpful invalid params exception.

An argument could be made to change the solc implementation to return an 0x-prefixed string, but I think it would be best to add some code to the web3.js implementation to automatically do that prefixing when required. That is already the case for parity.js.

Note that nowhere in the JS API docs there's mention of how such data should be encoded, the only guide is the JSONRPC documentation, which does ask for an 0x prefix for all hex data, but the user cannot be expected to be well informed about the lower level protocol to use the higher level JS API. Since the JS API already performs type conversion whenever convenient, it should fall into its responsibilities to handle this detail as well. It would also be helpful for web3.js to give a meaningful warning in case the prefix is omitted (as it's more sensible to include it anyway).

@epheph
Copy link

epheph commented Feb 27, 2018

👍 to this. The current behavior in web3-1.0.0 is problematic. When deploying a contract at least, it simply chops off the first 2 bytes, likely assuming that it was just a "0x" without checking first, damaging the transaction and causing deploy issues (likely stack underflow)

@nivida nivida self-assigned this Aug 9, 2018
@nivida nivida added the Bug Addressing a bug label Nov 29, 2018
@nivida nivida added 1.x 1.0 related issues 2.x 2.0 related issues labels Jun 20, 2019
@nivida nivida mentioned this issue Nov 21, 2019
7 tasks
@nivida
Copy link
Contributor

nivida commented Jan 20, 2020

When deploying a contract at least, it simply chops off the first 2 bytes, likely assuming that it was just a "0x" without checking first, damaging the transaction and causing deploy issues

I've checked that closer with version 1.2.4 of web3.js and I couldn't reproduce this issue. The web3.js library keeps the passed string as it is and doesn't remove any 0x prefix if passed with. Feel free to ping me here if I missed a case. @epheph @asmello.

@nivida nivida closed this as completed Jan 20, 2020
@nivida
Copy link
Contributor

nivida commented Jan 20, 2020

@asmello I apologize for the confusion. The described behaviour in the comment of @epheph is not reproducible but there is still no validation of the inputs in 1.0 to ensure the prefix '0x' is existing. I will re-open this and immediately fix it.

@asmello
Copy link
Author

asmello commented Jan 20, 2020

Thanks! Glad this is finally being looked into. I haven't touched blockchain code in a while, but I'll stick around in case I can be of any help.

@nivida
Copy link
Contributor

nivida commented Jan 21, 2020

@asmello Thanks! But we sadly had to add the hold label to the related PR because it would break too many already existing DApps. We will merge this fix later for version 2.0 of web3.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues 2.x 2.0 related issues Bug Addressing a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants