From 68239e9472e1325126af0fb2193bc34b0dacb3b5 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 31 Aug 2020 15:55:36 +0200 Subject: [PATCH] improve device detection and display --- .../sparrow/control/DeviceAddressDialog.java | 4 +-- .../sparrow/control/DeviceDialog.java | 20 ++++++++----- .../sparrow/control/DevicePane.java | 6 ++-- .../sparrow/control/DeviceSignDialog.java | 4 +-- .../transaction/HeadersController.java | 4 +-- .../sparrow/transaction/OutputController.java | 30 +++++++++++-------- .../sparrow/wallet/ReceiveController.java | 6 ++-- 7 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/DeviceAddressDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/DeviceAddressDialog.java index de524a7b..81c57615 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/DeviceAddressDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/DeviceAddressDialog.java @@ -13,8 +13,8 @@ public class DeviceAddressDialog extends DeviceDialog { private final Wallet wallet; private final KeyDerivation keyDerivation; - public DeviceAddressDialog(List devices, Wallet wallet, KeyDerivation keyDerivation) { - super(devices); + public DeviceAddressDialog(List operationFingerprints, Wallet wallet, KeyDerivation keyDerivation) { + super(operationFingerprints); this.wallet = wallet; this.keyDerivation = keyDerivation; diff --git a/src/main/java/com/sparrowwallet/sparrow/control/DeviceDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/DeviceDialog.java index dc93e035..226891b9 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/DeviceDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/DeviceDialog.java @@ -15,10 +15,12 @@ import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import org.controlsfx.glyphfont.Glyph; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; public abstract class DeviceDialog extends Dialog { - private final List operationDevices; + private final List operationFingerprints; private final Accordion deviceAccordion; private final VBox scanBox; private final Label scanLabel; @@ -27,8 +29,8 @@ public abstract class DeviceDialog extends Dialog { this(null); } - public DeviceDialog(List operationDevices) { - this.operationDevices = operationDevices; + public DeviceDialog(List operationFingerprints) { + this.operationFingerprints = operationFingerprints; final DialogPane dialogPane = getDialogPane(); dialogPane.getStylesheets().add(AppController.class.getResource("general.css").toExternalForm()); @@ -104,16 +106,20 @@ public abstract class DeviceDialog extends Dialog { } protected void setDevices(List devices) { - List dialogDevices = devices; - if(operationDevices != null && dialogDevices.containsAll(operationDevices)) { - dialogDevices = operationDevices; + List dialogDevices = new ArrayList<>(devices); + dialogDevices.removeIf(Objects::isNull); + + if(operationFingerprints != null) { + dialogDevices.removeIf(device -> { + return device.getFingerprint() != null && !operationFingerprints.contains(device.getFingerprint()) && !(device.getNeedsPinSent() || device.getNeedsPassphraseSent()); + }); } deviceAccordion.getPanes().clear(); if(dialogDevices.isEmpty()) { scanBox.setVisible(true); - scanLabel.setText("No devices found"); + scanLabel.setText("No matching devices found"); } else { scanBox.setVisible(false); for(Device device : dialogDevices) { diff --git a/src/main/java/com/sparrowwallet/sparrow/control/DevicePane.java b/src/main/java/com/sparrowwallet/sparrow/control/DevicePane.java index 8d37a9e8..ee022b1a 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/DevicePane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/DevicePane.java @@ -15,6 +15,7 @@ import com.sparrowwallet.sparrow.io.Device; import com.sparrowwallet.sparrow.io.Hwi; import com.sparrowwallet.drongo.wallet.WalletModel; import com.sparrowwallet.sparrow.glyphfont.FontAwesome5; +import javafx.application.Platform; import javafx.beans.property.SimpleStringProperty; import javafx.geometry.Insets; import javafx.geometry.Pos; @@ -273,6 +274,8 @@ public class DevicePane extends TitledDescriptionPane { contentBox.getChildren().add(sendPassphraseButton); contentBox.setPadding(new Insets(10, 30, 10, 30)); + Platform.runLater(passphraseField::requestFocus); + return contentBox; } @@ -342,8 +345,7 @@ public class DevicePane extends TitledDescriptionPane { if(device.getFingerprint() != null) { setPassphraseButton.setVisible(false); - device.setNeedsPassphraseSent(false); - setDefaultStatus(); + setDescription("Passphrase sent"); showOperationButton(); } else { setError("Passphrase send failed", null); diff --git a/src/main/java/com/sparrowwallet/sparrow/control/DeviceSignDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/DeviceSignDialog.java index 33d2974f..e430eb17 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/DeviceSignDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/DeviceSignDialog.java @@ -11,8 +11,8 @@ import java.util.List; public class DeviceSignDialog extends DeviceDialog { private final PSBT psbt; - public DeviceSignDialog(List devices, PSBT psbt) { - super(devices); + public DeviceSignDialog(List operationFingerprints, PSBT psbt) { + super(operationFingerprints); this.psbt = psbt; EventManager.get().register(this); setOnCloseRequest(event -> { diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java index 778e3ec6..742199cc 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java @@ -689,9 +689,7 @@ public class HeadersController extends TransactionFormController implements Init return; } - signingDevices.addAll(AppController.getDevices().stream().filter(device -> device.getNeedsPinSent() || device.getNeedsPassphraseSent()).collect(Collectors.toList())); - - DeviceSignDialog dlg = new DeviceSignDialog(signingDevices.isEmpty() ? null : signingDevices, headersForm.getPsbt()); + DeviceSignDialog dlg = new DeviceSignDialog(fingerprints, headersForm.getPsbt()); dlg.initModality(Modality.NONE); Stage stage = (Stage)dlg.getDialogPane().getScene().getWindow(); stage.setAlwaysOnTop(true); diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/OutputController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/OutputController.java index 5046c3da..5c0dda33 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/OutputController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/OutputController.java @@ -7,6 +7,7 @@ import com.sparrowwallet.drongo.protocol.NonStandardScriptException; import com.sparrowwallet.drongo.protocol.TransactionInput; import com.sparrowwallet.drongo.protocol.TransactionOutput; import com.sparrowwallet.drongo.wallet.BlockTransaction; +import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.control.AddressLabel; import com.sparrowwallet.sparrow.control.CoinLabel; @@ -87,19 +88,9 @@ public class OutputController extends TransactionFormController implements Initi walletType.managedProperty().bind(walletType.visibleProperty()); walletType.setVisible(false); outputForm.signingWalletProperty().addListener((observable, oldValue, signingWallet) -> { - if(signingWallet != null) { - walletType.setVisible(true); - if(signingWallet.getWalletOutputScripts(KeyPurpose.RECEIVE).containsKey(txOutput.getScript())) { - walletType.setText("(Consolidation)"); - } else if(signingWallet.getWalletOutputScripts(KeyPurpose.CHANGE).containsKey(txOutput.getScript())) { - walletType.setText("(Change)"); - } else { - walletType.setText("(Payment)"); - } - } else { - walletType.setVisible(false); - } + updateWalletType(txOutput, signingWallet); }); + updateWalletType(txOutput, outputForm.getSigningWallet()); spentField.managedProperty().bind(spentField.visibleProperty()); spentByField.managedProperty().bind(spentByField.visibleProperty()); @@ -118,6 +109,21 @@ public class OutputController extends TransactionFormController implements Initi scriptPubKeyArea.appendScript(txOutput.getScript(), null, null); } + private void updateWalletType(TransactionOutput txOutput, Wallet signingWallet) { + if(signingWallet != null) { + walletType.setVisible(true); + if(signingWallet.getWalletOutputScripts(KeyPurpose.RECEIVE).containsKey(txOutput.getScript())) { + walletType.setText("(Consolidation)"); + } else if(signingWallet.getWalletOutputScripts(KeyPurpose.CHANGE).containsKey(txOutput.getScript())) { + walletType.setText("(Change)"); + } else { + walletType.setText("(Payment)"); + } + } else { + walletType.setVisible(false); + } + } + private void updateSpent(List outputTransactions) { int outputIndex = outputForm.getIndex(); if(outputIndex < outputForm.getMaxOutputFetched()) { diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java index 5b87f38d..b6e53707 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java @@ -211,21 +211,21 @@ public class ReceiveController extends WalletFormController implements Initializ List possibleDevices = (List)displayAddress.getUserData(); if(possibleDevices != null && !possibleDevices.isEmpty()) { if(possibleDevices.size() > 1 || possibleDevices.get(0).getNeedsPinSent() || possibleDevices.get(0).getNeedsPassphraseSent()) { - DeviceAddressDialog dlg = new DeviceAddressDialog(possibleDevices.size() == 1 ? List.of(possibleDevices.get(0)) : null, wallet, fullDerivation); + DeviceAddressDialog dlg = new DeviceAddressDialog(List.of(keystore.getKeyDerivation().getMasterFingerprint()), wallet, fullDerivation); dlg.showAndWait(); } else { Device actualDevice = possibleDevices.get(0); Hwi.DisplayAddressService displayAddressService = new Hwi.DisplayAddressService(actualDevice, "", wallet.getScriptType(), fullDerivation.getDerivationPath()); displayAddressService.setOnFailed(failedEvent -> { Platform.runLater(() -> { - DeviceAddressDialog dlg = new DeviceAddressDialog(null, wallet, fullDerivation); + DeviceAddressDialog dlg = new DeviceAddressDialog(List.of(keystore.getKeyDerivation().getMasterFingerprint()), wallet, fullDerivation); dlg.showAndWait(); }); }); displayAddressService.start(); } } else { - DeviceAddressDialog dlg = new DeviceAddressDialog(null, wallet, fullDerivation); + DeviceAddressDialog dlg = new DeviceAddressDialog(List.of(keystore.getKeyDerivation().getMasterFingerprint()), wallet, fullDerivation); dlg.showAndWait(); } }