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

Commit

Permalink
Test for leading zeros in JSON-RPC response
Browse files Browse the repository at this point in the history
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)"
  • Loading branch information
attente committed Nov 27, 2017
1 parent a720023 commit ebb74a3
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions test/hex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var assert = require("assert");
var Web3 = require("web3");
var TestRPC = require("../index.js");
var to = require("../lib/utils/to.js");

describe("to.hexWithoutLeadingZeroes", function() {
Expand Down Expand Up @@ -27,3 +29,80 @@ describe("to.hexWithoutLeadingZeroes", function() {
done();
});
});

function noLeadingZeros(result) {
if (typeof result === "string") {
if (/^0x/.test(result)) {
assert.equal(result, to.hexWithoutLeadingZeroes(result));
}
} else if (typeof result === "object") {
for (var key in result) {
if (result.hasOwnProperty(key)) {
noLeadingZeros(result[key]);
}
}
}
}

describe("JSON-RPC Response", function() {
var web3 = new Web3();
var provider = TestRPC.provider();
web3.setProvider(provider);

var accounts;
before(function(done) {
web3.eth.getAccounts(function(err, accs) {
if (err) return done(err);
accounts = accs;
done();
});
});

it("should not have leading zeros in hex strings", function(done) {
var request = {
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": [
accounts[0],
"pending"
],
"id": 1
};

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

request = {
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params": [
{
"from": accounts[0],
"to": accounts[1],
"value": "0x100000000"
}
],
"id": 2
};

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

request = {
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": [
accounts[0],
"pending"
],
"id": 3
};

provider.sendAsync(request, function(err, result) {
noLeadingZeros(result);
done();
});
});
});
});
});

0 comments on commit ebb74a3

Please sign in to comment.