Merge pull request #603 from bitcoinjs/txbuffer
Transaction: allow parameterizable buffer
This commit is contained in:
commit
acbc8fcb2d
2 changed files with 25 additions and 4 deletions
|
@ -241,7 +241,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
||||||
// serialize and hash
|
// serialize and hash
|
||||||
var buffer = new Buffer(txTmp.byteLength() + 4)
|
var buffer = new Buffer(txTmp.byteLength() + 4)
|
||||||
buffer.writeInt32LE(hashType, buffer.length - 4)
|
buffer.writeInt32LE(hashType, buffer.length - 4)
|
||||||
txTmp.toBuffer().copy(buffer, 0)
|
txTmp.toBuffer(buffer, 0)
|
||||||
|
|
||||||
return bcrypto.hash256(buffer)
|
return bcrypto.hash256(buffer)
|
||||||
}
|
}
|
||||||
|
@ -255,10 +255,10 @@ Transaction.prototype.getId = function () {
|
||||||
return bufferReverse(this.getHash()).toString('hex')
|
return bufferReverse(this.getHash()).toString('hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.toBuffer = function () {
|
Transaction.prototype.toBuffer = function (buffer, initialOffset) {
|
||||||
var buffer = new Buffer(this.byteLength())
|
if (!buffer) buffer = new Buffer(this.byteLength())
|
||||||
|
|
||||||
var offset = 0
|
var offset = initialOffset || 0
|
||||||
function writeSlice (slice) {
|
function writeSlice (slice) {
|
||||||
slice.copy(buffer, offset)
|
slice.copy(buffer, offset)
|
||||||
offset += slice.length
|
offset += slice.length
|
||||||
|
@ -304,6 +304,9 @@ Transaction.prototype.toBuffer = function () {
|
||||||
|
|
||||||
writeUInt32(this.locktime)
|
writeUInt32(this.locktime)
|
||||||
|
|
||||||
|
// avoid slicing unless necessary
|
||||||
|
if (initialOffset !== undefined) return buffer.slice(initialOffset, offset)
|
||||||
|
|
||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,24 @@ describe('Transaction', function () {
|
||||||
assert.strictEqual(actual.toHex(), f.hex, actual.toHex())
|
assert.strictEqual(actual.toHex(), f.hex, actual.toHex())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('accepts target Buffer and offset parameters', function () {
|
||||||
|
var f = fixtures.valid[0]
|
||||||
|
var actual = fromRaw(f.raw)
|
||||||
|
var byteLength = actual.byteLength()
|
||||||
|
|
||||||
|
var target = new Buffer(byteLength * 2)
|
||||||
|
var a = actual.toBuffer(target, 0)
|
||||||
|
var b = actual.toBuffer(target, byteLength)
|
||||||
|
|
||||||
|
assert.strictEqual(a.length, byteLength)
|
||||||
|
assert.strictEqual(b.length, byteLength)
|
||||||
|
assert.strictEqual(a.toString('hex'), f.hex)
|
||||||
|
assert.strictEqual(b.toString('hex'), f.hex)
|
||||||
|
assert.deepEqual(a, b)
|
||||||
|
assert.deepEqual(a, target.slice(0, byteLength))
|
||||||
|
assert.deepEqual(b, target.slice(byteLength))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('addInput', function () {
|
describe('addInput', function () {
|
||||||
|
|
Loading…
Add table
Reference in a new issue