invalidate if derivation matches another script type

This commit is contained in:
Craig Raw 2020-05-14 13:33:55 +02:00
parent 02258dea8d
commit bb2ec1882d
5 changed files with 45 additions and 12 deletions

View file

@ -56,6 +56,7 @@ public class WalletNameDialog extends Dialog<String> {
okButton.disableProperty().bind(isInvalid);
name.setPromptText("Wallet Name");
name.requestFocus();
setResultConverter(dialogButton -> dialogButton == okButtonType ? name.getText() : null);
}
}

View file

@ -77,6 +77,7 @@ public class WalletPasswordDialog extends Dialog<String> {
}
password.setPromptText("Password");
password.requestFocus();
passwordConfirm.setPromptText("Password Confirmation");
setResultConverter(dialogButton -> dialogButton == okButtonType ? password.getText() : null);

View file

@ -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;
}
}

View file

@ -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);
}
}
}

View file

@ -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));
}
});