From 6eae0241aa0c6d8c24a2f877083585d12ec50b3a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 12 Jul 2016 12:31:54 +1000 Subject: [PATCH 1/3] Transaction: allow parameterizable buffer --- src/transaction.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index f645dca..382fc5d 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -241,7 +241,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT // serialize and hash var buffer = new Buffer(txTmp.byteLength() + 4) buffer.writeInt32LE(hashType, buffer.length - 4) - txTmp.toBuffer().copy(buffer, 0) + txTmp.toBuffer(buffer, 0) return bcrypto.hash256(buffer) } @@ -255,10 +255,10 @@ Transaction.prototype.getId = function () { return bufferReverse(this.getHash()).toString('hex') } -Transaction.prototype.toBuffer = function () { - var buffer = new Buffer(this.byteLength()) +Transaction.prototype.toBuffer = function (buffer, offset) { + if (!buffer) buffer = new Buffer(this.byteLength()) - var offset = 0 + offset = offset || 0 function writeSlice (slice) { slice.copy(buffer, offset) offset += slice.length From cc1af7d6a3bf69668f736b5731f75b480dedd94e Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 10 Aug 2016 11:43:36 +1000 Subject: [PATCH 2/3] Transaction: return a slice, not the original Buffer --- src/transaction.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index 382fc5d..cbd316c 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -255,10 +255,10 @@ Transaction.prototype.getId = function () { return bufferReverse(this.getHash()).toString('hex') } -Transaction.prototype.toBuffer = function (buffer, offset) { +Transaction.prototype.toBuffer = function (buffer, initialOffset) { if (!buffer) buffer = new Buffer(this.byteLength()) - offset = offset || 0 + var offset = initialOffset || 0 function writeSlice (slice) { slice.copy(buffer, offset) offset += slice.length @@ -304,6 +304,9 @@ Transaction.prototype.toBuffer = function (buffer, offset) { writeUInt32(this.locktime) + // avoid slicing unless necessary + if (initialOffset !== undefined) return buffer.slice(initialOffset, offset) + return buffer } From fa54ba4f82a7280bcd924a33de0e6952fff42b7d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 10 Aug 2016 11:43:48 +1000 Subject: [PATCH 3/3] tests/transaction: add test for target Buffer --- test/transaction.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/transaction.js b/test/transaction.js index d11ab29..b17dbf8 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -67,6 +67,24 @@ describe('Transaction', function () { 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 () {