switch the send amount and fee fields to btc when a btc amount below 1 is pasted into them in sats, and restore the default unit when the form is cleared
What changed, and why it matters
This commit changes how Sparrow Wallet handles pasted amounts in the send and fee fields. If a user has the wallet set to display amounts in satoshis (sats) and pastes a small Bitcoin amount written as BTC (for example, '0.001'), the wallet now automatically switches the field to BTC mode so the value is interpreted correctly. It also restores the default unit when the form is cleared. The change is a usability improvement to prevent users from accidentally sending the wrong amount because of unit confusion. There is no direct evidence in the commit of a security vulnerability or malicious behavior.
Treat as a routine UX fix. No security patch or incident response is indicated by the commit content. If reviewing for safety, verify that the pasted-amount detection regex correctly handles locale-specific decimal separators and that the deferred unit switch cannot race with user input.
Security signals we found
Unit-conversion logic change in financial input fields
Automatic unit switching on paste could surprise users or be misinterpreted
No input validation, signing, or cryptographic changes observed
Evidence from the diff
The patch adds a callback to CoinTextFormatter that detects when a fractional amount (matching a pattern like ‘0.xxx’) is pasted into a field currently configured for SATOSHIS. It then defers a UI update via Platform.runLater to switch the unit selector to BTC and inserts the pasted text unchanged. The same logic is applied to the send amount field (PaymentController) and the fee field (SendController). On clear(), both controllers now reset the unit selector back to the configured default. The change is defensive against user error but does not alter transaction signing, validation, or cryptographic logic.
Changed components
CoinTextFormatterPaymentController send amount fieldSendController fee fieldInspect captured patch +50 / −8
### src/main/java/com/sparrowwallet/sparrow/control/CoinTextFormatter.java
@@ -2,18 +2,27 @@
import com.sparrowwallet.drongo.BitcoinUnit;
import com.sparrowwallet.sparrow.UnitFormat;
+import javafx.application.Platform;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextInputControl;
import java.text.DecimalFormat;
import java.text.ParseException;
+import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CoinTextFormatter extends TextFormatter<String> {
public CoinTextFormatter(UnitFormat unitFormat, BitcoinUnit bitcoinUnit) {
- super(new CoinFilter(unitFormat == null ? UnitFormat.DOT : unitFormat, bitcoinUnit));
+ this(unitFormat, bitcoinUnit, null);
+ }
+
+ /**
+ * @param btcAmountPasted called with a BTC amount below 1 pasted over the entire text of a sats field, which is left unchanged so the handler can switch the unit before entering it
+ */
+ public CoinTextFormatter(UnitFormat unitFormat, BitcoinUnit bitcoinUnit, Consumer<String> btcAmountPasted) {
+ super(new CoinFilter(unitFormat == null ? UnitFormat.DOT : unitFormat, bitcoinUnit, btcAmountPasted));
}
public UnitFormat getUnitFormat() {
@@ -29,15 +38,19 @@ private static class CoinFilter implements UnaryOperator<Change> {
private final DecimalFormat coinFormat;
private final Pattern coinValidation;
private final Pattern anyPrecisionAmount;
+ private final Pattern subUnitBtcAmount;
+ private final Consumer<String> btcAmountPasted;
- public CoinFilter(UnitFormat unitFormat, BitcoinUnit bitcoinUnit) {
+ public CoinFilter(UnitFormat unitFormat, BitcoinUnit bitcoinUnit, Consumer<String> btcAmountPasted) {
this.unitFormat = unitFormat;
+ this.btcAmountPasted = bitcoinUnit == BitcoinUnit.SATOSHIS ? btcAmountPasted : null;
this.coinFormat = new DecimalFormat("###,###.########", unitFormat.getDecimalFormatSymbols());
String integer = "[\\d" + Pattern.quote(unitFormat.getGroupingSeparator()) + "]*";
//A satoshi is indivisible, so a sats amount has no fractional part to validate
String fraction = bitcoinUnit == BitcoinUnit.SATOSHIS ? "" : "(" + Pattern.quote(unitFormat.getDecimalSeparator()) + "\\d{0,8})?";
this.coinValidation = Pattern.compile(integer + fraction);
this.anyPrecisionAmount = Pattern.compile(integer + "(" + Pattern.quote(unitFormat.getDecimalSeparator()) + "\\d*)?");
+ this.subUnitBtcAmount = Pattern.compile("0*" + Pattern.quote(unitFormat.getDecimalSeparator()) + "\\d+");
}
@Override
@@ -47,6 +60,17 @@ public Change apply(Change change) {
change.setText(text);
}
+ //A sats amount has no fraction, so a pasted amount below 1 with one is a BTC amount, such as one quoted by a merchant. Above 1 the separator
+ //may group the digits of a sats amount instead
+ if(btcAmountPasted != null && text.length() > 1 && change.getRangeStart() == 0 && change.getRangeEnd() == change.getControlText().length()) {
+ Matcher btcAmount = subUnitBtcAmount.matcher(text.strip());
+ if(btcAmount.lookingAt()) {
+ String pastedAmount = btcAmount.group();
+ Platform.runLater(() -> btcAmountPasted.accept(pastedAmount));
+ return null;
+ }
+ }
+
String oldText = change.getControlText();
String newText = change.getControlNewText();
String deleted = null;
### src/main/java/com/sparrowwallet/sparrow/wallet/PaymentController.java
@@ -374,12 +374,12 @@ protected void updateItem(Wallet wallet, boolean empty) {
});
amountUnit.getSelectionModel().select(BitcoinUnit.BTC.equals(sendController.getBitcoinUnit(Config.get().getBitcoinUnit())) ? 0 : 1);
- amount.setTextFormatter(new CoinTextFormatter(Config.get().getUnitFormat(), amountUnit.getValue()));
+ amount.setTextFormatter(createAmountFormatter(Config.get().getUnitFormat(), amountUnit.getValue()));
amount.textProperty().addListener(amountListener);
amountUnit.valueProperty().addListener((observable, oldValue, newValue) -> {
Long value = getRecipientValueSats(oldValue);
- amount.setTextFormatter(new CoinTextFormatter(Config.get().getUnitFormat(), newValue));
+ amount.setTextFormatter(createAmountFormatter(Config.get().getUnitFormat(), newValue));
if(value != null) {
UnitFormat unitFormat = Config.get().getUnitFormat() == null ? UnitFormat.DOT : Config.get().getUnitFormat();
DecimalFormat df = new DecimalFormat("#.#", unitFormat.getDecimalFormatSymbols());
@@ -620,6 +620,14 @@ boolean isSentToSamePayNym(PaymentController paymentController) {
return (this != paymentController && payNymProperty.get() != null && payNymProperty.get().paymentCode().equals(paymentController.payNymProperty.get().paymentCode()));
}
+ private CoinTextFormatter createAmountFormatter(UnitFormat unitFormat, BitcoinUnit bitcoinUnit) {
+ return new CoinTextFormatter(unitFormat, bitcoinUnit, btcAmount -> {
+ amountUnit.getSelectionModel().select(BitcoinUnit.BTC);
+ amount.setText(btcAmount);
+ amount.end();
+ });
+ }
+
private Long getRecipientValueSats() {
return getRecipientValueSats(amountUnit.getSelectionModel().getSelectedItem());
}
@@ -764,6 +772,7 @@ public void clear() {
amount.textProperty().removeListener(amountListener);
amount.setText("");
amount.textProperty().addListener(amountListener);
+ amountUnit.getSelectionModel().select(sendController.getBitcoinUnit(Config.get().getBitcoinUnit()));
fiatAmount.setText("");
setSendMax(false);
@@ -952,7 +961,7 @@ public void bitcoinUnitChanged(BitcoinUnitChangedEvent event) {
public void unitFormatChanged(UnitFormatChangedEvent event) {
if(amount.getTextFormatter() instanceof CoinTextFormatter coinTextFormatter && coinTextFormatter.getUnitFormat() != event.getUnitFormat()) {
Long value = getRecipientValueSats(coinTextFormatter.getUnitFormat(), amountUnit.getSelectionModel().getSelectedItem());
- amount.setTextFormatter(new CoinTextFormatter(event.getUnitFormat(), amountUnit.getValue()));
+ amount.setTextFormatter(createAmountFormatter(event.getUnitFormat(), amountUnit.getValue()));
if(value != null) {
setRecipientValueSats(value);
### src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java
@@ -367,12 +367,12 @@ public Double fromString(String string) {
BitcoinUnit unit = getBitcoinUnit(Config.get().getBitcoinUnit());
feeAmountUnit.getSelectionModel().select(BitcoinUnit.BTC.equals(unit) ? 0 : 1);
- fee.setTextFormatter(new CoinTextFormatter(Config.get().getUnitFormat(), feeAmountUnit.getValue()));
+ fee.setTextFormatter(createFeeFormatter(Config.get().getUnitFormat(), feeAmountUnit.getValue()));
fee.textProperty().addListener(feeListener);
feeAmountUnit.valueProperty().addListener((observable, oldValue, newValue) -> {
Long value = getFeeValueSats(oldValue);
- fee.setTextFormatter(new CoinTextFormatter(Config.get().getUnitFormat(), newValue));
+ fee.setTextFormatter(createFeeFormatter(Config.get().getUnitFormat(), newValue));
if(value != null) {
setFeeValueSats(value);
}
@@ -786,6 +786,14 @@ private void setDefaultFeeRate() {
}
}
+ private CoinTextFormatter createFeeFormatter(UnitFormat unitFormat, BitcoinUnit bitcoinUnit) {
+ return new CoinTextFormatter(unitFormat, bitcoinUnit, btcAmount -> {
+ feeAmountUnit.getSelectionModel().select(BitcoinUnit.BTC);
+ fee.setText(btcAmount);
+ fee.end();
+ });
+ }
+
private Long getFeeValueSats() {
return getFeeValueSats(feeAmountUnit.getSelectionModel().getSelectedItem());
}
@@ -1112,6 +1120,7 @@ public void clear(ActionEvent event) {
fee.textProperty().removeListener(feeListener);
fee.setText("");
fee.textProperty().addListener(feeListener);
+ feeAmountUnit.getSelectionModel().select(getBitcoinUnit(Config.get().getBitcoinUnit()));
cpfpFeeRate.setVisible(false);
fiatFeeAmount.setText("");
@@ -1581,7 +1590,7 @@ public void unitFormatChanged(UnitFormatChangedEvent event) {
feeRange.refreshTickLabels();
if(fee.getTextFormatter() instanceof CoinTextFormatter coinTextFormatter && coinTextFormatter.getUnitFormat() != event.getUnitFormat()) {
Long value = getFeeValueSats(coinTextFormatter.getUnitFormat(), feeAmountUnit.getSelectionModel().getSelectedItem());
- fee.setTextFormatter(new CoinTextFormatter(event.getUnitFormat(), feeAmountUnit.getValue()));
+ fee.setTextFormatter(createFeeFormatter(event.getUnitFormat(), feeAmountUnit.getValue()));
if(value != null) {
setFeeValueSats(value);Why this scored 21/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.