Skip to content

Commit f1ab151

Browse files
committed
tests: use safe-buffers throughout
1 parent aeb0312 commit f1ab151

20 files changed

+88
-94
lines changed

src/ecsignature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ ECSignature.prototype.toCompact = function (i, compressed) {
5858

5959
i += 27
6060

61-
var buffer = Buffer.allocUnsafe(65)
61+
var buffer = Buffer.alloc(65)
6262
buffer.writeUInt8(i, 0)
6363
this.r.toBuffer(32).copy(buffer, 1)
6464
this.s.toBuffer(32).copy(buffer, 33)
@@ -77,7 +77,7 @@ ECSignature.prototype.toScriptSignature = function (hashType) {
7777
var hashTypeMod = hashType & ~0x80
7878
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
7979

80-
var hashTypeBuffer = Buffer.allocUnsafe(1)
80+
var hashTypeBuffer = Buffer.alloc(1)
8181
hashTypeBuffer.writeUInt8(hashType, 0)
8282

8383
return Buffer.concat([this.toDER(), hashTypeBuffer])

test/address.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('address', function () {
5050
describe('toBase58Check', function () {
5151
fixtures.valid.forEach(function (f) {
5252
it('formats ' + f.hash + ' (' + f.network + ')', function () {
53-
var address = baddress.toBase58Check(new Buffer(f.hash, 'hex'), f.version)
53+
var address = baddress.toBase58Check(Buffer.from(f.hash, 'hex'), f.version)
5454

5555
assert.strictEqual(address, f.base58check)
5656
})

test/bitcoin.core.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Bitcoin-core', function () {
2828
})
2929

3030
it('can encode ' + fhex, function () {
31-
var buffer = new Buffer(fhex, 'hex')
31+
var buffer = Buffer.from(fhex, 'hex')
3232
var actual = base58.encode(buffer)
3333

3434
assert.strictEqual(actual, fb58)
@@ -45,7 +45,7 @@ describe('Bitcoin-core', function () {
4545

4646
base58KeysValid.forEach(function (f) {
4747
var expected = f[0]
48-
var hash = new Buffer(f[1], 'hex')
48+
var hash = Buffer.from(f[1], 'hex')
4949
var params = f[2]
5050

5151
if (params.isPrivkey) return
@@ -150,7 +150,7 @@ describe('Bitcoin-core', function () {
150150
var input = inputs[i]
151151

152152
// reverse because test data is reversed
153-
var prevOutHash = new Buffer(input[0], 'hex').reverse()
153+
var prevOutHash = Buffer.from(input[0], 'hex').reverse()
154154
var prevOutIndex = input[1]
155155

156156
assert.deepEqual(txIn.hash, prevOutHash)
@@ -186,7 +186,7 @@ describe('Bitcoin-core', function () {
186186
var transaction = bitcoin.Transaction.fromHex(txHex)
187187
assert.strictEqual(transaction.toHex(), txHex)
188188

189-
var script = new Buffer(scriptHex, 'hex')
189+
var script = Buffer.from(scriptHex, 'hex')
190190
var scriptChunks = bitcoin.script.decompile(script)
191191
assert.strictEqual(bitcoin.script.compile(scriptChunks).toString('hex'), scriptHex)
192192

@@ -200,7 +200,7 @@ describe('Bitcoin-core', function () {
200200

201201
describe('ECSignature.parseScriptSignature', function () {
202202
sigCanonical.forEach(function (hex) {
203-
var buffer = new Buffer(hex, 'hex')
203+
var buffer = Buffer.from(hex, 'hex')
204204

205205
it('can parse ' + hex, function () {
206206
var parsed = bitcoin.ECSignature.parseScriptSignature(buffer)
@@ -214,7 +214,7 @@ describe('Bitcoin-core', function () {
214214
if (i % 2 !== 0) return
215215

216216
var description = sigNoncanonical[i - 1].slice(0, -1)
217-
var buffer = new Buffer(hex, 'hex')
217+
var buffer = Buffer.from(hex, 'hex')
218218

219219
it('throws on ' + description, function () {
220220
assert.throws(function () {

test/bufferutils.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('bufferutils', function () {
2323
if (!f.hexPD) return
2424

2525
it('decodes ' + f.hexPD + ' correctly', function () {
26-
var buffer = new Buffer(f.hexPD, 'hex')
26+
var buffer = Buffer.from(f.hexPD, 'hex')
2727
var d = bufferutils.readPushDataInt(buffer, 0)
2828
var fopcode = parseInt(f.hexPD.substr(0, 2), 16)
2929

@@ -37,7 +37,7 @@ describe('bufferutils', function () {
3737
if (!f.hexPD) return
3838

3939
it('decodes ' + f.hexPD + ' as null', function () {
40-
var buffer = new Buffer(f.hexPD, 'hex')
40+
var buffer = Buffer.from(f.hexPD, 'hex')
4141

4242
var n = bufferutils.readPushDataInt(buffer, 0)
4343
assert.strictEqual(n, null)
@@ -48,7 +48,7 @@ describe('bufferutils', function () {
4848
describe('readUInt64LE', function () {
4949
fixtures.valid.forEach(function (f) {
5050
it('decodes ' + f.hex64 + ' correctly', function () {
51-
var buffer = new Buffer(f.hex64, 'hex')
51+
var buffer = Buffer.from(f.hex64, 'hex')
5252
var number = bufferutils.readUInt64LE(buffer, 0)
5353

5454
assert.strictEqual(number, f.dec)
@@ -57,7 +57,7 @@ describe('bufferutils', function () {
5757

5858
fixtures.invalid.readUInt64LE.forEach(function (f) {
5959
it('throws on ' + f.description, function () {
60-
var buffer = new Buffer(f.hex64, 'hex')
60+
var buffer = Buffer.from(f.hex64, 'hex')
6161

6262
assert.throws(function () {
6363
bufferutils.readUInt64LE(buffer, 0)
@@ -69,7 +69,7 @@ describe('bufferutils', function () {
6969
describe('readVarInt', function () {
7070
fixtures.valid.forEach(function (f) {
7171
it('decodes ' + f.hexVI + ' correctly', function () {
72-
var buffer = new Buffer(f.hexVI, 'hex')
72+
var buffer = Buffer.from(f.hexVI, 'hex')
7373
var d = bufferutils.readVarInt(buffer, 0)
7474

7575
assert.strictEqual(d.number, f.dec)
@@ -79,7 +79,7 @@ describe('bufferutils', function () {
7979

8080
fixtures.invalid.readUInt64LE.forEach(function (f) {
8181
it('throws on ' + f.description, function () {
82-
var buffer = new Buffer(f.hexVI, 'hex')
82+
var buffer = Buffer.from(f.hexVI, 'hex')
8383

8484
assert.throws(function () {
8585
bufferutils.readVarInt(buffer, 0)
@@ -113,8 +113,7 @@ describe('bufferutils', function () {
113113
if (!f.hexPD) return
114114

115115
it('encodes ' + f.dec + ' correctly', function () {
116-
var buffer = new Buffer(5)
117-
buffer.fill(0)
116+
var buffer = Buffer.alloc(5, 0)
118117

119118
var n = bufferutils.writePushDataInt(buffer, f.dec, 0)
120119
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexPD)
@@ -125,8 +124,7 @@ describe('bufferutils', function () {
125124
describe('writeUInt64LE', function () {
126125
fixtures.valid.forEach(function (f) {
127126
it('encodes ' + f.dec + ' correctly', function () {
128-
var buffer = new Buffer(8)
129-
buffer.fill(0)
127+
var buffer = Buffer.alloc(8, 0)
130128

131129
bufferutils.writeUInt64LE(buffer, f.dec, 0)
132130
assert.strictEqual(buffer.toString('hex'), f.hex64)
@@ -135,8 +133,7 @@ describe('bufferutils', function () {
135133

136134
fixtures.invalid.readUInt64LE.forEach(function (f) {
137135
it('throws on ' + f.description, function () {
138-
var buffer = new Buffer(8)
139-
buffer.fill(0)
136+
var buffer = Buffer.alloc(8, 0)
140137

141138
assert.throws(function () {
142139
bufferutils.writeUInt64LE(buffer, f.dec, 0)
@@ -148,8 +145,7 @@ describe('bufferutils', function () {
148145
describe('writeVarInt', function () {
149146
fixtures.valid.forEach(function (f) {
150147
it('encodes ' + f.dec + ' correctly', function () {
151-
var buffer = new Buffer(9)
152-
buffer.fill(0)
148+
var buffer = Buffer.alloc(9, 0)
153149

154150
var n = bufferutils.writeVarInt(buffer, f.dec, 0)
155151
assert.strictEqual(buffer.slice(0, n).toString('hex'), f.hexVI)
@@ -158,8 +154,7 @@ describe('bufferutils', function () {
158154

159155
fixtures.invalid.readUInt64LE.forEach(function (f) {
160156
it('throws on ' + f.description, function () {
161-
var buffer = new Buffer(9)
162-
buffer.fill(0)
157+
var buffer = Buffer.alloc(9, 0)
163158

164159
assert.throws(function () {
165160
bufferutils.writeVarInt(buffer, f.dec, 0)

test/crypto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('crypto', function () {
1313
var expected = f[algorithm]
1414

1515
it('returns ' + expected + ' for ' + f.hex, function () {
16-
var data = new Buffer(f.hex, 'hex')
16+
var data = Buffer.from(f.hex, 'hex')
1717
var actual = fn(data).toString('hex')
1818

1919
assert.strictEqual(actual, expected)

test/ecdsa.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('ecdsa', function () {
3636
.onCall(2).returns(new BigInteger('42')) // valid
3737

3838
var x = new BigInteger('1').toBuffer(32)
39-
var h1 = new Buffer(32)
39+
var h1 = Buffer.alloc(32)
4040
var k = ecdsa.deterministicGenerateK(h1, x, checkSig)
4141

4242
assert.strictEqual(k.toString(), '42')
@@ -56,7 +56,7 @@ describe('ecdsa', function () {
5656
mockCheckSig.onCall(1).returns(true) // good signature
5757

5858
var x = new BigInteger('1').toBuffer(32)
59-
var h1 = new Buffer(32)
59+
var h1 = Buffer.alloc(32)
6060
var k = ecdsa.deterministicGenerateK(h1, x, mockCheckSig)
6161

6262
assert.strictEqual(k.toString(), '53')
@@ -107,7 +107,7 @@ describe('ecdsa', function () {
107107
it('verifies a valid signature for "' + f.message + '"', function () {
108108
var d = BigInteger.fromHex(f.d)
109109
var H = bcrypto.sha256(f.message)
110-
var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex'))
110+
var signature = ECSignature.fromDER(Buffer.from(f.signature, 'hex'))
111111
var Q = curve.G.multiply(d)
112112

113113
assert(ecdsa.verify(H, signature, Q))
@@ -121,7 +121,7 @@ describe('ecdsa', function () {
121121

122122
var signature
123123
if (f.signature) {
124-
signature = ECSignature.fromDER(new Buffer(f.signature, 'hex'))
124+
signature = ECSignature.fromDER(Buffer.from(f.signature, 'hex'))
125125
} else if (f.signatureRaw) {
126126
signature = new ECSignature(new BigInteger(f.signatureRaw.r, 16), new BigInteger(f.signatureRaw.s, 16))
127127
}

test/ecpair.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('ECPair', function () {
5858
fixtures.invalid.constructor.forEach(function (f) {
5959
it('throws ' + f.exception, function () {
6060
var d = f.d && new BigInteger(f.d)
61-
var Q = f.Q && ecurve.Point.decodeFrom(curve, new Buffer(f.Q, 'hex'))
61+
var Q = f.Q && ecurve.Point.decodeFrom(curve, Buffer.from(f.Q, 'hex'))
6262

6363
assert.throws(function () {
6464
new ECPair(d, Q, f.options)
@@ -127,7 +127,7 @@ describe('ECPair', function () {
127127
})
128128

129129
describe('makeRandom', function () {
130-
var d = new Buffer('0404040404040404040404040404040404040404040404040404040404040404', 'hex')
130+
var d = Buffer.from('0404040404040404040404040404040404040404040404040404040404040404', 'hex')
131131
var exWIF = 'KwMWvwRJeFqxYyhZgNwYuYjbQENDAPAudQx5VEmKJrUZcq6aL2pv'
132132

133133
describe('uses randombytes RNG', function () {
@@ -211,7 +211,7 @@ describe('ECPair', function () {
211211

212212
beforeEach(function () {
213213
keyPair = ECPair.makeRandom()
214-
hash = new Buffer(32)
214+
hash = Buffer.alloc(32)
215215
})
216216

217217
describe('signing', function () {

test/ecsignature.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('ECSignature', function () {
2222
describe('parseCompact', function () {
2323
fixtures.valid.forEach(function (f) {
2424
it('imports ' + f.compact.hex + ' correctly', function () {
25-
var buffer = new Buffer(f.compact.hex, 'hex')
25+
var buffer = Buffer.from(f.compact.hex, 'hex')
2626
var parsed = ECSignature.parseCompact(buffer)
2727

2828
assert.strictEqual(parsed.compressed, f.compact.compressed)
@@ -34,7 +34,7 @@ describe('ECSignature', function () {
3434

3535
fixtures.invalid.compact.forEach(function (f) {
3636
it('throws on ' + f.hex, function () {
37-
var buffer = new Buffer(f.hex, 'hex')
37+
var buffer = Buffer.from(f.hex, 'hex')
3838

3939
assert.throws(function () {
4040
ECSignature.parseCompact(buffer)
@@ -57,7 +57,7 @@ describe('ECSignature', function () {
5757
describe('fromDER', function () {
5858
fixtures.valid.forEach(function (f) {
5959
it('imports ' + f.DER + ' correctly', function () {
60-
var buffer = new Buffer(f.DER, 'hex')
60+
var buffer = Buffer.from(f.DER, 'hex')
6161
var signature = ECSignature.fromDER(buffer)
6262

6363
assert.strictEqual(signature.r.toString(), f.signature.r)
@@ -67,7 +67,7 @@ describe('ECSignature', function () {
6767

6868
fixtures.invalid.DER.forEach(function (f) {
6969
it('throws "' + f.exception + '" for ' + f.hex, function () {
70-
var buffer = new Buffer(f.hex, 'hex')
70+
var buffer = Buffer.from(f.hex, 'hex')
7171

7272
assert.throws(function () {
7373
ECSignature.fromDER(buffer)
@@ -100,7 +100,7 @@ describe('ECSignature', function () {
100100
describe('parseScriptSignature', function () {
101101
fixtures.valid.forEach(function (f) {
102102
it('imports ' + f.scriptSignature.hex + ' correctly', function () {
103-
var buffer = new Buffer(f.scriptSignature.hex, 'hex')
103+
var buffer = Buffer.from(f.scriptSignature.hex, 'hex')
104104
var parsed = ECSignature.parseScriptSignature(buffer)
105105

106106
assert.strictEqual(parsed.signature.r.toString(), f.signature.r)
@@ -111,7 +111,7 @@ describe('ECSignature', function () {
111111

112112
fixtures.invalid.scriptSignature.forEach(function (f) {
113113
it('throws on ' + f.hex, function () {
114-
var buffer = new Buffer(f.hex, 'hex')
114+
var buffer = Buffer.from(f.hex, 'hex')
115115

116116
assert.throws(function () {
117117
ECSignature.parseScriptSignature(buffer)

test/hdnode.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ describe('HDNode', function () {
3636
var d = BigInteger.ONE
3737

3838
keyPair = new ECPair(d, null)
39-
chainCode = new Buffer(32)
40-
chainCode.fill(1)
39+
chainCode = Buffer.alloc(32, 1)
4140
})
4241

4342
it('stores the keyPair/chainCode directly', function () {
@@ -64,7 +63,7 @@ describe('HDNode', function () {
6463

6564
it('throws when an invalid length chain code is given', function () {
6665
assert.throws(function () {
67-
new HDNode(keyPair, new Buffer(20))
66+
new HDNode(keyPair, Buffer.alloc(20))
6867
}, /Expected property "1" of type Buffer\(Length: 32\), got Buffer\(Length: 20\)/)
6968
})
7069
})
@@ -116,9 +115,9 @@ describe('HDNode', function () {
116115

117116
beforeEach(function () {
118117
keyPair = ECPair.makeRandom()
119-
hash = new Buffer(32)
118+
hash = Buffer.alloc(32)
120119

121-
var chainCode = new Buffer(32)
120+
var chainCode = Buffer.alloc(32)
122121
hd = new HDNode(keyPair, chainCode)
123122
})
124123

test/integration/advanced.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('bitcoinjs-lib (advanced)', function () {
1515
if (err) return done(err)
1616

1717
var tx = new bitcoin.TransactionBuilder(network)
18-
var data = new Buffer('bitcoinjs-lib')
18+
var data = Buffer.from('bitcoinjs-lib')
1919
var dataScript = bitcoin.script.nullData.output.encode(data)
2020

2121
tx.addInput(unspent.txId, unspent.vout)

0 commit comments

Comments
 (0)