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"
} }
} }

View File

@ -263,8 +263,9 @@ describe("WIF", function() {
}); });
}); });
// FIXME(Kagami): Add more fail tests. describe("High-level classes", function() {
describe("Address", function() { // FIXME(Kagami): Add more fail tests.
describe("Address", function() {
it("should decode Bitmessage address", function() { it("should decode Bitmessage address", function() {
var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z") var addr = Address.decode("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z")
expect(addr.version).to.equal(4); expect(addr.version).to.equal(4);
@ -303,4 +304,5 @@ describe("Address", function() {
expect(ripe.length).to.be.at.most(18); expect(ripe.length).to.be.at.most(18);
}); });
} }
});
}); });