From 0c56f5a9a17548fabb4682e74fb05197a9814c33 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 9 Sep 2020 16:15:33 +0200 Subject: [PATCH] add change password functionality --- .../sparrow/control/WalletPasswordDialog.java | 17 ++++++++++++++--- .../com/sparrowwallet/sparrow/io/Storage.java | 1 + .../sparrow/wallet/SettingsController.java | 16 +++++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java index ea41d155..8c760747 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java @@ -21,6 +21,7 @@ public class WalletPasswordDialog extends Dialog { private final CustomPasswordField password; private final CustomPasswordField passwordConfirm; private final CheckBox backupExisting; + private final CheckBox changePassword; public WalletPasswordDialog(PasswordRequirement requirement) { this(null, requirement); @@ -31,6 +32,7 @@ public class WalletPasswordDialog extends Dialog { this.password = (CustomPasswordField)TextFields.createClearablePasswordField(); this.passwordConfirm = (CustomPasswordField)TextFields.createClearablePasswordField(); this.backupExisting = new CheckBox("Backup existing wallet first"); + this.changePassword = new CheckBox("Change password"); final DialogPane dialogPane = getDialogPane(); setTitle("Wallet Password" + (walletName != null ? " - " + walletName : "")); @@ -55,6 +57,10 @@ public class WalletPasswordDialog extends Dialog { backupExisting.setSelected(true); } + if(requirement == PasswordRequirement.UPDATE_SET) { + content.getChildren().add(changePassword); + } + dialogPane.setContent(content); ValidationSupport validationSupport = new ValidationSupport(); @@ -70,12 +76,12 @@ public class WalletPasswordDialog extends Dialog { BooleanBinding isInvalid = Bindings.createBooleanBinding(() -> passwordConfirm.isVisible() && !password.getText().equals(passwordConfirm.getText()), password.textProperty(), passwordConfirm.textProperty()); okButton.disableProperty().bind(isInvalid); - if(requirement != PasswordRequirement.UPDATE_NEW) { + if(requirement != PasswordRequirement.UPDATE_NEW && requirement != PasswordRequirement.UPDATE_CHANGE) { passwordConfirm.setVisible(false); passwordConfirm.setManaged(false); } - if(requirement == PasswordRequirement.UPDATE_NEW || requirement == PasswordRequirement.UPDATE_EMPTY) { + if(requirement == PasswordRequirement.UPDATE_NEW || requirement == PasswordRequirement.UPDATE_EMPTY || requirement == PasswordRequirement.UPDATE_CHANGE) { password.textProperty().addListener((observable, oldValue, newValue) -> { if(newValue.isEmpty()) { okButton.setText("No Password"); @@ -100,11 +106,16 @@ public class WalletPasswordDialog extends Dialog { return backupExisting.isSelected(); } + public boolean isChangePassword() { + return changePassword.isSelected(); + } + public enum PasswordRequirement { LOAD("Please enter the wallet password:", "Unlock"), UPDATE_NEW("Add a password to the wallet?\nLeave empty for none:", "No Password"), UPDATE_EMPTY("This wallet has no password.\nAdd a password to the wallet?\nLeave empty for none:", "No Password"), - UPDATE_SET("Please re-enter the wallet password:", "Verify Password"); + UPDATE_SET("Please re-enter the wallet password:", "Verify Password"), + UPDATE_CHANGE("Enter the new wallet password.\nLeave empty for none:", "No Password"); private final String description; private final String okButtonText; diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 7ce96bd5..58aab090 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -84,6 +84,7 @@ public class Storage { Wallet wallet = gson.fromJson(reader, Wallet.class); reader.close(); + encryptionPubKey = NO_PASSWORD_KEY; return wallet; } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index 6a29e540..a1004873 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -176,7 +176,7 @@ public class SettingsController extends WalletFormController implements Initiali apply.setOnAction(event -> { revert.setDisable(true); apply.setDisable(true); - saveWallet(); + saveWallet(false); }); setFieldsFromWallet(walletForm.getWallet()); @@ -282,12 +282,16 @@ public class SettingsController extends WalletFormController implements Initiali } } - private void saveWallet() { + private void saveWallet(boolean changePassword) { ECKey existingPubKey = walletForm.getStorage().getEncryptionPubKey(); WalletPasswordDialog.PasswordRequirement requirement; if(existingPubKey == null) { - requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_NEW; + if(changePassword) { + requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_CHANGE; + } else { + requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_NEW; + } } else if(Storage.NO_PASSWORD_KEY.equals(existingPubKey)) { requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_EMPTY; } else { @@ -336,6 +340,12 @@ public class SettingsController extends WalletFormController implements Initiali return; } + if(dlg.isChangePassword()) { + walletForm.getStorage().setEncryptionPubKey(null); + saveWallet(true); + return; + } + key = new Key(encryptionFullKey.getPrivKeyBytes(), walletForm.getStorage().getKeyDeriver().getSalt(), EncryptionType.Deriver.ARGON2); walletForm.getWallet().encrypt(key);