From 7f254e763d142844ff9db62d7d7f6c504cd20d74 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 17 Feb 2023 09:10:40 +0200 Subject: [PATCH] fix keystore encryption issue when changing the password on a wallet with freshly added accounts --- .../event/NewChildWalletSavedEvent.java | 24 +++++++++++++++++++ .../sparrow/paynym/PayNymController.java | 1 + .../terminal/wallet/SettingsDialog.java | 10 +++++++- .../sparrow/terminal/wallet/WalletDialog.java | 6 ++--- .../sparrow/wallet/SendController.java | 2 ++ .../sparrow/wallet/SettingsController.java | 8 +++++++ .../sparrow/wallet/SettingsWalletForm.java | 11 +++++++++ .../sparrow/wallet/WalletForm.java | 1 + 8 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/NewChildWalletSavedEvent.java diff --git a/src/main/java/com/sparrowwallet/sparrow/event/NewChildWalletSavedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/NewChildWalletSavedEvent.java new file mode 100644 index 00000000..3fec4a4d --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/NewChildWalletSavedEvent.java @@ -0,0 +1,24 @@ +package com.sparrowwallet.sparrow.event; + +import com.sparrowwallet.drongo.wallet.Wallet; +import com.sparrowwallet.sparrow.io.Storage; + +public class NewChildWalletSavedEvent { + private final Storage storage; + private final Wallet masterWallet; + private final Wallet childWallet; + + public NewChildWalletSavedEvent(Storage storage, Wallet masterWallet, Wallet childWallet) { + this.storage = storage; + this.masterWallet = masterWallet; + this.childWallet = childWallet; + } + + public String getMasterWalletId() { + return storage.getWalletId(masterWallet); + } + + public Wallet getChildWallet() { + return childWallet; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java index 0f23027d..3269d841 100644 --- a/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java +++ b/src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java @@ -441,6 +441,7 @@ public class PayNymController { if(!storage.isPersisted(addedWallet)) { try { storage.saveWallet(addedWallet); + EventManager.get().post(new NewChildWalletSavedEvent(storage, masterWallet, addedWallet)); } catch(Exception e) { log.error("Error saving wallet", e); AppServices.showErrorDialog("Error saving wallet " + addedWallet.getName(), e.getMessage()); diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/SettingsDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/SettingsDialog.java index da35f64c..c723d689 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/SettingsDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/SettingsDialog.java @@ -1,5 +1,6 @@ package com.sparrowwallet.sparrow.terminal.wallet; +import com.google.common.eventbus.Subscribe; import com.googlecode.lanterna.TerminalSize; import com.googlecode.lanterna.gui2.*; import com.googlecode.lanterna.gui2.dialogs.TextInputDialogBuilder; @@ -21,8 +22,8 @@ import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.StorageException; import com.sparrowwallet.sparrow.terminal.SparrowTerminal; import com.sparrowwallet.sparrow.wallet.Function; +import com.sparrowwallet.sparrow.wallet.SettingsWalletForm; import com.sparrowwallet.sparrow.wallet.WalletForm; -import com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices; import javafx.application.Platform; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -276,6 +277,13 @@ public class SettingsDialog extends WalletDialog { return lines; } + @Subscribe + public void newChildWalletSaved(NewChildWalletSavedEvent event) { + if(event.getMasterWalletId().equals(getWalletForm().getMasterWalletId())) { + ((SettingsWalletForm)getWalletForm()).childWalletSaved(event.getChildWallet()); + } + } + public enum PasswordRequirement { UPDATE_NEW("Add a password to the wallet?\nLeave empty for no password:", "No Password"), UPDATE_EMPTY("This wallet has no password.\nAdd a password to the wallet?\nLeave empty for no password:", "No Password"), diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/WalletDialog.java b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/WalletDialog.java index 89c24fe1..8faae108 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/WalletDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/wallet/WalletDialog.java @@ -16,10 +16,7 @@ import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.CurrencyRate; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.UnitFormat; -import com.sparrowwallet.sparrow.event.ChildWalletsAddedEvent; -import com.sparrowwallet.sparrow.event.StorageEvent; -import com.sparrowwallet.sparrow.event.TimedEvent; -import com.sparrowwallet.sparrow.event.WalletHistoryClearedEvent; +import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.terminal.SparrowTerminal; @@ -151,6 +148,7 @@ public class WalletDialog extends DialogWindow { if(!storage.isPersisted(childWallet)) { try { storage.saveWallet(childWallet); + EventManager.get().post(new NewChildWalletSavedEvent(storage, masterWallet, childWallet)); } catch(Exception e) { log.error("Error saving wallet", e); showErrorDialog("Error saving wallet " + childWallet.getName(), e.getMessage()); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java index 7c52a0e0..5966f809 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java @@ -1196,6 +1196,7 @@ public class SendController extends WalletFormController implements Initializabl if(!storage.isPersisted(childWallet)) { try { storage.saveWallet(childWallet); + EventManager.get().post(new NewChildWalletSavedEvent(storage, masterWallet, childWallet)); } catch(Exception e) { AppServices.showErrorDialog("Error saving wallet " + childWallet.getName(), e.getMessage()); } @@ -1381,6 +1382,7 @@ public class SendController extends WalletFormController implements Initializabl if(!storage.isPersisted(addedWallet)) { try { storage.saveWallet(addedWallet); + EventManager.get().post(new NewChildWalletSavedEvent(storage, masterWallet, addedWallet)); } catch(Exception e) { log.error("Error saving wallet", e); AppServices.showErrorDialog("Error saving wallet " + addedWallet.getName(), e.getMessage()); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index 0ca1d30c..47e12410 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -631,6 +631,7 @@ public class SettingsController extends WalletFormController implements Initiali if(!storage.isPersisted(childWallet)) { try { storage.saveWallet(childWallet); + EventManager.get().post(new NewChildWalletSavedEvent(storage, masterWallet, childWallet)); } catch(Exception e) { log.error("Error saving wallet", e); AppServices.showErrorDialog("Error saving wallet " + childWallet.getName(), e.getMessage()); @@ -718,6 +719,13 @@ public class SettingsController extends WalletFormController implements Initiali } } + @Subscribe + public void newChildWalletSaved(NewChildWalletSavedEvent event) { + if(event.getMasterWalletId().equals(walletForm.getMasterWalletId())) { + ((SettingsWalletForm)walletForm).childWalletSaved(event.getChildWallet()); + } + } + private void saveWallet(boolean changePassword, boolean suggestChangePassword) { ECKey existingPubKey = walletForm.getStorage().getEncryptionPubKey(); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java index bf7bee15..37685cf8 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java @@ -242,4 +242,15 @@ public class SettingsWalletForm extends WalletForm { return changedKeystores; } + + public void childWalletSaved(Wallet childWallet) { + //Update the child wallets for the master wallet of the local walletCopy to ensure all KeystoreEncryptionChangedEvents are posted + Wallet masterWalletCopy = walletCopy.isMasterWallet() ? walletCopy : walletCopy.getMasterWallet(); + Wallet childWalletCopy = masterWalletCopy.getChildWallet(childWallet.getName()); + if(childWalletCopy == null) { + childWalletCopy = childWallet.copy(); + childWalletCopy.setMasterWallet(masterWalletCopy); + masterWalletCopy.getChildWallets().add(childWallet.copy()); + } + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index 9f014683..b6d30980 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -189,6 +189,7 @@ public class WalletForm { if(!storage.isPersisted(addedWallet)) { try { storage.saveWallet(addedWallet); + EventManager.get().post(new NewChildWalletSavedEvent(storage, wallet, addedWallet)); } catch(Exception e) { log.error("Error saving wallet", e); AppServices.showErrorDialog("Error saving wallet " + addedWallet.getName(), e.getMessage());