finalising wallet settings

This commit is contained in:
Craig Raw
2020-05-13 12:47:21 +02:00
parent 84888b4a43
commit bc5690346c
17 changed files with 101 additions and 22 deletions
@@ -259,7 +259,11 @@ public class AppController implements Initializable {
public Tab addWalletTab(File walletFile, ECKey encryptionPubKey, Wallet wallet) {
try {
Tab tab = new Tab(walletFile.getName());
String name = walletFile.getName();
if(name.endsWith(".json")) {
name = name.substring(0, name.lastIndexOf('.'));
}
Tab tab = new Tab(name);
TabData tabData = new TabData(TabData.TabType.WALLET);
tab.setUserData(tabData);
tab.setContextMenu(getTabContextMenu(tab));
@@ -40,8 +40,8 @@ public class MainApp extends Application {
wallet.setPolicyType(PolicyType.SINGLE);
wallet.setScriptType(ScriptType.P2WPKH);
KeystoreImportDialog dlg = new KeystoreImportDialog(wallet);
dlg.showAndWait();
// KeystoreImportDialog dlg = new KeystoreImportDialog(wallet);
// dlg.showAndWait();
stage.show();
}
@@ -62,6 +62,16 @@ public class Storage {
throw new IOException("Could not create folder " + parent);
}
if(!file.getName().endsWith(".json")) {
File jsonFile = new File(parent, file.getName() + ".json");
if(file.exists()) {
if(!file.renameTo(jsonFile)) {
throw new IOException("Could not rename " + file.getName() + " to " + jsonFile.getName());
}
}
file = jsonFile;
}
Writer writer = new FileWriter(file);
gson.toJson(wallet, writer);
writer.close();
@@ -73,6 +83,16 @@ public class Storage {
throw new IOException("Could not create folder " + parent);
}
if(file.getName().endsWith(".json")) {
File noJsonFile = new File(parent, file.getName().substring(0, file.getName().lastIndexOf('.')));
if(file.exists()) {
if(!file.renameTo(noJsonFile)) {
throw new IOException("Could not rename " + file.getName() + " to " + noJsonFile.getName());
}
}
file = noJsonFile;
}
OutputStreamWriter writer = new OutputStreamWriter(new DeflaterOutputStream(new ECIESOutputStream(new FileOutputStream(file), encryptionKey, getEncryptionMagic())), StandardCharsets.UTF_8);
gson.toJson(wallet, writer);
writer.close();
@@ -63,6 +63,9 @@ public class KeystoreController extends WalletFormController implements Initiali
Platform.runLater(this::setupValidation);
selectSourcePane.managedProperty().bind(selectSourcePane.visibleProperty());
if(keystore.isValid()) {
selectSourcePane.setVisible(false);
}
updateType();
@@ -88,14 +88,14 @@ public class SettingsController extends WalletFormController implements Initiali
scriptType.getSelectionModel().select(policyType.getDefaultScriptType());
}
if(oldValue != null) {
clearKeystoreTabs();
}
multisigFieldset.setVisible(policyType.equals(PolicyType.MULTI));
if(policyType.equals(PolicyType.MULTI)) {
totalKeystores.unbind();
totalKeystores.set(0);
totalKeystores.bind(multisigControl.highValueProperty());
} else {
totalKeystores.unbind();
totalKeystores.set(0);
totalKeystores.set(1);
}
});
@@ -159,9 +159,9 @@ public class SettingsController extends WalletFormController implements Initiali
apply.setOnAction(event -> {
try {
Optional<ECKey> optionalPubKey = askForWalletPassword(walletForm.getEncryptionPubKey());
Optional<ECKey> optionalPubKey = requestEncryption(walletForm.getEncryptionPubKey());
if(optionalPubKey.isPresent()) {
walletForm.setEncryptionPubKey(ECKey.fromPublicOnly(optionalPubKey.get()));
walletForm.setEncryptionPubKey(optionalPubKey.get());
walletForm.save();
revert.setDisable(true);
apply.setDisable(true);
@@ -175,6 +175,11 @@ public class SettingsController extends WalletFormController implements Initiali
setFieldsFromWallet(walletForm.getWallet());
}
private void clearKeystoreTabs() {
totalKeystores.unbind();
totalKeystores.set(0);
}
private void setFieldsFromWallet(Wallet wallet) {
if(wallet.getPolicyType() == null) {
wallet.setPolicyType(PolicyType.SINGLE);
@@ -261,7 +266,7 @@ public class SettingsController extends WalletFormController implements Initiali
Platform.runLater(() -> apply.setDisable(!tabsValidate()));
}
private Optional<ECKey> askForWalletPassword(ECKey existingPubKey) {
private Optional<ECKey> requestEncryption(ECKey existingPubKey) {
WalletPasswordDialog.PasswordRequirement requirement;
if(existingPubKey == null) {
requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_NEW;
@@ -280,16 +285,14 @@ public class SettingsController extends WalletFormController implements Initiali
ECKey encryptionFullKey = ECIESKeyCrypter.deriveECKey(password.get());
ECKey encryptionPubKey = ECKey.fromPublicOnly(encryptionFullKey);
if(existingPubKey != null) {
if(WalletForm.NO_PASSWORD_KEY.equals(existingPubKey) || existingPubKey.equals(encryptionPubKey)) {
return Optional.of(encryptionPubKey);
} else {
AppController.showErrorDialog("Incorrect Password", "The password was incorrect.");
return Optional.empty();
}
if(existingPubKey != null && !WalletForm.NO_PASSWORD_KEY.equals(existingPubKey) && !existingPubKey.equals(encryptionPubKey)) {
AppController.showErrorDialog("Incorrect Password", "The password was incorrect.");
return Optional.empty();
}
return Optional.of(encryptionFullKey);
walletForm.getWallet().encrypt(password.get());
return Optional.of(encryptionPubKey);
}
return Optional.empty();
@@ -2,10 +2,13 @@ package com.sparrowwallet.sparrow.wallet;
import com.sparrowwallet.sparrow.AppController;
import com.sparrowwallet.sparrow.EventManager;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
@@ -61,5 +64,25 @@ public class WalletController extends WalletFormController implements Initializa
throw new IllegalStateException("Can't find pane", e);
}
});
if(!walletForm.getWallet().isValid()) {
for(Toggle toggle : walletMenu.getToggles()) {
if(toggle.getUserData().equals(Function.SETTINGS)) {
toggle.setSelected(true);
} else {
((ToggleButton)toggle).setDisable(true);
}
}
}
}
public void selectFunction(Function function) {
Platform.runLater(() -> {
for(Toggle toggle : walletMenu.getToggles()) {
if(toggle.getUserData().equals(function)) {
toggle.setSelected(true);
}
}
});
}
}