Merge pull request #1123 from maraoz/transaction/amounts

add Transaction#inputAmount and outputAmount
This commit is contained in:
Esteban Ordano 2015-02-27 17:02:06 -03:00
commit 5d3b8fbbfb
3 changed files with 101 additions and 16 deletions

View File

@ -56,6 +56,9 @@ module.exports = [{
}, {
name: 'UnsupportedScript',
message: 'Unsupported input script type: {0}'
}, {
name: 'MissingPreviousOutput',
message: 'No previous output information.'
}]
}, {
name: 'NeedMoreInfo',

View File

@ -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}
@ -348,7 +362,7 @@ Transaction.prototype._checkConsistency = function() {
$.checkState(this._changeScript);
$.checkState(this.outputs[this._changeIndex]);
$.checkState(this.outputs[this._changeIndex].script.toString() ===
this._changeScript.toString());
this._changeScript.toString());
}
// TODO: add other checks
};
@ -509,7 +523,7 @@ Transaction.prototype._fromNonP2SH = function(utxo) {
Transaction.prototype._fromMultisigUtxo = function(utxo, pubkeys, threshold) {
$.checkArgument(threshold <= pubkeys.length,
'Number of required signatures must be greater than the number of public keys');
'Number of required signatures must be greater than the number of public keys');
utxo = new UnspentOutput(utxo);
this.addInput(new MultiSigScriptHashInput({
output: new Output({
@ -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