Browse Source

Transaction: extract byteLength calculation to prototype method

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
9cda36fc76
  1. 30
      src/transaction.js

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

Loading…
Cancel
Save