From 0ffb6e377bec82f7e385644ebe6137d07e4dec0a Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sat, 11 Jul 2020 11:40:28 +0200 Subject: [PATCH] dont send recipient amounts less than dust threshold --- drongo | 2 +- .../sparrow/wallet/SendController.java | 43 +++++++++++++------ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/drongo b/drongo index 49a4b548..b8688783 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 49a4b548b4b93aa9b765b0b6f9520d0314b1395f +Subproject commit b86887838f7145d532a4572b6b3a81700e74acfb diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java index 5d82af19..3a3f1f1f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java @@ -4,6 +4,9 @@ import com.google.common.eventbus.Subscribe; import com.sparrowwallet.drongo.BitcoinUnit; import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.address.InvalidAddressException; +import com.sparrowwallet.drongo.address.P2PKHAddress; +import com.sparrowwallet.drongo.protocol.Transaction; +import com.sparrowwallet.drongo.protocol.TransactionOutput; import com.sparrowwallet.drongo.wallet.*; import com.sparrowwallet.sparrow.AppController; import com.sparrowwallet.sparrow.CurrencyRate; @@ -150,6 +153,7 @@ public class SendController extends WalletFormController implements Initializabl addValidation(); address.textProperty().addListener((observable, oldValue, newValue) -> { + revalidate(amount, amountListener); maxButton.setDisable(!isValidRecipientAddress()); updateTransaction(); }); @@ -175,16 +179,8 @@ public class SendController extends WalletFormController implements Initializabl maxButton.setDisable(!isValidRecipientAddress()); insufficientInputsProperty.addListener((observable, oldValue, newValue) -> { - amount.textProperty().removeListener(amountListener); - String amt = amount.getText(); - amount.setText(amt + "0"); - amount.setText(amt); - amount.textProperty().addListener(amountListener); - fee.textProperty().removeListener(feeListener); - String feeAmt = fee.getText(); - fee.setText(feeAmt + "0"); - fee.setText(feeAmt); - fee.textProperty().addListener(feeListener); + revalidate(amount, amountListener); + revalidate(fee, feeListener); }); targetBlocks.setMin(0); @@ -282,7 +278,7 @@ public class SendController extends WalletFormController implements Initializabl )); validationSupport.registerValidator(amount, Validator.combine( (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Insufficient Inputs", insufficientInputsProperty.get()), - (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Insufficient Value", getRecipientValueSats() != null && getRecipientValueSats() == 0) + (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Insufficient Value", getRecipientValueSats() != null && getRecipientValueSats() <= getMinimumRecipientAmount()) )); validationSupport.registerValidator(fee, Validator.combine( (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Insufficient Inputs", userFeeSet.get() && insufficientInputsProperty.get()), @@ -300,7 +296,7 @@ public class SendController extends WalletFormController implements Initializabl try { Address recipientAddress = getRecipientAddress(); Long recipientAmount = sendAll ? Long.valueOf(1L) : getRecipientValueSats(); - if(recipientAmount != null && recipientAmount != 0 && (!userFeeSet.get() || (getFeeValueSats() != null && getFeeValueSats() > 0))) { + if(recipientAmount != null && recipientAmount > getMinimumRecipientAmount() && (!userFeeSet.get() || (getFeeValueSats() != null && getFeeValueSats() > 0))) { Wallet wallet = getWalletForm().getWallet(); Long userFee = userFeeSet.get() ? getFeeValueSats() : null; WalletTransaction walletTransaction = wallet.createWalletTransaction(getUtxoSelectors(), recipientAddress, recipientAmount, getFeeRate(), userFee, sendAll); @@ -335,11 +331,10 @@ public class SendController extends WalletFormController implements Initializabl private boolean isValidRecipientAddress() { try { getRecipientAddress(); + return true; } catch (InvalidAddressException e) { return false; } - - return true; } private Address getRecipientAddress() throws InvalidAddressException { @@ -458,6 +453,18 @@ public class SendController extends WalletFormController implements Initializabl } } + private long getMinimumRecipientAmount() { + Address address; + try { + address = getRecipientAddress(); + } catch(InvalidAddressException e) { + address = new P2PKHAddress(new byte[20]); + } + + TransactionOutput txOutput = new TransactionOutput(new Transaction(), 1L, address.getOutputScript()); + return address.getScriptType().getDustThreshold(txOutput, Transaction.DUST_RELAY_TX_FEE); + } + public void clear(ActionEvent event) { address.setText(""); label.setText(""); @@ -476,6 +483,14 @@ public class SendController extends WalletFormController implements Initializabl walletTransactionProperty.setValue(null); } + private void revalidate(TextField field, ChangeListener listener) { + field.textProperty().removeListener(listener); + String amt = field.getText(); + field.setText(amt + "0"); + field.setText(amt); + field.textProperty().addListener(listener); + } + public void createTransaction(ActionEvent event) { }