diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index d643ad787..06215f4dc 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -350,10 +350,14 @@ Transaction.prototype.toObject = function toObject() { return obj; }; -Transaction.prototype.fromObject = function(transaction) { +Transaction.prototype.fromObject = function(arg) { + /* jshint maxstatements: 20 */ var self = this; - if (transaction instanceof Transaction) { + var transaction; + if (arg instanceof Transaction) { transaction = transaction.toObject(); + } else { + transaction = arg; } _.each(transaction.inputs, function(input) { if (!input.output || !input.output.script) { @@ -387,18 +391,20 @@ Transaction.prototype.fromObject = function(transaction) { } this.nLockTime = transaction.nLockTime; this.version = transaction.version; - this._checkConsistency(); + this._checkConsistency(arg); return this; }; -Transaction.prototype._checkConsistency = function() { +Transaction.prototype._checkConsistency = function(arg) { if (!_.isUndefined(this._changeIndex)) { $.checkState(this._changeScript); $.checkState(this.outputs[this._changeIndex]); $.checkState(this.outputs[this._changeIndex].script.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'); + } }; /** diff --git a/test/transaction/transaction.js b/test/transaction/transaction.js index 07c52f28e..133ccb014 100644 --- a/test/transaction/transaction.js +++ b/test/transaction/transaction.js @@ -763,6 +763,15 @@ describe('Transaction', function() { .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() { it('returns correct values for simple transaction', function() { var transaction = new Transaction()