Check for consistency if object argument includes a hash.
This commit is contained in:
parent
be8cb7d245
commit
e7c7a9ad89
|
@ -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');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue