Merge pull request #1123 from maraoz/transaction/amounts
add Transaction#inputAmount and outputAmount
This commit is contained in:
commit
5d3b8fbbfb
|
@ -56,6 +56,9 @@ module.exports = [{
|
|||
}, {
|
||||
name: 'UnsupportedScript',
|
||||
message: 'Unsupported input script type: {0}'
|
||||
}, {
|
||||
name: 'MissingPreviousOutput',
|
||||
message: 'No previous output information.'
|
||||
}]
|
||||
}, {
|
||||
name: 'NeedMoreInfo',
|
||||
|
|
|
@ -36,8 +36,8 @@ function Transaction(serialized) {
|
|||
}
|
||||
this.inputs = [];
|
||||
this.outputs = [];
|
||||
this._inputAmount = 0;
|
||||
this._outputAmount = 0;
|
||||
this._inputAmount = undefined;
|
||||
this._outputAmount = undefined;
|
||||
|
||||
if (serialized) {
|
||||
if (serialized instanceof Transaction) {
|
||||
|
@ -109,6 +109,20 @@ var hashProperty = {
|
|||
Object.defineProperty(Transaction.prototype, 'hash', 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)
|
||||
* @return {Buffer}
|
||||
|
@ -559,9 +573,6 @@ Transaction.prototype.addInput = function(input, outputScript, satoshis) {
|
|||
Transaction.prototype.uncheckedAddInput = function(input) {
|
||||
$.checkArgumentType(input, Input, 'input');
|
||||
this.inputs.push(input);
|
||||
if (input.output) {
|
||||
this._inputAmount += input.output.satoshis;
|
||||
}
|
||||
this._updateChangeOutput();
|
||||
return this;
|
||||
};
|
||||
|
@ -655,15 +666,60 @@ Transaction.prototype.addData = function(value) {
|
|||
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) {
|
||||
$.checkArgumentType(output, Output, 'output');
|
||||
this._addOutput(output);
|
||||
this._updateChangeOutput();
|
||||
return this;
|
||||
};
|
||||
|
||||
Transaction.prototype._addOutput = function(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() {
|
||||
|
@ -714,7 +770,7 @@ Transaction.prototype._estimateFee = function() {
|
|||
};
|
||||
|
||||
Transaction.prototype._getUnspentValue = function() {
|
||||
return this._inputAmount - this._outputAmount;
|
||||
return this._getInputAmount() - this._getOutputAmount();
|
||||
};
|
||||
|
||||
Transaction.prototype._clearSignatures = function() {
|
||||
|
@ -744,7 +800,6 @@ Transaction.prototype._estimateSize = function() {
|
|||
|
||||
Transaction.prototype._removeOutput = function(index) {
|
||||
var output = this.outputs[index];
|
||||
this._outputAmount -= output.satoshis;
|
||||
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);
|
||||
}
|
||||
var input = this.inputs[index];
|
||||
this._inputAmount -= input.output.satoshis;
|
||||
this.inputs = _.without(this.inputs, input);
|
||||
this._updateChangeOutput();
|
||||
};
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue