From 8d9e253f93f5cb4b0d09d47f95a965c57d91ef14 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 19 Mar 2021 12:27:08 +0200 Subject: [PATCH] update wallet in walletform on label and keystore source changes, detect threshold change --- .../sparrowwallet/sparrow/AppController.java | 4 ++-- .../sparrowwallet/sparrow/AppServices.java | 2 +- .../sparrow/control/CoinTreeTable.java | 4 ++-- .../event/WalletAddressesChangedEvent.java | 18 ++++++++++++++++ .../event/WalletSettingsChangedEvent.java | 8 +++---- .../sparrow/wallet/ReceiveController.java | 2 +- .../sparrow/wallet/SettingsController.java | 11 +++------- .../sparrow/wallet/SettingsWalletForm.java | 12 +++++++++++ .../sparrow/wallet/WalletController.java | 4 ++-- .../sparrow/wallet/WalletForm.java | 21 +++++++++++-------- 10 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/WalletAddressesChangedEvent.java diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index b9723df0..36810d40 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -921,7 +921,7 @@ public class AppController implements Initializable { Wallet pastWallet = wallet.copy(); walletTabData.getStorage().backupTempWallet(); wallet.clearHistory(); - EventManager.get().post(new WalletSettingsChangedEvent(wallet, pastWallet, walletTabData.getStorage().getWalletFile())); + EventManager.get().post(new WalletAddressesChangedEvent(wallet, pastWallet, walletTabData.getStorage().getWalletFile())); } } @@ -1296,7 +1296,7 @@ public class AppController implements Initializable { } @Subscribe - public void walletSettingsChanged(WalletSettingsChangedEvent event) { + public void walletAddressesChanged(WalletAddressesChangedEvent event) { Tab tab = tabs.getSelectionModel().getSelectedItem(); TabData tabData = (TabData)tab.getUserData(); if(tabData instanceof WalletTabData) { diff --git a/src/main/java/com/sparrowwallet/sparrow/AppServices.java b/src/main/java/com/sparrowwallet/sparrow/AppServices.java index aee9cb47..434909a4 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppServices.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppServices.java @@ -585,7 +585,7 @@ public class AppServices { } @Subscribe - public void walletSettingsChanged(WalletSettingsChangedEvent event) { + public void walletAddressesChanged(WalletAddressesChangedEvent event) { restartBwt(event.getWallet()); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/CoinTreeTable.java b/src/main/java/com/sparrowwallet/sparrow/control/CoinTreeTable.java index d5aa5911..f27a9634 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/CoinTreeTable.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/CoinTreeTable.java @@ -6,7 +6,7 @@ import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.event.WalletDataChangedEvent; import com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent; -import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent; +import com.sparrowwallet.sparrow.event.WalletAddressesChangedEvent; import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.net.ServerType; @@ -92,7 +92,7 @@ public class CoinTreeTable extends TreeTableView { EventManager.get().post(new WalletDataChangedEvent(wallet)); //Trigger full wallet rescan wallet.clearHistory(); - EventManager.get().post(new WalletSettingsChangedEvent(wallet, pastWallet, storage.getWalletFile())); + EventManager.get().post(new WalletAddressesChangedEvent(wallet, pastWallet, storage.getWalletFile())); } }); if(wallet.getBirthDate() == null) { diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WalletAddressesChangedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WalletAddressesChangedEvent.java new file mode 100644 index 00000000..d430f502 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/WalletAddressesChangedEvent.java @@ -0,0 +1,18 @@ +package com.sparrowwallet.sparrow.event; + +import com.sparrowwallet.drongo.wallet.Wallet; + +import java.io.File; + +/** + * This event is posted when a wallet's addresses are changed (keystores, policy, script type). + * This event marks a fundamental change that is used to update application level UI, clear node entry caches and similar. It should only be subscribed to by application-level classes. + * Note that WalletForm does not listen to this event to save the wallet, since the wallet is foreground saved directly in SettingsController before this event is posted. + * This is because any failure in saving the wallet must be immediately reported to the user. + * Note that all wallet detail controllers that share a WalletForm, and that class posts WalletNodesChangedEvent once it has cleared it's entry caches. + */ +public class WalletAddressesChangedEvent extends WalletSettingsChangedEvent { + public WalletAddressesChangedEvent(Wallet wallet, Wallet pastWallet, File walletFile) { + super(wallet, pastWallet, walletFile); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WalletSettingsChangedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WalletSettingsChangedEvent.java index 962104ed..39543c16 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/WalletSettingsChangedEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/WalletSettingsChangedEvent.java @@ -5,11 +5,9 @@ import com.sparrowwallet.drongo.wallet.Wallet; import java.io.File; /** - * This event is posted when a wallet's settings are changed (keystores, policy, script type). - * This event marks a fundamental change that is used to update application level UI, clear node entry caches and similar. It should only be subscribed to by application-level classes. - * Note that WalletForm does not listen to this event to save the wallet, since the wallet is foreground saved directly in SettingsController before this event is posted. - * This is because any failure in saving the wallet must be immediately reported to the user. - * Note that all wallet detail controllers that share a WalletForm, and that class posts WalletNodesChangedEvent once it has cleared it's entry caches. + * This event is posted when a wallet's settings are changed in a way that does not update the wallet addresses or history. + * For example, changing the keystore source or label will trigger this event, which updates the wallet but avoids a full wallet refresh. + * It is impossible for the validity of a wallet to change on this event - listen for WalletAddressesChangedEvent for this */ public class WalletSettingsChangedEvent extends WalletChangedEvent { private final Wallet pastWallet; diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java index 3a454ab6..a6a61492 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/ReceiveController.java @@ -258,7 +258,7 @@ public class ReceiveController extends WalletFormController implements Initializ } @Subscribe - public void walletSettingsChanged(WalletSettingsChangedEvent event) { + public void walletAddressesChanged(WalletAddressesChangedEvent event) { displayAddress.setUserData(null); } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index 852f8279..d4c1e494 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -345,7 +345,7 @@ public class SettingsController extends WalletFormController implements Initiali } @Subscribe - public void walletSettingsChanged(WalletSettingsChangedEvent event) { + public void walletAddressesChanged(WalletAddressesChangedEvent event) { updateBirthDate(event.getWalletFile(), event.getWallet()); } @@ -404,9 +404,7 @@ public class SettingsController extends WalletFormController implements Initiali try { walletForm.getStorage().setEncryptionPubKey(Storage.NO_PASSWORD_KEY); walletForm.saveAndRefresh(); - if(requirement == WalletPasswordDialog.PasswordRequirement.UPDATE_NEW || requirement == WalletPasswordDialog.PasswordRequirement.UPDATE_CHANGE) { - EventManager.get().post(new RequestOpenWalletsEvent()); - } + EventManager.get().post(new RequestOpenWalletsEvent()); } catch (IOException e) { log.error("Error saving wallet", e); AppServices.showErrorDialog("Error saving wallet", e.getMessage()); @@ -450,10 +448,7 @@ public class SettingsController extends WalletFormController implements Initiali walletForm.getWallet().encrypt(key); walletForm.getStorage().setEncryptionPubKey(encryptionPubKey); walletForm.saveAndRefresh(); - - if(requirement == WalletPasswordDialog.PasswordRequirement.UPDATE_NEW || requirement == WalletPasswordDialog.PasswordRequirement.UPDATE_EMPTY) { - EventManager.get().post(new RequestOpenWalletsEvent()); - } + EventManager.get().post(new RequestOpenWalletsEvent()); } catch (Exception e) { log.error("Error saving wallet", e); AppServices.showErrorDialog("Error saving wallet", e.getMessage()); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java index 0f7b9e0a..13cc132d 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsWalletForm.java @@ -1,8 +1,10 @@ package com.sparrowwallet.sparrow.wallet; +import com.sparrowwallet.drongo.policy.Policy; import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.sparrow.EventManager; +import com.sparrowwallet.sparrow.event.WalletAddressesChangedEvent; import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent; import com.sparrowwallet.sparrow.io.Storage; @@ -52,6 +54,8 @@ public class SettingsWalletForm extends WalletForm { save(); if(refreshAll) { + EventManager.get().post(new WalletAddressesChangedEvent(wallet, pastWallet, getWalletFile())); + } else { EventManager.get().post(new WalletSettingsChangedEvent(wallet, pastWallet, getWalletFile())); } } @@ -91,6 +95,10 @@ public class SettingsWalletForm extends WalletForm { //TODO: Determine if Miniscript has changed for custom policies + if(!Objects.equals(getNumSignaturesRequired(original.getDefaultPolicy()), getNumSignaturesRequired(changed.getDefaultPolicy()))) { + return true; + } + if(original.getKeystores().size() != changed.getKeystores().size()) { return true; } @@ -110,4 +118,8 @@ public class SettingsWalletForm extends WalletForm { return false; } + + private Integer getNumSignaturesRequired(Policy policy) { + return policy == null ? null : policy.getNumSignaturesRequired(); + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletController.java index a664d52b..b27b82ee 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletController.java @@ -5,7 +5,7 @@ import com.sparrowwallet.sparrow.AppServices; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.event.ReceiveActionEvent; import com.sparrowwallet.sparrow.event.SendActionEvent; -import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent; +import com.sparrowwallet.sparrow.event.WalletAddressesChangedEvent; import javafx.application.Platform; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; @@ -106,7 +106,7 @@ public class WalletController extends WalletFormController implements Initializa } @Subscribe - public void walletSettingsChanged(WalletSettingsChangedEvent event) { + public void walletAddressesChanged(WalletAddressesChangedEvent event) { if(event.getWalletFile().equals(walletForm.getWalletFile())) { configure(event.getWallet().isValid()); } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index 5f54e5f0..e6a60cfd 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -265,17 +265,20 @@ public class WalletForm { public void walletSettingsChanged(WalletSettingsChangedEvent event) { if(event.getWalletFile().equals(storage.getWalletFile())) { wallet = event.getWallet(); - walletTransactionsEntry = null; - walletUtxosEntry = null; - accountEntries.clear(); - EventManager.get().post(new WalletNodesChangedEvent(wallet)); - //It is necessary to save the past wallet because the actual copying of the past labels only occurs on a later ConnectionEvent with bwt - if(Config.get().getServerType() == ServerType.BITCOIN_CORE) { - savedPastWallet = event.getPastWallet(); + if(event instanceof WalletAddressesChangedEvent) { + walletTransactionsEntry = null; + walletUtxosEntry = null; + accountEntries.clear(); + EventManager.get().post(new WalletNodesChangedEvent(wallet)); + + //It is necessary to save the past wallet because the actual copying of the past labels only occurs on a later ConnectionEvent with bwt + if(Config.get().getServerType() == ServerType.BITCOIN_CORE) { + savedPastWallet = event.getPastWallet(); + } + + refreshHistory(AppServices.getCurrentBlockHeight(), event.getPastWallet()); } - - refreshHistory(AppServices.getCurrentBlockHeight(), event.getPastWallet()); } }