Skip to content

Commit aeb0312

Browse files
committed
use safe-buffers throughout impl
1 parent 342633d commit aeb0312

File tree

11 files changed

+44
-44
lines changed

11 files changed

+44
-44
lines changed

src/address.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function fromBase58Check (address) {
99
if (payload.length < 21) throw new TypeError(address + ' is too short')
1010
if (payload.length > 21) throw new TypeError(address + ' is too long')
1111

12-
var version = payload[0]
12+
var version = payload.readUInt8(0)
1313
var hash = payload.slice(1)
1414

1515
return { hash: hash, version: version }
@@ -18,7 +18,7 @@ function fromBase58Check (address) {
1818
function toBase58Check (hash, version) {
1919
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
2020

21-
var payload = new Buffer(21)
21+
var payload = Buffer.allocUnsafe(21)
2222
payload.writeUInt8(version, 0)
2323
hash.copy(payload, 1)
2424

src/block.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Block.prototype.byteLength = function (headersOnly) {
7878
}
7979

8080
Block.fromHex = function (hex) {
81-
return Block.fromBuffer(new Buffer(hex, 'hex'))
81+
return Block.fromBuffer(Buffer.from(hex, 'hex'))
8282
}
8383

8484
Block.prototype.getHash = function () {
@@ -98,7 +98,7 @@ Block.prototype.getUTCDate = function () {
9898

9999
// TODO: buffer, offset compatibility
100100
Block.prototype.toBuffer = function (headersOnly) {
101-
var buffer = new Buffer(this.byteLength(headersOnly))
101+
var buffer = Buffer.allocUnsafe(this.byteLength(headersOnly))
102102

103103
var offset = 0
104104
function writeSlice (slice) {
@@ -143,8 +143,7 @@ Block.prototype.toHex = function (headersOnly) {
143143
Block.calculateTarget = function (bits) {
144144
var exponent = ((bits & 0xff000000) >> 24) - 3
145145
var mantissa = bits & 0x007fffff
146-
var target = new Buffer(32)
147-
target.fill(0)
146+
var target = Buffer.alloc(32, 0)
148147
target.writeUInt32BE(mantissa, 28 - exponent)
149148
return target
150149
}

src/ecdsa.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var types = require('./types')
55
var BigInteger = require('bigi')
66
var ECSignature = require('./ecsignature')
77

8-
var ZERO = new Buffer([0])
9-
var ONE = new Buffer([1])
8+
var ZERO = Buffer.alloc(1, 0)
9+
var ONE = Buffer.alloc(1, 1)
1010

1111
var ecurve = require('ecurve')
1212
var secp256k1 = ecurve.getCurveByName('secp256k1')
@@ -19,15 +19,11 @@ function deterministicGenerateK (hash, x, checkSig) {
1919
types.Function
2020
), arguments)
2121

22-
var k = new Buffer(32)
23-
var v = new Buffer(32)
24-
2522
// Step A, ignored as hash already provided
2623
// Step B
27-
v.fill(1)
28-
2924
// Step C
30-
k.fill(0)
25+
var k = Buffer.alloc(32, 0)
26+
var v = Buffer.alloc(32, 1)
3127

3228
// Step D
3329
k = createHmac('sha256', k)

src/ecsignature.js

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

5959
i += 27
6060

61-
var buffer = new Buffer(65)
61+
var buffer = Buffer.allocUnsafe(65)
6262
buffer.writeUInt8(i, 0)
63-
6463
this.r.toBuffer(32).copy(buffer, 1)
6564
this.s.toBuffer(32).copy(buffer, 33)
6665

6766
return buffer
6867
}
6968

7069
ECSignature.prototype.toDER = function () {
71-
var r = new Buffer(this.r.toDERInteger())
72-
var s = new Buffer(this.s.toDERInteger())
70+
var r = Buffer.from(this.r.toDERInteger())
71+
var s = Buffer.from(this.s.toDERInteger())
7372

7473
return bip66.encode(r, s)
7574
}
@@ -78,7 +77,7 @@ ECSignature.prototype.toScriptSignature = function (hashType) {
7877
var hashTypeMod = hashType & ~0x80
7978
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
8079

81-
var hashTypeBuffer = new Buffer(1)
80+
var hashTypeBuffer = Buffer.allocUnsafe(1)
8281
hashTypeBuffer.writeUInt8(hashType, 0)
8382

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

src/hdnode.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function HDNode (keyPair, chainCode) {
2525

2626
HDNode.HIGHEST_BIT = 0x80000000
2727
HDNode.LENGTH = 78
28-
HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
28+
HDNode.MASTER_SECRET = Buffer.from('Bitcoin seed', 'utf8')
2929

3030
HDNode.fromSeedBuffer = function (seed, network) {
3131
typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments)
@@ -48,7 +48,7 @@ HDNode.fromSeedBuffer = function (seed, network) {
4848
}
4949

5050
HDNode.fromSeedHex = function (hex, network) {
51-
return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network)
51+
return HDNode.fromSeedBuffer(Buffer.from(hex, 'hex'), network)
5252
}
5353

5454
HDNode.fromBase58 = function (string, networks) {
@@ -168,7 +168,7 @@ HDNode.prototype.toBase58 = function (__isPrivate) {
168168
// Version
169169
var network = this.keyPair.network
170170
var version = (!this.isNeutered()) ? network.bip32.private : network.bip32.public
171-
var buffer = new Buffer(78)
171+
var buffer = Buffer.allocUnsafe(78)
172172

173173
// 4 bytes: version bytes
174174
buffer.writeUInt32BE(version, 0)
@@ -206,7 +206,7 @@ HDNode.prototype.derive = function (index) {
206206
typeforce(types.UInt32, index)
207207

208208
var isHardened = index >= HDNode.HIGHEST_BIT
209-
var data = new Buffer(37)
209+
var data = Buffer.allocUnsafe(37)
210210

211211
// Hardened child
212212
if (isHardened) {

src/script.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function compile (chunks) {
4444
return accum + 1
4545
}, 0.0)
4646

47-
var buffer = new Buffer(bufferSize)
47+
var buffer = Buffer.allocUnsafe(bufferSize)
4848
var offset = 0
4949

5050
chunks.forEach(function (chunk) {
@@ -142,7 +142,7 @@ function fromASM (asm) {
142142
typeforce(types.Hex, chunkStr)
143143

144144
// data!
145-
return new Buffer(chunkStr, 'hex')
145+
return Buffer.from(chunkStr, 'hex')
146146
}))
147147
}
148148

@@ -152,7 +152,7 @@ function toStack (chunks) {
152152

153153
return chunks.map(function (op) {
154154
if (Buffer.isBuffer(op)) return op
155-
if (op === OPS.OP_0) return new Buffer(0)
155+
if (op === OPS.OP_0) return Buffer.allocUnsafe(0)
156156

157157
return scriptNumber.encode(op - OP_INT_BASE)
158158
})

src/script_number.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function decode (buffer, maxLength, minimal) {
1616
var a = buffer.readUInt32LE(0)
1717
var b = buffer.readUInt8(4)
1818

19-
if (b & 0x80) return -((b & ~0x80) * 0x100000000 + a)
20-
return b * 0x100000000 + a
19+
if (b & 0x80) return -(((b & ~0x80) * 0x100000000) + a)
20+
return (b * 0x100000000) + a
2121
}
2222

2323
var result = 0
@@ -43,7 +43,7 @@ function scriptNumSize (i) {
4343
function encode (number) {
4444
var value = Math.abs(number)
4545
var size = scriptNumSize(value)
46-
var buffer = new Buffer(size)
46+
var buffer = Buffer.allocUnsafe(size)
4747
var negative = number < 0
4848

4949
for (var i = 0; i < size; ++i) {

src/templates/multisig/input.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function check (script, allowIncomplete) {
2121
}
2222
check.toJSON = function () { return 'multisig input' }
2323

24+
var EMPTY_BUFFER = Buffer.allocUnsafe(0)
25+
2426
function encodeStack (signatures, scriptPubKey) {
2527
typeforce([partialSignature], signatures)
2628

@@ -36,7 +38,7 @@ function encodeStack (signatures, scriptPubKey) {
3638
}
3739
}
3840

39-
return [].concat(new Buffer(0), signatures)
41+
return [].concat(EMPTY_BUFFER, signatures)
4042
}
4143

4244
function encode (signatures, scriptPubKey) {

src/templates/witnesscommitment/output.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var types = require('../../types')
55
var typeforce = require('typeforce')
66
var OPS = require('bitcoin-ops')
77

8-
var HEADER = new Buffer('aa21a9ed', 'hex')
8+
var HEADER = Buffer.from('aa21a9ed', 'hex')
99

1010
function check (script) {
1111
var buffer = bscript.compile(script)
@@ -21,7 +21,11 @@ check.toJSON = function () { return 'Witness commitment output' }
2121
function encode (commitment) {
2222
typeforce(types.Hash256bit, commitment)
2323

24-
return bscript.compile([OPS.OP_RETURN, Buffer.concat([HEADER, commitment])])
24+
var buffer = Buffer.allocUnsafe(36)
25+
HEADER.copy(buffer, 0)
26+
commitment.copy(buffer, 4)
27+
28+
return bscript.compile([OPS.OP_RETURN, buffer])
2529
}
2630

2731
function decode (buffer) {

src/transaction.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ Transaction.SIGHASH_ANYONECANPAY = 0x80
3535
Transaction.ADVANCED_TRANSACTION_MARKER = 0x00
3636
Transaction.ADVANCED_TRANSACTION_FLAG = 0x01
3737

38-
var EMPTY_SCRIPT = new Buffer(0)
38+
var EMPTY_SCRIPT = Buffer.allocUnsafe(0)
3939
var EMPTY_WITNESS = []
40-
var ZERO = new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
41-
var ONE = new Buffer('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
42-
var VALUE_UINT64_MAX = new Buffer('ffffffffffffffff', 'hex')
40+
var ZERO = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
41+
var ONE = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
42+
var VALUE_UINT64_MAX = Buffer.from('ffffffffffffffff', 'hex')
4343
var BLANK_OUTPUT = {
4444
script: EMPTY_SCRIPT,
4545
valueBuffer: VALUE_UINT64_MAX
@@ -298,7 +298,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
298298
}
299299

300300
// serialize and hash
301-
var buffer = new Buffer(txTmp.__byteLength(false) + 4)
301+
var buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4)
302302
buffer.writeInt32LE(hashType, buffer.length - 4)
303303
txTmp.__toBuffer(buffer, 0, false)
304304

@@ -323,7 +323,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
323323
var hashSequence = ZERO
324324

325325
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
326-
tbuffer = new Buffer(36 * this.ins.length)
326+
tbuffer = Buffer.allocUnsafe(36 * this.ins.length)
327327
toffset = 0
328328

329329
this.ins.forEach(function (txIn) {
@@ -337,7 +337,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
337337
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
338338
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
339339
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
340-
tbuffer = new Buffer(4 * this.ins.length)
340+
tbuffer = Buffer.allocUnsafe(4 * this.ins.length)
341341
toffset = 0
342342

343343
this.ins.forEach(function (txIn) {
@@ -353,7 +353,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
353353
return sum + 8 + varSliceSize(output.script)
354354
}, 0)
355355

356-
tbuffer = new Buffer(txOutsSize)
356+
tbuffer = Buffer.allocUnsafe(txOutsSize)
357357
toffset = 0
358358

359359
this.outs.forEach(function (out) {
@@ -365,15 +365,15 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
365365
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
366366
var output = this.outs[inIndex]
367367

368-
tbuffer = new Buffer(8 + varSliceSize(output.script))
368+
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script))
369369
toffset = 0
370370
writeUInt64(output.value)
371371
writeVarSlice(output.script)
372372

373373
hashOutputs = bcrypto.hash256(tbuffer)
374374
}
375375

376-
tbuffer = new Buffer(156 + varSliceSize(prevOutScript))
376+
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript))
377377
toffset = 0
378378

379379
var input = this.ins[inIndex]
@@ -405,7 +405,7 @@ Transaction.prototype.toBuffer = function (buffer, initialOffset) {
405405
}
406406

407407
Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitness) {
408-
if (!buffer) buffer = new Buffer(this.__byteLength(__allowWitness))
408+
if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(__allowWitness))
409409

410410
var offset = initialOffset || 0
411411
function writeSlice (slice) { offset += slice.copy(buffer, offset) }

0 commit comments

Comments
 (0)