diff --git a/.buildbot/node/Dockerfile b/.buildbot/node/Dockerfile new file mode 100644 index 0000000..c119147 --- /dev/null +++ b/.buildbot/node/Dockerfile @@ -0,0 +1,14 @@ +FROM node:18-bullseye + +RUN apt-get update +RUN apt-get install -yq --no-install-suggests --no-install-recommends \ + xauth xvfb chromium firefox-esr + +WORKDIR /app +ADD src ./src +COPY package*.json binding.gyp ./ + +RUN npm install +# RUN npm audit fix + +COPY . . diff --git a/.buildbot/node/build.sh b/.buildbot/node/build.sh new file mode 100644 index 0000000..da381c9 --- /dev/null +++ b/.buildbot/node/build.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +mkdir -p ../out +cp package*.json ../out +npm pack +mv *.tgz ../out/ diff --git a/.buildbot/node/test.sh b/.buildbot/node/test.sh new file mode 100755 index 0000000..b41801e --- /dev/null +++ b/.buildbot/node/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +npm test diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml new file mode 100644 index 0000000..cc2cd90 --- /dev/null +++ b/.gitea/workflows/test.yaml @@ -0,0 +1,18 @@ +name: Testing +on: [push] + +jobs: + default: + runs-on: ubuntu-latest + steps: + - name: Install dependencies + run: | + apt-get update + apt-get install -yq --no-install-suggests --no-install-recommends \ + xauth xvfb chromium firefox-esr + - name: Check out repository code + uses: actions/checkout@v3 + - name: Install + run: npm install + - name: Run tests + run: npm test diff --git a/README.md b/README.md index f5dcbbe..f4ce541 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ API documentation is available [here](https://bitchan.github.io/bitmessage/docs/ ## References * [Bitmessage wiki](https://bitmessage.org/wiki/Main_Page) -* [Protocol specification](https://bitmessage.org/wiki/Protocol_specification) +* [Protocol specification](https://pybitmessage.rtfd.io/en/v0.6/protocol.html) * [Whitepaper](https://bitmessage.org/bitmessage.pdf) ## Feature matrix @@ -51,7 +51,7 @@ API documentation is available [here](https://bitchan.github.io/bitmessage/docs/ - [x] POW - [x] High-level classes - [x] Address - - [x] UserAgent + - [ ] UserAgent - [ ] PyBitmessage configs parsing - [ ] keys.dat - [ ] knownnodes.dat diff --git a/docker-test.sh b/docker-test.sh new file mode 100755 index 0000000..a752603 --- /dev/null +++ b/docker-test.sh @@ -0,0 +1,3 @@ +#!/bin/sh +docker build --tag bitmessage-js -f .buildbot/node/Dockerfile . +docker run -it --rm bitmessage-js:latest .buildbot/node/test.sh diff --git a/karma.conf.js b/karma.conf.js index 6c2dec6..9525dd8 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,4 +1,4 @@ -process.env.CHROME_BIN = "chromium-browser"; +process.env.CHROME_BIN = "/usr/bin/chromium"; var allTests = process.env.ALL_TESTS === "1"; module.exports = function(config) { diff --git a/lib/_util.js b/lib/_util.js index 86bcab7..989eadc 100644 --- a/lib/_util.js +++ b/lib/_util.js @@ -48,7 +48,7 @@ exports.readTime64BE = function(buf, offset) { }; function writeUInt64BE(buf, value, offset, noAssert) { - buf = buf || new Buffer(8); + buf = buf || Buffer.alloc(8); offset = offset || 0; assert(noAssert || value <= MAX_SAFE_INTEGER, "Unsafe integer"); buf.writeUInt32BE(Math.floor(value / 4294967296), offset, noAssert); @@ -96,12 +96,12 @@ exports.popkey = function(obj, key) { }; // 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 = Buffer.from("00000000000000000000ffff", "hex"); exports.IPv4_MAPPING = IPv4_MAPPING; // Very simple inet_pton(3) equivalent. exports.inet_pton = function(str) { - var buf = new Buffer(16); + var buf = Buffer.alloc(16); buf.fill(0); // IPv4-mapped IPv6. if (str.slice(0, 7) === "::ffff:") { diff --git a/lib/address.js b/lib/address.js index 7f3272c..9051249 100644 --- a/lib/address.js +++ b/lib/address.js @@ -11,7 +11,7 @@ * // Or create it from passphrase. * var addr2 = Address.fromPassphrase("test"); * console.log("Deterministic Bitmessage address:", addr2.encode()); - * @see {@link https://bitmessage.org/wiki/Address} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/address.html} * @module bitmessage/address */ // TODO(Kagami): Document getters/setters. @@ -96,8 +96,8 @@ Address.decode = function(str) { } var bytes = bs58.decode(str); - var data = new Buffer(bytes.slice(0, -4)); - var checksum = new Buffer(bytes.slice(-4)); + var data = Buffer.from(bytes.slice(0, -4)); + var checksum = Buffer.from(bytes.slice(-4)); assert(bufferEqual(checksum, getaddrchecksum(data)), "Bad checkum"); var decoded = var_int.decode(data); @@ -269,7 +269,7 @@ Address.fromRandom = function(opts) { var encPrivateKey, encPublicKey, ripe, len; var signPrivateKey = bmcrypto.getPrivate(); var signPublicKey = bmcrypto.getPublic(signPrivateKey); - var keysbuf = new Buffer(130); + var keysbuf = Buffer.alloc(130); signPublicKey.copy(keysbuf); while (true) { encPrivateKey = bmcrypto.getPrivate(); @@ -313,9 +313,9 @@ Address.fromPassphrase = function(opts) { var ripe, len, tmp; var signnonce = 0; var encnonce = 1; - var keysbuf = new Buffer(130); + var keysbuf = Buffer.alloc(130); // XXX(Kagami): Spec doesn't mention encoding, using UTF-8. - var phrasebuf = new Buffer(passphrase, "utf8"); + var phrasebuf = Buffer.from(passphrase, "utf8"); while (true) { // TODO(Kagami): We may slightly optimize it and pre-create tmp // buffers based on the encoded nonce size (1, 3, 5 and 9 bytes). @@ -409,7 +409,7 @@ Object.defineProperty(Address.prototype, "ripe", { set: function(ripe) { assertripelen(getripelen(ripe), this.version, ripe); if (ripe.length < 20) { - var fullripe = new Buffer(20); + var fullripe = Buffer.alloc(20); fullripe.fill(0); ripe.copy(fullripe, 20 - ripe.length); ripe = fullripe; diff --git a/lib/crypto.js b/lib/crypto.js index 95daf2f..08fa9ef 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -104,17 +104,15 @@ var encrypted = exports.encrypted = { assert(buf.readUInt16BE(16, true) === SECP256K1_TYPE, "Bad curve type"); assert(buf.readUInt16BE(18, true) === 32, "Bad Rx length"); assert(buf.readUInt16BE(52, true) === 32, "Bad Ry length"); - var iv = new Buffer(16); + var iv = Buffer.alloc(16); buf.copy(iv, 0, 0, 16); - var ephemPublicKey = new Buffer(65); - ephemPublicKey[0] = 0x04; - buf.copy(ephemPublicKey, 1, 20, 52); - buf.copy(ephemPublicKey, 33, 54, 86); + var ephemPublicKey = Buffer.alloc(70); + buf.copy(ephemPublicKey, 0, 16, 86); // NOTE(Kagami): We do copy instead of slice to protect against // possible source buffer modification by user. - var ciphertext = new Buffer(buf.length - 118); + var ciphertext = Buffer.alloc(buf.length - 118); buf.copy(ciphertext, 0, 86, buf.length - 32); - var mac = new Buffer(32); + var mac = Buffer.alloc(32); buf.copy(mac, 0, buf.length - 32); return { iv: iv, @@ -126,19 +124,13 @@ var encrypted = exports.encrypted = { encode: function(opts) { assert(opts.iv.length === 16, "Bad IV"); - assert(opts.ephemPublicKey.length === 65, "Bad public key"); + assert(opts.ephemPublicKey.length === 70, "Bad public key"); + assert( + opts.ephemPublicKey.readUInt16BE(0, true) === SECP256K1_TYPE, + "Bad curve type"); assert(opts.mac.length === 32, "Bad MAC"); - // 16 + 2 + 2 + 32 + 2 + 32 + ? + 32 - var buf = new Buffer(118 + opts.ciphertext.length); - opts.iv.copy(buf); - buf.writeUInt16BE(SECP256K1_TYPE, 16, true); // Curve type - buf.writeUInt16BE(32, 18, true); // Rx length - opts.ephemPublicKey.copy(buf, 20, 1, 33); // Rx - buf.writeUInt16BE(32, 52, true); // Ry length - opts.ephemPublicKey.copy(buf, 54, 33); // Ry - opts.ciphertext.copy(buf, 86); - opts.mac.copy(buf, 86 + opts.ciphertext.length); - return buf; + return Buffer.concat( + [opts.iv, opts.ephemPublicKey, opts.ciphertext, opts.mac]); }, }; diff --git a/lib/messages.js b/lib/messages.js index 0b011e7..c173f7c 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -1,8 +1,7 @@ /** * Working with messages. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_types} - * @see {@link https://bitmessage.org/wiki/Protocol_specification_v3#Message_types} - * @see {@link https://bitmessage.org/Bitmessage%20Technical%20Paper.pdf} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#message-types} + * @see {@link https://wiki.bitmessage.org/w/index.php?title=Protocol_specification&oldid=23#Message_types} * @module bitmessage/messages * @example * var structs = require("bitmessage").structs; @@ -30,6 +29,8 @@ * console.log(structs.message.decode(verackmsg).command); // verack */ +// broken link: https://bitmessage.org/Bitmessage%20Technical%20Paper.pdf + "use strict"; var objectAssign = Object.assign || require("object-assign"); @@ -71,7 +72,7 @@ exports.getCommand = function(buf) { /** * `version` message. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#version} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#version} * @namespace * @static */ @@ -133,7 +134,7 @@ var version = exports.version = { var short = {short: true}; var addrRecv = structs.net_addr.decode(buf.slice(20, 46), short); var addrFrom = structs.net_addr.decode(buf.slice(46, 72), short); - var nonce = new Buffer(8); + var nonce = Buffer.alloc(8); buf.copy(nonce, 0, 72, 80); var decodedUa = UserAgent.decode(buf.slice(80)); assert(decodedUa.length <= 5000, "User agent is too long"); @@ -202,7 +203,7 @@ var version = exports.version = { var streams = opts.streams || [1]; assert(streams.length <= 160000, "Too many streams"); // Start encoding. - var protoVersion = new Buffer(4); + var protoVersion = Buffer.alloc(4); protoVersion.writeUInt32BE(util.PROTOCOL_VERSION, 0); var addrRecv = structs.net_addr.encode({ services: services, @@ -229,9 +230,7 @@ var version = exports.version = { }, }; -var IPv6_LOOPBACK = new Buffer( - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] -); +var IPv6_LOOPBACK = Buffer.from("00000000000000000000000000000001", "hex"); // Check whether given encoded IPv6 or IPv4-mapped IPv6 is in private // network range. See @@ -279,7 +278,7 @@ function checkAddrOpts(opts) { /** * `addr` message. Provide information on known nodes of the network. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#addr} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#addr} * @namespace * @static */ @@ -359,7 +358,7 @@ var addr = exports.addr = { /** * `inv` message. Allows a node to advertise its knowledge of one or * more objects. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#inv} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#inv} * @namespace * @static */ @@ -433,7 +432,7 @@ var inv = exports.inv = { * `getdata` message. `getdata` is used in response to an * [inv]{@link module:bitmessage/messages.inv} message to retrieve the * content of a specific object after filtering known elements. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#getdata} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#getdata} * @namespace */ exports.getdata = objectAssign({}, inv, { @@ -566,7 +565,7 @@ var error = exports.error = { assert(rest.length >= vectorLength, "Buffer is too small"); var vector = null; if (vectorLength) { - vector = new Buffer(vectorLength); + vector = Buffer.alloc(vectorLength); rest.copy(vector); rest = rest.slice(vectorLength); } @@ -616,7 +615,7 @@ var error = exports.error = { var type = opts.type || error.WARNING; var banTime = opts.banTime || 0; // TODO(Kagami): Validate vector length. - var vector = opts.vector || new Buffer(0); + var vector = opts.vector || Buffer.alloc(0); return Buffer.concat([ structs.var_int.encode(type), structs.var_int.encode(banTime), diff --git a/lib/objects.js b/lib/objects.js index f83e0ec..8fafedf 100644 --- a/lib/objects.js +++ b/lib/objects.js @@ -2,7 +2,7 @@ * Working with objects. * NOTE: Most operations with objects are asynchronous and return * promises. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Object_types} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#object-types} * @module bitmessage/objects */ @@ -61,7 +61,7 @@ function prependNonce(obj, opts) { opts = objectAssign({}, opts); var nonce, target, powp; if (opts.skipPow) { - nonce = new Buffer(8); + nonce = Buffer.alloc(8); nonce.fill(0); resolve(Buffer.concat([nonce, obj])); } else { @@ -71,7 +71,7 @@ function prependNonce(obj, opts) { .then(function(nonce) { // TODO(Kagami): We may want to receive nonce as a Buffer from // POW module to skip conversion step. - var payload = new Buffer(opts.payloadLength); + var payload = Buffer.alloc(opts.payloadLength); util.writeUInt64BE(payload, nonce, 0, true); obj.copy(payload, 8); return payload; @@ -85,7 +85,7 @@ function prependNonce(obj, opts) { * `getpubkey` object. When a node has the hash of a public key (from an * address) but not the public key itself, it must send out a request * for the public key. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#getpubkey} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#getpubkey} * @namespace * @static */ @@ -193,10 +193,10 @@ function extractPubkey(buf) { // We assume here that input buffer was copied before so it's safe to // return reference to it. decoded.behavior = PubkeyBitfield(buf.slice(0, 4)); - var signPublicKey = decoded.signPublicKey = new Buffer(65); + var signPublicKey = decoded.signPublicKey = Buffer.alloc(65); signPublicKey[0] = 4; buf.copy(signPublicKey, 1, 4, 68); - var encPublicKey = decoded.encPublicKey = new Buffer(65); + var encPublicKey = decoded.encPublicKey = Buffer.alloc(65); encPublicKey[0] = 4; buf.copy(encPublicKey, 1, 68, 132); return decoded; @@ -246,7 +246,7 @@ function findAddrByTag(addrs, tag) { /** * `pubkey` object. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#pubkey} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#pubkey} * @namespace * @static */ @@ -509,20 +509,20 @@ function encodeMessage(opts) { var subject = opts.subject; if (encoding === msg.IGNORE && !message) { // User may omit message for IGNORE encoding. - message = new Buffer(0); + message = Buffer.alloc(0); } else if (!Buffer.isBuffer(message)) { // User may specify message as a string. - message = new Buffer(message, "utf8"); + message = Buffer.from(message, "utf8"); } if (encoding === msg.SIMPLE && subject) { // User may specify subject for SIMPLE encoding. if (!Buffer.isBuffer(subject)) { - subject = new Buffer(subject, "utf8"); + subject = Buffer.from(subject, "utf8"); } message = Buffer.concat([ - new Buffer("Subject:"), + Buffer.from("Subject:"), subject, - new Buffer("\nBody:"), + Buffer.from("\nBody:"), message, ]); } @@ -562,7 +562,7 @@ function decodeMessage(message, encoding) { /** * `msg` object. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#msg} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#msg} * @namespace * @static */ @@ -813,7 +813,7 @@ var msg = exports.msg = { msgData.push(var_int.encode(0)); // Sign and encrypt. - opts.objectPayload = new Buffer(0); + opts.objectPayload = Buffer.alloc(0); var obj = object.encodePayloadWithoutNonce(opts); var dataToSign = Buffer.concat([obj].concat(msgData)); var msgp = bmcrypto @@ -871,7 +871,7 @@ function tryDecryptBroadcastV4(subscriptions, buf) { /** * `broadcast` object. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#broadcast} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#broadcast} * @namespace * @static */ @@ -1119,7 +1119,7 @@ var broadcast = exports.broadcast = { ); // Sign and encrypt. - opts.objectPayload = from.version >= 4 ? from.getTag() : new Buffer(0); + opts.objectPayload = from.version >= 4 ? from.getTag() : Buffer.alloc(0); var obj = object.encodePayloadWithoutNonce(opts); var dataToSign = Buffer.concat([obj].concat(broadData)); var broadp = bmcrypto diff --git a/lib/pow.js b/lib/pow.js index 97bfd5e..f84914b 100644 --- a/lib/pow.js +++ b/lib/pow.js @@ -1,6 +1,6 @@ /** * Implements proof of work. - * @see {@link https://bitmessage.org/wiki/Proof_of_work} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/pow.html} * @module bitmessage/pow */ @@ -64,7 +64,7 @@ exports.check = function(opts) { initialHash = bmcrypto.sha512(opts.payload.slice(8)); } else { if (typeof opts.nonce === "number") { - nonce = new Buffer(8); + nonce = Buffer.alloc(8); // High 32 bits. nonce.writeUInt32BE(Math.floor(opts.nonce / 4294967296), 0, true); // Low 32 bits. diff --git a/lib/structs.js b/lib/structs.js index 1b06d31..c20a568 100644 --- a/lib/structs.js +++ b/lib/structs.js @@ -1,14 +1,14 @@ /** * Implements common structures. * @see {@link - * https://bitmessage.org/wiki/Protocol_specification#Common_structures} + * https://pybitmessage.rtfd.io/en/v0.6/protocol.html#common-structures} * @module bitmessage/structs * @example * var structs = require("bitmessage").structs; * * var encoded = Buffer.concat([ * structs.var_int.encode(4), - * Buffer("test"), + * Buffer.from("test"), * structs.var_str.encode("test2"), * structs.var_int_list.encode([1, 2, 3]), * ]); @@ -93,7 +93,7 @@ function findMagic(buf) { /** * Message structure. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_structure} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#id1} * @namespace * @static */ @@ -137,12 +137,12 @@ var message = exports.message = { if (mindex !== 0) { if (mindex === -1) { res.error = new Error("Magic not found, skipping buffer data"); - res.rest = new Buffer(0); + res.rest = Buffer.alloc(0); } else { res.error = new Error( "Magic in the middle of buffer, skipping some data at start" ); - res.rest = new Buffer(buf.length - mindex); + res.rest = Buffer.alloc(buf.length - mindex); buf.copy(res.rest, 0, mindex); } return res; @@ -155,10 +155,10 @@ var message = exports.message = { if (payloadLength > 1600003) { res.error = new Error("Message is too large, skipping it"); if (buf.length > msgLength) { - res.rest = new Buffer(buf.length - msgLength); + res.rest = Buffer.alloc(buf.length - msgLength); buf.copy(res.rest, 0, msgLength); } else { - res.rest = new Buffer(0); + res.rest = Buffer.alloc(0); } return res; } @@ -169,7 +169,7 @@ var message = exports.message = { } // Now we can set `rest` value. - res.rest = new Buffer(buf.length - msgLength); + res.rest = Buffer.alloc(buf.length - msgLength); buf.copy(res.rest, 0, msgLength); // Command. @@ -190,7 +190,7 @@ var message = exports.message = { command = command.slice(0, firstNonNull).toString("ascii"); // Payload. - var payload = new Buffer(payloadLength); + var payload = Buffer.alloc(payloadLength); buf.copy(payload, 0, 24, msgLength); var checksum = buf.slice(20, 24); if (!bufferEqual(checksum, getmsgchecksum(payload))) { @@ -241,7 +241,7 @@ var message = exports.message = { var checksum = buf.slice(20, 24); // NOTE(Kagami): We do copy instead of slice to protect against // possible source buffer modification by user. - var payload = new Buffer(payloadLength); + var payload = Buffer.alloc(payloadLength); buf.copy(payload, 0, 24, length); assert(bufferEqual(checksum, getmsgchecksum(payload)), "Bad checksum"); var rest = buf.slice(length); @@ -257,9 +257,9 @@ var message = exports.message = { encode: function(command, payload) { assert(command.length <= 12, "Command is too long"); assert(isAscii(command), "Non-ASCII characters in command"); - payload = payload || new Buffer(0); + payload = payload || Buffer.alloc(0); assert(payload.length <= 1600003, "Message payload is too big"); - var buf = new Buffer(24 + payload.length); + var buf = Buffer.alloc(24 + payload.length); buf.fill(0); buf.writeUInt32BE(message.MAGIC, 0, true); buf.write(command, 4); @@ -274,7 +274,7 @@ var message = exports.message = { * An `object` is a message which is shared throughout a stream. It is * the only message which propagates; all others are only between two * nodes. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#object} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#object} * @namespace * @static */ @@ -342,7 +342,7 @@ var object = exports.object = { assert(buf.length <= 262144, "object message payload is too big"); var nonce; if (!opts._validate) { - nonce = new Buffer(8); + nonce = Buffer.alloc(8); buf.copy(nonce, 0, 0, 8); } @@ -371,7 +371,7 @@ var object = exports.object = { if (opts._validate) { return {stream: decodedStream.value}; } - var objectPayload = new Buffer(decodedStream.rest.length); + var objectPayload = Buffer.alloc(decodedStream.rest.length); decodedStream.rest.copy(objectPayload); return { @@ -482,7 +482,7 @@ var object = exports.object = { assert(opts.ttl > 0, "Bad TTL"); assert(opts.ttl <= 2430000, "TTL may not be larger than 28 days + 3 hours"); var expiresTime = util.tnow() + opts.ttl; - var type = new Buffer(4); + var type = Buffer.alloc(4); type.writeUInt32BE(opts.type, 0); var stream = opts.stream || 1; var obj = Buffer.concat([ @@ -499,7 +499,7 @@ var object = exports.object = { /** * Variable length integer. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_integer} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#variable-length-integer} * @namespace * @static */ @@ -569,25 +569,26 @@ var var_int = exports.var_int = { if (typeof value === "number") { assert(value >= 0, "Value cannot be less than zero"); if (value < 253) { - buf = new Buffer([value]); + buf = Buffer.alloc(1); + buf[0] = value; } else if (value < 65536) { - buf = new Buffer(3); + buf = Buffer.alloc(3); buf[0] = 253; buf.writeUInt16BE(value, 1, true); } else if (value < 4294967296) { - buf = new Buffer(5); + buf = Buffer.alloc(5); buf[0] = 254; buf.writeUInt32BE(value, 1, true); } else { assert(value <= 9007199254740991, "Unsafe integer"); - buf = new Buffer(9); + buf = Buffer.alloc(9); buf[0] = 255; buf.writeUInt32BE(Math.floor(value / 4294967296), 1, true); // high32 buf.writeUInt32BE(value % 4294967296, 5, true); // low32 } } else if (Buffer.isBuffer(value)) { assert(value.length <= 8, "Buffer is too big"); - buf = new Buffer(9); + buf = Buffer.alloc(9); buf.fill(0); buf[0] = 255; targetStart = 1 + (8 - value.length); @@ -601,7 +602,7 @@ var var_int = exports.var_int = { /** * Variable length string. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_string} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#variable-length-string} * @namespace */ exports.var_str = { @@ -638,14 +639,14 @@ exports.var_str = { */ encode: function(str) { // XXX(Kagami): Spec doesn't mention encoding, using UTF-8. - var strBuf = new Buffer(str, "utf8"); + var strBuf = Buffer.from(str, "utf8"); return Buffer.concat([var_int.encode(strBuf.length), strBuf]); }, }; /** * Variable length list of integers. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Variable_length_list_of_integers} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#variable-length-list-of-integers} * @namespace */ exports.var_int_list = { @@ -711,7 +712,7 @@ function inet_ntop(buf) { /** * Network address. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Network_address} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#network-address} * @namespace */ exports.net_addr = { @@ -785,10 +786,10 @@ exports.net_addr = { // old browsers). So we use offset instead of `buf = buf.slice`. var buf, shift; if (opts.short) { - buf = new Buffer(26); + buf = Buffer.alloc(26); shift = 0; } else { - buf = new Buffer(38); + buf = Buffer.alloc(38); var time = opts.time || new Date(); time = Math.floor(time.getTime() / 1000); buf.writeUInt32BE(Math.floor(time / 4294967296), 0, true); // high32 @@ -813,7 +814,7 @@ exports.net_addr = { /** * Inventory vector. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Inventory_Vectors} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#inventory-vectors} * @namespace */ exports.inv_vect = { @@ -832,7 +833,7 @@ exports.inv_vect = { /** * Encrypted payload. - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Encrypted_payload} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#encrypted-payload} * @namespace encrypted * @static */ @@ -883,13 +884,13 @@ var Bitfield = function(size) { if (buf) { assert(buf.length === bytesize, "Bad buffer size"); if (opts.copy) { - var dup = new Buffer(bytesize); + var dup = Buffer.alloc(bytesize); dup.fill(0); buf.copy(dup); buf = dup; } } else { - buf = new Buffer(bytesize); + buf = Buffer.alloc(bytesize); buf.fill(0); } this.buffer = buf; @@ -939,7 +940,7 @@ var Bitfield = function(size) { /** * Service features bitfield (MSB 0). - * @see {@link https://bitmessage.org/wiki/Protocol_specification#version} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#version} * @param {Buffer=} buf - A 8-byte bitfield buffer (will be created if * not provided or will be copied if `opts.copy` is `true`) * @param {Object=} opts - Options @@ -1005,7 +1006,7 @@ var ServicesBitfield = exports.ServicesBitfield = objectAssign(Bitfield(64), { /** * Pubkey features bitfield (MSB 0). - * @see {@link https://bitmessage.org/wiki/Protocol_specification#Pubkey_bitfield_features} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/protocol.html#pubkey-bitfield-features} * @param {Buffer=} buf - A 4-byte bitfield buffer (will be created if * not provided or will be copied if `opts.copy` is `true`) * @param {Object=} opts - Options diff --git a/lib/user-agent.js b/lib/user-agent.js index f1f05c5..131a8da 100644 --- a/lib/user-agent.js +++ b/lib/user-agent.js @@ -1,13 +1,13 @@ /** * Working with Bitmessage user agents. - * @see {@link https://bitmessage.org/wiki/User_Agent} + * @see {@link https://pybitmessage.rtfd.io/en/v0.6/useragent.html} * @module bitmessage/user-agent */ "use strict"; var var_str = require("./structs").var_str; -var BM_NAME = require("../package.json").name; +var BM_NAME = require("../package.json").name.replace("@bitmessage/", ""); var BM_VERSION = require("../package.json").version; /** diff --git a/lib/wif.js b/lib/wif.js index 79fba9b..b20d915 100644 --- a/lib/wif.js +++ b/lib/wif.js @@ -25,8 +25,8 @@ function getwifchecksum(data) { exports.decode = function(wif) { var bytes = bs58.decode(wif); assert(bytes[0] === 0x80, "Bad WIF"); - var data = new Buffer(bytes.slice(0, -4)); - var checksum = new Buffer(bytes.slice(-4)); + var data = Buffer.from(bytes.slice(0, -4)); + var checksum = Buffer.from(bytes.slice(-4)); assert(bufferEqual(checksum, getwifchecksum(data)), "Bad checkum"); return data.slice(1); }; @@ -38,7 +38,7 @@ exports.decode = function(wif) { * @return {string} WIF-encoded private key. */ exports.encode = function(privateKey) { - var data = Buffer.concat([new Buffer([0x80]), privateKey]); + var data = Buffer.concat([Buffer.from([0x80]), privateKey]); var checksum = getwifchecksum(data); var bytes = Buffer.concat([data, checksum]); return bs58.encode(bytes); diff --git a/lib/worker.browser.js b/lib/worker.browser.js index 4a33243..8390bdf 100644 --- a/lib/worker.browser.js +++ b/lib/worker.browser.js @@ -13,7 +13,7 @@ function sha512(buf) { function pow(opts) { var nonce = opts.num; var poolSize = opts.poolSize; - var message = new Buffer(72); + var message = Buffer.alloc(72); message.fill(0); Buffer(opts.initialHash).copy(message, 8); var targetHi = Math.floor(opts.target / 4294967296); diff --git a/package.json b/package.json index 6ac4b49..ad1c507 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "bitmessage", - "version": "0.6.6", + "name": "@bitmessage/bitmessage", + "version": "0.6.7", "description": "JavaScript Bitmessage library", "main": "./lib/index.js", "browser": { @@ -8,7 +8,7 @@ }, "scripts": { "install": "node-gyp rebuild || exit 0", - "test": "ALL_TESTS=1 mocha && ALL_TESTS=1 xvfb-run -a karma start --browsers Chromium && ALL_TESTS=1 xvfb-run -a karma start --browsers Firefox && jshint .", + "test": "ALL_TESTS=1 mocha && xvfb-run -a karma start && jshint .", "m": "mocha", "kc": "xvfb-run -a karma start --browsers Chromium", "kf": "xvfb-run -a karma start --browsers Firefox", @@ -18,7 +18,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/bitchan/bitmessage.git" + "url": "https://git.bitmessage.org/Bitmessage/bitmessage-js.git" }, "keywords": [ "bitmessage", @@ -29,36 +29,37 @@ "author": "Kagami Hiiragi", "license": "CC0-1.0", "bugs": { - "url": "https://github.com/bitchan/bitmessage/issues" + "url": "https://git.bitmessage.org/Bitmessage/bitmessage-js/issues" }, - "homepage": "https://github.com/bitchan/bitmessage", + "homepage": "https://git.bitmessage.org/Bitmessage/bitmessage-js", "devDependencies": { - "chai": "*", - "jsdoc": "^3.3.2", + "browserify": "16.5.0", + "chai": "~4.3.4", + "jsdoc": "^4.0.2", "jshint": "*", - "karma": "^0.12.31", - "karma-browserify": "^2.0.0", - "karma-chrome-launcher": "^0.1.7", - "karma-cli": "~0.0.4", + "karma": "6.4.1", + "karma-browserify": "8.1.0", + "karma-chrome-launcher": "3.1.0", + "karma-cli": "2.0.0", "karma-env-preprocessor": "^0.1.0", - "karma-firefox-launcher": "^0.1.4", - "karma-mocha": "^0.1.10", - "karma-mocha-reporter": "^0.3.1", + "karma-firefox-launcher": "1.2.0", + "karma-mocha": "2.0.1", + "karma-mocha-reporter": "2.2.5", "mocha": "*" }, "dependencies": { "bn.js": "^3.0.1", "bs58": "^2.0.0", "buffer-equal": "~0.0.1", - "eccrypto": "^1.0.3", - "es6-promise": "^3.0.2", + "eccrypto": "git+https://git.bitmessage.org/Bitmessage/eccrypto.git", + "es6-promise": "4.2.8", "hash.js": "^1.0.2", - "nan": "^2.1.0", + "nan": "~2.15.0", "object-assign": "^2.0.0", "sha.js": "^2.3.1", "webworkify": "^1.0.1" }, "optionalDependencies": { - "bignum": "^0.11.0" + "bignum": "^0.13.1" } } diff --git a/src/worker.cc b/src/worker.cc index 8c4cf70..e7abd87 100644 --- a/src/worker.cc +++ b/src/worker.cc @@ -2,7 +2,7 @@ #include #include "./pow.h" -using v8::Handle; +// using v8::Handle; using v8::Local; using v8::FunctionTemplate; using v8::Function; @@ -71,8 +71,8 @@ NAN_METHOD(PowAsync) { return Nan::ThrowError("Bad input"); } - size_t pool_size = info[0]->Uint32Value(); - uint64_t target = info[1]->IntegerValue(); + size_t pool_size = (size_t)Nan::To(info[0]).FromJust(); + uint64_t target = Nan::To(info[1]).FromJust(); char* buf = node::Buffer::Data(info[2]); size_t length = node::Buffer::Length(info[2]); if (pool_size < 1 || diff --git a/test.js b/test.js index 959056f..c87b95c 100644 --- a/test.js +++ b/test.js @@ -35,19 +35,19 @@ var skipPow = {skipPow: true}; describe("Crypto", function() { it("should implement SHA-1 hash", function() { - expect(bmcrypto.sha1(Buffer("test")).toString("hex")).to.equal("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); + expect(bmcrypto.sha1(Buffer.from("test")).toString("hex")).to.equal("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"); }); it("should implement SHA-256 hash", function() { - expect(bmcrypto.sha256(Buffer("test")).toString("hex")).to.equal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); + expect(bmcrypto.sha256(Buffer.from("test")).toString("hex")).to.equal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); }); it("should implement SHA-512 hash", function() { - expect(bmcrypto.sha512(Buffer("test")).toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"); + expect(bmcrypto.sha512(Buffer.from("test")).toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"); }); it("should implement RIPEMD-160 hash", function() { - expect(bmcrypto.ripemd160(Buffer("test")).toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107"); + expect(bmcrypto.ripemd160(Buffer.from("test")).toString("hex")).to.equal("5e52fee47e6b070565f74372468cdc699de89107"); }); it("should implement cryptographically secure PRNG", function() { @@ -84,16 +84,16 @@ describe("Crypto", function() { }); it("should allow to convert private key to public", function() { - var privateKey = Buffer(32); + var privateKey = Buffer.alloc(32); privateKey.fill(1); expect(bmcrypto.getPublic(privateKey).toString("hex")).to.equal("041b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f70beaf8f588b541507fed6a642c5ab42dfdf8120a7f639de5122d47a69a8e8d1"); }); it("should allow to sign and verify message", function() { - var privateKey = Buffer(32); + var privateKey = Buffer.alloc(32); privateKey.fill(1); var publicKey = bmcrypto.getPublic(privateKey); - var message = Buffer("test"); + var message = Buffer.from("test"); return bmcrypto.sign(privateKey, message).then(function(sig) { expect(Buffer.isBuffer(sig)).to.be.true; expect(sig.toString("hex")).to.equal("304402204737396b697e5a3400e3aedd203d8be89879f97708647252bd0c17752ff4c8f302201d52ef234de82ce0719679fa220334c83b80e21b8505a781d32d94a27d9310aa"); @@ -104,7 +104,7 @@ describe("Crypto", function() { it("should allow to encrypt and decrypt message", function() { var privateKeyA = bmcrypto.getPrivate(); var publicKeyA = bmcrypto.getPublic(privateKeyA); - return bmcrypto.encrypt(publicKeyA, Buffer("msg to a")).then(function(buf) { + return bmcrypto.encrypt(publicKeyA, Buffer.from("msg to a")).then(function(buf) { expect(Buffer.isBuffer(buf)).to.be.true; return bmcrypto.decrypt(privateKeyA, buf).then(function(plaintext) { expect(Buffer.isBuffer(plaintext)).to.be.true; @@ -118,22 +118,28 @@ describe("Common structures", function() { describe("message", function() { it("should decode", function() { var res; - res = message.decode(Buffer("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164", "hex")); + res = message.decode(Buffer.from( + "e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164", "hex" + )); expect(res.command).to.equal("test"); expect(res.payload.toString()).to.equal("payload"); expect(res.length).to.equal(31); expect(res.rest.toString("hex")).to.equal(""); - res = message.decode(Buffer("e9beb4d90000000000000000000000000000000770b33ce97061796c6f6164", "hex")); + res = message.decode(Buffer.from( + "e9beb4d90000000000000000000000000000000770b33ce97061796c6f6164", "hex" + )); expect(res.command).to.equal(""); }); it("should throw when decoding message with truncated payload", function() { - expect(message.decode.bind(null, Buffer("e9beb4d97465737400000000000000000000000770b33ce97061796c6f61", "hex"))).to.throw(Error); + expect(message.decode.bind(null, Buffer.from( + "e9beb4d97465737400000000000000000000000770b33ce97061796c6f61", "hex")) + ).to.throw(Error); }); it("should encode", function() { - expect(message.encode("test", Buffer("payload")).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164"); + expect(message.encode("test", Buffer.from("payload")).toString("hex")).to.equal("e9beb4d97465737400000000000000000000000770b33ce97061796c6f6164"); }); it("should encode empty payload without second argument", function() { @@ -143,29 +149,29 @@ describe("Common structures", function() { }); it("should decode messages in stream mode", function() { - var res = message.tryDecode(Buffer("")); + var res = message.tryDecode(Buffer.from("")); expect(res).to.not.exist; - res = message.tryDecode(Buffer(25)); + res = message.tryDecode(Buffer.alloc(25)); expect(res.error).to.match(/magic not found/i); expect(res.rest.toString("hex")).to.equal(""); expect(res).to.not.have.property("message"); - res = message.tryDecode(message.encode("test", Buffer([1,2,3]))); + res = message.tryDecode(message.encode("test", Buffer.from("010203", "hex"))); expect(res).to.not.have.property("error"); expect(res.message.command).to.equal("test"); expect(res.message.payload.toString("hex")).to.equal("010203"); expect(res.rest.toString("hex")).to.equal(""); - var encoded = message.encode("cmd", Buffer("buf")); + var encoded = message.encode("cmd", Buffer.from("buf")); encoded[20] ^= 1; // Corrupt checksum - encoded = Buffer.concat([encoded, Buffer("rest")]); + encoded = Buffer.concat([encoded, Buffer.from("rest")]); res = message.tryDecode(encoded); expect(res.error).to.match(/bad checksum/i); expect(res.rest.toString()).to.equal("rest"); expect(res).to.not.have.property("message"); - encoded = Buffer.concat([Buffer(10), encoded]); + encoded = Buffer.concat([Buffer.alloc(10), encoded]); res = message.tryDecode(encoded); expect(res.error).to.match(/magic in the middle/i); expect(res.rest).to.have.length(31); @@ -174,7 +180,7 @@ describe("Common structures", function() { }); it("should check for max payload length", function() { - var fn = message.encode.bind(null, "test", Buffer(2000000)); + var fn = message.encode.bind(null, "test", Buffer.alloc(2000000)); expect(fn).to.throw(/payload is too big/i); var bigmsg = message.encode("test"); @@ -186,14 +192,14 @@ describe("Common structures", function() { describe("object", function() { it("should encode and decode", function() { - var nonce = Buffer(8); + var nonce = Buffer.alloc(8); var now = new Date().getTime(); var res = object.decode(object.encode({ nonce: nonce, ttl: 100, type: 2, version: 1, - objectPayload: Buffer("test"), + objectPayload: Buffer.from("test"), }), skipPow); expect(bufferEqual(nonce, res.nonce)).to.be.true; @@ -209,43 +215,43 @@ describe("Common structures", function() { it("shouldn't encode too big TTL", function() { expect(object.encode.bind(null, { - nonce: Buffer(8), + nonce: Buffer.alloc(8), ttl: 10000000, type: 2, version: 1, - objectPayload: Buffer("test"), + objectPayload: Buffer.from("test"), })).to.throw(Error); }); it("shouldn't encode message payload bigger than 2^18 bytes", function() { expect(object.encodePayload.bind(null, { - nonce: Buffer(8), + nonce: Buffer.alloc(8), ttl: 100, type: object.MSG, version: 1, - objectPayload: Buffer(300000), + objectPayload: Buffer.alloc(300000), })).to.throw(/too big/i); }); it("shouldn't decode message payload bigger than 2^18 bytes", function() { var encoded = object.encodePayload({ - nonce: Buffer(8), + nonce: Buffer.alloc(8), ttl: 100, type: object.MSG, version: 1, - objectPayload: Buffer("test"), + objectPayload: Buffer.from("test"), }); - encoded = Buffer.concat([encoded, Buffer(300000)]); + encoded = Buffer.concat([encoded, Buffer.alloc(300000)]); expect(object.decodePayload.bind(null, encoded)).to.throw(/too big/i); }); it("shouldn't decode object with insufficient nonce", function() { expect(object.decode.bind(null, object.encode({ - nonce: Buffer(8), + nonce: Buffer.alloc(8), ttl: 100, type: 2, version: 1, - objectPayload: Buffer("test"), + objectPayload: Buffer.from("test"), }))).to.throw(/insufficient/i); }); @@ -255,11 +261,11 @@ describe("Common structures", function() { expect(err.message).to.match(/not an object/i); var obj = object.encodePayload({ - nonce: Buffer(8), + nonce: Buffer.alloc(8), ttl: 111, type: object.MSG, version: 1, - objectPayload: Buffer(0), + objectPayload: Buffer.alloc(0), }); err = object.validatePayload(obj); expect(err.message).to.match(/insufficient pow/i); @@ -269,11 +275,11 @@ describe("Common structures", function() { it("should allow to validate stream", function() { var obj = object.encodePayload({ - nonce: Buffer(8), + nonce: Buffer.alloc(8), ttl: 111, type: object.MSG, version: 1, - objectPayload: Buffer(0), + objectPayload: Buffer.alloc(0), }); var err = object.validatePayload(obj, {skipPow: true, stream: 3}); expect(err.message).to.match(/stream.*is not the one/i); @@ -283,35 +289,35 @@ describe("Common structures", function() { describe("var_int", function() { it("should decode", function() { var res; - expect(var_int.decode.bind(null, Buffer([]))).to.throw(Error); - expect(var_int.decode.bind(null, Buffer("fd00", "hex"))).to.throw(Error); - expect(var_int.decode.bind(null, Buffer("ff004170706e9b0368", "hex"))).to.throw(Error); + expect(var_int.decode.bind(null, Buffer.alloc(0))).to.throw(Error); + expect(var_int.decode.bind(null, Buffer.from("fd00", "hex"))).to.throw(Error); + expect(var_int.decode.bind(null, Buffer.from("ff004170706e9b0368", "hex"))).to.throw(Error); - res = var_int.decode(Buffer([123])); + res = var_int.decode(Buffer.from("7b", "hex")); expect(res.value).to.equal(123); expect(res.length).to.equal(1); - expect(res.rest.toString("hex")).to.equal("") + expect(res.rest.toString("hex")).to.equal(""); - res = var_int.decode(Buffer("fd123456", "hex")); + res = var_int.decode(Buffer.from("fd123456", "hex")); expect(res.value).to.equal(0x1234); expect(res.length).to.equal(3); expect(res.rest.toString("hex")).to.equal("56"); - res = var_int.decode(Buffer("fe1234567890", "hex")); + res = var_int.decode(Buffer.from("fe1234567890", "hex")); expect(res.value).to.equal(0x12345678); expect(res.length).to.equal(5); expect(res.rest.toString("hex")).to.equal("90"); - res = var_int.decode(Buffer("ff0000001234567890", "hex")); + res = var_int.decode(Buffer.from("ff0000001234567890", "hex")); expect(res.value).to.equal(0x1234567890); expect(res.length).to.equal(9); expect(res.rest.length).to.equal(0); }); it("should check for lowest length on decode", function() { - expect(var_int.decode.bind(null, Buffer("fd00fc", "hex"))).to.throw(Error); - expect(var_int.decode.bind(null, Buffer("fe0000ffff", "hex"))).to.throw(Error); - expect(var_int.decode.bind(null, Buffer("ff00000000ffffffff", "hex"))).to.throw(Error); + expect(var_int.decode.bind(null, Buffer.from("fd00fc", "hex"))).to.throw(Error); + expect(var_int.decode.bind(null, Buffer.from("fe0000ffff", "hex"))).to.throw(Error); + expect(var_int.decode.bind(null, Buffer.from("ff00000000ffffffff", "hex"))).to.throw(Error); }); it("should encode", function() { @@ -319,10 +325,10 @@ describe("Common structures", function() { expect(var_int.encode(0x1234).toString("hex")).to.equal("fd1234"); expect(var_int.encode(0x12345678).toString("hex")).to.equal("fe12345678"); expect(var_int.encode(0x1234567890).toString("hex")).to.equal("ff0000001234567890"); - expect(var_int.encode(Buffer("1234567890", "hex")).toString("hex")).to.equal("ff0000001234567890"); + expect(var_int.encode(Buffer.from("1234567890", "hex")).toString("hex")).to.equal("ff0000001234567890"); expect(var_int.encode.bind(null, -123)).to.throw(Error); expect(var_int.encode.bind(null, 0x4170706e9b0368)).to.throw(Error); - expect(var_int.encode.bind(null, Buffer("123456789012345678", "hex"))).to.throw(Error); + expect(var_int.encode.bind(null, Buffer.from("123456789012345678", "hex"))).to.throw(Error); expect(var_int.encode.bind(null, "test")).to.throw(Error); }); }); @@ -330,23 +336,23 @@ describe("Common structures", function() { describe("var_str", function() { it("should decode", function() { var res; - res = var_str.decode(Buffer("00", "hex")); + res = var_str.decode(Buffer.from("00", "hex")); expect(res.str).to.equal(""); expect(res.length).to.equal(1); expect(res.rest.toString("hex")).to.equal(""); - res = var_str.decode(Buffer("0474657374", "hex")); + res = var_str.decode(Buffer.from("0474657374", "hex")); expect(res.str).to.equal("test"); expect(res.length).to.equal(5); expect(res.rest.toString("hex")).to.equal(""); - res = var_str.decode(Buffer("0474657374ffffff", "hex")); + res = var_str.decode(Buffer.from("0474657374ffffff", "hex")); expect(res.str).to.equal("test"); expect(res.length).to.equal(5); expect(res.rest.toString("hex")).to.equal("ffffff"); // Truncated input. - expect(var_str.decode.bind(null, Buffer("04746573", "hex"))).to.throw(Error); + expect(var_str.decode.bind(null, Buffer.from("04746573", "hex"))).to.throw(Error); }); it("should encode", function() { @@ -358,12 +364,13 @@ describe("Common structures", function() { describe("var_int_list", function() { it("should decode", function() { var res; - res = var_int_list.decode(Buffer("00", "hex")); + res = var_int_list.decode(Buffer.from("00", "hex")); expect(res.list).to.deep.equal([]); expect(res.length).to.equal(1); expect(res.rest.toString("hex")).to.equal(""); - res = var_int_list.decode(Buffer("0501fd0400ff0004000000000000fd9c40fe000186a0", "hex")); + res = var_int_list.decode(Buffer.from( + "0501fd0400ff0004000000000000fd9c40fe000186a0", "hex")); expect(res.length).to.equal(22); expect(res.list.length).to.equal(5); expect(res.list[0]).to.equal(1); @@ -373,13 +380,15 @@ describe("Common structures", function() { expect(res.list[4]).to.equal(100000); expect(res.rest.toString("hex")).to.equal(""); - res = var_int_list.decode(Buffer("0501fd0400ff0004000000000000fd9c40fe000186a0ffffff", "hex")); + res = var_int_list.decode(Buffer.from( + "0501fd0400ff0004000000000000fd9c40fe000186a0ffffff", "hex")); expect(res.length).to.equal(22); expect(res.list.length).to.equal(5); expect(res.rest.toString("hex")).to.equal("ffffff"); // Truncated input. - expect(var_int_list.decode.bind(null, Buffer("0501fd0400ff0004000000000000fd9c40fe000186", "hex"))).to.throw(Error); + expect(var_int_list.decode.bind(null, Buffer.from( + "0501fd0400ff0004000000000000fd9c40fe000186", "hex"))).to.throw(Error); }); it("should encode", function() { @@ -393,21 +402,22 @@ describe("Common structures", function() { describe("net_addr", function() { it("should decode", function() { var res; - res = net_addr.decode(Buffer("0000000054aaf6c000000001000000000000000100000000000000000000ffff7f00000120fc", "hex")); + res = net_addr.decode(Buffer.from("0000000054aaf6c000000001000000000000000100000000000000000000ffff7f00000120fc", "hex")); expect(res.time.getTime()).to.equal(1420490432000); expect(res.stream).to.equal(1); expect(res.services.get(ServicesBitfield.NODE_NETWORK)).to.be.true; expect(res.host).to.equal("127.0.0.1"); expect(res.port).to.equal(8444); - expect(net_addr.decode.bind(null, Buffer("000000000000000100000000000000000000ffff7f00000120fc", "hex"))).to.throw(Error);; + expect(net_addr.decode.bind(null, Buffer.from( + "000000000000000100000000000000000000ffff7f00000120fc", "hex"))).to.throw(Error); - res = net_addr.decode(Buffer("000000000000000100000000000000000000ffff7f00000120fc", "hex"), {short: true}); + res = net_addr.decode(Buffer.from("000000000000000100000000000000000000ffff7f00000120fc", "hex"), {short: true}); expect(res.services.get(ServicesBitfield.NODE_NETWORK)).to.be.true; expect(res.host).to.equal("127.0.0.1"); expect(res.port).to.equal(8444); - res = net_addr.decode(Buffer("000000000000000100000000000000000000000000000001fde8", "hex"), {short: true}); + res = net_addr.decode(Buffer.from("000000000000000100000000000000000000000000000001fde8", "hex"), {short: true}); expect(res.services.get(ServicesBitfield.NODE_NETWORK)).to.be.true; expect(res.host).to.equal("0:0:0:0:0:0:0:1"); expect(res.port).to.equal(65000); @@ -431,7 +441,7 @@ describe("Common structures", function() { }); it("should allow to pass services as Buffer", function() { - var services = Buffer(8); + var services = Buffer.alloc(8); var res = net_addr.decode(net_addr.encode({ host: "1.2.3.4", port: 1234, @@ -449,11 +459,13 @@ describe("Common structures", function() { describe("encrypted", function() { it("should encode and decode", function() { - var iv = Buffer(16); - var ephemPublicKey = Buffer(65); - ephemPublicKey[0] = 0x04; - var ciphertext = Buffer("test"); - var mac = Buffer(32); + var iv = Buffer.alloc(16); + var ephemPublicKey = Buffer.alloc(70); + ephemPublicKey.writeUInt16BE(714, 0, true); + ephemPublicKey.writeUInt16BE(32, 2, true); + ephemPublicKey.writeUInt16BE(32, 36, true); + var ciphertext = Buffer.from("test"); + var mac = Buffer.alloc(32); var inopts = { iv: iv, ephemPublicKey: ephemPublicKey, @@ -473,7 +485,7 @@ describe("Common structures", function() { describe("service features", function() { it("should allow to check bits", function() { - expect(ServicesBitfield(Buffer("0000000000000001", "hex")).get(ServicesBitfield.NODE_NETWORK)).to.be.true; + expect(ServicesBitfield(Buffer.from("0000000000000001", "hex")).get(ServicesBitfield.NODE_NETWORK)).to.be.true; }); it("should allow to set bits", function() { @@ -492,7 +504,7 @@ describe("Common structures", function() { describe("pubkey features", function() { it("should allow to check bits", function() { - expect(PubkeyBitfield(Buffer("00000003", "hex")).get([PubkeyBitfield.DOES_ACK, PubkeyBitfield.INCLUDE_DESTINATION])).to.be.true; + expect(PubkeyBitfield(Buffer.from("00000003", "hex")).get([PubkeyBitfield.DOES_ACK, PubkeyBitfield.INCLUDE_DESTINATION])).to.be.true; }); it("should allow to set bits", function() { @@ -505,14 +517,14 @@ describe("Common structures", function() { // TODO(Kagami): Add tests for encodePayload/decodePayload as well. describe("Message types", function() { it("should get command for encoded message", function() { - var encoded = message.encode("test", Buffer(0)); + var encoded = message.encode("test", Buffer.alloc(0)); expect(messages.getCommand(encoded)).to.equal("test"); - expect(messages.getCommand(Buffer("test"))).to.be.undefined; + expect(messages.getCommand(Buffer.from("test"))).to.be.undefined; }); describe("version", function() { it("should encode and decode", function() { - var nonce = Buffer(8); + var nonce = Buffer.alloc(8); var encoded = version.encode({ remoteHost: "1.2.3.4", remotePort: 48444, @@ -547,7 +559,7 @@ describe("Message types", function() { expect(version.encode.bind(null, { remoteHost: "1.2.3.4", remotePort: 8444, - userAgent: Buffer(6000), + userAgent: Buffer.alloc(6000), })).to.throw(/agent is too long/i); }); @@ -603,7 +615,7 @@ describe("Message types", function() { {host: "fc00::3:2:1", port: 9}, ]).toString("hex")).to.equal("010000000054f04bfe00000001000000000000000100000000000000000000ffff010203040003"); - var res = addr.decodePayload(Buffer("090000000054f04b9b00000001000000000000000100000000000000000000ffff7f00000100010000000054f04b9b00000001000000000000000100000000000000000000ffff7f05030100020000000054f04b9b00000001000000000000000100000000000000000000ffff0102030400030000000054f04b9b00000001000000000000000100000000000000000000ffffc0a80f1400040000000054f04b9b00000001000000000000000100000000000000000000ffff0a0a0a0a00050000000054f04b9b00000001000000000000000100000000000000000000ffffac112a0100060000000054f04b9b0000000100000000000000010000000000000000000000000000000100070000000054f04b9b000000010000000000000001fe80000000000000000000010002000300080000000054f04b9b000000010000000000000001fc0000000000000000000003000200010009", "hex")); + var res = addr.decodePayload(Buffer.from("090000000054f04b9b00000001000000000000000100000000000000000000ffff7f00000100010000000054f04b9b00000001000000000000000100000000000000000000ffff7f05030100020000000054f04b9b00000001000000000000000100000000000000000000ffff0102030400030000000054f04b9b00000001000000000000000100000000000000000000ffffc0a80f1400040000000054f04b9b00000001000000000000000100000000000000000000ffff0a0a0a0a00050000000054f04b9b00000001000000000000000100000000000000000000ffffac112a0100060000000054f04b9b0000000100000000000000010000000000000000000000000000000100070000000054f04b9b000000010000000000000001fe80000000000000000000010002000300080000000054f04b9b000000010000000000000001fc0000000000000000000003000200010009", "hex")); expect(res.addrs).to.have.length(1); expect(res.addrs[0].host).to.equal("1.2.3.4"); expect(res.addrs[0].port).to.equal(3); @@ -612,8 +624,8 @@ describe("Message types", function() { describe("inv", function() { it("should encode and decode", function() { - var vect1 = inv_vect.encode(Buffer("test")); - var vect2 = inv_vect.encode(Buffer("test2")); + var vect1 = inv_vect.encode(Buffer.from("test")); + var vect2 = inv_vect.encode(Buffer.from("test2")); var vectors = [vect1, vect2]; var encoded = inv.encode(vectors); expect(message.decode(encoded).command).to.equal("inv"); @@ -632,8 +644,8 @@ describe("Message types", function() { describe("getdata", function() { it("should encode and decode", function() { - var vect1 = inv_vect.encode(Buffer("test")); - var vect2 = inv_vect.encode(Buffer("test2")); + var vect1 = inv_vect.encode(Buffer.from("test")); + var vect2 = inv_vect.encode(Buffer.from("test2")); var vectors = [vect1, vect2]; var encoded = getdata.encode(vectors); expect(message.decode(encoded).command).to.equal("getdata"); @@ -662,7 +674,7 @@ describe("Message types", function() { expect(res.length).to.equal(8); expect(error.type2str(res.type)).to.equal("warning"); - var vector = inv_vect.encode(Buffer("test")); + var vector = inv_vect.encode(Buffer.from("test")); var res = error.decode(error.encode({ type: error.FATAL, banTime: 120, @@ -681,9 +693,11 @@ describe("Message types", function() { // TODO(Kagami): Add tests for encodePayloadAsync/decodePayloadAsync as well. describe("Object types", function() { - var signPrivateKey = Buffer("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"); + var signPrivateKey = Buffer.from( + "71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"); var signPublicKey = bmcrypto.getPublic(signPrivateKey); - var encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); + var encPrivateKey = Buffer.from( + "9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); var encPublicKey = bmcrypto.getPublic(encPrivateKey); var fromV2 = Address({ version: 2, @@ -703,26 +717,26 @@ describe("Object types", function() { it("should get type of the encoded object message", function() { var encoded = object.encode({ - nonce: Buffer(8), + nonce: Buffer.alloc(8), ttl: 100, type: object.BROADCAST, version: 1, - objectPayload: Buffer("test"), + objectPayload: Buffer.from("test"), }); expect(objects.getType(encoded)).to.equal(object.BROADCAST); - expect(objects.getType(Buffer(4))).to.be.undefined; + expect(objects.getType(Buffer.alloc(4))).to.be.undefined; }); it("should get type of the object message payload", function() { var encoded = object.encodePayload({ - nonce: Buffer(8), + nonce: Buffer.alloc(8), ttl: 333, type: object.MSG, version: 1, - objectPayload: Buffer("test"), + objectPayload: Buffer.from("test"), }); expect(objects.getPayloadType(encoded)).to.equal(object.MSG); - expect(objects.getPayloadType(Buffer(7))).to.be.undefined; + expect(objects.getPayloadType(Buffer.alloc(7))).to.be.undefined; }); describe("getpubkey", function() { @@ -762,14 +776,13 @@ describe("Object types", function() { }); }); - it("shouldn't decode getpubkey with insufficient nonce", function(done) { + it("shouldn't decode getpubkey with insufficient nonce", function() { return getpubkey.encodeAsync({ ttl: 100, to: "2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z", skipPow: true, }).then(getpubkey.decodeAsync).catch(function(err) { expect(err.message).to.match(/insufficient/i); - done(); }); }); @@ -780,7 +793,7 @@ describe("Object types", function() { ttl: 100, to: "2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z", }).then(function(payload) { - expect(POW.check({ttl: 100, payload: payload})).to.be.true;; + expect(POW.check({ttl: 100, payload: payload})).to.be.true; return getpubkey.decodePayloadAsync(payload); }).then(function(res) { expect(res.ttl).to.be.at.most(100); @@ -864,7 +877,7 @@ describe("Object types", function() { this.timeout(300000); return pubkey.encodePayloadAsync({ttl: 789, from: from, to: from}) .then(function(payload) { - expect(POW.check({ttl: 789, payload: payload})).to.be.true;; + expect(POW.check({ttl: 789, payload: payload})).to.be.true; return pubkey.decodePayloadAsync(payload, {needed: from}); }).then(function(res) { expect(res.ttl).to.be.at.most(789); @@ -935,7 +948,7 @@ describe("Object types", function() { }); }); - it("shouldn't decode msg without identities", function(done) { + it("shouldn't decode msg without identities", function() { return msg.encodeAsync({ ttl: 111, from: from, @@ -946,7 +959,6 @@ describe("Object types", function() { return msg.decodeAsync(buf, {identities: [], skipPow: true}); }).catch(function(err) { expect(err.message).to.match(/with given identities/i); - done(); }); }); @@ -968,16 +980,15 @@ describe("Object types", function() { }); }); - it("shouldn't encode too big msg", function(done) { + it("shouldn't encode too big msg", function() { return msg.encodeAsync({ ttl: 111, from: from, to: from, - message: Buffer(300000), + message: Buffer.alloc(300000), skipPow: true, }).catch(function(err) { expect(err.message).to.match(/too big/i); - done(); }); }); @@ -990,7 +1001,7 @@ describe("Object types", function() { to: from, message: "test", }).then(function(payload) { - expect(POW.check({ttl: 111, payload: payload})).to.be.true;; + expect(POW.check({ttl: 111, payload: payload})).to.be.true; return msg.decodePayloadAsync(payload, {identities: from}); }).then(function(res) { expect(res.ttl).to.be.at.most(111); @@ -1085,7 +1096,7 @@ describe("Object types", function() { }); }); - it("shouldn't decode broadcast without subscriptions", function(done) { + it("shouldn't decode broadcast without subscriptions", function() { return broadcast.encodeAsync({ ttl: 101, from: from, @@ -1098,19 +1109,17 @@ describe("Object types", function() { }); }).catch(function(err) { expect(err.message).to.match(/not interested/i); - done(); }); }); - it("shouldn't encode too big broadcast", function(done) { + it("shouldn't encode too big broadcast", function() { return broadcast.encodeAsync({ ttl: 101, from: from, - message: Buffer(300000), + message: Buffer.alloc(300000), skipPow: true, }).catch(function(err) { expect(err.message).to.match(/too big/i); - done(); }); }); @@ -1122,7 +1131,7 @@ describe("Object types", function() { from: from, message: "test", }).then(function(payload) { - expect(POW.check({ttl: 101, payload: payload})).to.be.true;; + expect(POW.check({ttl: 101, payload: payload})).to.be.true; return broadcast.decodePayloadAsync(payload, {subscriptions: from}); }).then(function(res) { expect(res.ttl).to.be.at.most(101); @@ -1136,8 +1145,10 @@ describe("Object types", function() { describe("WIF", function() { var wifSign = "5JgQ79vTBusc61xYPtUEHYQ38AXKdDZgQ5rFp7Cbb4ZjXUKFZEV"; var wifEnc = "5K2aL8cnsEWHwHfHnUrPo8QdYyRfoYUBmhAnWY5GTpDLbeyusnE"; - var signPrivateKey = Buffer("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"); - var encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); + var signPrivateKey = Buffer.from( + "71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"); + var encPrivateKey = Buffer.from( + "9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); it("should decode", function() { var key1 = WIF.decode(wifSign); @@ -1167,15 +1178,15 @@ describe("POW", function() { }); it("should check a POW", function() { - expect(POW.check({nonce: 21997550, target: 297422525267, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.true; - expect(POW.check({nonce: 3122437, target: 4864647698763, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.true; - expect(POW.check({nonce: 3122436, target: 4864647698763, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.false; + expect(POW.check({nonce: 21997550, target: 297422525267, initialHash: Buffer.from("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.true; + expect(POW.check({nonce: 3122437, target: 4864647698763, initialHash: Buffer.from("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.true; + expect(POW.check({nonce: 3122436, target: 4864647698763, initialHash: Buffer.from("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.false; }); it("should reject promise on bad POW arguments", function(done) { POW.doAsync({target: 123, initialHash: {}}).catch(function() { - POW.doAsync({target: 123, initialHash: Buffer("test")}).catch(function() { - POW.doAsync({poolSize: -1, target: 123, initialHash: Buffer(64)}) + POW.doAsync({target: 123, initialHash: Buffer.from("test")}).catch(function() { + POW.doAsync({poolSize: -1, target: 123, initialHash: Buffer.alloc(64)}) .catch(function() { done(); }); @@ -1187,7 +1198,7 @@ describe("POW", function() { it("should do a POW", function() { this.timeout(300000); var target = typeof window === "undefined" ? 297422525267 : 10688385392246; - var initialHash = Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex"); + var initialHash = Buffer.from("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex"); return POW.doAsync({target: target, initialHash: initialHash}) .then(function(nonce) { // FIXME(Kagami): Chromium behaves very strangely on Travis CI: @@ -1316,14 +1327,17 @@ describe("High-level classes", function() { it("should provide setters for keys and ripe", function() { var addr = Address(); expect(function(){addr.ripe}).to.throw(Error); - addr.signPrivateKey = Buffer("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"); + addr.signPrivateKey = Buffer.from( + "71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"); expect(addr.signPublicKey.toString("hex")).to.equal("042d391543f574608cbcdfd12a37cc4c74dd36e54510b13a6a1d8b7b1498fb96c92873d33ca17586dace7f5ad0f4532a954061ac06bc5230aed9c8374072546571"); expect(function(){addr.ripe}).to.throw(Error); - addr.encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); + addr.encPrivateKey = Buffer.from( + "9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); expect(addr.encPublicKey.toString("hex")).to.equal("04c6ed1b56f2da97fec1b762d43364566faf082c1e4918ae1dbb41757cad41b03b2cc5087f341414e63f6eee72a1fbf0b5f346a1bb3ba944cad204ca597db2bfc8"); expect(addr.ripe.toString("hex")).to.equal("003ab6655de4bd8c603eba9b00dd5970725fdd56"); expect(addr.getShortRipe().toString("hex")).to.equal("3ab6655de4bd8c603eba9b00dd5970725fdd56"); - addr.encPrivateKey = Buffer("009969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); + addr.encPrivateKey = Buffer.from( + "009969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); expect(addr.getShortRipe().toString("hex")).to.equal("69617ddb1946dc327cadffcf33889fed587fc1e7"); }); @@ -1347,8 +1361,10 @@ describe("High-level classes", function() { expect(addr2.getTag().toString("hex")).to.equal("d6487aaea3d2d022d80abbce2605089523ba2b516b81c03545f19a5c85f15fa2"); var addr3 = Address({ - signPrivateKey: Buffer("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"), - encPrivateKey: Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"), + signPrivateKey: Buffer.from( + "71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex"), + encPrivateKey: Buffer.from( + "9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"), }); var addr4 = addr3.clone(); expect(addr4.signPrivateKey.toString("hex")).to.equal("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f"); @@ -1396,7 +1412,7 @@ describe("High-level classes", function() { }); it("should encode bitmessage's user agent", function() { - var res = UserAgent.decode(UserAgent.encodeSelf()) + var res = UserAgent.decode(UserAgent.encodeSelf()); var software = UserAgent.parse(res.str); expect(software[0].name).to.equal("bitmessage"); expect(software[0]).to.have.property("version");