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

Commit

Permalink
Merge pull request #2 from attente/fix-transaction-count-leading-zeros
Browse files Browse the repository at this point in the history
Add tests for leading zeros in hex values
  • Loading branch information
natanrolnik authored Nov 28, 2017
2 parents 6002841 + ebb74a3 commit cd55e15
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions test/hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var assert = require("assert");
var Web3 = require("web3");
var TestRPC = require("../index.js");
var to = require("../lib/utils/to.js");

describe("to.hexWithoutLeadingZeroes", function() {
it("should print '0x0' for input 0", function(done) {
assert.equal(to.hexWithoutLeadingZeroes(0), "0x0");
done();
});

it("should print '0x0' for input '0'", function(done) {
assert.equal(to.hexWithoutLeadingZeroes("0"), "0x0");
done();
});

it("should print '0x0' for input '000'", function(done) {
assert.equal(to.hexWithoutLeadingZeroes("000"), "0x0");
done();
});

it("should print '0x0' for input '0x000'", function(done) {
assert.equal(to.hexWithoutLeadingZeroes("0x000"), "0x0");
done();
});

it("should print '0x20' for input '0x0020'", function(done) {
assert.equal(to.hexWithoutLeadingZeroes("0x0020"), "0x20");
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 cd55e15

Please sign in to comment.