diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 53925f21..96123ae9 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 {Buffer} challenge - Local nonce. + * @property {BN} 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 || utils.cmp(data.nonce, this.challenge) !== 0) + if (!this.challenge || this.challenge.cmp(data.nonce) !== 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 23a21eca..9b50770b 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.writeBytes(utils.nonce()); + p.writeU64(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); - data.nonce.copy(p, 0, 0, 8); + utils.writeU64(p, data.nonce, 0); return p; }; @@ -549,7 +549,7 @@ Framer.ping = function ping(data) { Framer.pong = function pong(data) { var p = new Buffer(8); - data.nonce.copy(p, 0, 0, 8); + utils.writeU64(p, data.nonce, 0); return p; }; diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 0d2c91b3..f470b8c2 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.readBytes(8) + nonce: p.readU64() }; }; @@ -449,7 +449,7 @@ Parser.parsePong = function parsePong(p) { p = new BufferReader(p); return { - nonce: p.readBytes(8) + nonce: p.readU64() }; }; @@ -469,7 +469,7 @@ Parser.parseVersion = function parseVersion(p) { ts = p.read53(); recv = Parser.parseAddress(p, false); from = Parser.parseAddress(p, false); - nonce = p.readBytes(8); + nonce = p.readU64(); agent = p.readVarString('ascii'); height = p.read32(); diff --git a/lib/bcoin/types.js b/lib/bcoin/types.js index 3b186abe..17382deb 100644 --- a/lib/bcoin/types.js +++ b/lib/bcoin/types.js @@ -240,7 +240,7 @@ /** * @typedef {Object} PingPacket - * @property {Buffer} nonce + * @property {BN} nonce * @global */ @@ -265,7 +265,7 @@ * @property {Number} ts - Timestamp of discovery. * @property {NetworkAddress} local - Our address. * @property {NetworkAddress} remote - Their address. - * @property {Buffer} nonce + * @property {BN} 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 08358c72..fff7ffce 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 {Buffer} + * @returns {BN} */ 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 nonce; + return new bn(nonce); }; //