Merge pull request #916 from eordano/dusterror

Don't allow serialization if dust amounts are detected
This commit is contained in:
Manuel Aráoz 2015-01-09 13:45:07 -03:00
commit f6cc8c695b
4 changed files with 30 additions and 2 deletions

View File

@ -61,6 +61,9 @@ module.exports = [{
}, {
name: 'UnableToVerifySignature',
message: format('Unable to verify signature: {0}')
}, {
name: 'DustOutputs',
message: format('Dust amount detected in one output')
}, {
name: 'FeeError',
message: format('Fees are not correctly set {0}'),

View File

@ -128,6 +128,9 @@ Transaction.prototype.checkedSerialize = Transaction.prototype.toString = functi
throw new errors.Transaction.FeeError(feeError);
}
}
if (this._hasDustOutputs()) {
throw new errors.Transaction.DustOutputs();
}
return this.uncheckedSerialize();
};
@ -145,6 +148,18 @@ Transaction.prototype._validateChange = function() {
}
};
Transaction.DUST_AMOUNT = 5460;
Transaction.prototype._hasDustOutputs = function() {
var output;
for (output in this.outputs) {
if (this.outputs[output].satoshis < Transaction.DUST_AMOUNT) {
return true;
}
}
return false;
};
Transaction.prototype.inspect = function() {
return '<Transaction: ' + this.toString() + '>';
};

View File

@ -12,7 +12,7 @@ describe('Transaction deserialization', function() {
vectors_valid.forEach(function(vector) {
if (vector.length > 1) {
var hexa = vector[1];
Transaction(hexa).serialize().should.equal(hexa);
Transaction(hexa).serialize(true).should.equal(hexa);
index++;
}
});
@ -22,7 +22,7 @@ describe('Transaction deserialization', function() {
vectors_invalid.forEach(function(vector) {
if (vector.length > 1) {
var hexa = vector[1];
Transaction(hexa).serialize().should.equal(hexa);
Transaction(hexa).serialize(true).should.equal(hexa);
index++;
}
});

View File

@ -242,6 +242,16 @@ describe('Transaction', function() {
return transaction.serialize();
}).to.throw(errors.Transaction.FeeError);
});
it('fails if a dust transaction is created', function() {
var transaction = new Transaction()
.from(simpleUtxoWith1BTC)
.to(toAddress, 1)
.change(changeAddress)
.sign(privateKey);
expect(function() {
return transaction.serialize();
}).to.throw(errors.Transaction.DustOutputs);
});
});
describe('to and from JSON', function() {