Transaction: extract byteLength calculation to prototype method

This commit is contained in:
Daniel Cousens 2015-03-16 10:50:47 +11:00
parent c3a39444e3
commit 9cda36fc76

View file

@ -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) {