Skip to content

Commit

Permalink
Throw error if dust amount is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
eordano committed Jan 8, 2015
1 parent 5f59fd0 commit 0f73c3f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Expand Up @@ -28,7 +28,7 @@
"maxcomplexity": 6, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity)
"maxdepth": 4, // Maximum depth of nested control structures
"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.
"after",
Expand Down
3 changes: 3 additions & 0 deletions lib/errors/spec.js
Expand Up @@ -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}'),
Expand Down
15 changes: 15 additions & 0 deletions lib/transaction/transaction.js
Expand Up @@ -126,6 +126,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();
};

Expand All @@ -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() {
return '<Transaction: ' + this.toString() + '>';
};
Expand Down
4 changes: 2 additions & 2 deletions test/transaction/deserialize.js
Expand Up @@ -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++;
}
});
Expand All @@ -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++;
}
});
Expand Down
10 changes: 10 additions & 0 deletions test/transaction/transaction.js
Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit 0f73c3f

Please sign in to comment.