Merge pull request #1035 from bitcoinjs/buremove

Remove bufferutils
This commit is contained in:
Daniel Cousens 2018-04-30 10:41:27 +10:00 committed by GitHub
commit 6b3c41a06c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 207 deletions

View file

@ -1,6 +1,3 @@
var pushdata = require('pushdata-bitcoin')
var varuint = require('varuint-bitcoin')
// https://github.com/feross/buffer/blob/master/index.js#L1127 // https://github.com/feross/buffer/blob/master/index.js#L1127
function verifuint (value, max) { function verifuint (value, max) {
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number') if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
@ -15,7 +12,6 @@ function readUInt64LE (buffer, offset) {
b *= 0x100000000 b *= 0x100000000
verifuint(b + a, 0x001fffffffffffff) verifuint(b + a, 0x001fffffffffffff)
return b + a return b + a
} }
@ -27,30 +23,7 @@ function writeUInt64LE (buffer, value, offset) {
return offset + 8 return offset + 8
} }
// TODO: remove in 4.0.0?
function readVarInt (buffer, offset) {
var result = varuint.decode(buffer, offset)
return {
number: result,
size: varuint.decode.bytes
}
}
// TODO: remove in 4.0.0?
function writeVarInt (buffer, number, offset) {
varuint.encode(number, buffer, offset)
return varuint.encode.bytes
}
module.exports = { module.exports = {
pushDataSize: pushdata.encodingLength,
readPushDataInt: pushdata.decode,
readUInt64LE: readUInt64LE, readUInt64LE: readUInt64LE,
readVarInt: readVarInt, writeUInt64LE: writeUInt64LE
varIntBuffer: varuint.encode,
varIntSize: varuint.encodingLength,
writePushDataInt: pushdata.encode,
writeUInt64LE: writeUInt64LE,
writeVarInt: writeVarInt
} }

View file

@ -6,8 +6,6 @@ for (var key in templates) {
} }
module.exports = { module.exports = {
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
Block: require('./block'), Block: require('./block'),
ECPair: require('./ecpair'), ECPair: require('./ecpair'),
ECSignature: require('./ecsignature'), ECSignature: require('./ecsignature'),

View file

@ -6,49 +6,10 @@ var bufferutils = require('../src/bufferutils')
var fixtures = require('./fixtures/bufferutils.json') var fixtures = require('./fixtures/bufferutils.json')
describe('bufferutils', function () { describe('bufferutils', function () {
describe('pushDataSize', function () {
fixtures.valid.forEach(function (f) {
it('determines the pushDataSize of ' + f.dec + ' correctly', function () {
if (!f.hexPD) return
var size = bufferutils.pushDataSize(f.dec)
assert.strictEqual(size, f.hexPD.length / 2)
})
})
})
describe('readPushDataInt', function () {
fixtures.valid.forEach(function (f) {
if (!f.hexPD) return
it('decodes ' + f.hexPD + ' correctly', function () {
var buffer = Buffer.from(f.hexPD, 'hex')
var d = bufferutils.readPushDataInt(buffer, 0)
var fopcode = parseInt(f.hexPD.substr(0, 2), 16)
assert.strictEqual(d.opcode, fopcode)
assert.strictEqual(d.number, f.dec)
assert.strictEqual(d.size, buffer.length)
})
})
fixtures.invalid.readPushDataInt.forEach(function (f) {
if (!f.hexPD) return
it('decodes ' + f.hexPD + ' as null', function () {
var buffer = Buffer.from(f.hexPD, 'hex')
var n = bufferutils.readPushDataInt(buffer, 0)
assert.strictEqual(n, null)
})
})
})
describe('readUInt64LE', function () { describe('readUInt64LE', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
it('decodes ' + f.hex64 + ' correctly', function () { it('decodes ' + f.hex, function () {
var buffer = Buffer.from(f.hex64, 'hex') var buffer = Buffer.from(f.hex, 'hex')
var number = bufferutils.readUInt64LE(buffer, 0) var number = bufferutils.readUInt64LE(buffer, 0)
assert.strictEqual(number, f.dec) assert.strictEqual(number, f.dec)
@ -57,7 +18,7 @@ describe('bufferutils', function () {
fixtures.invalid.readUInt64LE.forEach(function (f) { fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () { it('throws on ' + f.description, function () {
var buffer = Buffer.from(f.hex64, 'hex') var buffer = Buffer.from(f.hex, 'hex')
assert.throws(function () { assert.throws(function () {
bufferutils.readUInt64LE(buffer, 0) bufferutils.readUInt64LE(buffer, 0)
@ -66,68 +27,13 @@ describe('bufferutils', function () {
}) })
}) })
describe('readVarInt', function () {
fixtures.valid.forEach(function (f) {
it('decodes ' + f.hexVI + ' correctly', function () {
var buffer = Buffer.from(f.hexVI, 'hex')
var d = bufferutils.readVarInt(buffer, 0)
assert.strictEqual(d.number, f.dec)
assert.strictEqual(d.size, buffer.length)
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.from(f.hexVI, 'hex')
assert.throws(function () {
bufferutils.readVarInt(buffer, 0)
}, new RegExp(f.exception))
})
})
})
describe('varIntBuffer', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
var buffer = bufferutils.varIntBuffer(f.dec)
assert.strictEqual(buffer.toString('hex'), f.hexVI)
})
})
})
describe('varIntSize', function () {
fixtures.valid.forEach(function (f) {
it('determines the varIntSize of ' + f.dec + ' correctly', function () {
var size = bufferutils.varIntSize(f.dec)
assert.strictEqual(size, f.hexVI.length / 2)
})
})
})
describe('writePushDataInt', function () {
fixtures.valid.forEach(function (f) {
if (!f.hexPD) return
it('encodes ' + f.dec + ' correctly', function () {
var buffer = Buffer.alloc(5, 0)
var n = bufferutils.writePushDataInt(buffer, f.dec, 0)
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexPD)
})
})
})
describe('writeUInt64LE', function () { describe('writeUInt64LE', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () { it('encodes ' + f.dec, function () {
var buffer = Buffer.alloc(8, 0) var buffer = Buffer.alloc(8, 0)
bufferutils.writeUInt64LE(buffer, f.dec, 0) bufferutils.writeUInt64LE(buffer, f.dec, 0)
assert.strictEqual(buffer.toString('hex'), f.hex64) assert.strictEqual(buffer.toString('hex'), f.hex)
}) })
}) })
@ -141,25 +47,4 @@ describe('bufferutils', function () {
}) })
}) })
}) })
describe('writeVarInt', function () {
fixtures.valid.forEach(function (f) {
it('encodes ' + f.dec + ' correctly', function () {
var buffer = Buffer.alloc(9, 0)
var n = bufferutils.writeVarInt(buffer, f.dec, 0)
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexVI)
})
})
fixtures.invalid.readUInt64LE.forEach(function (f) {
it('throws on ' + f.description, function () {
var buffer = Buffer.alloc(9, 0)
assert.throws(function () {
bufferutils.writeVarInt(buffer, f.dec, 0)
}, new RegExp(f.exception))
})
})
})
}) })

View file

@ -2,84 +2,59 @@
"valid": [ "valid": [
{ {
"dec": 0, "dec": 0,
"hex64": "0000000000000000", "hex": "0000000000000000"
"hexVI": "00",
"hexPD": "00"
}, },
{ {
"dec": 1, "dec": 1,
"hex64": "0100000000000000", "hex": "0100000000000000"
"hexVI": "01",
"hexPD": "01"
}, },
{ {
"dec": 252, "dec": 252,
"hex64": "fc00000000000000", "hex": "fc00000000000000"
"hexVI": "fc",
"hexPD": "4cfc"
}, },
{ {
"dec": 253, "dec": 253,
"hex64": "fd00000000000000", "hex": "fd00000000000000"
"hexVI": "fdfd00",
"hexPD": "4cfd"
}, },
{ {
"dec": 254, "dec": 254,
"hex64": "fe00000000000000", "hex": "fe00000000000000"
"hexVI": "fdfe00",
"hexPD": "4cfe"
}, },
{ {
"dec": 255, "dec": 255,
"hex64": "ff00000000000000", "hex": "ff00000000000000"
"hexVI": "fdff00",
"hexPD": "4cff"
}, },
{ {
"dec": 65534, "dec": 65534,
"hex64": "feff000000000000", "hex": "feff000000000000"
"hexVI": "fdfeff",
"hexPD": "4dfeff"
}, },
{ {
"dec": 65535, "dec": 65535,
"hex64": "ffff000000000000", "hex": "ffff000000000000"
"hexVI": "fdffff",
"hexPD": "4dffff"
}, },
{ {
"dec": 65536, "dec": 65536,
"hex64": "0000010000000000", "hex": "0000010000000000"
"hexVI": "fe00000100",
"hexPD": "4e00000100"
}, },
{ {
"dec": 65537, "dec": 65537,
"hex64": "0100010000000000", "hex": "0100010000000000"
"hexVI": "fe01000100",
"hexPD": "4e01000100"
}, },
{ {
"dec": 4294967295, "dec": 4294967295,
"hex64": "ffffffff00000000", "hex": "ffffffff00000000"
"hexVI": "feffffffff",
"hexPD": "4effffffff"
}, },
{ {
"dec": 4294967296, "dec": 4294967296,
"hex64": "0000000001000000", "hex": "0000000001000000"
"hexVI": "ff0000000001000000"
}, },
{ {
"dec": 4294967297, "dec": 4294967297,
"hex64": "0100000001000000", "hex": "0100000001000000"
"hexVI": "ff0100000001000000"
}, },
{ {
"dec": 9007199254740991, "dec": 9007199254740991,
"hex64": "ffffffffffff1f00", "hex": "ffffffffffff1f00"
"hexVI": "ffffffffffffff1f00"
} }
], ],
"invalid": { "invalid": {
@ -87,31 +62,15 @@
{ {
"description": "n === 2^53", "description": "n === 2^53",
"exception": "RangeError: value out of range", "exception": "RangeError: value out of range",
"hex64": "0000000000002000", "hex": "0000000000002000",
"hexVI": "ff0000000000000020",
"dec": 9007199254740992 "dec": 9007199254740992
}, },
{ {
"description": "n > 2^53", "description": "n > 2^53",
"exception": "RangeError: value out of range", "exception": "RangeError: value out of range",
"hex64": "0100000000002000", "hex": "0100000000002000",
"hexVI": "ff0100000000000020",
"dec": 9007199254740993 "dec": 9007199254740993
} }
],
"readPushDataInt": [
{
"description": "OP_PUSHDATA1, no size",
"hexPD": "4c"
},
{
"description": "OP_PUSHDATA2, no size",
"hexPD": "4d"
},
{
"description": "OP_PUSHDATA4, no size",
"hexPD": "4e"
}
] ]
} }
} }