From c6b6e7451572d57d09c86897b5cd48dc2c804aaf Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 19 Apr 2024 10:49:42 +0200 Subject: [PATCH] maintain strong reference to key derivation service until action completion --- .../com/sparrowwallet/sparrow/AppController.java | 16 +++++++++++----- .../sparrow/control/FileImportPane.java | 2 ++ .../control/FileWalletKeystoreImportPane.java | 3 +++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 1be9b7c5..71dae58b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -215,6 +215,8 @@ public class AppController implements Initializable { @FXML private UnlabeledToggleSwitch serverToggle; + private Storage.KeyDerivationService keyDerivationService; + private PauseTransition wait; private Timeline statusTimeline; @@ -1322,7 +1324,7 @@ public class AppController implements Initializable { log.error("Error saving imported wallet", e); } } else { - Storage.KeyDerivationService keyDerivationService = new Storage.KeyDerivationService(storage, password.get()); + keyDerivationService = new Storage.KeyDerivationService(storage, password.get()); keyDerivationService.setOnSucceeded(workerStateEvent -> { EventManager.get().post(new StorageEvent(Storage.getWalletFile(wallet.getName()).getAbsolutePath(), TimedEvent.Action.END, "Done")); ECKey encryptionFullKey = keyDerivationService.getValue(); @@ -1353,11 +1355,13 @@ public class AppController implements Initializable { if(key != null) { key.clear(); } + keyDerivationService = null; } }); keyDerivationService.setOnFailed(workerStateEvent -> { EventManager.get().post(new StorageEvent(Storage.getWalletFile(wallet.getName()).getAbsolutePath(), TimedEvent.Action.END, "Failed")); showErrorDialog("Error encrypting wallet", keyDerivationService.getException().getMessage()); + keyDerivationService = null; }); EventManager.get().post(new StorageEvent(Storage.getWalletFile(wallet.getName()).getAbsolutePath(), TimedEvent.Action.START, "Encrypting wallet...")); keyDerivationService.start(); @@ -1468,7 +1472,7 @@ public class AppController implements Initializable { Optional password = dlg.showAndWait(); if(password.isPresent()) { Storage storage = selectedWalletForm.getStorage(); - Storage.KeyDerivationService keyDerivationService = new Storage.KeyDerivationService(storage, password.get(), true); + keyDerivationService = new Storage.KeyDerivationService(storage, password.get(), true); keyDerivationService.setOnSucceeded(workerStateEvent -> { EventManager.get().post(new StorageEvent(selectedWalletForm.getWalletId(), TimedEvent.Action.END, "Done")); ECKey encryptionFullKey = keyDerivationService.getValue(); @@ -1486,7 +1490,7 @@ public class AppController implements Initializable { } finally { key.clear(); encryptionFullKey.clear(); - password.get().clear(); + keyDerivationService = null; } }); keyDerivationService.setOnFailed(workerStateEvent -> { @@ -1499,6 +1503,7 @@ public class AppController implements Initializable { } else { log.error("Error deriving wallet key", keyDerivationService.getException()); } + keyDerivationService = null; }); EventManager.get().post(new StorageEvent(selectedWalletForm.getWalletId(), TimedEvent.Action.START, "Decrypting wallet...")); keyDerivationService.start(); @@ -2223,7 +2228,7 @@ public class AppController implements Initializable { dlg.initOwner(rootStack.getScene().getWindow()); Optional password = dlg.showAndWait(); if(password.isPresent()) { - Storage.KeyDerivationService keyDerivationService = new Storage.KeyDerivationService(storage, password.get(), true); + keyDerivationService = new Storage.KeyDerivationService(storage, password.get(), true); keyDerivationService.setOnSucceeded(workerStateEvent -> { EventManager.get().post(new StorageEvent(selectedWalletForm.getWalletId(), TimedEvent.Action.END, "Done")); ECKey encryptionFullKey = keyDerivationService.getValue(); @@ -2233,7 +2238,7 @@ public class AppController implements Initializable { deleteStorage(storage, true); } finally { encryptionFullKey.clear(); - password.get().clear(); + keyDerivationService = null; } }); keyDerivationService.setOnFailed(workerStateEvent -> { @@ -2246,6 +2251,7 @@ public class AppController implements Initializable { } else { log.error("Error deriving wallet key", keyDerivationService.getException()); } + keyDerivationService = null; }); EventManager.get().post(new StorageEvent(selectedWalletForm.getWalletId(), TimedEvent.Action.START, "Decrypting wallet...")); keyDerivationService.start(); diff --git a/src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java b/src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java index 9e2806c3..39560560 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/FileImportPane.java @@ -240,6 +240,8 @@ public abstract class FileImportPane extends TitledDescriptionPane { contentBox.setPadding(new Insets(10, 30, 10, 30)); contentBox.setPrefHeight(60); + javafx.application.Platform.runLater(passwordField::requestFocus); + return contentBox; } } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/FileWalletKeystoreImportPane.java b/src/main/java/com/sparrowwallet/sparrow/control/FileWalletKeystoreImportPane.java index e715d39a..212ae93d 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/FileWalletKeystoreImportPane.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/FileWalletKeystoreImportPane.java @@ -12,6 +12,7 @@ import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.event.WalletImportEvent; import com.sparrowwallet.sparrow.io.ImportException; import com.sparrowwallet.sparrow.io.KeystoreFileImport; +import javafx.application.Platform; import javafx.collections.FXCollections; import javafx.geometry.Insets; import javafx.geometry.Pos; @@ -153,6 +154,8 @@ public class FileWalletKeystoreImportPane extends FileImportPane { contentBox.setPadding(new Insets(10, 30, 10, 30)); contentBox.setPrefHeight(60); + Platform.runLater(scriptTypeComboBox::requestFocus); + return contentBox; } }