From 7a554df619bbc96c14f62babf1f004c73117b6bc Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 29 Apr 2020 16:51:33 +0200 Subject: [PATCH] software wallet import and airgapped fixes --- drongo | 2 +- .../sparrow/control/DevicePane.java | 8 +++++- .../control/KeystoreFileImportPane.java | 26 +++++++++++++++--- .../control/KeystoreImportAccordion.java | 2 +- .../sparrow/external/Electrum.java | 10 +++---- ...roller.java => HwAirgappedController.java} | 2 +- ...oller.java => HwUsbDevicesController.java} | 2 +- ...ntroller.java => HwUsbScanController.java} | 2 +- .../KeystoreImportController.java | 10 +++++-- .../sparrow/keystoreimport/SwController.java | 19 +++++++++++++ .../sparrow/keystoreimport/hw_airgapped.fxml | 2 +- .../keystoreimport/hw_usb-devices.fxml | 2 +- .../sparrow/keystoreimport/hw_usb-error.fxml | 2 +- .../sparrow/keystoreimport/hw_usb.fxml | 2 +- .../sparrow/keystoreimport/sw.fxml | 14 ++++++++++ src/main/resources/image/electrum.png | Bin 0 -> 16367 bytes 16 files changed, 83 insertions(+), 22 deletions(-) rename src/main/java/com/sparrowwallet/sparrow/keystoreimport/{UsbAirgappedController.java => HwAirgappedController.java} (93%) rename src/main/java/com/sparrowwallet/sparrow/keystoreimport/{UsbDevicesController.java => HwUsbDevicesController.java} (87%) rename src/main/java/com/sparrowwallet/sparrow/keystoreimport/{UsbScanController.java => HwUsbScanController.java} (93%) create mode 100644 src/main/java/com/sparrowwallet/sparrow/keystoreimport/SwController.java create mode 100644 src/main/resources/com/sparrowwallet/sparrow/keystoreimport/sw.fxml create mode 100644 src/main/resources/image/electrum.png diff --git a/drongo b/drongo index cbffaf3e..019a3cf3 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit cbffaf3e416e0d94a01a9c00c99d9f2d61018d1e +Subproject commit 019a3cf34f60da053ca7b5540e2bb7bcee6fbd50 diff --git a/src/main/java/com/sparrowwallet/sparrow/control/DevicePane.java b/src/main/java/com/sparrowwallet/sparrow/control/DevicePane.java index be4edfdf..e6b6c63e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/DevicePane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/DevicePane.java @@ -57,11 +57,17 @@ public class DevicePane extends TitledPane { getStyleClass().add("devicepane"); setDefaultStatus(); + removeArrow(); + } + + private void removeArrow() { Platform.runLater(() -> { Node arrow = this.lookup(".arrow"); - if(arrow != null) { + if (arrow != null) { arrow.setVisible(false); arrow.setManaged(false); + } else { + removeArrow(); } }); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/KeystoreFileImportPane.java b/src/main/java/com/sparrowwallet/sparrow/control/KeystoreFileImportPane.java index 1c2a87d3..72ba2a76 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/KeystoreFileImportPane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/KeystoreFileImportPane.java @@ -1,5 +1,6 @@ package com.sparrowwallet.sparrow.control; +import com.google.gson.JsonParseException; import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.EventManager; @@ -42,11 +43,17 @@ public class KeystoreFileImportPane extends TitledPane { getStyleClass().add("importpane"); setContent(getContentBox(importer.getKeystoreImportDescription())); + removeArrow(); + } + + private void removeArrow() { Platform.runLater(() -> { Node arrow = this.lookup(".arrow"); - if(arrow != null) { + if (arrow != null) { arrow.setVisible(false); arrow.setManaged(false); + } else { + removeArrow(); } }); } @@ -133,7 +140,14 @@ public class KeystoreFileImportPane extends TitledPane { descriptionLabel.getStyleClass().remove("description-label"); descriptionLabel.getStyleClass().add("description-error"); descriptionLabel.setText("Error Importing [View Details...]"); - setContent(getContentBox(e.getMessage())); + String errorMessage = e.getMessage(); + if(e.getCause() != null) { + errorMessage = e.getCause().getMessage(); + } + if(e instanceof JsonParseException || e.getCause() instanceof JsonParseException) { + errorMessage = "File was not in JSON format"; + } + setContent(getContentBox(errorMessage)); } } } @@ -143,10 +157,14 @@ public class KeystoreFileImportPane extends TitledPane { details.setWrapText(true); HBox contentBox = new HBox(); - contentBox.setAlignment(Pos.TOP_RIGHT); + contentBox.setAlignment(Pos.TOP_LEFT); contentBox.getChildren().add(details); contentBox.setPadding(new Insets(10, 30, 10, 30)); - contentBox.setPrefHeight(60); + + double width = TextUtils.computeTextWidth(details.getFont(), message, 0.0D); + double numLines = Math.max(1, width / 400); + double height = Math.max(60, numLines * 40); + contentBox.setPrefHeight(height); return contentBox; } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/KeystoreImportAccordion.java b/src/main/java/com/sparrowwallet/sparrow/control/KeystoreImportAccordion.java index 60dd2f57..3ab6686f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/KeystoreImportAccordion.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/KeystoreImportAccordion.java @@ -21,7 +21,7 @@ public class KeystoreImportAccordion extends Accordion { if(importer instanceof KeystoreFileImport) { importPane = new KeystoreFileImportPane(this, wallet, (KeystoreFileImport)importer); } else if(importer instanceof KeystoreMnemonicImport) { - //TODO: + //TODO: Import from the new Bip39KeystoreImport } else { throw new IllegalArgumentException("Could not create ImportPane for importer of type " + importer.getClass()); } diff --git a/src/main/java/com/sparrowwallet/sparrow/external/Electrum.java b/src/main/java/com/sparrowwallet/sparrow/external/Electrum.java index d4a3f16c..34841838 100644 --- a/src/main/java/com/sparrowwallet/sparrow/external/Electrum.java +++ b/src/main/java/com/sparrowwallet/sparrow/external/Electrum.java @@ -37,7 +37,7 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult @Override public String getKeystoreImportDescription() { - return "Import from an Electrum wallet"; + return "Import a single keystore from an Electrum wallet (use File > Import > Electrum to import a multisig wallet)"; } @Override @@ -45,12 +45,12 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult Wallet wallet = importWallet(inputStream); if(!wallet.getPolicyType().equals(PolicyType.SINGLE) || wallet.getKeystores().size() != 1) { - throw new ImportException("Multisig wallet detected - import it through the File > Import menu"); + throw new ImportException("Multisig wallet detected - import it using File > Import > Electrum"); } if(!wallet.getScriptType().equals(scriptType)) { - //TODO: Derive appropriate ScriptType keystore - throw new ImportException("Wallet has an incompatible script type of " + wallet.getScriptType()); + //TODO: Derive appropriate ScriptType keystore from xprv if present + throw new ImportException("Wallet has an incompatible script type of " + wallet.getScriptType() + ", and the correct script type cannot be derived without the master private key"); } return wallet.getKeystores().get(0); @@ -88,7 +88,7 @@ public class Electrum implements KeystoreFileImport, SinglesigWalletImport, Mult keystore.setSource(KeystoreSource.HW_USB); keystore.setWalletModel(WalletModel.fromType(ek.hw_type)); if(keystore.getWalletModel() == null) { - throw new ImportException("Unknown hardware wallet type " + ek.hw_type); + throw new ImportException("Wallet has keystore of unknown hardware wallet type \"" + ek.hw_type + "\""); } } else if("bip32".equals(ek.type)) { if(ek.xprv != null) { diff --git a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbAirgappedController.java b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwAirgappedController.java similarity index 93% rename from src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbAirgappedController.java rename to src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwAirgappedController.java index 1a2ed110..4a981d06 100644 --- a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbAirgappedController.java +++ b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwAirgappedController.java @@ -11,7 +11,7 @@ import javafx.fxml.FXML; import java.util.Collections; import java.util.List; -public class UsbAirgappedController extends KeystoreImportDetailController { +public class HwAirgappedController extends KeystoreImportDetailController { @FXML private KeystoreImportAccordion importAccordion; diff --git a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbDevicesController.java b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwUsbDevicesController.java similarity index 87% rename from src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbDevicesController.java rename to src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwUsbDevicesController.java index e341ed60..f7405fb9 100644 --- a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbDevicesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwUsbDevicesController.java @@ -7,7 +7,7 @@ import javafx.fxml.FXML; import java.util.List; -public class UsbDevicesController extends KeystoreImportDetailController { +public class HwUsbDevicesController extends KeystoreImportDetailController { @FXML private DeviceAccordion deviceAccordion; diff --git a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbScanController.java b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwUsbScanController.java similarity index 93% rename from src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbScanController.java rename to src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwUsbScanController.java index afe2e06f..0ad99d4c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/UsbScanController.java +++ b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/HwUsbScanController.java @@ -9,7 +9,7 @@ import javafx.scene.control.Label; import java.util.List; -public class UsbScanController extends KeystoreImportDetailController { +public class HwUsbScanController extends KeystoreImportDetailController { @FXML private Label message; diff --git a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/KeystoreImportController.java b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/KeystoreImportController.java index 4914a0f5..3cd171f3 100644 --- a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/KeystoreImportController.java +++ b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/KeystoreImportController.java @@ -39,19 +39,23 @@ public class KeystoreImportController implements Initializable { importMenu.selectedToggleProperty().addListener((observable, oldValue, selectedToggle) -> { KeystoreSource importType = (KeystoreSource) selectedToggle.getUserData(); System.out.println(importType); - setImportPane(importType.toString().toLowerCase()); + String fxmlName = importType.toString().toLowerCase(); + if(importType == KeystoreSource.SW_SEED || importType == KeystoreSource.SW_WATCH) { + fxmlName = "sw"; + } + setImportPane(fxmlName); }); } void showUsbDevices(List devices) { FXMLLoader loader = setImportPane("hw_usb-devices"); - UsbDevicesController controller = loader.getController(); + HwUsbDevicesController controller = loader.getController(); controller.initializeView(devices); } void showUsbError(String message) { FXMLLoader loader = setImportPane("hw_usb-error"); - UsbScanController controller = loader.getController(); + HwUsbScanController controller = loader.getController(); controller.initializeView(message); } diff --git a/src/main/java/com/sparrowwallet/sparrow/keystoreimport/SwController.java b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/SwController.java new file mode 100644 index 00000000..27ec6c39 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/keystoreimport/SwController.java @@ -0,0 +1,19 @@ +package com.sparrowwallet.sparrow.keystoreimport; + +import com.sparrowwallet.sparrow.control.KeystoreImportAccordion; +import com.sparrowwallet.sparrow.external.Electrum; +import com.sparrowwallet.sparrow.external.KeystoreImport; +import javafx.collections.FXCollections; +import javafx.fxml.FXML; + +import java.util.List; + +public class SwController extends KeystoreImportDetailController { + @FXML + private KeystoreImportAccordion importAccordion; + + public void initializeView() { + List importers = List.of(new Electrum()); + importAccordion.setKeystoreImporters(getMasterController().getWallet(), FXCollections.observableList(importers)); + } +} diff --git a/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_airgapped.fxml b/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_airgapped.fxml index 77e73fb2..e5c85782 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_airgapped.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_airgapped.fxml @@ -8,7 +8,7 @@ - + diff --git a/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_usb-devices.fxml b/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_usb-devices.fxml index b1479641..9de99e2e 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_usb-devices.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_usb-devices.fxml @@ -7,7 +7,7 @@ - + diff --git a/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_usb-error.fxml b/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_usb-error.fxml index 58ce3392..e163ce13 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_usb-error.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/keystoreimport/hw_usb-error.fxml @@ -7,7 +7,7 @@ - +