Transaction: extract byteLength calculation to prototype method
This commit is contained in:
parent
c3a39444e3
commit
9cda36fc76
1 changed files with 17 additions and 13 deletions
|
@ -137,6 +137,22 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
|
||||||
}) - 1)
|
}) - 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 () {
|
Transaction.prototype.clone = function () {
|
||||||
var newTx = new Transaction()
|
var newTx = new Transaction()
|
||||||
newTx.version = this.version
|
newTx.version = this.version
|
||||||
|
@ -215,19 +231,7 @@ Transaction.prototype.getId = function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.toBuffer = function () {
|
Transaction.prototype.toBuffer = function () {
|
||||||
function scriptSize (script) {
|
var buffer = new Buffer(this.byteLength())
|
||||||
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 offset = 0
|
var offset = 0
|
||||||
function writeSlice (slice) {
|
function writeSlice (slice) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue