use safe-buffers throughout impl
This commit is contained in:
parent
342633d7c9
commit
aeb0312d63
11 changed files with 44 additions and 44 deletions
|
@ -9,7 +9,7 @@ function fromBase58Check (address) {
|
||||||
if (payload.length < 21) throw new TypeError(address + ' is too short')
|
if (payload.length < 21) throw new TypeError(address + ' is too short')
|
||||||
if (payload.length > 21) throw new TypeError(address + ' is too long')
|
if (payload.length > 21) throw new TypeError(address + ' is too long')
|
||||||
|
|
||||||
var version = payload[0]
|
var version = payload.readUInt8(0)
|
||||||
var hash = payload.slice(1)
|
var hash = payload.slice(1)
|
||||||
|
|
||||||
return { hash: hash, version: version }
|
return { hash: hash, version: version }
|
||||||
|
@ -18,7 +18,7 @@ function fromBase58Check (address) {
|
||||||
function toBase58Check (hash, version) {
|
function toBase58Check (hash, version) {
|
||||||
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
||||||
|
|
||||||
var payload = new Buffer(21)
|
var payload = Buffer.allocUnsafe(21)
|
||||||
payload.writeUInt8(version, 0)
|
payload.writeUInt8(version, 0)
|
||||||
hash.copy(payload, 1)
|
hash.copy(payload, 1)
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ Block.prototype.byteLength = function (headersOnly) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Block.fromHex = function (hex) {
|
Block.fromHex = function (hex) {
|
||||||
return Block.fromBuffer(new Buffer(hex, 'hex'))
|
return Block.fromBuffer(Buffer.from(hex, 'hex'))
|
||||||
}
|
}
|
||||||
|
|
||||||
Block.prototype.getHash = function () {
|
Block.prototype.getHash = function () {
|
||||||
|
@ -98,7 +98,7 @@ Block.prototype.getUTCDate = function () {
|
||||||
|
|
||||||
// TODO: buffer, offset compatibility
|
// TODO: buffer, offset compatibility
|
||||||
Block.prototype.toBuffer = function (headersOnly) {
|
Block.prototype.toBuffer = function (headersOnly) {
|
||||||
var buffer = new Buffer(this.byteLength(headersOnly))
|
var buffer = Buffer.allocUnsafe(this.byteLength(headersOnly))
|
||||||
|
|
||||||
var offset = 0
|
var offset = 0
|
||||||
function writeSlice (slice) {
|
function writeSlice (slice) {
|
||||||
|
@ -143,8 +143,7 @@ Block.prototype.toHex = function (headersOnly) {
|
||||||
Block.calculateTarget = function (bits) {
|
Block.calculateTarget = function (bits) {
|
||||||
var exponent = ((bits & 0xff000000) >> 24) - 3
|
var exponent = ((bits & 0xff000000) >> 24) - 3
|
||||||
var mantissa = bits & 0x007fffff
|
var mantissa = bits & 0x007fffff
|
||||||
var target = new Buffer(32)
|
var target = Buffer.alloc(32, 0)
|
||||||
target.fill(0)
|
|
||||||
target.writeUInt32BE(mantissa, 28 - exponent)
|
target.writeUInt32BE(mantissa, 28 - exponent)
|
||||||
return target
|
return target
|
||||||
}
|
}
|
||||||
|
|
12
src/ecdsa.js
12
src/ecdsa.js
|
@ -5,8 +5,8 @@ var types = require('./types')
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECSignature = require('./ecsignature')
|
var ECSignature = require('./ecsignature')
|
||||||
|
|
||||||
var ZERO = new Buffer([0])
|
var ZERO = Buffer.alloc(1, 0)
|
||||||
var ONE = new Buffer([1])
|
var ONE = Buffer.alloc(1, 1)
|
||||||
|
|
||||||
var ecurve = require('ecurve')
|
var ecurve = require('ecurve')
|
||||||
var secp256k1 = ecurve.getCurveByName('secp256k1')
|
var secp256k1 = ecurve.getCurveByName('secp256k1')
|
||||||
|
@ -19,15 +19,11 @@ function deterministicGenerateK (hash, x, checkSig) {
|
||||||
types.Function
|
types.Function
|
||||||
), arguments)
|
), arguments)
|
||||||
|
|
||||||
var k = new Buffer(32)
|
|
||||||
var v = new Buffer(32)
|
|
||||||
|
|
||||||
// Step A, ignored as hash already provided
|
// Step A, ignored as hash already provided
|
||||||
// Step B
|
// Step B
|
||||||
v.fill(1)
|
|
||||||
|
|
||||||
// Step C
|
// Step C
|
||||||
k.fill(0)
|
var k = Buffer.alloc(32, 0)
|
||||||
|
var v = Buffer.alloc(32, 1)
|
||||||
|
|
||||||
// Step D
|
// Step D
|
||||||
k = createHmac('sha256', k)
|
k = createHmac('sha256', k)
|
||||||
|
|
|
@ -58,9 +58,8 @@ ECSignature.prototype.toCompact = function (i, compressed) {
|
||||||
|
|
||||||
i += 27
|
i += 27
|
||||||
|
|
||||||
var buffer = new Buffer(65)
|
var buffer = Buffer.allocUnsafe(65)
|
||||||
buffer.writeUInt8(i, 0)
|
buffer.writeUInt8(i, 0)
|
||||||
|
|
||||||
this.r.toBuffer(32).copy(buffer, 1)
|
this.r.toBuffer(32).copy(buffer, 1)
|
||||||
this.s.toBuffer(32).copy(buffer, 33)
|
this.s.toBuffer(32).copy(buffer, 33)
|
||||||
|
|
||||||
|
@ -68,8 +67,8 @@ ECSignature.prototype.toCompact = function (i, compressed) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ECSignature.prototype.toDER = function () {
|
ECSignature.prototype.toDER = function () {
|
||||||
var r = new Buffer(this.r.toDERInteger())
|
var r = Buffer.from(this.r.toDERInteger())
|
||||||
var s = new Buffer(this.s.toDERInteger())
|
var s = Buffer.from(this.s.toDERInteger())
|
||||||
|
|
||||||
return bip66.encode(r, s)
|
return bip66.encode(r, s)
|
||||||
}
|
}
|
||||||
|
@ -78,7 +77,7 @@ ECSignature.prototype.toScriptSignature = function (hashType) {
|
||||||
var hashTypeMod = hashType & ~0x80
|
var hashTypeMod = hashType & ~0x80
|
||||||
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
||||||
|
|
||||||
var hashTypeBuffer = new Buffer(1)
|
var hashTypeBuffer = Buffer.allocUnsafe(1)
|
||||||
hashTypeBuffer.writeUInt8(hashType, 0)
|
hashTypeBuffer.writeUInt8(hashType, 0)
|
||||||
|
|
||||||
return Buffer.concat([this.toDER(), hashTypeBuffer])
|
return Buffer.concat([this.toDER(), hashTypeBuffer])
|
||||||
|
|
|
@ -25,7 +25,7 @@ function HDNode (keyPair, chainCode) {
|
||||||
|
|
||||||
HDNode.HIGHEST_BIT = 0x80000000
|
HDNode.HIGHEST_BIT = 0x80000000
|
||||||
HDNode.LENGTH = 78
|
HDNode.LENGTH = 78
|
||||||
HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
|
HDNode.MASTER_SECRET = Buffer.from('Bitcoin seed', 'utf8')
|
||||||
|
|
||||||
HDNode.fromSeedBuffer = function (seed, network) {
|
HDNode.fromSeedBuffer = function (seed, network) {
|
||||||
typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments)
|
typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments)
|
||||||
|
@ -48,7 +48,7 @@ HDNode.fromSeedBuffer = function (seed, network) {
|
||||||
}
|
}
|
||||||
|
|
||||||
HDNode.fromSeedHex = function (hex, network) {
|
HDNode.fromSeedHex = function (hex, network) {
|
||||||
return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network)
|
return HDNode.fromSeedBuffer(Buffer.from(hex, 'hex'), network)
|
||||||
}
|
}
|
||||||
|
|
||||||
HDNode.fromBase58 = function (string, networks) {
|
HDNode.fromBase58 = function (string, networks) {
|
||||||
|
@ -168,7 +168,7 @@ HDNode.prototype.toBase58 = function (__isPrivate) {
|
||||||
// Version
|
// Version
|
||||||
var network = this.keyPair.network
|
var network = this.keyPair.network
|
||||||
var version = (!this.isNeutered()) ? network.bip32.private : network.bip32.public
|
var version = (!this.isNeutered()) ? network.bip32.private : network.bip32.public
|
||||||
var buffer = new Buffer(78)
|
var buffer = Buffer.allocUnsafe(78)
|
||||||
|
|
||||||
// 4 bytes: version bytes
|
// 4 bytes: version bytes
|
||||||
buffer.writeUInt32BE(version, 0)
|
buffer.writeUInt32BE(version, 0)
|
||||||
|
@ -206,7 +206,7 @@ HDNode.prototype.derive = function (index) {
|
||||||
typeforce(types.UInt32, index)
|
typeforce(types.UInt32, index)
|
||||||
|
|
||||||
var isHardened = index >= HDNode.HIGHEST_BIT
|
var isHardened = index >= HDNode.HIGHEST_BIT
|
||||||
var data = new Buffer(37)
|
var data = Buffer.allocUnsafe(37)
|
||||||
|
|
||||||
// Hardened child
|
// Hardened child
|
||||||
if (isHardened) {
|
if (isHardened) {
|
||||||
|
|
|
@ -44,7 +44,7 @@ function compile (chunks) {
|
||||||
return accum + 1
|
return accum + 1
|
||||||
}, 0.0)
|
}, 0.0)
|
||||||
|
|
||||||
var buffer = new Buffer(bufferSize)
|
var buffer = Buffer.allocUnsafe(bufferSize)
|
||||||
var offset = 0
|
var offset = 0
|
||||||
|
|
||||||
chunks.forEach(function (chunk) {
|
chunks.forEach(function (chunk) {
|
||||||
|
@ -142,7 +142,7 @@ function fromASM (asm) {
|
||||||
typeforce(types.Hex, chunkStr)
|
typeforce(types.Hex, chunkStr)
|
||||||
|
|
||||||
// data!
|
// data!
|
||||||
return new Buffer(chunkStr, 'hex')
|
return Buffer.from(chunkStr, 'hex')
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +152,7 @@ function toStack (chunks) {
|
||||||
|
|
||||||
return chunks.map(function (op) {
|
return chunks.map(function (op) {
|
||||||
if (Buffer.isBuffer(op)) return op
|
if (Buffer.isBuffer(op)) return op
|
||||||
if (op === OPS.OP_0) return new Buffer(0)
|
if (op === OPS.OP_0) return Buffer.allocUnsafe(0)
|
||||||
|
|
||||||
return scriptNumber.encode(op - OP_INT_BASE)
|
return scriptNumber.encode(op - OP_INT_BASE)
|
||||||
})
|
})
|
||||||
|
|
|
@ -16,8 +16,8 @@ function decode (buffer, maxLength, minimal) {
|
||||||
var a = buffer.readUInt32LE(0)
|
var a = buffer.readUInt32LE(0)
|
||||||
var b = buffer.readUInt8(4)
|
var b = buffer.readUInt8(4)
|
||||||
|
|
||||||
if (b & 0x80) return -((b & ~0x80) * 0x100000000 + a)
|
if (b & 0x80) return -(((b & ~0x80) * 0x100000000) + a)
|
||||||
return b * 0x100000000 + a
|
return (b * 0x100000000) + a
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = 0
|
var result = 0
|
||||||
|
@ -43,7 +43,7 @@ function scriptNumSize (i) {
|
||||||
function encode (number) {
|
function encode (number) {
|
||||||
var value = Math.abs(number)
|
var value = Math.abs(number)
|
||||||
var size = scriptNumSize(value)
|
var size = scriptNumSize(value)
|
||||||
var buffer = new Buffer(size)
|
var buffer = Buffer.allocUnsafe(size)
|
||||||
var negative = number < 0
|
var negative = number < 0
|
||||||
|
|
||||||
for (var i = 0; i < size; ++i) {
|
for (var i = 0; i < size; ++i) {
|
||||||
|
|
|
@ -21,6 +21,8 @@ function check (script, allowIncomplete) {
|
||||||
}
|
}
|
||||||
check.toJSON = function () { return 'multisig input' }
|
check.toJSON = function () { return 'multisig input' }
|
||||||
|
|
||||||
|
var EMPTY_BUFFER = Buffer.allocUnsafe(0)
|
||||||
|
|
||||||
function encodeStack (signatures, scriptPubKey) {
|
function encodeStack (signatures, scriptPubKey) {
|
||||||
typeforce([partialSignature], signatures)
|
typeforce([partialSignature], signatures)
|
||||||
|
|
||||||
|
@ -36,7 +38,7 @@ function encodeStack (signatures, scriptPubKey) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [].concat(new Buffer(0), signatures)
|
return [].concat(EMPTY_BUFFER, signatures)
|
||||||
}
|
}
|
||||||
|
|
||||||
function encode (signatures, scriptPubKey) {
|
function encode (signatures, scriptPubKey) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ var types = require('../../types')
|
||||||
var typeforce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
var OPS = require('bitcoin-ops')
|
var OPS = require('bitcoin-ops')
|
||||||
|
|
||||||
var HEADER = new Buffer('aa21a9ed', 'hex')
|
var HEADER = Buffer.from('aa21a9ed', 'hex')
|
||||||
|
|
||||||
function check (script) {
|
function check (script) {
|
||||||
var buffer = bscript.compile(script)
|
var buffer = bscript.compile(script)
|
||||||
|
@ -21,7 +21,11 @@ check.toJSON = function () { return 'Witness commitment output' }
|
||||||
function encode (commitment) {
|
function encode (commitment) {
|
||||||
typeforce(types.Hash256bit, commitment)
|
typeforce(types.Hash256bit, commitment)
|
||||||
|
|
||||||
return bscript.compile([OPS.OP_RETURN, Buffer.concat([HEADER, commitment])])
|
var buffer = Buffer.allocUnsafe(36)
|
||||||
|
HEADER.copy(buffer, 0)
|
||||||
|
commitment.copy(buffer, 4)
|
||||||
|
|
||||||
|
return bscript.compile([OPS.OP_RETURN, buffer])
|
||||||
}
|
}
|
||||||
|
|
||||||
function decode (buffer) {
|
function decode (buffer) {
|
||||||
|
|
|
@ -35,11 +35,11 @@ Transaction.SIGHASH_ANYONECANPAY = 0x80
|
||||||
Transaction.ADVANCED_TRANSACTION_MARKER = 0x00
|
Transaction.ADVANCED_TRANSACTION_MARKER = 0x00
|
||||||
Transaction.ADVANCED_TRANSACTION_FLAG = 0x01
|
Transaction.ADVANCED_TRANSACTION_FLAG = 0x01
|
||||||
|
|
||||||
var EMPTY_SCRIPT = new Buffer(0)
|
var EMPTY_SCRIPT = Buffer.allocUnsafe(0)
|
||||||
var EMPTY_WITNESS = []
|
var EMPTY_WITNESS = []
|
||||||
var ZERO = new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
var ZERO = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
||||||
var ONE = new Buffer('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
|
var ONE = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
|
||||||
var VALUE_UINT64_MAX = new Buffer('ffffffffffffffff', 'hex')
|
var VALUE_UINT64_MAX = Buffer.from('ffffffffffffffff', 'hex')
|
||||||
var BLANK_OUTPUT = {
|
var BLANK_OUTPUT = {
|
||||||
script: EMPTY_SCRIPT,
|
script: EMPTY_SCRIPT,
|
||||||
valueBuffer: VALUE_UINT64_MAX
|
valueBuffer: VALUE_UINT64_MAX
|
||||||
|
@ -298,7 +298,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
||||||
}
|
}
|
||||||
|
|
||||||
// serialize and hash
|
// serialize and hash
|
||||||
var buffer = new Buffer(txTmp.__byteLength(false) + 4)
|
var buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4)
|
||||||
buffer.writeInt32LE(hashType, buffer.length - 4)
|
buffer.writeInt32LE(hashType, buffer.length - 4)
|
||||||
txTmp.__toBuffer(buffer, 0, false)
|
txTmp.__toBuffer(buffer, 0, false)
|
||||||
|
|
||||||
|
@ -323,7 +323,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
||||||
var hashSequence = ZERO
|
var hashSequence = ZERO
|
||||||
|
|
||||||
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
|
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
|
||||||
tbuffer = new Buffer(36 * this.ins.length)
|
tbuffer = Buffer.allocUnsafe(36 * this.ins.length)
|
||||||
toffset = 0
|
toffset = 0
|
||||||
|
|
||||||
this.ins.forEach(function (txIn) {
|
this.ins.forEach(function (txIn) {
|
||||||
|
@ -337,7 +337,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
||||||
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
|
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
|
||||||
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
||||||
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
|
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
|
||||||
tbuffer = new Buffer(4 * this.ins.length)
|
tbuffer = Buffer.allocUnsafe(4 * this.ins.length)
|
||||||
toffset = 0
|
toffset = 0
|
||||||
|
|
||||||
this.ins.forEach(function (txIn) {
|
this.ins.forEach(function (txIn) {
|
||||||
|
@ -353,7 +353,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
||||||
return sum + 8 + varSliceSize(output.script)
|
return sum + 8 + varSliceSize(output.script)
|
||||||
}, 0)
|
}, 0)
|
||||||
|
|
||||||
tbuffer = new Buffer(txOutsSize)
|
tbuffer = Buffer.allocUnsafe(txOutsSize)
|
||||||
toffset = 0
|
toffset = 0
|
||||||
|
|
||||||
this.outs.forEach(function (out) {
|
this.outs.forEach(function (out) {
|
||||||
|
@ -365,7 +365,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
||||||
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
|
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
|
||||||
var output = this.outs[inIndex]
|
var output = this.outs[inIndex]
|
||||||
|
|
||||||
tbuffer = new Buffer(8 + varSliceSize(output.script))
|
tbuffer = Buffer.allocUnsafe(8 + varSliceSize(output.script))
|
||||||
toffset = 0
|
toffset = 0
|
||||||
writeUInt64(output.value)
|
writeUInt64(output.value)
|
||||||
writeVarSlice(output.script)
|
writeVarSlice(output.script)
|
||||||
|
@ -373,7 +373,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
||||||
hashOutputs = bcrypto.hash256(tbuffer)
|
hashOutputs = bcrypto.hash256(tbuffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
tbuffer = new Buffer(156 + varSliceSize(prevOutScript))
|
tbuffer = Buffer.allocUnsafe(156 + varSliceSize(prevOutScript))
|
||||||
toffset = 0
|
toffset = 0
|
||||||
|
|
||||||
var input = this.ins[inIndex]
|
var input = this.ins[inIndex]
|
||||||
|
@ -405,7 +405,7 @@ Transaction.prototype.toBuffer = function (buffer, initialOffset) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitness) {
|
Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitness) {
|
||||||
if (!buffer) buffer = new Buffer(this.__byteLength(__allowWitness))
|
if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(__allowWitness))
|
||||||
|
|
||||||
var offset = initialOffset || 0
|
var offset = initialOffset || 0
|
||||||
function writeSlice (slice) { offset += slice.copy(buffer, offset) }
|
function writeSlice (slice) { offset += slice.copy(buffer, offset) }
|
||||||
|
|
|
@ -515,7 +515,7 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
|
||||||
// is it a hex string?
|
// is it a hex string?
|
||||||
if (typeof txHash === 'string') {
|
if (typeof txHash === 'string') {
|
||||||
// transaction hashs's are displayed in reverse order, un-reverse it
|
// transaction hashs's are displayed in reverse order, un-reverse it
|
||||||
txHash = new Buffer(txHash, 'hex').reverse()
|
txHash = Buffer.from(txHash, 'hex').reverse()
|
||||||
|
|
||||||
// is it a Transaction object?
|
// is it a Transaction object?
|
||||||
} else if (txHash instanceof Transaction) {
|
} else if (txHash instanceof Transaction) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue