mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2024-11-04 21:36:45 +00:00
invalidate if derivation matches another script type
This commit is contained in:
parent
02258dea8d
commit
bb2ec1882d
5 changed files with 45 additions and 12 deletions
|
@ -56,6 +56,7 @@ public class WalletNameDialog extends Dialog<String> {
|
||||||
okButton.disableProperty().bind(isInvalid);
|
okButton.disableProperty().bind(isInvalid);
|
||||||
|
|
||||||
name.setPromptText("Wallet Name");
|
name.setPromptText("Wallet Name");
|
||||||
|
name.requestFocus();
|
||||||
setResultConverter(dialogButton -> dialogButton == okButtonType ? name.getText() : null);
|
setResultConverter(dialogButton -> dialogButton == okButtonType ? name.getText() : null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,6 +77,7 @@ public class WalletPasswordDialog extends Dialog<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
password.setPromptText("Password");
|
password.setPromptText("Password");
|
||||||
|
password.requestFocus();
|
||||||
passwordConfirm.setPromptText("Password Confirmation");
|
passwordConfirm.setPromptText("Password Confirmation");
|
||||||
|
|
||||||
setResultConverter(dialogButton -> dialogButton == okButtonType ? password.getText() : null);
|
setResultConverter(dialogButton -> dialogButton == okButtonType ? password.getText() : null);
|
||||||
|
|
|
@ -3,13 +3,23 @@ package com.sparrowwallet.sparrow.event;
|
||||||
import com.sparrowwallet.drongo.wallet.Wallet;
|
import com.sparrowwallet.drongo.wallet.Wallet;
|
||||||
|
|
||||||
public class SettingsChangedEvent {
|
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.wallet = wallet;
|
||||||
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Wallet getWallet() {
|
public Wallet getWallet() {
|
||||||
return wallet;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package com.sparrowwallet.sparrow.wallet;
|
package com.sparrowwallet.sparrow.wallet;
|
||||||
|
|
||||||
|
import com.google.common.eventbus.Subscribe;
|
||||||
import com.sparrowwallet.drongo.ExtendedKey;
|
import com.sparrowwallet.drongo.ExtendedKey;
|
||||||
import com.sparrowwallet.drongo.KeyDerivation;
|
import com.sparrowwallet.drongo.KeyDerivation;
|
||||||
import com.sparrowwallet.drongo.Utils;
|
import com.sparrowwallet.drongo.Utils;
|
||||||
|
import com.sparrowwallet.drongo.protocol.ScriptType;
|
||||||
import com.sparrowwallet.drongo.wallet.Keystore;
|
import com.sparrowwallet.drongo.wallet.Keystore;
|
||||||
import com.sparrowwallet.drongo.wallet.KeystoreSource;
|
import com.sparrowwallet.drongo.wallet.KeystoreSource;
|
||||||
import com.sparrowwallet.sparrow.EventManager;
|
import com.sparrowwallet.sparrow.EventManager;
|
||||||
|
@ -21,6 +23,7 @@ import org.controlsfx.validation.decoration.StyleClassValidationDecoration;
|
||||||
import tornadofx.control.Form;
|
import tornadofx.control.Form;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
@ -50,7 +53,7 @@ public class KeystoreController extends WalletFormController implements Initiali
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(URL location, ResourceBundle resources) {
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
|
EventManager.get().register(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setKeystore(WalletForm walletForm, Keystore keystore) {
|
public void setKeystore(WalletForm walletForm, Keystore keystore) {
|
||||||
|
@ -84,22 +87,22 @@ public class KeystoreController extends WalletFormController implements Initiali
|
||||||
|
|
||||||
label.textProperty().addListener((observable, oldValue, newValue) -> {
|
label.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
keystore.setLabel(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) -> {
|
fingerprint.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
keystore.setKeyDerivation(new KeyDerivation(newValue, keystore.getKeyDerivation().getDerivationPath()));
|
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) -> {
|
derivation.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
if(KeyDerivation.isValid(newValue)) {
|
if(KeyDerivation.isValid(newValue) && !matchesAnotherScriptType(newValue)) {
|
||||||
keystore.setKeyDerivation(new KeyDerivation(keystore.getKeyDerivation().getMasterFingerprint(), 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) -> {
|
xpub.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
if(ExtendedKey.isValid(newValue)) {
|
if(ExtendedKey.isValid(newValue)) {
|
||||||
keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(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(
|
validationSupport.registerValidator(derivation, Validator.combine(
|
||||||
Validator.createEmptyValidator("Derivation is required"),
|
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(
|
validationSupport.registerValidator(fingerprint, Validator.combine(
|
||||||
|
@ -147,6 +151,14 @@ public class KeystoreController extends WalletFormController implements Initiali
|
||||||
validationSupport.setValidationDecorator(new StyleClassValidationDecoration());
|
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() {
|
private void updateType() {
|
||||||
type.setText(getTypeLabel(keystore));
|
type.setText(getTypeLabel(keystore));
|
||||||
|
|
||||||
|
@ -196,4 +208,13 @@ public class KeystoreController extends WalletFormController implements Initiali
|
||||||
xpub.setText(keystore.getExtendedPublicKey().toString());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,14 +105,14 @@ public class SettingsController extends WalletFormController implements Initiali
|
||||||
walletForm.getWallet().setScriptType(scriptType);
|
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") );
|
multisigLowLabel.textProperty().bind(multisigControl.lowValueProperty().asString("%.0f") );
|
||||||
multisigHighLabel.textProperty().bind(multisigControl.highValueProperty().asString("%.0f"));
|
multisigHighLabel.textProperty().bind(multisigControl.highValueProperty().asString("%.0f"));
|
||||||
|
|
||||||
multisigControl.lowValueProperty().addListener((observable, oldValue, threshold) -> {
|
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());
|
multisigFieldset.managedProperty().bind(multisigFieldset.visibleProperty());
|
||||||
|
@ -145,7 +145,7 @@ public class SettingsController extends WalletFormController implements Initiali
|
||||||
}
|
}
|
||||||
|
|
||||||
if(walletForm.getWallet().getPolicyType().equals(PolicyType.MULTI)) {
|
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));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue