diff --git a/src/transaction.js b/src/transaction.js index fa1b18a..6e8fc6c 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -137,6 +137,22 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) { }) - 1) } +Transaction.prototype.byteLength = function () { + function scriptSize (script) { + var length = script.buffer.length + + return bufferutils.varIntSize(length) + length + } + + return ( + 8 + + bufferutils.varIntSize(this.ins.length) + + bufferutils.varIntSize(this.outs.length) + + 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) + ) +} + Transaction.prototype.clone = function () { var newTx = new Transaction() newTx.version = this.version @@ -215,19 +231,7 @@ Transaction.prototype.getId = function () { } Transaction.prototype.toBuffer = function () { - function scriptSize (script) { - var length = script.buffer.length - - return bufferutils.varIntSize(length) + length - } - - var buffer = new Buffer( - 8 + - bufferutils.varIntSize(this.ins.length) + - bufferutils.varIntSize(this.outs.length) + - 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 buffer = new Buffer(this.byteLength()) var offset = 0 function writeSlice (slice) {