From 37bb1c5d97a49bd2e001ef317dca00479205991a Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 11 Nov 2020 12:26:36 +0200 Subject: [PATCH] import descriptor from qr --- .../sparrow/control/QRScanDialog.java | 2 +- .../sparrow/wallet/SettingsController.java | 52 ++++++++++++------- .../sparrow/wallet/settings.fxml | 9 ++++ 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java index 60562c88..aa7a3928 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/QRScanDialog.java @@ -289,7 +289,7 @@ public class QRScanDialog extends Dialog { private Address getAddress(CryptoAddress cryptoAddress) { Address address = null; - if(cryptoAddress.getType() == CryptoAddress.Type.P2PKH) { + if(cryptoAddress.getType() == null || cryptoAddress.getType() == CryptoAddress.Type.P2PKH) { address = new P2PKHAddress(cryptoAddress.getData()); } else if(cryptoAddress.getType() == CryptoAddress.Type.P2SH) { address = new P2SHAddress(cryptoAddress.getData()); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index d72717ea..bf44be1c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -13,10 +13,7 @@ import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletModel; import com.sparrowwallet.sparrow.AppController; import com.sparrowwallet.sparrow.EventManager; -import com.sparrowwallet.sparrow.control.CopyableLabel; -import com.sparrowwallet.sparrow.control.DescriptorArea; -import com.sparrowwallet.sparrow.control.TextAreaDialog; -import com.sparrowwallet.sparrow.control.WalletPasswordDialog; +import com.sparrowwallet.sparrow.control.*; import com.sparrowwallet.sparrow.event.RequestOpenWalletsEvent; import com.sparrowwallet.sparrow.event.SettingsChangedEvent; import com.sparrowwallet.sparrow.event.StorageEvent; @@ -251,6 +248,21 @@ public class SettingsController extends WalletFormController implements Initiali } } + public void scanDescriptorQR(ActionEvent event) { + QRScanDialog qrScanDialog = new QRScanDialog(); + Optional optionalResult = qrScanDialog.showAndWait(); + if(optionalResult.isPresent()) { + QRScanDialog.Result result = optionalResult.get(); + if(result.outputDescriptor != null) { + setDescriptorText(result.outputDescriptor.toString()); + } else if(result.payload != null && !result.payload.isEmpty()) { + setDescriptorText(result.payload); + } else if(result.exception != null) { + AppController.showErrorDialog("Error scanning QR", result.exception.getMessage()); + } + } + } + public void editDescriptor(ActionEvent event) { OutputDescriptor outputDescriptor = OutputDescriptor.getOutputDescriptor(walletForm.getWallet()); String outputDescriptorString = outputDescriptor.toString(walletForm.getWallet().isValid()); @@ -260,22 +272,26 @@ public class SettingsController extends WalletFormController implements Initiali dialog.getDialogPane().setHeaderText("Wallet output descriptor:"); Optional text = dialog.showAndWait(); if(text.isPresent() && !text.get().isEmpty() && !text.get().equals(outputDescriptorString)) { - try { - OutputDescriptor editedOutputDescriptor = OutputDescriptor.getOutputDescriptor(text.get().trim().replace("\\", "")); - Wallet editedWallet = editedOutputDescriptor.toWallet(); + setDescriptorText(text.get()); + } + } - editedWallet.setName(getWalletForm().getWallet().getName()); - keystoreTabs.getTabs().removeAll(keystoreTabs.getTabs()); - totalKeystores.unbind(); - totalKeystores.setValue(0); - walletForm.setWallet(editedWallet); - initialising = true; - setFieldsFromWallet(editedWallet); + private void setDescriptorText(String text) { + try { + OutputDescriptor editedOutputDescriptor = OutputDescriptor.getOutputDescriptor(text.trim().replace("\\", "")); + Wallet editedWallet = editedOutputDescriptor.toWallet(); - EventManager.get().post(new SettingsChangedEvent(editedWallet, SettingsChangedEvent.Type.POLICY)); - } catch(Exception e) { - AppController.showErrorDialog("Invalid output descriptor", e.getMessage()); - } + editedWallet.setName(getWalletForm().getWallet().getName()); + keystoreTabs.getTabs().removeAll(keystoreTabs.getTabs()); + totalKeystores.unbind(); + totalKeystores.setValue(0); + walletForm.setWallet(editedWallet); + initialising = true; + setFieldsFromWallet(editedWallet); + + EventManager.get().post(new SettingsChangedEvent(editedWallet, SettingsChangedEvent.Type.POLICY)); + } catch(Exception e) { + AppController.showErrorDialog("Invalid output descriptor", e.getMessage()); } } diff --git a/src/main/resources/com/sparrowwallet/sparrow/wallet/settings.fxml b/src/main/resources/com/sparrowwallet/sparrow/wallet/settings.fxml index 162c1d0f..a416df3a 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/wallet/settings.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/wallet/settings.fxml @@ -11,6 +11,7 @@ +
@@ -79,6 +80,14 @@ +