Search in sources :

Example 6 with ChildNumber

use of com.sparrowwallet.drongo.crypto.ChildNumber in project sparrow by sparrowwallet.

the class XprvKeystoreImportPane method getDerivationEntry.

private Node getDerivationEntry(List<ChildNumber> derivation) {
    TextField derivationField = new TextField();
    derivationField.setPromptText("Derivation path");
    derivationField.setText(KeyDerivation.writePath(derivation));
    HBox.setHgrow(derivationField, Priority.ALWAYS);
    ValidationSupport validationSupport = new ValidationSupport();
    validationSupport.setValidationDecorator(new StyleClassValidationDecoration());
    validationSupport.registerValidator(derivationField, Validator.combine(Validator.createEmptyValidator("Derivation is required"), (Control c, String newValue) -> ValidationResult.fromErrorIf(c, "Invalid derivation", !KeyDerivation.isValid(newValue))));
    Button importDerivationButton = new Button("Import Custom Derivation Keystore");
    importDerivationButton.setDisable(true);
    importDerivationButton.setOnAction(event -> {
        showHideLink.setVisible(true);
        setExpanded(false);
        List<ChildNumber> importDerivation = KeyDerivation.parsePath(derivationField.getText());
        importKeystore(importDerivation);
    });
    derivationField.textProperty().addListener((observable, oldValue, newValue) -> {
        importButton.setDisable(newValue.isEmpty() || !KeyDerivation.isValid(newValue) || !KeyDerivation.parsePath(newValue).equals(derivation));
        importDerivationButton.setDisable(newValue.isEmpty() || !KeyDerivation.isValid(newValue) || KeyDerivation.parsePath(newValue).equals(derivation));
    });
    HBox contentBox = new HBox();
    contentBox.setAlignment(Pos.TOP_RIGHT);
    contentBox.setSpacing(20);
    contentBox.getChildren().add(derivationField);
    contentBox.getChildren().add(importDerivationButton);
    contentBox.setPadding(new Insets(10, 30, 10, 30));
    contentBox.setPrefHeight(60);
    return contentBox;
}
Also used : HBox(javafx.scene.layout.HBox) ValidationSupport(org.controlsfx.validation.ValidationSupport) Insets(javafx.geometry.Insets) StyleClassValidationDecoration(org.controlsfx.validation.decoration.StyleClassValidationDecoration) ChildNumber(com.sparrowwallet.drongo.crypto.ChildNumber)

Example 7 with ChildNumber

use of com.sparrowwallet.drongo.crypto.ChildNumber in project sparrow by sparrowwallet.

the class PayNymService method getSignature.

public String getSignature(Wallet wallet, String authToken) {
    Wallet masterWallet = wallet.isMasterWallet() ? wallet : wallet.getMasterWallet();
    Keystore keystore = masterWallet.getKeystores().get(0);
    List<ChildNumber> derivation = keystore.getKeyDerivation().getDerivation();
    ChildNumber derivationStart = derivation.isEmpty() ? ChildNumber.ZERO_HARDENED : derivation.get(derivation.size() - 1);
    ECKey notificationPrivKey = keystore.getBip47ExtendedPrivateKey().getKey(List.of(derivationStart, new ChildNumber(0)));
    return notificationPrivKey.signMessage(authToken, ScriptType.P2PKH);
}
Also used : Keystore(com.sparrowwallet.drongo.wallet.Keystore) Wallet(com.sparrowwallet.drongo.wallet.Wallet) ECKey(com.sparrowwallet.drongo.crypto.ECKey) ChildNumber(com.sparrowwallet.drongo.crypto.ChildNumber)

Example 8 with ChildNumber

use of com.sparrowwallet.drongo.crypto.ChildNumber in project sparrow by sparrowwallet.

the class MnemonicKeystoreImportPane method getDerivationEntry.

private Node getDerivationEntry(List<ChildNumber> derivation) {
    TextField derivationField = new TextField();
    derivationField.setPromptText("Derivation path");
    derivationField.setText(KeyDerivation.writePath(derivation));
    HBox.setHgrow(derivationField, Priority.ALWAYS);
    ValidationSupport validationSupport = new ValidationSupport();
    validationSupport.setValidationDecorator(new StyleClassValidationDecoration());
    validationSupport.registerValidator(derivationField, Validator.combine(Validator.createEmptyValidator("Derivation is required"), (Control c, String newValue) -> ValidationResult.fromErrorIf(c, "Invalid derivation", !KeyDerivation.isValid(newValue))));
    Button importDerivationButton = new Button("Import Custom Derivation Keystore");
    importDerivationButton.setDisable(true);
    importDerivationButton.setOnAction(event -> {
        showHideLink.setVisible(true);
        setExpanded(false);
        List<ChildNumber> importDerivation = KeyDerivation.parsePath(derivationField.getText());
        importKeystore(importDerivation, false);
    });
    derivationField.textProperty().addListener((observable, oldValue, newValue) -> {
        importButton.setDisable(newValue.isEmpty() || !KeyDerivation.isValid(newValue) || !KeyDerivation.parsePath(newValue).equals(derivation));
        importDerivationButton.setDisable(newValue.isEmpty() || !KeyDerivation.isValid(newValue) || KeyDerivation.parsePath(newValue).equals(derivation));
    });
    HBox contentBox = new HBox();
    contentBox.setAlignment(Pos.TOP_RIGHT);
    contentBox.setSpacing(20);
    contentBox.getChildren().add(derivationField);
    contentBox.getChildren().add(importDerivationButton);
    contentBox.setPadding(new Insets(10, 30, 10, 30));
    contentBox.setPrefHeight(60);
    return contentBox;
}
Also used : HBox(javafx.scene.layout.HBox) ValidationSupport(org.controlsfx.validation.ValidationSupport) Insets(javafx.geometry.Insets) StyleClassValidationDecoration(org.controlsfx.validation.decoration.StyleClassValidationDecoration) ChildNumber(com.sparrowwallet.drongo.crypto.ChildNumber)

Example 9 with ChildNumber

use of com.sparrowwallet.drongo.crypto.ChildNumber in project sparrow by sparrowwallet.

the class MnemonicWalletKeystoreImportPane method discoverWallet.

private void discoverWallet() {
    discoverButton.setDisable(true);
    discoverButton.setMaxHeight(discoverButton.getHeight());
    ProgressIndicator progressIndicator = new ProgressIndicator(0);
    progressIndicator.getStyleClass().add("button-progress");
    discoverButton.setGraphic(progressIndicator);
    List<Wallet> wallets = new ArrayList<>();
    List<List<ChildNumber>> derivations = ScriptType.getScriptTypesForPolicyType(PolicyType.SINGLE).stream().map(ScriptType::getDefaultDerivation).collect(Collectors.toList());
    derivations.add(List.of(new ChildNumber(0, true)));
    for (ScriptType scriptType : ScriptType.getScriptTypesForPolicyType(PolicyType.SINGLE)) {
        for (List<ChildNumber> derivation : derivations) {
            try {
                Wallet wallet = getWallet(scriptType, derivation);
                wallets.add(wallet);
            } catch (ImportException e) {
                String errorMessage = e.getMessage();
                if (e.getCause() instanceof MnemonicException.MnemonicChecksumException) {
                    errorMessage = "Invalid word list - checksum incorrect";
                } else if (e.getCause() != null && e.getCause().getMessage() != null && !e.getCause().getMessage().isEmpty()) {
                    errorMessage = e.getCause().getMessage();
                }
                setError("Import Error", errorMessage + ".");
                discoverButton.setDisable(!AppServices.isConnected());
            }
        }
    }
    ElectrumServer.WalletDiscoveryService walletDiscoveryService = new ElectrumServer.WalletDiscoveryService(wallets);
    progressIndicator.progressProperty().bind(walletDiscoveryService.progressProperty());
    walletDiscoveryService.setOnSucceeded(successEvent -> {
        discoverButton.setGraphic(null);
        Optional<Wallet> optWallet = walletDiscoveryService.getValue();
        if (optWallet.isPresent()) {
            EventManager.get().post(new WalletImportEvent(optWallet.get()));
        } else {
            discoverButton.setDisable(false);
            Optional<ButtonType> optButtonType = AppServices.showErrorDialog("No existing wallet found", "Could not find a wallet with existing transactions using this mnemonic. Import this wallet anyway?", ButtonType.NO, ButtonType.YES);
            if (optButtonType.isPresent() && optButtonType.get() == ButtonType.YES) {
                setContent(getScriptTypeEntry());
                setExpanded(true);
            }
        }
    });
    walletDiscoveryService.setOnFailed(failedEvent -> {
        discoverButton.setGraphic(null);
        log.error("Failed to discover wallets", failedEvent.getSource().getException());
        setError("Failed to discover wallets", failedEvent.getSource().getException().getMessage());
    });
    walletDiscoveryService.start();
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) WalletImportEvent(com.sparrowwallet.sparrow.event.WalletImportEvent) Wallet(com.sparrowwallet.drongo.wallet.Wallet) ArrayList(java.util.ArrayList) ChildNumber(com.sparrowwallet.drongo.crypto.ChildNumber) ImportException(com.sparrowwallet.sparrow.io.ImportException) MnemonicException(com.sparrowwallet.drongo.wallet.MnemonicException) ElectrumServer(com.sparrowwallet.sparrow.net.ElectrumServer) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with ChildNumber

use of com.sparrowwallet.drongo.crypto.ChildNumber in project drongo by sparrowwallet.

the class Wallet method addChildWallet.

public Wallet addChildWallet(PaymentCode externalPaymentCode, ScriptType childScriptType, String label) {
    if (policyType != PolicyType.SINGLE) {
        throw new IllegalStateException("Cannot add payment code wallet to " + policyType.getName() + " wallet");
    }
    if (scriptType != P2PKH && scriptType != P2SH_P2WPKH && scriptType != P2WPKH) {
        throw new IllegalStateException("Cannot add payment code wallet to " + scriptType.getName() + " wallet");
    }
    Keystore masterKeystore = getKeystores().get(0);
    if (masterKeystore.getBip47ExtendedPrivateKey() == null) {
        throw new IllegalStateException("Cannot add payment code wallet, BIP47 extended private key not present");
    }
    Wallet childWallet = new Wallet(childScriptType + "-" + externalPaymentCode.toString());
    childWallet.setLabel(label);
    childWallet.setPolicyType(PolicyType.SINGLE);
    childWallet.setScriptType(childScriptType);
    childWallet.setGapLimit(5);
    Keystore keystore = new Keystore("BIP47");
    keystore.setSource(KeystoreSource.SW_PAYMENT_CODE);
    keystore.setWalletModel(WalletModel.SPARROW);
    List<ChildNumber> derivation = KeyDerivation.getBip47Derivation(getAccountIndex());
    keystore.setKeyDerivation(new KeyDerivation(masterKeystore.getKeyDerivation().getMasterFingerprint(), derivation));
    keystore.setExternalPaymentCode(externalPaymentCode);
    keystore.setBip47ExtendedPrivateKey(masterKeystore.getBip47ExtendedPrivateKey());
    DeterministicKey pubKey = keystore.getBip47ExtendedPrivateKey().getKey().dropPrivateBytes().dropParent();
    keystore.setExtendedPublicKey(new ExtendedKey(pubKey, keystore.getBip47ExtendedPrivateKey().getParentFingerprint(), derivation.get(derivation.size() - 1)));
    childWallet.getKeystores().add(keystore);
    childWallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE, scriptType, childWallet.getKeystores(), 1));
    childWallet.setMasterWallet(this);
    getChildWallets().add(childWallet);
    return childWallet;
}
Also used : ChildNumber(com.sparrowwallet.drongo.crypto.ChildNumber) DeterministicKey(com.sparrowwallet.drongo.crypto.DeterministicKey)

Aggregations

ChildNumber (com.sparrowwallet.drongo.crypto.ChildNumber)17 ECKey (com.sparrowwallet.drongo.crypto.ECKey)4 Test (org.junit.Test)4 DeterministicKey (com.sparrowwallet.drongo.crypto.DeterministicKey)3 ArrayList (java.util.ArrayList)3 Wallet (com.sparrowwallet.drongo.wallet.Wallet)2 Insets (javafx.geometry.Insets)2 HBox (javafx.scene.layout.HBox)2 ValidationSupport (org.controlsfx.validation.ValidationSupport)2 StyleClassValidationDecoration (org.controlsfx.validation.decoration.StyleClassValidationDecoration)2 ExtendedKey (com.sparrowwallet.drongo.ExtendedKey)1 KeyDerivation (com.sparrowwallet.drongo.KeyDerivation)1 Address (com.sparrowwallet.drongo.address.Address)1 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)1 Keystore (com.sparrowwallet.drongo.wallet.Keystore)1 MnemonicException (com.sparrowwallet.drongo.wallet.MnemonicException)1 WalletImportEvent (com.sparrowwallet.sparrow.event.WalletImportEvent)1 ImportException (com.sparrowwallet.sparrow.io.ImportException)1 ElectrumServer (com.sparrowwallet.sparrow.net.ElectrumServer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1