From 84888b4a43a4ebd1b96fcaa3b6712cf1692ba844 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 12 May 2020 14:17:19 +0200 Subject: [PATCH] refactor to prepare for keystore encryption --- drongo | 2 +- .../com/sparrowwallet/sparrow/AppController.java | 15 +++++++++------ .../sparrow/control/FileKeystoreImportPane.java | 4 ++-- .../sparrow/wallet/SettingsController.java | 10 ++++------ 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/drongo b/drongo index 242c8373..b951f79c 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 242c83735a24456a4bd23fd556a56122c9b259ab +Subproject commit b951f79cfded507112cc7f213567c6a43c00e7e8 diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 2c453a03..397b0187 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -3,9 +3,10 @@ package com.sparrowwallet.sparrow; import com.google.common.base.Charsets; import com.google.common.eventbus.Subscribe; import com.google.common.io.ByteSource; -import com.google.gson.JsonSyntaxException; import com.sparrowwallet.drongo.Utils; +import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter; import com.sparrowwallet.drongo.crypto.ECKey; +import com.sparrowwallet.drongo.crypto.InvalidPasswordException; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.protocol.Transaction; @@ -14,6 +15,7 @@ import com.sparrowwallet.drongo.psbt.PSBTParseException; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.control.TextAreaDialog; import com.sparrowwallet.sparrow.control.WalletNameDialog; +import com.sparrowwallet.sparrow.control.WalletPasswordDialog; import com.sparrowwallet.sparrow.event.TabEvent; import com.sparrowwallet.sparrow.event.TransactionTabChangedEvent; import com.sparrowwallet.sparrow.event.TransactionTabSelectedEvent; @@ -21,7 +23,6 @@ import com.sparrowwallet.sparrow.io.FileType; import com.sparrowwallet.sparrow.io.IOUtils; import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.transaction.TransactionController; -import com.sparrowwallet.sparrow.wallet.SettingsController; import com.sparrowwallet.sparrow.wallet.WalletController; import com.sparrowwallet.sparrow.wallet.WalletForm; import javafx.event.ActionEvent; @@ -233,12 +234,13 @@ public class AppController implements Initializable { if(FileType.JSON.equals(fileType)) { wallet = Storage.getStorage().loadWallet(file); } else if(FileType.BINARY.equals(fileType)) { - Optional optionalFullKey = SettingsController.askForWalletPassword(null, true); - if(!optionalFullKey.isPresent()) { + WalletPasswordDialog dlg = new WalletPasswordDialog(WalletPasswordDialog.PasswordRequirement.LOAD); + Optional password = dlg.showAndWait(); + if(!password.isPresent()) { return; } - ECKey encryptionFullKey = optionalFullKey.get(); + ECKey encryptionFullKey = ECIESKeyCrypter.deriveECKey(password.get()); wallet = Storage.getStorage().loadWallet(file, encryptionFullKey); encryptionPubKey = ECKey.fromPublicOnly(encryptionFullKey); } else { @@ -247,8 +249,9 @@ public class AppController implements Initializable { Tab tab = addWalletTab(file, encryptionPubKey, wallet); tabs.getSelectionModel().select(tab); + } catch (InvalidPasswordException e) { + showErrorDialog("Invalid Password", "The password was invalid."); } catch (Exception e) { - e.printStackTrace(); showErrorDialog("Error opening wallet", e.getMessage()); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/FileKeystoreImportPane.java b/src/main/java/com/sparrowwallet/sparrow/control/FileKeystoreImportPane.java index 2882d38c..fc2ce087 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/FileKeystoreImportPane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/FileKeystoreImportPane.java @@ -1,7 +1,7 @@ package com.sparrowwallet.sparrow.control; import com.google.gson.JsonParseException; -import com.sparrowwallet.drongo.crypto.ECIESKeyCrypter; +import com.sparrowwallet.drongo.crypto.InvalidPasswordException; import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.EventManager; @@ -83,7 +83,7 @@ public class FileKeystoreImportPane extends KeystoreImportPane { if(e.getCause() != null && e.getCause().getMessage() != null && !e.getCause().getMessage().isEmpty()) { errorMessage = e.getCause().getMessage(); } - if(e instanceof ECIESKeyCrypter.InvalidPasswordException || e.getCause() instanceof ECIESKeyCrypter.InvalidPasswordException) { + if(e instanceof InvalidPasswordException || e.getCause() instanceof InvalidPasswordException) { errorMessage = "Invalid wallet password"; } if(e instanceof JsonParseException || e.getCause() instanceof JsonParseException) { diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index b8d051cb..0d61166b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -159,7 +159,7 @@ public class SettingsController extends WalletFormController implements Initiali apply.setOnAction(event -> { try { - Optional optionalPubKey = askForWalletPassword(walletForm.getEncryptionPubKey(), false); + Optional optionalPubKey = askForWalletPassword(walletForm.getEncryptionPubKey()); if(optionalPubKey.isPresent()) { walletForm.setEncryptionPubKey(ECKey.fromPublicOnly(optionalPubKey.get())); walletForm.save(); @@ -261,11 +261,9 @@ public class SettingsController extends WalletFormController implements Initiali Platform.runLater(() -> apply.setDisable(!tabsValidate())); } - public static Optional askForWalletPassword(ECKey existingPubKey, boolean required) { + private Optional askForWalletPassword(ECKey existingPubKey) { WalletPasswordDialog.PasswordRequirement requirement; - if(existingPubKey == null && required) { - requirement = WalletPasswordDialog.PasswordRequirement.LOAD; - } else if(existingPubKey == null) { + if(existingPubKey == null) { requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_NEW; } else if(WalletForm.NO_PASSWORD_KEY.equals(existingPubKey)) { requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_EMPTY; @@ -276,7 +274,7 @@ public class SettingsController extends WalletFormController implements Initiali WalletPasswordDialog dlg = new WalletPasswordDialog(requirement); Optional password = dlg.showAndWait(); if(password.isPresent()) { - if(!required && password.get().isEmpty()) { + if(password.get().isEmpty()) { return Optional.of(WalletForm.NO_PASSWORD_KEY); }