From 2dc2f48a47e4ffcaa9300e99eb6650ded6011c60 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Tue, 6 Jan 2015 14:06:15 +0300 Subject: [PATCH] Fixes --- lib/address.js | 6 ++-- lib/structs.js | 25 ++++++++-------- package.json | 2 +- test.js | 80 ++++++++++++++++++++++++++------------------------ 4 files changed, 58 insertions(+), 55 deletions(-) diff --git a/lib/address.js b/lib/address.js index 18d0d4b..3fc88f4 100644 --- a/lib/address.js +++ b/lib/address.js @@ -6,7 +6,7 @@ "use strict"; -require("object.assign").shim(); +var objectAssign = Object.assign || require("object-assign"); var bufferEqual = require("buffer-equal"); var bs58 = require("bs58"); var assert = require("./util").assert; @@ -24,7 +24,7 @@ function Address(opts) { return new Address(opts); } opts = opts || {}; - Object.assign(this, opts); + objectAssign(this, opts); this.version = this.version || 4; assert(this.version <= 4, "Version too high"); assert(this.version >= 1, "Version too low"); @@ -193,7 +193,7 @@ function popkey(obj, key) { * @return {Address} Generated address object. */ Address.fromRandom = function(opts) { - opts = Object.assign({}, opts); + opts = objectAssign({}, opts); var version = opts.version = opts.version || 4; var ripelen = popkey(opts, "ripelen") || 19; assertripelen(ripelen, version); diff --git a/lib/structs.js b/lib/structs.js index 5bd2b6b..a9dfa93 100644 --- a/lib/structs.js +++ b/lib/structs.js @@ -6,7 +6,7 @@ "use strict"; -require("object.assign").shim(); +var objectAssign = Object.assign || require("object-assign"); var bufferEqual = require("buffer-equal"); var assert = require("./util").assert; var bmcrypto = require("./crypto"); @@ -254,7 +254,7 @@ exports.var_int_list = { // See https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses var IPv4_MAPPING = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255]); -// Very simple equivalent of inet_ntop(3). +// Very simple inet_ntop(3) equivalent. function inet_ntop(buf) { assert(buf.length === 16, "Bad buffer size"); // IPv4 mapped to IPv6. @@ -270,7 +270,7 @@ function inet_ntop(buf) { } } -// Very simple equivalent of inet_pton(3). +// Very simple inet_pton(3) equivalent. function inet_pton(str) { var buf = new Buffer(16); buf.fill(0); @@ -282,7 +282,7 @@ function inet_pton(str) { if (octets.length === 1) { buf.writeUInt32BE(octets[0], 12); } else { - // To check against 1000.bad.addr + // Check against 1000.bad.addr octets.forEach(function(octet) { assert(octet >= 0, "Bad IPv4 address"); assert(octet <= 255, "Bad IPv4 address"); @@ -306,7 +306,7 @@ function inet_pton(str) { // IPv6. } else { var dgroups = str.split(/::/g); - // To check against 1::1::1 + // Check against 1::1::1 assert(dgroups.length <= 2, "Bad IPv6 address"); var groups = []; var i; @@ -317,18 +317,18 @@ function inet_pton(str) { if (dgroups[1]) { var splitted = dgroups[1].split(/:/g); var fill = 8 - (groups.length + splitted.length); - // To check against 1:1:1:1::1:1:1:1 + // Check against 1:1:1:1::1:1:1:1 assert(fill > 0, "Bad IPv6 address"); for (i = 0; i < fill; i++) { groups.push(0); } groups.push.apply(groups, splitted); } else { - // To check against 1:1:1:1:1:1:1:1:: + // Check against 1:1:1:1:1:1:1:1:: assert(groups.length <= 7, "Bad IPv6 address"); } } else { - // To check against 1:1:1 + // Check against 1:1:1 assert(groups.length === 8, "Bad IPv6 address"); } for (i = 0; i < Math.min(groups.length, 8); i++) { @@ -361,7 +361,8 @@ exports.net_addr = { var timeHi = buf.readUInt32BE(0, true); var timeLo = buf.readUInt32BE(4, true); // JavaScript's Date object can't work with timestamps higher than - // 8640000000000 (~2^43, ~275760 year). + // 8640000000000 (~2^43, ~275760 year). Hope JavaScript will + // support 64-bit numbers up to this date. assert(timeHi <= 2011, "Time is too high"); assert(timeHi !== 2011 || timeLo <= 2820767744, "Time is too high"); res.time = new Date((timeHi * 4294967296 + timeLo) * 1000); @@ -411,7 +412,7 @@ exports.net_addr = { * @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_Encodings} * @namespace */ -exports.messageEncodings = Object.assign(Object.create(var_int), { +exports.messageEncodings = objectAssign(Object.create(var_int), { /** * Any data with this number may be ignored. The sending node might * simply be sharing its public key with you. @@ -466,7 +467,7 @@ var bitfield = function(size) { * @namespace * @static */ -var serviceFeatures = exports.serviceFeatures = Object.assign(bitfield(64), { +var serviceFeatures = exports.serviceFeatures = objectAssign(bitfield(64), { /** This is a normal network node. */ NODE_NETWORK: 0, }); @@ -477,7 +478,7 @@ var serviceFeatures = exports.serviceFeatures = Object.assign(bitfield(64), { * @see {@link https://bitmessage.org/wiki/Protocol_specification#Pubkey_bitfield_features} * @namespace */ -exports.pubkeyFeatures = Object.assign(bitfield(32), { +exports.pubkeyFeatures = objectAssign(bitfield(32), { /** * Receiving node expects that the RIPE hash encoded in their address * preceedes the encrypted message data of msg messages bound for diff --git a/package.json b/package.json index 7f3d580..11784b4 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,6 @@ "buffer-equal": "~0.0.1", "eccrypto": "^0.1.2", "hash.js": "^1.0.2", - "object.assign": "^1.1.1" + "object-assign": "^2.0.0" } } diff --git a/test.js b/test.js index cdbee40..090745a 100644 --- a/test.js +++ b/test.js @@ -263,44 +263,46 @@ describe("WIF", function() { }); }); -// FIXME(Kagami): Add more fail tests. -describe("Address", function() { - it("should decode Bitmessage address", function() { - var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z") - expect(addr.version).to.equal(4); - expect(addr.stream).to.equal(1); - expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56"); - }); - - it("should decode Bitmessage address badly formatted", function() { - var addr = Address.decode(" 2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z ") - expect(addr.version).to.equal(4); - expect(addr.stream).to.equal(1); - expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56"); - }); - - it("should allow to generate new Bitmessage address", function() { - this.timeout(10000); - var addr = Address.fromRandom(); - expect(addr.version).to.equal(4); - expect(addr.stream).to.equal(1); - expect(addr.signPrivateKey.length).to.equal(32); - expect(addr.encPrivateKey.length).to.equal(32); - var str = addr.encode(); - expect(str.slice(0, 3)).to.equal("BM-"); - var addr2 = Address.decode(str); - expect(addr2.version).to.equal(4); - expect(addr2.stream).to.equal(1); - expect(addr2.ripe.length).to.equal(20); - expect(addr2.ripe[0]).to.equal(0); - }); - - if (allTests) { - it("should allow to generate shorter address", function() { - this.timeout(60000); - var addr = Address.fromRandom({ripelen: 18}); - var ripe = addr.getRipe({short: true}); - expect(ripe.length).to.be.at.most(18); +describe("High-level classes", function() { + // FIXME(Kagami): Add more fail tests. + describe("Address", function() { + it("should decode Bitmessage address", function() { + var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z") + expect(addr.version).to.equal(4); + expect(addr.stream).to.equal(1); + expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56"); }); - } + + it("should decode Bitmessage address badly formatted", function() { + var addr = Address.decode(" 2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z ") + expect(addr.version).to.equal(4); + expect(addr.stream).to.equal(1); + expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56"); + }); + + it("should allow to generate new Bitmessage address", function() { + this.timeout(10000); + var addr = Address.fromRandom(); + expect(addr.version).to.equal(4); + expect(addr.stream).to.equal(1); + expect(addr.signPrivateKey.length).to.equal(32); + expect(addr.encPrivateKey.length).to.equal(32); + var str = addr.encode(); + expect(str.slice(0, 3)).to.equal("BM-"); + var addr2 = Address.decode(str); + expect(addr2.version).to.equal(4); + expect(addr2.stream).to.equal(1); + expect(addr2.ripe.length).to.equal(20); + expect(addr2.ripe[0]).to.equal(0); + }); + + if (allTests) { + it("should allow to generate shorter address", function() { + this.timeout(60000); + var addr = Address.fromRandom({ripelen: 18}); + var ripe = addr.getRipe({short: true}); + expect(ripe.length).to.be.at.most(18); + }); + } + }); });