suggest clearing any existing keystores when script type is changed in settings

This commit is contained in:
Craig Raw 2023-08-17 15:35:33 +02:00
parent 407dde2703
commit fb981f1548

View file

@ -40,6 +40,7 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.sparrowwallet.sparrow.AppServices.showErrorDialog; import static com.sparrowwallet.sparrow.AppServices.showErrorDialog;
import static com.sparrowwallet.sparrow.AppServices.showWarningDialog;
public class SettingsController extends WalletFormController implements Initializable { public class SettingsController extends WalletFormController implements Initializable {
private static final Logger log = LoggerFactory.getLogger(SettingsController.class); private static final Logger log = LoggerFactory.getLogger(SettingsController.class);
@ -97,6 +98,7 @@ public class SettingsController extends WalletFormController implements Initiali
private final SimpleIntegerProperty totalKeystores = new SimpleIntegerProperty(0); private final SimpleIntegerProperty totalKeystores = new SimpleIntegerProperty(0);
private boolean initialising = true; private boolean initialising = true;
private boolean reverting;
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
@ -141,9 +143,32 @@ public class SettingsController extends WalletFormController implements Initiali
} }
}); });
scriptType.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, scriptType) -> { scriptType.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if(scriptType != null) { if(newValue != null) {
walletForm.getWallet().setScriptType(scriptType); if(oldValue != null && !reverting && walletForm.getWallet().getKeystores().stream().anyMatch(keystore -> keystore.getExtendedPublicKey() != null)) {
Optional<ButtonType> optType = showWarningDialog("Clear keystores?",
"You are changing the script type on a wallet with existing key information. Usually this means the keys need to be re-imported using a different derivation path.\n\n" +
"Do you want to clear the current key information?", ButtonType.YES, ButtonType.NO, ButtonType.CANCEL);
if(optType.isPresent()) {
if(optType.get() == ButtonType.CANCEL) {
reverting = true;
Platform.runLater(() -> {
scriptType.getSelectionModel().select(oldValue);
reverting = false;
});
return;
} else if(optType.get() == ButtonType.YES) {
clearKeystoreTabs();
if(walletForm.getWallet().getPolicyType() == PolicyType.MULTI) {
totalKeystores.bind(multisigControl.highValueProperty());
} else {
totalKeystores.set(1);
}
}
}
}
walletForm.getWallet().setScriptType(newValue);
} }
EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.SCRIPT_TYPE)); EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.SCRIPT_TYPE));
@ -215,7 +240,10 @@ public class SettingsController extends WalletFormController implements Initiali
totalKeystores.setValue(0); totalKeystores.setValue(0);
walletForm.revert(); walletForm.revert();
initialising = true; initialising = true;
reverting = true;
setFieldsFromWallet(walletForm.getWallet()); setFieldsFromWallet(walletForm.getWallet());
reverting = false;
initialising = false;
}); });
apply.setOnAction(event -> { apply.setOnAction(event -> {