wallet save improvements and fixes

This commit is contained in:
Craig Raw 2020-05-30 13:15:56 +02:00
parent 7e8496915a
commit b1785f352b
10 changed files with 92 additions and 40 deletions

2
drongo

@ -1 +1 @@
Subproject commit 60a0d450e0c35fe9e806cffb587fcb4edd603377 Subproject commit d0a75fd26809d47fddf0c432bd8f6e6a9a522c64

View file

@ -382,17 +382,6 @@ public class AppController implements Initializable {
WalletForm walletForm = new WalletForm(storage, wallet); WalletForm walletForm = new WalletForm(storage, wallet);
controller.setWalletForm(walletForm); 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)) { if(!storage.getWalletFile().exists() || wallet.containsSource(KeystoreSource.HW_USB)) {
Hwi.ScheduledEnumerateService enumerateService = new Hwi.ScheduledEnumerateService(null); Hwi.ScheduledEnumerateService enumerateService = new Hwi.ScheduledEnumerateService(null);
enumerateService.setPeriod(new Duration(30 * 1000)); enumerateService.setPeriod(new Duration(30 * 1000));
@ -525,7 +514,7 @@ public class AppController implements Initializable {
} }
@Subscribe @Subscribe
public void walletChanged(WalletChangedEvent event) { public void walletSettingsChanged(WalletSettingsChangedEvent event) {
exportWallet.setDisable(!event.getWallet().isValid()); exportWallet.setDisable(!event.getWallet().isValid());
} }

View file

@ -2,7 +2,7 @@ package com.sparrowwallet.sparrow;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.event.WalletChangedEvent; import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent;
import com.sparrowwallet.sparrow.io.Storage; import com.sparrowwallet.sparrow.io.Storage;
public class WalletTabData extends TabData { public class WalletTabData extends TabData {
@ -26,7 +26,7 @@ public class WalletTabData extends TabData {
} }
@Subscribe @Subscribe
public void walletChanged(WalletChangedEvent event) { public void walletSettingsChanged(WalletSettingsChangedEvent event) {
if(event.getWalletFile().equals(storage.getWalletFile())) { if(event.getWalletFile().equals(storage.getWalletFile())) {
wallet = event.getWallet(); wallet = event.getWallet();
} }

View file

@ -2,22 +2,14 @@ package com.sparrowwallet.sparrow.event;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import java.io.File;
public class WalletChangedEvent { public class WalletChangedEvent {
private final Wallet wallet; private final Wallet wallet;
private final File walletFile;
public WalletChangedEvent(Wallet wallet, File walletFile) { public WalletChangedEvent(Wallet wallet) {
this.wallet = wallet; this.wallet = wallet;
this.walletFile = walletFile;
} }
public Wallet getWallet() { public Wallet getWallet() {
return wallet; return wallet;
} }
public File getWalletFile() {
return walletFile;
}
} }

View file

@ -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;
}
}

View file

@ -314,10 +314,6 @@ public class Storage {
Iterator<JsonElement> iter = children.iterator(); Iterator<JsonElement> iter = children.iterator();
while(iter.hasNext()) { while(iter.hasNext()) {
JsonObject childObject = (JsonObject)iter.next(); JsonObject childObject = (JsonObject)iter.next();
if(childObject.get("label") == null) {
iter.remove();
}
if(childObject.get("children") != null && childObject.getAsJsonArray("children").size() == 0) { if(childObject.get("children") != null && childObject.getAsJsonArray("children").size() == 0) {
childObject.remove("children"); childObject.remove("children");
} }
@ -325,6 +321,10 @@ public class Storage {
if(childObject.get("history") != null && childObject.getAsJsonArray("history").size() == 0) { if(childObject.get("history") != null && childObject.getAsJsonArray("history").size() == 0) {
childObject.remove("history"); childObject.remove("history");
} }
if(childObject.get("label") == null && childObject.get("children") == null && childObject.get("history") == null) {
iter.remove();
}
} }
return jsonObject; return jsonObject;

View file

@ -4,6 +4,8 @@ import com.sparrowwallet.drongo.address.Address;
import com.sparrowwallet.drongo.protocol.Script; import com.sparrowwallet.drongo.protocol.Script;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletNode; import com.sparrowwallet.drongo.wallet.WalletNode;
import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.WalletChangedEvent;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -16,7 +18,10 @@ public class NodeEntry extends Entry {
this.wallet = wallet; this.wallet = wallet;
this.node = node; 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() { public Address getAddress() {

View file

@ -17,7 +17,7 @@ import com.sparrowwallet.sparrow.control.WalletPasswordDialog;
import com.sparrowwallet.sparrow.event.SettingsChangedEvent; import com.sparrowwallet.sparrow.event.SettingsChangedEvent;
import com.sparrowwallet.sparrow.event.StorageEvent; import com.sparrowwallet.sparrow.event.StorageEvent;
import com.sparrowwallet.sparrow.event.TimedEvent; 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 com.sparrowwallet.sparrow.io.Storage;
import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
@ -155,7 +155,7 @@ public class SettingsController extends WalletFormController implements Initiali
keystoreTabs.getTabs().removeAll(keystoreTabs.getTabs()); keystoreTabs.getTabs().removeAll(keystoreTabs.getTabs());
totalKeystores.unbind(); totalKeystores.unbind();
totalKeystores.setValue(0); totalKeystores.setValue(0);
walletForm.revert(); walletForm.revertAndRefresh();
setFieldsFromWallet(walletForm.getWallet()); setFieldsFromWallet(walletForm.getWallet());
}); });
@ -267,8 +267,8 @@ public class SettingsController extends WalletFormController implements Initiali
if(password.get().length() == 0) { if(password.get().length() == 0) {
try { try {
walletForm.getStorage().setEncryptionPubKey(Storage.NO_PASSWORD_KEY); walletForm.getStorage().setEncryptionPubKey(Storage.NO_PASSWORD_KEY);
walletForm.save(); walletForm.saveAndRefresh();
EventManager.get().post(new WalletChangedEvent(walletForm.getWallet(), walletForm.getWalletFile())); EventManager.get().post(new WalletSettingsChangedEvent(walletForm.getWallet(), walletForm.getWalletFile()));
} catch (IOException e) { } catch (IOException e) {
AppController.showErrorDialog("Error saving wallet", e.getMessage()); AppController.showErrorDialog("Error saving wallet", e.getMessage());
revert.setDisable(false); revert.setDisable(false);
@ -295,8 +295,8 @@ public class SettingsController extends WalletFormController implements Initiali
walletForm.getWallet().encrypt(key); walletForm.getWallet().encrypt(key);
walletForm.getStorage().setEncryptionPubKey(encryptionPubKey); walletForm.getStorage().setEncryptionPubKey(encryptionPubKey);
walletForm.save(); walletForm.saveAndRefresh();
EventManager.get().post(new WalletChangedEvent(walletForm.getWallet(), walletForm.getWalletFile())); EventManager.get().post(new WalletSettingsChangedEvent(walletForm.getWallet(), walletForm.getWalletFile()));
} catch (Exception e) { } catch (Exception e) {
AppController.showErrorDialog("Error saving wallet", e.getMessage()); AppController.showErrorDialog("Error saving wallet", e.getMessage());
revert.setDisable(false); revert.setDisable(false);

View file

@ -4,7 +4,7 @@ import com.google.common.eventbus.Subscribe;
import com.sparrowwallet.sparrow.AppController; import com.sparrowwallet.sparrow.AppController;
import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.EventManager;
import com.sparrowwallet.sparrow.event.ReceiveActionEvent; import com.sparrowwallet.sparrow.event.ReceiveActionEvent;
import com.sparrowwallet.sparrow.event.WalletChangedEvent; import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
@ -95,7 +95,7 @@ public class WalletController extends WalletFormController implements Initializa
} }
@Subscribe @Subscribe
public void walletChanged(WalletChangedEvent event) { public void walletSettingsChanged(WalletSettingsChangedEvent event) {
configure(walletForm.getWallet().isValid()); configure(walletForm.getWallet().isValid());
} }

View file

@ -1,8 +1,12 @@
package com.sparrowwallet.sparrow.wallet; package com.sparrowwallet.sparrow.wallet;
import com.google.common.eventbus.Subscribe;
import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.KeyPurpose;
import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.drongo.wallet.WalletNode; 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 com.sparrowwallet.sparrow.io.Storage;
import java.io.File; import java.io.File;
@ -20,8 +24,11 @@ public class WalletForm {
public WalletForm(Storage storage, Wallet currentWallet) { public WalletForm(Storage storage, Wallet currentWallet) {
this.storage = storage; this.storage = storage;
this.oldWallet = currentWallet; this.oldWallet = currentWallet.copy();
this.wallet = currentWallet.copy(); this.wallet = currentWallet;
refreshHistory();
EventManager.get().register(this);
} }
public Wallet getWallet() { public Wallet getWallet() {
@ -36,8 +43,9 @@ public class WalletForm {
return storage.getWalletFile(); return storage.getWalletFile();
} }
public void revert() { public void revertAndRefresh() {
this.wallet = oldWallet.copy(); this.wallet = oldWallet.copy();
refreshHistory();
} }
public void save() throws IOException { public void save() throws IOException {
@ -45,6 +53,31 @@ public class WalletForm {
oldWallet = wallet.copy(); 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) { public NodeEntry getNodeEntry(KeyPurpose keyPurpose) {
NodeEntry purposeEntry; NodeEntry purposeEntry;
Optional<NodeEntry> optionalPurposeEntry = accountEntries.stream().filter(entry -> entry.getNode().getKeyPurpose().equals(keyPurpose)).findFirst(); Optional<NodeEntry> optionalPurposeEntry = accountEntries.stream().filter(entry -> entry.getNode().getKeyPurpose().equals(keyPurpose)).findFirst();
@ -74,4 +107,14 @@ public class WalletForm {
rootEntry.getChildren().add(freshEntry); rootEntry.getChildren().add(freshEntry);
return freshEntry; return freshEntry;
} }
@Subscribe
public void walletChanged(WalletChangedEvent event) {
try {
save();
} catch (IOException e) {
//Background save failed
e.printStackTrace();
}
}
} }