add change password functionality

This commit is contained in:
Craig Raw 2020-09-09 16:15:33 +02:00
parent 38b27bb091
commit 0c56f5a9a1
3 changed files with 28 additions and 6 deletions

View file

@ -21,6 +21,7 @@ public class WalletPasswordDialog extends Dialog<SecureString> {
private final CustomPasswordField password; private final CustomPasswordField password;
private final CustomPasswordField passwordConfirm; private final CustomPasswordField passwordConfirm;
private final CheckBox backupExisting; private final CheckBox backupExisting;
private final CheckBox changePassword;
public WalletPasswordDialog(PasswordRequirement requirement) { public WalletPasswordDialog(PasswordRequirement requirement) {
this(null, requirement); this(null, requirement);
@ -31,6 +32,7 @@ public class WalletPasswordDialog extends Dialog<SecureString> {
this.password = (CustomPasswordField)TextFields.createClearablePasswordField(); this.password = (CustomPasswordField)TextFields.createClearablePasswordField();
this.passwordConfirm = (CustomPasswordField)TextFields.createClearablePasswordField(); this.passwordConfirm = (CustomPasswordField)TextFields.createClearablePasswordField();
this.backupExisting = new CheckBox("Backup existing wallet first"); this.backupExisting = new CheckBox("Backup existing wallet first");
this.changePassword = new CheckBox("Change password");
final DialogPane dialogPane = getDialogPane(); final DialogPane dialogPane = getDialogPane();
setTitle("Wallet Password" + (walletName != null ? " - " + walletName : "")); setTitle("Wallet Password" + (walletName != null ? " - " + walletName : ""));
@ -55,6 +57,10 @@ public class WalletPasswordDialog extends Dialog<SecureString> {
backupExisting.setSelected(true); backupExisting.setSelected(true);
} }
if(requirement == PasswordRequirement.UPDATE_SET) {
content.getChildren().add(changePassword);
}
dialogPane.setContent(content); dialogPane.setContent(content);
ValidationSupport validationSupport = new ValidationSupport(); ValidationSupport validationSupport = new ValidationSupport();
@ -70,12 +76,12 @@ public class WalletPasswordDialog extends Dialog<SecureString> {
BooleanBinding isInvalid = Bindings.createBooleanBinding(() -> passwordConfirm.isVisible() && !password.getText().equals(passwordConfirm.getText()), password.textProperty(), passwordConfirm.textProperty()); BooleanBinding isInvalid = Bindings.createBooleanBinding(() -> passwordConfirm.isVisible() && !password.getText().equals(passwordConfirm.getText()), password.textProperty(), passwordConfirm.textProperty());
okButton.disableProperty().bind(isInvalid); okButton.disableProperty().bind(isInvalid);
if(requirement != PasswordRequirement.UPDATE_NEW) { if(requirement != PasswordRequirement.UPDATE_NEW && requirement != PasswordRequirement.UPDATE_CHANGE) {
passwordConfirm.setVisible(false); passwordConfirm.setVisible(false);
passwordConfirm.setManaged(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) -> { password.textProperty().addListener((observable, oldValue, newValue) -> {
if(newValue.isEmpty()) { if(newValue.isEmpty()) {
okButton.setText("No Password"); okButton.setText("No Password");
@ -100,11 +106,16 @@ public class WalletPasswordDialog extends Dialog<SecureString> {
return backupExisting.isSelected(); return backupExisting.isSelected();
} }
public boolean isChangePassword() {
return changePassword.isSelected();
}
public enum PasswordRequirement { public enum PasswordRequirement {
LOAD("Please enter the wallet password:", "Unlock"), LOAD("Please enter the wallet password:", "Unlock"),
UPDATE_NEW("Add a password to the wallet?\nLeave empty for none:", "No Password"), 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_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 description;
private final String okButtonText; private final String okButtonText;

View file

@ -84,6 +84,7 @@ public class Storage {
Wallet wallet = gson.fromJson(reader, Wallet.class); Wallet wallet = gson.fromJson(reader, Wallet.class);
reader.close(); reader.close();
encryptionPubKey = NO_PASSWORD_KEY;
return wallet; return wallet;
} }

View file

@ -176,7 +176,7 @@ public class SettingsController extends WalletFormController implements Initiali
apply.setOnAction(event -> { apply.setOnAction(event -> {
revert.setDisable(true); revert.setDisable(true);
apply.setDisable(true); apply.setDisable(true);
saveWallet(); saveWallet(false);
}); });
setFieldsFromWallet(walletForm.getWallet()); 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(); ECKey existingPubKey = walletForm.getStorage().getEncryptionPubKey();
WalletPasswordDialog.PasswordRequirement requirement; WalletPasswordDialog.PasswordRequirement requirement;
if(existingPubKey == null) { 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)) { } else if(Storage.NO_PASSWORD_KEY.equals(existingPubKey)) {
requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_EMPTY; requirement = WalletPasswordDialog.PasswordRequirement.UPDATE_EMPTY;
} else { } else {
@ -336,6 +340,12 @@ public class SettingsController extends WalletFormController implements Initiali
return; return;
} }
if(dlg.isChangePassword()) {
walletForm.getStorage().setEncryptionPubKey(null);
saveWallet(true);
return;
}
key = new Key(encryptionFullKey.getPrivKeyBytes(), walletForm.getStorage().getKeyDeriver().getSalt(), EncryptionType.Deriver.ARGON2); key = new Key(encryptionFullKey.getPrivKeyBytes(), walletForm.getStorage().getKeyDeriver().getSalt(), EncryptionType.Deriver.ARGON2);
walletForm.getWallet().encrypt(key); walletForm.getWallet().encrypt(key);