From ebb74a3abce9ba05d17edd47bc617517f24b0240 Mon Sep 17 00:00:00 2001 From: William Hua Date: Mon, 27 Nov 2017 12:50:29 -0500 Subject: [PATCH] Test for leading zeros in JSON-RPC response 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)" --- test/hex.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/test/hex.js b/test/hex.js index 6ef00a20f3..b8c5eea4b0 100644 --- a/test/hex.js +++ b/test/hex.js @@ -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() { @@ -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(); + }); + }); + }); + }); +});