From bb2ec1882da9910c110d4aeaa465a9abfef3cd58 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 14 May 2020 13:33:55 +0200 Subject: [PATCH] invalidate if derivation matches another script type --- .../sparrow/control/WalletNameDialog.java | 1 + .../sparrow/control/WalletPasswordDialog.java | 1 + .../sparrow/event/SettingsChangedEvent.java | 14 ++++++-- .../sparrow/wallet/KeystoreController.java | 35 +++++++++++++++---- .../sparrow/wallet/SettingsController.java | 6 ++-- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/WalletNameDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/WalletNameDialog.java index f795b97c..195de0a9 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/WalletNameDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/WalletNameDialog.java @@ -56,6 +56,7 @@ public class WalletNameDialog extends Dialog { okButton.disableProperty().bind(isInvalid); name.setPromptText("Wallet Name"); + name.requestFocus(); setResultConverter(dialogButton -> dialogButton == okButtonType ? name.getText() : null); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java index 82db83e6..0d5ff1db 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java @@ -77,6 +77,7 @@ public class WalletPasswordDialog extends Dialog { } password.setPromptText("Password"); + password.requestFocus(); passwordConfirm.setPromptText("Password Confirmation"); setResultConverter(dialogButton -> dialogButton == okButtonType ? password.getText() : null); diff --git a/src/main/java/com/sparrowwallet/sparrow/event/SettingsChangedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/SettingsChangedEvent.java index 89b4a327..bf5c79b4 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/SettingsChangedEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/SettingsChangedEvent.java @@ -3,13 +3,23 @@ package com.sparrowwallet.sparrow.event; import com.sparrowwallet.drongo.wallet.Wallet; public class SettingsChangedEvent { - private Wallet wallet; + private final Wallet wallet; + private final Type type; - public SettingsChangedEvent(Wallet wallet) { + public SettingsChangedEvent(Wallet wallet, Type type) { this.wallet = wallet; + this.type = type; } public Wallet getWallet() { return wallet; } + + public Type getType() { + return type; + } + + public enum Type { + POLICY, SCRIPT_TYPE, MUTLISIG_THRESHOLD, MULTISIG_TOTAL, KEYSTORE_LABEL, KEYSTORE_FINGERPRINT, KEYSTORE_DERIVATION, KEYSTORE_XPUB; + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/KeystoreController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/KeystoreController.java index 03346972..5fd3fa76 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/KeystoreController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/KeystoreController.java @@ -1,8 +1,10 @@ package com.sparrowwallet.sparrow.wallet; +import com.google.common.eventbus.Subscribe; import com.sparrowwallet.drongo.ExtendedKey; import com.sparrowwallet.drongo.KeyDerivation; import com.sparrowwallet.drongo.Utils; +import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.wallet.Keystore; import com.sparrowwallet.drongo.wallet.KeystoreSource; import com.sparrowwallet.sparrow.EventManager; @@ -21,6 +23,7 @@ import org.controlsfx.validation.decoration.StyleClassValidationDecoration; import tornadofx.control.Form; import java.net.URL; +import java.util.Arrays; import java.util.Optional; import java.util.ResourceBundle; import java.util.stream.Collectors; @@ -50,7 +53,7 @@ public class KeystoreController extends WalletFormController implements Initiali @Override public void initialize(URL location, ResourceBundle resources) { - + EventManager.get().register(this); } public void setKeystore(WalletForm walletForm, Keystore keystore) { @@ -84,22 +87,22 @@ public class KeystoreController extends WalletFormController implements Initiali label.textProperty().addListener((observable, oldValue, newValue) -> { keystore.setLabel(newValue); - EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet())); + EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.KEYSTORE_LABEL)); }); fingerprint.textProperty().addListener((observable, oldValue, newValue) -> { keystore.setKeyDerivation(new KeyDerivation(newValue, keystore.getKeyDerivation().getDerivationPath())); - EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet())); + EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.KEYSTORE_FINGERPRINT)); }); derivation.textProperty().addListener((observable, oldValue, newValue) -> { - if(KeyDerivation.isValid(newValue)) { + if(KeyDerivation.isValid(newValue) && !matchesAnotherScriptType(newValue)) { keystore.setKeyDerivation(new KeyDerivation(keystore.getKeyDerivation().getMasterFingerprint(), newValue)); - EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet())); + EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.KEYSTORE_DERIVATION)); } }); xpub.textProperty().addListener((observable, oldValue, newValue) -> { if(ExtendedKey.isValid(newValue)) { keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(newValue)); - EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet())); + EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.KEYSTORE_XPUB)); } }); } @@ -136,7 +139,8 @@ public class KeystoreController extends WalletFormController implements Initiali validationSupport.registerValidator(derivation, Validator.combine( Validator.createEmptyValidator("Derivation is required"), - (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Derivation is invalid", !KeyDerivation.isValid(newValue)) + (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Derivation is invalid", !KeyDerivation.isValid(newValue)), + (Control c, String newValue) -> ValidationResult.fromErrorIf( c, "Derivation matches another script type", matchesAnotherScriptType(newValue)) )); validationSupport.registerValidator(fingerprint, Validator.combine( @@ -147,6 +151,14 @@ public class KeystoreController extends WalletFormController implements Initiali validationSupport.setValidationDecorator(new StyleClassValidationDecoration()); } + private boolean matchesAnotherScriptType(String derivationPath) { + if(walletForm.getWallet().getScriptType() != null && walletForm.getWallet().getScriptType().getAccount(derivationPath) > -1) { + return false; + } + + return Arrays.stream(ScriptType.values()).anyMatch(scriptType -> !scriptType.equals(walletForm.getWallet().getScriptType()) && scriptType.getAccount(derivationPath) > -1); + } + private void updateType() { type.setText(getTypeLabel(keystore)); @@ -196,4 +208,13 @@ public class KeystoreController extends WalletFormController implements Initiali xpub.setText(keystore.getExtendedPublicKey().toString()); } } + + @Subscribe + public void update(SettingsChangedEvent event) { + if(event.getType().equals(SettingsChangedEvent.Type.SCRIPT_TYPE) && !derivation.getText().isEmpty()) { + String derivationPath = derivation.getText(); + derivation.setText(""); + derivation.setText(derivationPath); + } + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java index 69d7e7fa..8ece1e06 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java @@ -105,14 +105,14 @@ public class SettingsController extends WalletFormController implements Initiali walletForm.getWallet().setScriptType(scriptType); } - EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet())); + EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.SCRIPT_TYPE)); }); multisigLowLabel.textProperty().bind(multisigControl.lowValueProperty().asString("%.0f") ); multisigHighLabel.textProperty().bind(multisigControl.highValueProperty().asString("%.0f")); multisigControl.lowValueProperty().addListener((observable, oldValue, threshold) -> { - EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet())); + EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.MUTLISIG_THRESHOLD)); }); multisigFieldset.managedProperty().bind(multisigFieldset.visibleProperty()); @@ -145,7 +145,7 @@ public class SettingsController extends WalletFormController implements Initiali } if(walletForm.getWallet().getPolicyType().equals(PolicyType.MULTI)) { - EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet())); + EventManager.get().post(new SettingsChangedEvent(walletForm.getWallet(), SettingsChangedEvent.Type.MULTISIG_TOTAL)); } });