From dc253bb77ccb544d9cb344ba20abf4f9d493c88e Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 19 Feb 2015 12:11:37 +1100 Subject: [PATCH] Transaction: extract Script size calculations --- src/transaction.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index 7d45e09..eae644b 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -68,7 +68,7 @@ Transaction.fromBuffer = function(buffer, __disableAssert) { for (i = 0; i < voutLen; ++i) { tx.outs.push({ value: readUInt64(), - script: readScript(), + script: readScript() }) } @@ -252,20 +252,18 @@ Transaction.prototype.getId = function () { } Transaction.prototype.toBuffer = function () { - var txInSize = this.ins.reduce(function(a, x) { - return a + (40 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length) - }, 0) + function scriptSize(script) { + var length = script.buffer.length - var txOutSize = this.outs.reduce(function(a, x) { - return a + (8 + bufferutils.varIntSize(x.script.buffer.length) + x.script.buffer.length) - }, 0) + return bufferutils.varIntSize(length) + length + } var buffer = new Buffer( 8 + bufferutils.varIntSize(this.ins.length) + bufferutils.varIntSize(this.outs.length) + - txInSize + - txOutSize + this.ins.reduce(function(sum, input) { return sum + 40 + scriptSize(input.script) }, 0) + + this.outs.reduce(function(sum, output) { return sum + 8 + scriptSize(output.script) }, 0) ) var offset = 0