|
@ -36,8 +36,8 @@ function Transaction(serialized) { |
|
|
} |
|
|
} |
|
|
this.inputs = []; |
|
|
this.inputs = []; |
|
|
this.outputs = []; |
|
|
this.outputs = []; |
|
|
this._inputAmount = 0; |
|
|
this._inputAmount = undefined; |
|
|
this._outputAmount = 0; |
|
|
this._outputAmount = undefined; |
|
|
|
|
|
|
|
|
if (serialized) { |
|
|
if (serialized) { |
|
|
if (serialized instanceof Transaction) { |
|
|
if (serialized instanceof Transaction) { |
|
@ -109,6 +109,20 @@ var hashProperty = { |
|
|
Object.defineProperty(Transaction.prototype, 'hash', hashProperty); |
|
|
Object.defineProperty(Transaction.prototype, 'hash', hashProperty); |
|
|
Object.defineProperty(Transaction.prototype, 'id', hashProperty); |
|
|
Object.defineProperty(Transaction.prototype, 'id', hashProperty); |
|
|
|
|
|
|
|
|
|
|
|
var ioProperty = { |
|
|
|
|
|
configurable: false, |
|
|
|
|
|
writeable: false, |
|
|
|
|
|
enumerable: true, |
|
|
|
|
|
get: function() { |
|
|
|
|
|
return this._getInputAmount(); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
Object.defineProperty(Transaction.prototype, 'inputAmount', ioProperty); |
|
|
|
|
|
ioProperty.get = function() { |
|
|
|
|
|
return this._getOutputAmount(); |
|
|
|
|
|
}; |
|
|
|
|
|
Object.defineProperty(Transaction.prototype, 'outputAmount', ioProperty); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Retrieve the little endian hash of the transaction (used for serialization) |
|
|
* Retrieve the little endian hash of the transaction (used for serialization) |
|
|
* @return {Buffer} |
|
|
* @return {Buffer} |
|
@ -559,9 +573,6 @@ Transaction.prototype.addInput = function(input, outputScript, satoshis) { |
|
|
Transaction.prototype.uncheckedAddInput = function(input) { |
|
|
Transaction.prototype.uncheckedAddInput = function(input) { |
|
|
$.checkArgumentType(input, Input, 'input'); |
|
|
$.checkArgumentType(input, Input, 'input'); |
|
|
this.inputs.push(input); |
|
|
this.inputs.push(input); |
|
|
if (input.output) { |
|
|
|
|
|
this._inputAmount += input.output.satoshis; |
|
|
|
|
|
} |
|
|
|
|
|
this._updateChangeOutput(); |
|
|
this._updateChangeOutput(); |
|
|
return this; |
|
|
return this; |
|
|
}; |
|
|
}; |
|
@ -655,15 +666,60 @@ Transaction.prototype.addData = function(value) { |
|
|
return this; |
|
|
return this; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Add an output to the transaction. |
|
|
|
|
|
* |
|
|
|
|
|
* @param {Output} output the output to add. |
|
|
|
|
|
* @return {Transaction} this, for chaining |
|
|
|
|
|
*/ |
|
|
Transaction.prototype.addOutput = function(output) { |
|
|
Transaction.prototype.addOutput = function(output) { |
|
|
$.checkArgumentType(output, Output, 'output'); |
|
|
$.checkArgumentType(output, Output, 'output'); |
|
|
this._addOutput(output); |
|
|
this._addOutput(output); |
|
|
this._updateChangeOutput(); |
|
|
this._updateChangeOutput(); |
|
|
|
|
|
return this; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Transaction.prototype._addOutput = function(output) { |
|
|
Transaction.prototype._addOutput = function(output) { |
|
|
this.outputs.push(output); |
|
|
this.outputs.push(output); |
|
|
this._outputAmount += output.satoshis; |
|
|
this._outputAmount = undefined; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Calculates or gets the total output amount in satoshis |
|
|
|
|
|
* |
|
|
|
|
|
* @return {Number} the transaction total output amount |
|
|
|
|
|
*/ |
|
|
|
|
|
Transaction.prototype._getOutputAmount = function() { |
|
|
|
|
|
if (_.isUndefined(this._outputAmount)) { |
|
|
|
|
|
var self = this; |
|
|
|
|
|
this._outputAmount = 0; |
|
|
|
|
|
_.each(this.outputs, function(output) { |
|
|
|
|
|
self._outputAmount += output.satoshis; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
return this._outputAmount; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Calculates or gets the total input amount in satoshis |
|
|
|
|
|
* |
|
|
|
|
|
* @return {Number} the transaction total input amount |
|
|
|
|
|
*/ |
|
|
|
|
|
Transaction.prototype._getInputAmount = function() { |
|
|
|
|
|
if (_.isUndefined(this._inputAmount)) { |
|
|
|
|
|
var self = this; |
|
|
|
|
|
this._inputAmount = 0; |
|
|
|
|
|
_.each(this.inputs, function(input) { |
|
|
|
|
|
if (_.isUndefined(input.output)) { |
|
|
|
|
|
throw new errors.Transaction.Input.MissingPreviousOutput(); |
|
|
|
|
|
} |
|
|
|
|
|
self._inputAmount += input.output.satoshis; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
return this._inputAmount; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Transaction.prototype._updateChangeOutput = function() { |
|
|
Transaction.prototype._updateChangeOutput = function() { |
|
@ -714,7 +770,7 @@ Transaction.prototype._estimateFee = function() { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Transaction.prototype._getUnspentValue = function() { |
|
|
Transaction.prototype._getUnspentValue = function() { |
|
|
return this._inputAmount - this._outputAmount; |
|
|
return this._getInputAmount() - this._getOutputAmount(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Transaction.prototype._clearSignatures = function() { |
|
|
Transaction.prototype._clearSignatures = function() { |
|
@ -744,7 +800,6 @@ Transaction.prototype._estimateSize = function() { |
|
|
|
|
|
|
|
|
Transaction.prototype._removeOutput = function(index) { |
|
|
Transaction.prototype._removeOutput = function(index) { |
|
|
var output = this.outputs[index]; |
|
|
var output = this.outputs[index]; |
|
|
this._outputAmount -= output.satoshis; |
|
|
|
|
|
this.outputs = _.without(this.outputs, output); |
|
|
this.outputs = _.without(this.outputs, output); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -766,7 +821,6 @@ Transaction.prototype.removeInput = function(txId, outputIndex) { |
|
|
throw new errors.Transaction.InvalidIndex(index, this.inputs.length); |
|
|
throw new errors.Transaction.InvalidIndex(index, this.inputs.length); |
|
|
} |
|
|
} |
|
|
var input = this.inputs[index]; |
|
|
var input = this.inputs[index]; |
|
|
this._inputAmount -= input.output.satoshis; |
|
|
|
|
|
this.inputs = _.without(this.inputs, input); |
|
|
this.inputs = _.without(this.inputs, input); |
|
|
this._updateChangeOutput(); |
|
|
this._updateChangeOutput(); |
|
|
}; |
|
|
}; |
|
|