Skip to content

Commit

Permalink
Fixing leading zeros for hex encoded quantities in tx trufflesuite#166
Browse files Browse the repository at this point in the history
  • Loading branch information
gislik committed Sep 4, 2018
1 parent 1cc5eb3 commit b12a29a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/utils/txhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ module.exports = {
}
var resultJSON = {
hash: to.hex(tx.hash()),
nonce: to.hex(tx.nonce),
nonce: to.rpcQuantityHexString(tx.nonce),
blockHash: to.hex(block.hash()),
blockNumber: to.hex(block.header.number),
transactionIndex: to.hex(transactionIndex),
from: to.hex(tx.from),
to: to.hex(tx.to),
value: to.hex(tx.value),
gas: to.hex(tx.gasLimit),
gasPrice: to.hex(tx.gasPrice),
input: to.hex(tx.data),
value: to.rpcQuantityHexString(tx.value),
gas: to.rpcQuantityHexString(tx.gasLimit),
gasPrice: to.rpcQuantityHexString(tx.gasPrice),
input: to.rpcQuantityHexString(tx.data),
};

if (tx.v && tx.v.length > 0 &&
tx.r && tx.r.length > 0 &&
tx.s && tx.s.length > 0) {
resultJSON.v = to.hex(tx.v);
resultJSON.r = to.hex(tx.r);
resultJSON.s = to.hex(tx.s);
resultJSON.v = to.rpcQuantityHexString(tx.v);
resultJSON.r = to.rpcQuantityHexString(tx.r);
resultJSON.s = to.rpcQuantityHexString(tx.s);
}

return resultJSON;
Expand Down
49 changes: 49 additions & 0 deletions test/hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ function noLeadingZeros(method, result, path) {
}
}

function noLeadingZerosTx(tx) {
const fields = ['nonce', 'gasPrice', 'gas', 'value', 'input'];
for (var i in fields) {
const field = fields[i]
if (typeof tx[field] === "undefined") {
assert.fail(`Field ${field} should not be undefined`)
}
assert.equal(tx[field], to.rpcQuantityHexString(tx[field]), assert `Quantity field ${field} in transaction is missing or has leading zeroes.`);
}
}


describe("JSON-RPC Response", function() {
var web3 = new Web3();
var provider = Ganache.provider();
Expand Down Expand Up @@ -126,4 +138,41 @@ describe("JSON-RPC Response", function() {
});
});
});

it("should not have leading zeros in rpc tx quantity hex strings", function(done) {
var request = {
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params": [
{
"from": accounts[0],
"to": accounts[1],
"value": "0x100000000",
"gas": "0x015F90",
"gasPrice": "0x01",
}
],
"id": 1
};
provider.sendAsync(request, function() {
// force nonce to be at least 1 (since 0 encodes to 0x0)
provider.sendAsync(request, function(err, result) {
request = {
"jsonrpc": "2.0",
"method": "eth_getTransactionByHash",
"params": [
result.result
],
"id": 2
};

provider.sendAsync(request, function(err, result) {

noLeadingZerosTx(result.result);
done();

});
});
});
});
});

0 comments on commit b12a29a

Please sign in to comment.