Throw error if dust amount is detected

This commit is contained in:
Esteban Ordano 2015-01-08 16:38:43 -03:00
parent 5f59fd0d05
commit 0f73c3f7a3
5 changed files with 31 additions and 3 deletions

View File

@ -28,7 +28,7 @@
"maxcomplexity": 6, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity) "maxcomplexity": 6, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity)
"maxdepth": 4, // Maximum depth of nested control structures "maxdepth": 4, // Maximum depth of nested control structures
"maxlen": 120, // Maximum number of cols in a line "maxlen": 120, // Maximum number of cols in a line
"multistr": true // Allow use of multiline EOL escaping "multistr": true, // Allow use of multiline EOL escaping
"predef": [ // Extra globals. "predef": [ // Extra globals.
"after", "after",

View File

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

View File

@ -126,6 +126,9 @@ Transaction.prototype.checkedSerialize = Transaction.prototype.toString = functi
throw new errors.Transaction.FeeError(feeError); throw new errors.Transaction.FeeError(feeError);
} }
} }
if (this._hasDustOutputs()) {
throw new errors.Transaction.DustOutputs();
}
return this.uncheckedSerialize(); return this.uncheckedSerialize();
}; };
@ -143,6 +146,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() { Transaction.prototype.inspect = function() {
return '<Transaction: ' + this.toString() + '>'; return '<Transaction: ' + this.toString() + '>';
}; };

View File

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

View File

@ -242,6 +242,16 @@ describe('Transaction', function() {
return transaction.serialize(); return transaction.serialize();
}).to.throw(errors.Transaction.FeeError); }).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);
});
}); });
}); });