Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Fix getTransactionCount leading zeros in hex string #32

Conversation

natanrolnik
Copy link
Contributor

@natanrolnik natanrolnik commented Nov 19, 2017

As described here (emphasis mine):

When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x", the most compact representation (slight exception: zero should be represented as "0x0").

The implementation of getTransactionCount would encode numbers including a leading zero, for example: 4 would become 0x04 instead of the correct way 0x4.

This PR fixes it, and also fixes the function hexWithoutLeadingZeroes in lib/utils/to.js to correctly convert 0 to 0x0 instead of 0x (which would lead to an exception in Web3js when creating a BigNumber, more specifically at line 363).

I tried to add a test for this change, but as Web3 already returns the decoded number, I wasn't able to find the right place to check that the hex string is returned correctly.

Related issues and PRs that might related to this PR:

@natanrolnik natanrolnik deleted the fix-transaction-count-leading-zeros branch November 19, 2017 18:13
@natanrolnik natanrolnik restored the fix-transaction-count-leading-zeros branch November 20, 2017 08:38
@natanrolnik natanrolnik reopened this Nov 20, 2017
@natanrolnik natanrolnik changed the title Fix transaction count leading zeros Fix getTransactionCount leading zeros in hex string Nov 20, 2017
@@ -13,7 +13,6 @@ var async = require("async");
var BlockchainDouble = require("./blockchain_double.js");
var ForkedBlockchain = require("./utils/forkedblockchain.js");
var Web3 = require('web3');
var async = require("async");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed because it's already present at line 12

From https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding:

"When encoding QUANTITIES (integers, numbers): encode as hex, prefix
with '0x', the most compact representation (slight exception: zero
should be represented as '0x0'). Examples:"

"WRONG: 0x0400 (no leading zeroes allowed)"
@attente
Copy link
Contributor

attente commented Nov 27, 2017

Hello, thank you for working on this! Would you mind also merging in some tests I wrote for this?

kinecosystem#2

@natanrolnik
Copy link
Contributor Author

@attente thanks for writing the tests. Merged.

@benjamincburns
Copy link
Contributor

@natanrolnik & @attente thanks for teaming up on this one!

@benjamincburns benjamincburns merged commit cd55e15 into trufflesuite:develop Dec 26, 2017
@FutureOfAI
Copy link

FutureOfAI commented Jun 8, 2019

I have same problem as you, can you help me how to deal with leading zero in raw-transaction?
My raw transaction is: f86b0285012a05f2008261a8948b733353ce21ebd8eb5ffd9f49d57830942e88158769ec95a3fa70008025a04f9dd75069b51d36ec47b5bf68e6c45f8b854cf17a661e734e7a7c651240eeaca00044280475c3d355c44829ac93118b4ebe044356185c1c08724c8bcfebbd4b3d

including:
R is 4f9dd75069b51d36ec47b5bf68e6c45f8b854cf17a661e734e7a7c651240eeac
S is 0044280475c3d355c44829ac93118b4ebe044356185c1c08724c8bcfebbd4b3d
How can I deal with leading zero, and how can I reconstruction raw-transaction after correct it?
Does the process like this:
RLP{ RLP(nonce) + RLP(Gas price) + RLP(Gas limit) + RLP(to address) + RLP(value) + RLP(data) + V + [a0 + FixLeadingZero(R)] + [a0 + FixLeadingZero(S)] }

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

Successfully merging this pull request may close these issues.

Quantities such as from eth_getTransactionCount have leading zeroes
4 participants