From b1785f352bb35d8556d23b41ed2bed9a62951b80 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sat, 30 May 2020 13:15:56 +0200 Subject: [PATCH] wallet save improvements and fixes --- drongo | 2 +- .../sparrowwallet/sparrow/AppController.java | 13 +---- .../sparrowwallet/sparrow/WalletTabData.java | 4 +- .../sparrow/event/WalletChangedEvent.java | 10 +--- .../event/WalletSettingsChangedEvent.java | 23 +++++++++ .../com/sparrowwallet/sparrow/io/Storage.java | 8 +-- .../sparrow/wallet/NodeEntry.java | 7 ++- .../sparrow/wallet/SettingsController.java | 12 ++--- .../sparrow/wallet/WalletController.java | 4 +- .../sparrow/wallet/WalletForm.java | 49 +++++++++++++++++-- 10 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/WalletSettingsChangedEvent.java diff --git a/drongo b/drongo index 60a0d450..d0a75fd2 160000 --- a/drongo +++ b/drongo @@ -1 +1 @@ -Subproject commit 60a0d450e0c35fe9e806cffb587fcb4edd603377 +Subproject commit d0a75fd26809d47fddf0c432bd8f6e6a9a522c64 diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index a49e3e9a..47d29e8e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -382,17 +382,6 @@ public class AppController implements Initializable { WalletForm walletForm = new WalletForm(storage, wallet); controller.setWalletForm(walletForm); - if(wallet.isValid()) { - ElectrumServer.TransactionHistoryService historyService = new ElectrumServer.TransactionHistoryService(wallet); - historyService.setOnSucceeded(workerStateEvent -> { - //TODO: Show connected - }); - historyService.setOnFailed(workerStateEvent -> { - //TODO: Show not connected, log exception - }); - historyService.start(); - } - if(!storage.getWalletFile().exists() || wallet.containsSource(KeystoreSource.HW_USB)) { Hwi.ScheduledEnumerateService enumerateService = new Hwi.ScheduledEnumerateService(null); enumerateService.setPeriod(new Duration(30 * 1000)); @@ -525,7 +514,7 @@ public class AppController implements Initializable { } @Subscribe - public void walletChanged(WalletChangedEvent event) { + public void walletSettingsChanged(WalletSettingsChangedEvent event) { exportWallet.setDisable(!event.getWallet().isValid()); } diff --git a/src/main/java/com/sparrowwallet/sparrow/WalletTabData.java b/src/main/java/com/sparrowwallet/sparrow/WalletTabData.java index abba5d3a..424c6512 100644 --- a/src/main/java/com/sparrowwallet/sparrow/WalletTabData.java +++ b/src/main/java/com/sparrowwallet/sparrow/WalletTabData.java @@ -2,7 +2,7 @@ package com.sparrowwallet.sparrow; import com.google.common.eventbus.Subscribe; import com.sparrowwallet.drongo.wallet.Wallet; -import com.sparrowwallet.sparrow.event.WalletChangedEvent; +import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent; import com.sparrowwallet.sparrow.io.Storage; public class WalletTabData extends TabData { @@ -26,7 +26,7 @@ public class WalletTabData extends TabData { } @Subscribe - public void walletChanged(WalletChangedEvent event) { + public void walletSettingsChanged(WalletSettingsChangedEvent event) { if(event.getWalletFile().equals(storage.getWalletFile())) { wallet = event.getWallet(); } diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WalletChangedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WalletChangedEvent.java index 60202e8d..06da2a83 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/WalletChangedEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/WalletChangedEvent.java @@ -2,22 +2,14 @@ package com.sparrowwallet.sparrow.event; import com.sparrowwallet.drongo.wallet.Wallet; -import java.io.File; - public class WalletChangedEvent { private final Wallet wallet; - private final File walletFile; - public WalletChangedEvent(Wallet wallet, File walletFile) { + public WalletChangedEvent(Wallet wallet) { this.wallet = wallet; - this.walletFile = walletFile; } public Wallet getWallet() { return wallet; } - - public File getWalletFile() { - return walletFile; - } } diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WalletSettingsChangedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WalletSettingsChangedEvent.java new file mode 100644 index 00000000..af841811 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/WalletSettingsChangedEvent.java @@ -0,0 +1,23 @@ +package com.sparrowwallet.sparrow.event; + +import com.sparrowwallet.drongo.wallet.Wallet; + +import java.io.File; + +public class WalletSettingsChangedEvent { + private final Wallet wallet; + private final File walletFile; + + public WalletSettingsChangedEvent(Wallet wallet, File walletFile) { + this.wallet = wallet; + this.walletFile = walletFile; + } + + public Wallet getWallet() { + return wallet; + } + + public File getWalletFile() { + return walletFile; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 54d23ef2..525c80bd 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -314,10 +314,6 @@ public class Storage { Iterator iter = children.iterator(); while(iter.hasNext()) { JsonObject childObject = (JsonObject)iter.next(); - if(childObject.get("label") == null) { - iter.remove(); - } - if(childObject.get("children") != null && childObject.getAsJsonArray("children").size() == 0) { childObject.remove("children"); } @@ -325,6 +321,10 @@ public class Storage { if(childObject.get("history") != null && childObject.getAsJsonArray("history").size() == 0) { childObject.remove("history"); } + + if(childObject.get("label") == null && childObject.get("children") == null && childObject.get("history") == null) { + iter.remove(); + } } return jsonObject; diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java b/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java index 4fb969f1..0e3bb112 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java @@ -4,6 +4,8 @@ import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.protocol.Script; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletNode; +import com.sparrowwallet.sparrow.EventManager; +import com.sparrowwallet.sparrow.event.WalletChangedEvent; import java.util.stream.Collectors; @@ -16,7 +18,10 @@ public class NodeEntry extends Entry { this.wallet = wallet; this.node = node; - labelProperty().addListener((observable, oldValue, newValue) -> node.setLabel(newValue)); + labelProperty().addListener((observable, oldValue, newValue) -> { + node.setLabel(newValue); + EventManager.get().post(new WalletChangedEvent(wallet)); + }); } public Address getAddress() { diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index 92f8a7f9..2350566f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -17,7 +17,7 @@ import com.sparrowwallet.sparrow.control.WalletPasswordDialog; import com.sparrowwallet.sparrow.event.SettingsChangedEvent; import com.sparrowwallet.sparrow.event.StorageEvent; import com.sparrowwallet.sparrow.event.TimedEvent; -import com.sparrowwallet.sparrow.event.WalletChangedEvent; +import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent; import com.sparrowwallet.sparrow.io.Storage; import javafx.beans.property.SimpleIntegerProperty; import javafx.collections.FXCollections; @@ -155,7 +155,7 @@ public class SettingsController extends WalletFormController implements Initiali keystoreTabs.getTabs().removeAll(keystoreTabs.getTabs()); totalKeystores.unbind(); totalKeystores.setValue(0); - walletForm.revert(); + walletForm.revertAndRefresh(); setFieldsFromWallet(walletForm.getWallet()); }); @@ -267,8 +267,8 @@ public class SettingsController extends WalletFormController implements Initiali if(password.get().length() == 0) { try { walletForm.getStorage().setEncryptionPubKey(Storage.NO_PASSWORD_KEY); - walletForm.save(); - EventManager.get().post(new WalletChangedEvent(walletForm.getWallet(), walletForm.getWalletFile())); + walletForm.saveAndRefresh(); + EventManager.get().post(new WalletSettingsChangedEvent(walletForm.getWallet(), walletForm.getWalletFile())); } catch (IOException e) { AppController.showErrorDialog("Error saving wallet", e.getMessage()); revert.setDisable(false); @@ -295,8 +295,8 @@ public class SettingsController extends WalletFormController implements Initiali walletForm.getWallet().encrypt(key); walletForm.getStorage().setEncryptionPubKey(encryptionPubKey); - walletForm.save(); - EventManager.get().post(new WalletChangedEvent(walletForm.getWallet(), walletForm.getWalletFile())); + walletForm.saveAndRefresh(); + EventManager.get().post(new WalletSettingsChangedEvent(walletForm.getWallet(), walletForm.getWalletFile())); } catch (Exception e) { AppController.showErrorDialog("Error saving wallet", e.getMessage()); revert.setDisable(false); diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletController.java index b40d38c8..71849aba 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletController.java @@ -4,7 +4,7 @@ import com.google.common.eventbus.Subscribe; import com.sparrowwallet.sparrow.AppController; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.event.ReceiveActionEvent; -import com.sparrowwallet.sparrow.event.WalletChangedEvent; +import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent; import javafx.application.Platform; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; @@ -95,7 +95,7 @@ public class WalletController extends WalletFormController implements Initializa } @Subscribe - public void walletChanged(WalletChangedEvent event) { + public void walletSettingsChanged(WalletSettingsChangedEvent event) { configure(walletForm.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 18eca127..3a92eae0 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -1,8 +1,12 @@ package com.sparrowwallet.sparrow.wallet; +import com.google.common.eventbus.Subscribe; import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletNode; +import com.sparrowwallet.sparrow.EventManager; +import com.sparrowwallet.sparrow.event.WalletChangedEvent; +import com.sparrowwallet.sparrow.io.ElectrumServer; import com.sparrowwallet.sparrow.io.Storage; import java.io.File; @@ -20,8 +24,11 @@ public class WalletForm { public WalletForm(Storage storage, Wallet currentWallet) { this.storage = storage; - this.oldWallet = currentWallet; - this.wallet = currentWallet.copy(); + this.oldWallet = currentWallet.copy(); + this.wallet = currentWallet; + refreshHistory(); + + EventManager.get().register(this); } public Wallet getWallet() { @@ -36,8 +43,9 @@ public class WalletForm { return storage.getWalletFile(); } - public void revert() { + public void revertAndRefresh() { this.wallet = oldWallet.copy(); + refreshHistory(); } public void save() throws IOException { @@ -45,6 +53,31 @@ public class WalletForm { oldWallet = wallet.copy(); } + public void saveAndRefresh() throws IOException { + //TODO: Detect trivial changes and don't clear history + wallet.clearHistory(); + save(); + refreshHistory(); + } + + public void refreshHistory() { + if(wallet.isValid()) { + ElectrumServer.TransactionHistoryService historyService = new ElectrumServer.TransactionHistoryService(wallet); + historyService.setOnSucceeded(workerStateEvent -> { + //TODO: Show connected + try { + storage.storeWallet(wallet); + } catch (IOException e) { + e.printStackTrace(); + } + }); + historyService.setOnFailed(workerStateEvent -> { + //TODO: Show not connected, log exception + }); + historyService.start(); + } + } + public NodeEntry getNodeEntry(KeyPurpose keyPurpose) { NodeEntry purposeEntry; Optional optionalPurposeEntry = accountEntries.stream().filter(entry -> entry.getNode().getKeyPurpose().equals(keyPurpose)).findFirst(); @@ -74,4 +107,14 @@ public class WalletForm { rootEntry.getChildren().add(freshEntry); return freshEntry; } + + @Subscribe + public void walletChanged(WalletChangedEvent event) { + try { + save(); + } catch (IOException e) { + //Background save failed + e.printStackTrace(); + } + } }