Check for consistency if object argument includes a hash.

This commit is contained in:
Braydon Fuller 2015-08-12 20:26:25 -04:00
parent be8cb7d245
commit e7c7a9ad89
2 changed files with 20 additions and 5 deletions

View File

@ -350,10 +350,14 @@ Transaction.prototype.toObject = function toObject() {
return obj; return obj;
}; };
Transaction.prototype.fromObject = function(transaction) { Transaction.prototype.fromObject = function(arg) {
/* jshint maxstatements: 20 */
var self = this; var self = this;
if (transaction instanceof Transaction) { var transaction;
if (arg instanceof Transaction) {
transaction = transaction.toObject(); transaction = transaction.toObject();
} else {
transaction = arg;
} }
_.each(transaction.inputs, function(input) { _.each(transaction.inputs, function(input) {
if (!input.output || !input.output.script) { if (!input.output || !input.output.script) {
@ -387,18 +391,20 @@ Transaction.prototype.fromObject = function(transaction) {
} }
this.nLockTime = transaction.nLockTime; this.nLockTime = transaction.nLockTime;
this.version = transaction.version; this.version = transaction.version;
this._checkConsistency(); this._checkConsistency(arg);
return this; return this;
}; };
Transaction.prototype._checkConsistency = function() { Transaction.prototype._checkConsistency = function(arg) {
if (!_.isUndefined(this._changeIndex)) { if (!_.isUndefined(this._changeIndex)) {
$.checkState(this._changeScript); $.checkState(this._changeScript);
$.checkState(this.outputs[this._changeIndex]); $.checkState(this.outputs[this._changeIndex]);
$.checkState(this.outputs[this._changeIndex].script.toString() === $.checkState(this.outputs[this._changeIndex].script.toString() ===
this._changeScript.toString()); this._changeScript.toString());
} }
// TODO: add other checks if (arg && arg.hash) {
$.checkState(arg.hash === this.hash, 'Hash in object does not match transaction hash');
}
}; };
/** /**

View File

@ -763,6 +763,15 @@ describe('Transaction', function() {
.should.throw('Unsupported input script type: OP_1 OP_ADD OP_2 OP_EQUAL'); .should.throw('Unsupported input script type: OP_1 OP_ADD OP_2 OP_EQUAL');
}); });
it('will error if object hash does not match transaction hash', function() {
var tx = new Transaction(tx_1_hex);
var txObj = tx.toObject();
txObj.hash = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458';
(function() {
var tx2 = new Transaction(txObj);
}).should.throw('Hash in object does not match transaction hash');
});
describe('inputAmount + outputAmount', function() { describe('inputAmount + outputAmount', function() {
it('returns correct values for simple transaction', function() { it('returns correct values for simple transaction', function() {
var transaction = new Transaction() var transaction = new Transaction()