From 2214ccfdd80205276c96fd569f98aa37c0d6692d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 17 Oct 2014 13:07:14 +1100 Subject: [PATCH] bufferutils: add varIntBuffer --- src/block.js | 8 +------- src/bufferutils.js | 9 +++++++++ src/message.js | 3 +-- test/bufferutils.js | 12 +++++++++++- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/block.js b/src/block.js index f25e1c5..8fcce5a 100644 --- a/src/block.js +++ b/src/block.js @@ -120,13 +120,7 @@ Block.prototype.toBuffer = function(headersOnly) { if (headersOnly || !this.transactions) return buffer - function varIntBuffer(i) { - var ib = new Buffer(bufferutils.varIntSize(i)) - bufferutils.writeVarInt(ib, i, 0) - return ib - } - - var txLenBuffer = varIntBuffer(this.transactions.length) + var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length) var txBuffers = this.transactions.map(function(tx) { return tx.toBuffer() }) diff --git a/src/bufferutils.js b/src/bufferutils.js index 858a934..644db88 100644 --- a/src/bufferutils.js +++ b/src/bufferutils.js @@ -159,6 +159,14 @@ function writeVarInt(buffer, number, offset) { return size } +function varIntBuffer(i) { + var size = varIntSize(i) + var buffer = new Buffer(size) + writeVarInt(buffer, i, 0) + + return buffer +} + function reverse(buffer) { var buffer2 = new Buffer(buffer) Array.prototype.reverse.call(buffer2) @@ -171,6 +179,7 @@ module.exports = { readUInt64LE: readUInt64LE, readVarInt: readVarInt, reverse: reverse, + varIntBuffer: varIntBuffer, varIntSize: varIntSize, writePushDataInt: writePushDataInt, writeUInt64LE: writeUInt64LE, diff --git a/src/message.js b/src/message.js index 76ba3f0..6305a69 100644 --- a/src/message.js +++ b/src/message.js @@ -13,8 +13,7 @@ var ecparams = ecurve.getCurveByName('secp256k1') function magicHash(message, network) { var magicPrefix = new Buffer(network.magicPrefix) var messageBuffer = new Buffer(message) - var lengthBuffer = new Buffer(bufferutils.varIntSize(messageBuffer.length)) - bufferutils.writeVarInt(lengthBuffer, messageBuffer.length, 0) + var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length) var buffer = Buffer.concat([magicPrefix, lengthBuffer, messageBuffer]) return crypto.hash256(buffer) diff --git a/test/bufferutils.js b/test/bufferutils.js index 447d516..8fc054c 100644 --- a/test/bufferutils.js +++ b/test/bufferutils.js @@ -88,6 +88,16 @@ describe('bufferutils', function() { }) }) + describe('varIntBuffer', function() { + fixtures.valid.forEach(function(f) { + it('encodes ' + f.dec + ' correctly', function() { + var buffer = bufferutils.varIntBuffer(f.dec) + + assert.equal(buffer.toString('hex'), f.hexVI) + }) + }) + }) + describe('varIntSize', function() { fixtures.valid.forEach(function(f) { it('determines the varIntSize of ' + f.dec + ' correctly', function() { @@ -99,7 +109,7 @@ describe('bufferutils', function() { }) describe('writePushDataInt', function() { - fixtures.valid.forEach(function(f, i) { + fixtures.valid.forEach(function(f) { if (!f.hexPD) return it('encodes ' + f.dec + ' correctly', function() {