This commit is contained in:
Kagami Hiiragi 2015-01-06 14:06:15 +03:00
parent 6c20b3efd7
commit 2dc2f48a47
4 changed files with 58 additions and 55 deletions

View File

@ -6,7 +6,7 @@
"use strict"; "use strict";
require("object.assign").shim(); var objectAssign = Object.assign || require("object-assign");
var bufferEqual = require("buffer-equal"); var bufferEqual = require("buffer-equal");
var bs58 = require("bs58"); var bs58 = require("bs58");
var assert = require("./util").assert; var assert = require("./util").assert;
@ -24,7 +24,7 @@ function Address(opts) {
return new Address(opts); return new Address(opts);
} }
opts = opts || {}; opts = opts || {};
Object.assign(this, opts); objectAssign(this, opts);
this.version = this.version || 4; this.version = this.version || 4;
assert(this.version <= 4, "Version too high"); assert(this.version <= 4, "Version too high");
assert(this.version >= 1, "Version too low"); assert(this.version >= 1, "Version too low");
@ -193,7 +193,7 @@ function popkey(obj, key) {
* @return {Address} Generated address object. * @return {Address} Generated address object.
*/ */
Address.fromRandom = function(opts) { Address.fromRandom = function(opts) {
opts = Object.assign({}, opts); opts = objectAssign({}, opts);
var version = opts.version = opts.version || 4; var version = opts.version = opts.version || 4;
var ripelen = popkey(opts, "ripelen") || 19; var ripelen = popkey(opts, "ripelen") || 19;
assertripelen(ripelen, version); assertripelen(ripelen, version);

View File

@ -6,7 +6,7 @@
"use strict"; "use strict";
require("object.assign").shim(); var objectAssign = Object.assign || require("object-assign");
var bufferEqual = require("buffer-equal"); var bufferEqual = require("buffer-equal");
var assert = require("./util").assert; var assert = require("./util").assert;
var bmcrypto = require("./crypto"); var bmcrypto = require("./crypto");
@ -254,7 +254,7 @@ exports.var_int_list = {
// See https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses // 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]); 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) { function inet_ntop(buf) {
assert(buf.length === 16, "Bad buffer size"); assert(buf.length === 16, "Bad buffer size");
// IPv4 mapped to IPv6. // 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) { function inet_pton(str) {
var buf = new Buffer(16); var buf = new Buffer(16);
buf.fill(0); buf.fill(0);
@ -282,7 +282,7 @@ function inet_pton(str) {
if (octets.length === 1) { if (octets.length === 1) {
buf.writeUInt32BE(octets[0], 12); buf.writeUInt32BE(octets[0], 12);
} else { } else {
// To check against 1000.bad.addr // Check against 1000.bad.addr
octets.forEach(function(octet) { octets.forEach(function(octet) {
assert(octet >= 0, "Bad IPv4 address"); assert(octet >= 0, "Bad IPv4 address");
assert(octet <= 255, "Bad IPv4 address"); assert(octet <= 255, "Bad IPv4 address");
@ -306,7 +306,7 @@ function inet_pton(str) {
// IPv6. // IPv6.
} else { } else {
var dgroups = str.split(/::/g); var dgroups = str.split(/::/g);
// To check against 1::1::1 // Check against 1::1::1
assert(dgroups.length <= 2, "Bad IPv6 address"); assert(dgroups.length <= 2, "Bad IPv6 address");
var groups = []; var groups = [];
var i; var i;
@ -317,18 +317,18 @@ function inet_pton(str) {
if (dgroups[1]) { if (dgroups[1]) {
var splitted = dgroups[1].split(/:/g); var splitted = dgroups[1].split(/:/g);
var fill = 8 - (groups.length + splitted.length); 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"); assert(fill > 0, "Bad IPv6 address");
for (i = 0; i < fill; i++) { for (i = 0; i < fill; i++) {
groups.push(0); groups.push(0);
} }
groups.push.apply(groups, splitted); groups.push.apply(groups, splitted);
} else { } 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"); assert(groups.length <= 7, "Bad IPv6 address");
} }
} else { } else {
// To check against 1:1:1 // Check against 1:1:1
assert(groups.length === 8, "Bad IPv6 address"); assert(groups.length === 8, "Bad IPv6 address");
} }
for (i = 0; i < Math.min(groups.length, 8); i++) { for (i = 0; i < Math.min(groups.length, 8); i++) {
@ -361,7 +361,8 @@ exports.net_addr = {
var timeHi = buf.readUInt32BE(0, true); var timeHi = buf.readUInt32BE(0, true);
var timeLo = buf.readUInt32BE(4, true); var timeLo = buf.readUInt32BE(4, true);
// JavaScript's Date object can't work with timestamps higher than // 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, "Time is too high");
assert(timeHi !== 2011 || timeLo <= 2820767744, "Time is too high"); assert(timeHi !== 2011 || timeLo <= 2820767744, "Time is too high");
res.time = new Date((timeHi * 4294967296 + timeLo) * 1000); 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} * @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_Encodings}
* @namespace * @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 * Any data with this number may be ignored. The sending node might
* simply be sharing its public key with you. * simply be sharing its public key with you.
@ -466,7 +467,7 @@ var bitfield = function(size) {
* @namespace * @namespace
* @static * @static
*/ */
var serviceFeatures = exports.serviceFeatures = Object.assign(bitfield(64), { var serviceFeatures = exports.serviceFeatures = objectAssign(bitfield(64), {
/** This is a normal network node. */ /** This is a normal network node. */
NODE_NETWORK: 0, 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} * @see {@link https://bitmessage.org/wiki/Protocol_specification#Pubkey_bitfield_features}
* @namespace * @namespace
*/ */
exports.pubkeyFeatures = Object.assign(bitfield(32), { exports.pubkeyFeatures = objectAssign(bitfield(32), {
/** /**
* Receiving node expects that the RIPE hash encoded in their address * Receiving node expects that the RIPE hash encoded in their address
* preceedes the encrypted message data of msg messages bound for * preceedes the encrypted message data of msg messages bound for

View File

@ -50,6 +50,6 @@
"buffer-equal": "~0.0.1", "buffer-equal": "~0.0.1",
"eccrypto": "^0.1.2", "eccrypto": "^0.1.2",
"hash.js": "^1.0.2", "hash.js": "^1.0.2",
"object.assign": "^1.1.1" "object-assign": "^2.0.0"
} }
} }

80
test.js
View File

@ -263,44 +263,46 @@ describe("WIF", function() {
}); });
}); });
// FIXME(Kagami): Add more fail tests. describe("High-level classes", function() {
describe("Address", function() { // FIXME(Kagami): Add more fail tests.
it("should decode Bitmessage address", function() { describe("Address", function() {
var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z") it("should decode Bitmessage address", function() {
expect(addr.version).to.equal(4); var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z")
expect(addr.stream).to.equal(1); expect(addr.version).to.equal(4);
expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56"); 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);
}); });
}
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);
});
}
});
}); });