From 404bdcd1097caeec1d52e3c6e93d1d797dfd17b5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 16 May 2016 03:47:49 -0700 Subject: [PATCH] use buffers for nonces. --- lib/bcoin/peer.js | 4 ++-- lib/bcoin/protocol/framer.js | 6 +++--- lib/bcoin/protocol/parser.js | 6 +++--- lib/bcoin/types.js | 4 ++-- lib/bcoin/utils.js | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 96123ae9..53925f21 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -52,7 +52,7 @@ var constants = bcoin.protocol.constants; * @property {Bloom?} filter - The _peer's_ bloom filter. * @property {Boolean} relay - Whether to relay transactions * immediately to the peer. - * @property {BN} challenge - Local nonce. + * @property {Buffer} challenge - Local nonce. * @property {Number} lastPong - Timestamp for last `pong` * received (unix time). * @property {String} id - Peer's uid. @@ -1235,7 +1235,7 @@ Peer.prototype._handlePing = function handlePing(data) { }; Peer.prototype._handlePong = function handlePong(data) { - if (!this.challenge || this.challenge.cmp(data.nonce) !== 0) + if (!this.challenge || utils.cmp(data.nonce, this.challenge) !== 0) return this.emit('pong', false); this.lastPong = utils.now(); diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 9b50770b..23a21eca 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -450,7 +450,7 @@ Framer.version = function version(options, writer) { p.write64(utils.now()); Framer.address(remote, false, p); Framer.address(local, false, p); - p.writeU64(utils.nonce()); + p.writeBytes(utils.nonce()); p.writeVarString(agent); p.write32(options.height || 0); p.writeU8(options.relay ? 1 : 0); @@ -536,7 +536,7 @@ Framer.notFound = function notFound(items, writer) { Framer.ping = function ping(data) { var p = new Buffer(8); - utils.writeU64(p, data.nonce, 0); + data.nonce.copy(p, 0, 0, 8); return p; }; @@ -549,7 +549,7 @@ Framer.ping = function ping(data) { Framer.pong = function pong(data) { var p = new Buffer(8); - utils.writeU64(p, data.nonce, 0); + data.nonce.copy(p, 0, 0, 8); return p; }; diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index f470b8c2..0d2c91b3 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -435,7 +435,7 @@ Parser.parsePing = function parsePing(p) { p = new BufferReader(p); return { - nonce: p.readU64() + nonce: p.readBytes(8) }; }; @@ -449,7 +449,7 @@ Parser.parsePong = function parsePong(p) { p = new BufferReader(p); return { - nonce: p.readU64() + nonce: p.readBytes(8) }; }; @@ -469,7 +469,7 @@ Parser.parseVersion = function parseVersion(p) { ts = p.read53(); recv = Parser.parseAddress(p, false); from = Parser.parseAddress(p, false); - nonce = p.readU64(); + nonce = p.readBytes(8); agent = p.readVarString('ascii'); height = p.read32(); diff --git a/lib/bcoin/types.js b/lib/bcoin/types.js index 17382deb..3b186abe 100644 --- a/lib/bcoin/types.js +++ b/lib/bcoin/types.js @@ -240,7 +240,7 @@ /** * @typedef {Object} PingPacket - * @property {BN} nonce + * @property {Buffer} nonce * @global */ @@ -265,7 +265,7 @@ * @property {Number} ts - Timestamp of discovery. * @property {NetworkAddress} local - Our address. * @property {NetworkAddress} remote - Their address. - * @property {BN} nonce + * @property {Buffer} nonce * @property {String} agent - User agent string. * @property {Number} height - Chain height. * @property {Boolean} relay - Whether transactions diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index fff7ffce..08358c72 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1286,14 +1286,14 @@ utils.U64 = new bn('ffffffffffffffff', 'hex'); /** * Create an 8 byte nonce. - * @returns {BN} + * @returns {Buffer} */ utils.nonce = function _nonce() { var nonce = new Buffer(8); utils.writeU32(nonce, Math.random() * 0x100000000 | 0, 0); utils.writeU32(nonce, Math.random() * 0x100000000 | 0, 4); - return new bn(nonce); + return nonce; }; //