@@ -40,7 +40,7 @@ public class TransactionBuilder {
40
40
41
41
private final long DEFAULT_FEE_PER_KB = 512 ; //amount in satoshis
42
42
43
- static final BigInteger DUST_AMOUNT = BigInteger .valueOf (546 );
43
+ static final BigInteger DUST_AMOUNT = BigInteger .valueOf (256 );
44
44
45
45
/// Margin of error to allow fees in the vecinity of the expected value but doesn't allow a big difference
46
46
private static final BigInteger FEE_SECURITY_MARGIN = BigInteger .valueOf (150 );
@@ -264,6 +264,10 @@ public Transaction build(boolean performChecks) throws TransactionException {
264
264
//add transaction outputs
265
265
tx .addOutputs (outputs );
266
266
267
+ if (changeScriptBuilder != null ) {
268
+ tx .addOutput (getChangeOutput ());
269
+ }
270
+
267
271
tx .setLockTime (nLockTime );
268
272
269
273
return tx ;
@@ -309,6 +313,12 @@ private void checkForDustErrors() throws TransactionException {
309
313
throw new TransactionException ("You have outputs with spending values below the dust limit of " + DUST_AMOUNT .toString ());
310
314
}
311
315
}
316
+
317
+ //check for dust on change output
318
+ if (getChangeOutput () != null && (getChangeOutput ().getAmount ().compareTo (DUST_AMOUNT ) == -1 )){
319
+ throw new TransactionException ("You have a change output with spending value below the dust limit of " + DUST_AMOUNT .toString ());
320
+ }
321
+
312
322
}
313
323
314
324
@@ -358,6 +368,9 @@ private void updateChangeOutput(){
358
368
//spent amount equals input amount. No change generated. Return.
359
369
if (calcRecipientTotals () == calcInputTotals ()) return ;
360
370
371
+ //clear change outputs
372
+ changeOutput = null ;
373
+
361
374
changeAmount = calculateChange ();
362
375
TransactionOutput output = getChangeOutput ();
363
376
output .setAmount (changeAmount );
@@ -399,6 +412,13 @@ private BigInteger estimateFee(){
399
412
400
413
BigInteger fee = BigInteger .valueOf (new Float (size / 1000 * feePerKb ).longValue ());
401
414
415
+ //if fee is less that 256, set fee at 256 satoshis
416
+ //this is current minimum we set automatically if no explicit fee given
417
+ //FIXME: Make this configurable
418
+ if (fee .compareTo (BigInteger .valueOf (256 )) == -1 ){
419
+ fee = BigInteger .valueOf (256 );
420
+ }
421
+
402
422
return fee ;
403
423
}
404
424
@@ -433,6 +453,12 @@ private BigInteger calcRecipientTotals() {
433
453
amount = amount .add (output .getAmount ());
434
454
};
435
455
456
+ //deduct change output
457
+ if (changeScriptBuilder != null ){
458
+ TransactionOutput changeOutput = getChangeOutput ();
459
+ amount = amount .add (changeOutput .getAmount ());
460
+ }
461
+
436
462
return amount ;
437
463
}
438
464
0 commit comments