Skip to content

Commit 6b3c41a

Browse files
authored
Merge pull request bitcoinjs#1035 from bitcoinjs/buremove
Remove bufferutils
2 parents 68dd16a + 549b36b commit 6b3c41a

File tree

4 files changed

+22
-207
lines changed

4 files changed

+22
-207
lines changed

src/bufferutils.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
var pushdata = require('pushdata-bitcoin')
2-
var varuint = require('varuint-bitcoin')
3-
41
// https://github.com/feross/buffer/blob/master/index.js#L1127
52
function verifuint (value, max) {
63
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
@@ -15,7 +12,6 @@ function readUInt64LE (buffer, offset) {
1512
b *= 0x100000000
1613

1714
verifuint(b + a, 0x001fffffffffffff)
18-
1915
return b + a
2016
}
2117

@@ -27,30 +23,7 @@ function writeUInt64LE (buffer, value, offset) {
2723
return offset + 8
2824
}
2925

30-
// TODO: remove in 4.0.0?
31-
function readVarInt (buffer, offset) {
32-
var result = varuint.decode(buffer, offset)
33-
34-
return {
35-
number: result,
36-
size: varuint.decode.bytes
37-
}
38-
}
39-
40-
// TODO: remove in 4.0.0?
41-
function writeVarInt (buffer, number, offset) {
42-
varuint.encode(number, buffer, offset)
43-
return varuint.encode.bytes
44-
}
45-
4626
module.exports = {
47-
pushDataSize: pushdata.encodingLength,
48-
readPushDataInt: pushdata.decode,
4927
readUInt64LE: readUInt64LE,
50-
readVarInt: readVarInt,
51-
varIntBuffer: varuint.encode,
52-
varIntSize: varuint.encodingLength,
53-
writePushDataInt: pushdata.encode,
54-
writeUInt64LE: writeUInt64LE,
55-
writeVarInt: writeVarInt
28+
writeUInt64LE: writeUInt64LE
5629
}

src/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ for (var key in templates) {
66
}
77

88
module.exports = {
9-
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
10-
119
Block: require('./block'),
1210
ECPair: require('./ecpair'),
1311
ECSignature: require('./ecsignature'),

test/bufferutils.js

Lines changed: 5 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,10 @@ var bufferutils = require('../src/bufferutils')
66
var fixtures = require('./fixtures/bufferutils.json')
77

88
describe('bufferutils', function () {
9-
describe('pushDataSize', function () {
10-
fixtures.valid.forEach(function (f) {
11-
it('determines the pushDataSize of ' + f.dec + ' correctly', function () {
12-
if (!f.hexPD) return
13-
14-
var size = bufferutils.pushDataSize(f.dec)
15-
16-
assert.strictEqual(size, f.hexPD.length / 2)
17-
})
18-
})
19-
})
20-
21-
describe('readPushDataInt', function () {
22-
fixtures.valid.forEach(function (f) {
23-
if (!f.hexPD) return
24-
25-
it('decodes ' + f.hexPD + ' correctly', function () {
26-
var buffer = Buffer.from(f.hexPD, 'hex')
27-
var d = bufferutils.readPushDataInt(buffer, 0)
28-
var fopcode = parseInt(f.hexPD.substr(0, 2), 16)
29-
30-
assert.strictEqual(d.opcode, fopcode)
31-
assert.strictEqual(d.number, f.dec)
32-
assert.strictEqual(d.size, buffer.length)
33-
})
34-
})
35-
36-
fixtures.invalid.readPushDataInt.forEach(function (f) {
37-
if (!f.hexPD) return
38-
39-
it('decodes ' + f.hexPD + ' as null', function () {
40-
var buffer = Buffer.from(f.hexPD, 'hex')
41-
42-
var n = bufferutils.readPushDataInt(buffer, 0)
43-
assert.strictEqual(n, null)
44-
})
45-
})
46-
})
47-
489
describe('readUInt64LE', function () {
4910
fixtures.valid.forEach(function (f) {
50-
it('decodes ' + f.hex64 + ' correctly', function () {
51-
var buffer = Buffer.from(f.hex64, 'hex')
11+
it('decodes ' + f.hex, function () {
12+
var buffer = Buffer.from(f.hex, 'hex')
5213
var number = bufferutils.readUInt64LE(buffer, 0)
5314

5415
assert.strictEqual(number, f.dec)
@@ -57,7 +18,7 @@ describe('bufferutils', function () {
5718

5819
fixtures.invalid.readUInt64LE.forEach(function (f) {
5920
it('throws on ' + f.description, function () {
60-
var buffer = Buffer.from(f.hex64, 'hex')
21+
var buffer = Buffer.from(f.hex, 'hex')
6122

6223
assert.throws(function () {
6324
bufferutils.readUInt64LE(buffer, 0)
@@ -66,68 +27,13 @@ describe('bufferutils', function () {
6627
})
6728
})
6829

69-
describe('readVarInt', function () {
70-
fixtures.valid.forEach(function (f) {
71-
it('decodes ' + f.hexVI + ' correctly', function () {
72-
var buffer = Buffer.from(f.hexVI, 'hex')
73-
var d = bufferutils.readVarInt(buffer, 0)
74-
75-
assert.strictEqual(d.number, f.dec)
76-
assert.strictEqual(d.size, buffer.length)
77-
})
78-
})
79-
80-
fixtures.invalid.readUInt64LE.forEach(function (f) {
81-
it('throws on ' + f.description, function () {
82-
var buffer = Buffer.from(f.hexVI, 'hex')
83-
84-
assert.throws(function () {
85-
bufferutils.readVarInt(buffer, 0)
86-
}, new RegExp(f.exception))
87-
})
88-
})
89-
})
90-
91-
describe('varIntBuffer', function () {
92-
fixtures.valid.forEach(function (f) {
93-
it('encodes ' + f.dec + ' correctly', function () {
94-
var buffer = bufferutils.varIntBuffer(f.dec)
95-
96-
assert.strictEqual(buffer.toString('hex'), f.hexVI)
97-
})
98-
})
99-
})
100-
101-
describe('varIntSize', function () {
102-
fixtures.valid.forEach(function (f) {
103-
it('determines the varIntSize of ' + f.dec + ' correctly', function () {
104-
var size = bufferutils.varIntSize(f.dec)
105-
106-
assert.strictEqual(size, f.hexVI.length / 2)
107-
})
108-
})
109-
})
110-
111-
describe('writePushDataInt', function () {
112-
fixtures.valid.forEach(function (f) {
113-
if (!f.hexPD) return
114-
115-
it('encodes ' + f.dec + ' correctly', function () {
116-
var buffer = Buffer.alloc(5, 0)
117-
118-
var n = bufferutils.writePushDataInt(buffer, f.dec, 0)
119-
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexPD)
120-
})
121-
})
122-
})
123-
12430
describe('writeUInt64LE', function () {
12531
fixtures.valid.forEach(function (f) {
126-
it('encodes ' + f.dec + ' correctly', function () {
32+
it('encodes ' + f.dec, function () {
12733
var buffer = Buffer.alloc(8, 0)
12834

12935
bufferutils.writeUInt64LE(buffer, f.dec, 0)
130-
assert.strictEqual(buffer.toString('hex'), f.hex64)
36+
assert.strictEqual(buffer.toString('hex'), f.hex)
13137
})
13238
})
13339

@@ -141,25 +47,4 @@ describe('bufferutils', function () {
14147
})
14248
})
14349
})
144-
145-
describe('writeVarInt', function () {
146-
fixtures.valid.forEach(function (f) {
147-
it('encodes ' + f.dec + ' correctly', function () {
148-
var buffer = Buffer.alloc(9, 0)
149-
150-
var n = bufferutils.writeVarInt(buffer, f.dec, 0)
151-
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexVI)
152-
})
153-
})
154-
155-
fixtures.invalid.readUInt64LE.forEach(function (f) {
156-
it('throws on ' + f.description, function () {
157-
var buffer = Buffer.alloc(9, 0)
158-
159-
assert.throws(function () {
160-
bufferutils.writeVarInt(buffer, f.dec, 0)
161-
}, new RegExp(f.exception))
162-
})
163-
})
164-
})
16550
})

test/fixtures/bufferutils.json

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,75 @@
22
"valid": [
33
{
44
"dec": 0,
5-
"hex64": "0000000000000000",
6-
"hexVI": "00",
7-
"hexPD": "00"
5+
"hex": "0000000000000000"
86
},
97
{
108
"dec": 1,
11-
"hex64": "0100000000000000",
12-
"hexVI": "01",
13-
"hexPD": "01"
9+
"hex": "0100000000000000"
1410
},
1511
{
1612
"dec": 252,
17-
"hex64": "fc00000000000000",
18-
"hexVI": "fc",
19-
"hexPD": "4cfc"
13+
"hex": "fc00000000000000"
2014
},
2115
{
2216
"dec": 253,
23-
"hex64": "fd00000000000000",
24-
"hexVI": "fdfd00",
25-
"hexPD": "4cfd"
17+
"hex": "fd00000000000000"
2618
},
2719
{
2820
"dec": 254,
29-
"hex64": "fe00000000000000",
30-
"hexVI": "fdfe00",
31-
"hexPD": "4cfe"
21+
"hex": "fe00000000000000"
3222
},
3323
{
3424
"dec": 255,
35-
"hex64": "ff00000000000000",
36-
"hexVI": "fdff00",
37-
"hexPD": "4cff"
25+
"hex": "ff00000000000000"
3826
},
3927
{
4028
"dec": 65534,
41-
"hex64": "feff000000000000",
42-
"hexVI": "fdfeff",
43-
"hexPD": "4dfeff"
29+
"hex": "feff000000000000"
4430
},
4531
{
4632
"dec": 65535,
47-
"hex64": "ffff000000000000",
48-
"hexVI": "fdffff",
49-
"hexPD": "4dffff"
33+
"hex": "ffff000000000000"
5034
},
5135
{
5236
"dec": 65536,
53-
"hex64": "0000010000000000",
54-
"hexVI": "fe00000100",
55-
"hexPD": "4e00000100"
37+
"hex": "0000010000000000"
5638
},
5739
{
5840
"dec": 65537,
59-
"hex64": "0100010000000000",
60-
"hexVI": "fe01000100",
61-
"hexPD": "4e01000100"
41+
"hex": "0100010000000000"
6242
},
6343
{
6444
"dec": 4294967295,
65-
"hex64": "ffffffff00000000",
66-
"hexVI": "feffffffff",
67-
"hexPD": "4effffffff"
45+
"hex": "ffffffff00000000"
6846
},
6947
{
7048
"dec": 4294967296,
71-
"hex64": "0000000001000000",
72-
"hexVI": "ff0000000001000000"
49+
"hex": "0000000001000000"
7350
},
7451
{
7552
"dec": 4294967297,
76-
"hex64": "0100000001000000",
77-
"hexVI": "ff0100000001000000"
53+
"hex": "0100000001000000"
7854
},
7955
{
8056
"dec": 9007199254740991,
81-
"hex64": "ffffffffffff1f00",
82-
"hexVI": "ffffffffffffff1f00"
57+
"hex": "ffffffffffff1f00"
8358
}
8459
],
8560
"invalid": {
8661
"readUInt64LE": [
8762
{
8863
"description": "n === 2^53",
8964
"exception": "RangeError: value out of range",
90-
"hex64": "0000000000002000",
91-
"hexVI": "ff0000000000000020",
65+
"hex": "0000000000002000",
9266
"dec": 9007199254740992
9367
},
9468
{
9569
"description": "n > 2^53",
9670
"exception": "RangeError: value out of range",
97-
"hex64": "0100000000002000",
98-
"hexVI": "ff0100000000000020",
71+
"hex": "0100000000002000",
9972
"dec": 9007199254740993
10073
}
101-
],
102-
"readPushDataInt": [
103-
{
104-
"description": "OP_PUSHDATA1, no size",
105-
"hexPD": "4c"
106-
},
107-
{
108-
"description": "OP_PUSHDATA2, no size",
109-
"hexPD": "4d"
110-
},
111-
{
112-
"description": "OP_PUSHDATA4, no size",
113-
"hexPD": "4e"
114-
}
11574
]
11675
}
11776
}

0 commit comments

Comments
 (0)