Merge pull request #1032 from eordano/transaction/removeInput
Transaction: add `removeInput` member
This commit is contained in:
commit
e1869ba164
|
@ -54,6 +54,9 @@ module.exports = [{
|
||||||
}, {
|
}, {
|
||||||
name: 'NeedMoreInfo',
|
name: 'NeedMoreInfo',
|
||||||
message: '{0}'
|
message: '{0}'
|
||||||
|
}, {
|
||||||
|
name: 'InvalidIndex',
|
||||||
|
message: 'Invalid index: {0} is not between 0, {1}'
|
||||||
}, {
|
}, {
|
||||||
name: 'UnableToVerifySignature',
|
name: 'UnableToVerifySignature',
|
||||||
message: 'Unable to verify signature: {0}'
|
message: 'Unable to verify signature: {0}'
|
||||||
|
|
|
@ -529,7 +529,7 @@ Transaction.prototype._updateChangeOutput = function() {
|
||||||
}
|
}
|
||||||
this._clearSignatures();
|
this._clearSignatures();
|
||||||
if (!_.isUndefined(this._changeOutput)) {
|
if (!_.isUndefined(this._changeOutput)) {
|
||||||
this.removeOutput(this._changeOutput);
|
this._removeOutput(this._changeOutput);
|
||||||
}
|
}
|
||||||
var available = this._getUnspentValue();
|
var available = this._getUnspentValue();
|
||||||
var fee = this.getFee();
|
var fee = this.getFee();
|
||||||
|
@ -589,10 +589,33 @@ Transaction.prototype._estimateSize = function() {
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
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._outputAmount -= output.satoshis;
|
||||||
this.outputs = _.without(this.outputs, this.outputs[this._changeOutput]);
|
this.outputs = _.without(this.outputs, output);
|
||||||
|
};
|
||||||
|
|
||||||
|
Transaction.prototype.removeOutput = function(index) {
|
||||||
|
this._removeOutput(index);
|
||||||
|
this._updateChangeOutput();
|
||||||
|
};
|
||||||
|
|
||||||
|
Transaction.prototype.removeInput = function(txId, outputIndex) {
|
||||||
|
var index;
|
||||||
|
if (!outputIndex && _.isNumber(txId)) {
|
||||||
|
index = txId;
|
||||||
|
} else {
|
||||||
|
index = _.findIndex(this.inputs, function(input) {
|
||||||
|
return input.prevTxId.toString('hex') === txId && input.outputIndex === outputIndex;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (index < 0 || index >= this.inputs.length) {
|
||||||
|
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();
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Signature handling */
|
/* Signature handling */
|
||||||
|
|
|
@ -357,6 +357,40 @@ describe('Transaction', function() {
|
||||||
}).to.not.throw();
|
}).to.not.throw();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('removeInput and removeOutput', function() {
|
||||||
|
it('can remove an input by index', function() {
|
||||||
|
var transaction = new Transaction()
|
||||||
|
.from(simpleUtxoWith1BTC);
|
||||||
|
transaction.inputs.length.should.equal(1);
|
||||||
|
transaction.removeInput(0);
|
||||||
|
transaction._inputAmount.should.equal(0);
|
||||||
|
transaction.inputs.length.should.equal(0);
|
||||||
|
});
|
||||||
|
it('can remove an input by transaction id', function() {
|
||||||
|
var transaction = new Transaction()
|
||||||
|
.from(simpleUtxoWith1BTC);
|
||||||
|
transaction.inputs.length.should.equal(1);
|
||||||
|
transaction.removeInput(simpleUtxoWith1BTC.txId, simpleUtxoWith1BTC.outputIndex);
|
||||||
|
transaction._inputAmount.should.equal(0);
|
||||||
|
transaction.inputs.length.should.equal(0);
|
||||||
|
});
|
||||||
|
it('fails if the index provided is invalid', function() {
|
||||||
|
var transaction = new Transaction()
|
||||||
|
.from(simpleUtxoWith1BTC);
|
||||||
|
expect(function() {
|
||||||
|
transaction.removeInput(2);
|
||||||
|
}).to.throw(errors.Transaction.InvalidIndex);
|
||||||
|
});
|
||||||
|
it('an output can be removed by index', function() {
|
||||||
|
var transaction = new Transaction()
|
||||||
|
.to(toAddress, 40000000)
|
||||||
|
.to(toAddress, 40000000);
|
||||||
|
transaction.outputs.length.should.equal(2);
|
||||||
|
transaction.removeOutput(0);
|
||||||
|
transaction.outputs.length.should.equal(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
var tx_empty_hex = '01000000000000000000';
|
var tx_empty_hex = '01000000000000000000';
|
||||||
|
|
Loading…
Reference in New Issue