-
Notifications
You must be signed in to change notification settings - Fork 239
Incorrect Hex Encoding for Quantities #51
Comments
0x01 was made instead of 0x1 for node Buffer Patch which will fix this issue need major version bump. |
This is b/c ethereum TX was written to parse raw TX from the network
directly. Nodes buffer API expects hex number to also be an even number.
This lib should probably be wrapped by something higher level for the RPC.
But I'm open to suggestions!
…On 26 Feb 2017 12:53 p.m., "Fabio Berger" ***@***.***> wrote:
Hi there,
As per the Ethereum wiki JSON-rpc section on hex encoding
<https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding>
correct encoding of quantities (numbers e.g nonces, etc...) should be the
most compact hex representation:
- 0x41 (65 in decimal)
- 0x400 (1024 in decimal)
- WRONG: 0x (should always have at least one digit - zero is "0x0")
- WRONG: 0x0400 (no leading zeroes allowed)
- WRONG: ff (must be prefixed 0x)
The ethereumjs-tx library quantities are padded to an even number of
digits, i.e. 0x01, instead of 0x1 as expected by the specification. This
has already caused issues for dependent projects such as testrpc
<https://github.com/ethereumjs/testrpc> (trufflesuite/ganache-cli-archive#220
<trufflesuite/ganache-cli-archive#220>) and introduces an
incompatibility with the Geth client and the parity client (their wiki
entry on encoding <https://github.com/ethcore/parity/wiki/JSONRPC>).
Happy to help implement this fix, but since it's a quite pervasive issue,
thought I'd get your input on how to approach it first.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#51>, or mute the
thread
<https://github.com/notifications/unsubscribe-auth/AAJqA8jkeXLu3qSbQcTuXKxmwEUxGUjoks5rgWergaJpZM4MMW67>
.
|
After further investigation, it seems this issue is related to the Buffer classes conversion of bytes to hex. Since a byte is 8 bits, values below 16 will result in the first 4 bits being represented by a zero. Since the Ethereum spec expects a more compact representation, one solution would be to wrap the Buffer implementation and have the caller specify whether the value being stored in the buffer is a quantity or unformatted data. The wrapper will then hex encode the value using the spec rules accordingly. e.g: module.exports = class EthBuffer extends Buffer {
constructor(value, encoding = 'utf8', type = 'unformatted') {
if (arguments.length === 2 && typeof value === 'string') {
super(value, encoding)
} else {
super(value)
}
if (arguments.length === 2 && typeof value !== 'string') {
type = encoding
}
this._type = type
}
toString() {
const args = Array.prototype.slice.call(arguments)
const val = super.toString.apply(this, args)
if (args[0] === 'hex' && this._type === 'quantity' && val[0] === '0') {
return val.substring(1)
} else {
return val
}
}
} And then replacing instantiations of Buffer with EthBuffer. The issue I ran into is that instantiating an |
For posterity, |
@fabioberger You're welcome to chime in in that thread. It would certainly be helpful to have an example of a real-world use case for extending Buffer. |
Hello,
I have managed to have transactions registered while using web3 but I can't do it the same way if I want to be able to sign transactions myself. Tests were done using both a local or remote ( infura ) node on the testnet ( ropsten and kovan ). I may have a hint of the problem :
Could it be that this issue is related to this padding problem ? I have yet to manage to send a transaction that I have signed myself in nodejs. |
@AlTremor I don't believe this issue is the one you are running into. It looks like you are simply missing the |
ethereumjs-tx only exposes buffers, not strings. coercing buffers to hex strings is out of scope. |
Hi there,
As per the Ethereum wiki JSON-rpc section on hex encoding correct encoding of quantities (numbers e.g nonces, etc...) should be the most compact hex representation:
The ethereumjs-tx library quantities are padded to an even number of digits, i.e. 0x01, instead of 0x1 as expected by the specification. This has already caused issues for dependent projects such as testrpc (trufflesuite/ganache-cli-archive#220) and introduces an incompatibility with the Geth client and the parity client (their wiki entry on encoding).
Happy to help implement this fix, but since it's a quite pervasive issue, thought I'd get your input on how to approach it first.
The text was updated successfully, but these errors were encountered: