diff --git a/.gitignore b/.gitignore index 7fc04fe..a6c0ab8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ coverage node_modules .nyc_output +npm-debug.log diff --git a/package.json b/package.json index 7f6bb52..bba1e9f 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "ecurve": "^1.0.0", "randombytes": "^2.0.1", "typeforce": "^1.8.7", + "varuint-bitcoin": "^1.0.4", "wif": "^2.0.1" }, "devDependencies": { diff --git a/src/bufferutils.js b/src/bufferutils.js index 458a1e7..bd5f278 100644 --- a/src/bufferutils.js +++ b/src/bufferutils.js @@ -1,12 +1,5 @@ var opcodes = require('./opcodes.json') - -// https://github.com/feross/buffer/blob/master/index.js#L1127 -function verifuint (value, max) { - if (typeof value !== 'number') throw new Error('cannot write a non-number as a number') - if (value < 0) throw new Error('specified a negative value for writing an unsigned value') - if (value > max) throw new Error('value is larger than maximum value for type') - if (Math.floor(value) !== value) throw new Error('value has a fractional component') -} +var varuint = require('varuint-bitcoin') function pushDataSize (i) { return i < opcodes.OP_PUSHDATA1 ? 1 @@ -52,6 +45,14 @@ function readPushDataInt (buffer, offset) { } } +// https://github.com/feross/buffer/blob/master/index.js#L1127 +function verifuint (value, max) { + if (typeof value !== 'number') throw new Error('cannot write a non-number as a number') + if (value < 0) throw new Error('specified a negative value for writing an unsigned value') + if (value > max) throw new Error('RangeError: value out of range') + if (Math.floor(value) !== value) throw new Error('value has a fractional component') +} + function readUInt64LE (buffer, offset) { var a = buffer.readUInt32LE(offset) var b = buffer.readUInt32LE(offset + 4) @@ -62,37 +63,6 @@ function readUInt64LE (buffer, offset) { return b + a } -function readVarInt (buffer, offset) { - var t = buffer.readUInt8(offset) - var number, size - - // 8 bit - if (t < 253) { - number = t - size = 1 - - // 16 bit - } else if (t < 254) { - number = buffer.readUInt16LE(offset + 1) - size = 3 - - // 32 bit - } else if (t < 255) { - number = buffer.readUInt32LE(offset + 1) - size = 5 - - // 64 bit - } else { - number = readUInt64LE(buffer, offset + 1) - size = 9 - } - - return { - number: number, - size: size - } -} - function writePushDataInt (buffer, number, offset) { var size = pushDataSize(number) @@ -126,45 +96,20 @@ function writeUInt64LE (buffer, value, offset) { buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4) } -function varIntSize (i) { - return i < 253 ? 1 - : i < 0x10000 ? 3 - : i < 0x100000000 ? 5 - : 9 -} +// TODO: remove in 4.0.0? +function readVarInt (buffer, offset) { + var result = varuint.decode(buffer, offset) -function writeVarInt (buffer, number, offset) { - var size = varIntSize(number) - - // 8 bit - if (size === 1) { - buffer.writeUInt8(number, offset) - - // 16 bit - } else if (size === 3) { - buffer.writeUInt8(253, offset) - buffer.writeUInt16LE(number, offset + 1) - - // 32 bit - } else if (size === 5) { - buffer.writeUInt8(254, offset) - buffer.writeUInt32LE(number, offset + 1) - - // 64 bit - } else { - buffer.writeUInt8(255, offset) - writeUInt64LE(buffer, number, offset + 1) + return { + number: result, + size: varuint.decode.bytes } - - return size } -function varIntBuffer (i) { - var size = varIntSize(i) - var buffer = new Buffer(size) - writeVarInt(buffer, i, 0) - - return buffer +// TODO: remove in 4.0.0? +function writeVarInt (buffer, number, offset) { + varuint.encode(number, buffer, offset) + return varuint.encode.bytes } module.exports = { @@ -172,13 +117,9 @@ module.exports = { readPushDataInt: readPushDataInt, readUInt64LE: readUInt64LE, readVarInt: readVarInt, - varIntBuffer: varIntBuffer, - varIntSize: varIntSize, + varIntBuffer: varuint.encode, + varIntSize: varuint.encodingLength, writePushDataInt: writePushDataInt, writeUInt64LE: writeUInt64LE, - writeVarInt: writeVarInt, - - // TODO: remove in 3.0.0 - equal: function BufferEquals (a, b) { return a.equals(b) }, - reverse: require('buffer-reverse') + writeVarInt: writeVarInt } diff --git a/test/bufferutils.js b/test/bufferutils.js index 484bf4f..3a780a7 100644 --- a/test/bufferutils.js +++ b/test/bufferutils.js @@ -6,25 +6,6 @@ var bufferutils = require('../src/bufferutils') var fixtures = require('./fixtures/bufferutils.json') describe('bufferutils', function () { - // TODO: remove - describe('equals', function () { - it('works', function () { - var a = new Buffer('abcd', 'hex') - var b = new Buffer('bbbb', 'hex') - - assert.strictEqual(bufferutils.equal(a, a), true) - assert.strictEqual(bufferutils.equal(a, b), false) - }) - }) - - describe('reverse', function () { - it('works', function () { - var a = new Buffer('abcd', 'hex') - - assert.strictEqual(bufferutils.reverse(a).toString('hex'), 'cdab') - }) - }) - describe('pushDataSize', function () { fixtures.valid.forEach(function (f) { it('determines the pushDataSize of ' + f.dec + ' correctly', function () { diff --git a/test/fixtures/bufferutils.json b/test/fixtures/bufferutils.json index db1e1da..2b884cf 100644 --- a/test/fixtures/bufferutils.json +++ b/test/fixtures/bufferutils.json @@ -86,14 +86,14 @@ "readUInt64LE": [ { "description": "n === 2^53", - "exception": "value is larger than maximum value for type", + "exception": "RangeError: value out of range", "hex64": "0000000000002000", "hexVI": "ff0000000000000020", "dec": 9007199254740992 }, { "description": "n > 2^53", - "exception": "value is larger than maximum value for type", + "exception": "RangeError: value out of range", "hex64": "0100000000002000", "hexVI": "ff0100000000000020", "dec": 9007199254740993