Search in sources :

Example 6 with ScriptType

use of com.sparrowwallet.drongo.protocol.ScriptType in project sparrow by sparrowwallet.

the class SendController method addChildWallets.

public List<Wallet> addChildWallets(PaymentCode externalPaymentCode, PayNym payNym) {
    List<Wallet> addedWallets = new ArrayList<>();
    Wallet masterWallet = getWalletForm().getMasterWallet();
    Storage storage = AppServices.get().getOpenWallets().get(masterWallet);
    List<ScriptType> scriptTypes = PayNym.getSegwitScriptTypes();
    for (ScriptType childScriptType : scriptTypes) {
        String label = (payNym == null ? externalPaymentCode.toAbbreviatedString() : payNym.nymName()) + " " + childScriptType.getName();
        Wallet addedWallet = masterWallet.addChildWallet(externalPaymentCode, childScriptType, label);
        if (!storage.isPersisted(addedWallet)) {
            try {
                storage.saveWallet(addedWallet);
            } catch (Exception e) {
                log.error("Error saving wallet", e);
                AppServices.showErrorDialog("Error saving wallet " + addedWallet.getName(), e.getMessage());
            }
        }
        addedWallets.add(addedWallet);
    }
    return addedWallets;
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) Storage(com.sparrowwallet.sparrow.io.Storage) SecureString(com.sparrowwallet.drongo.SecureString) InvalidAddressException(com.sparrowwallet.drongo.address.InvalidAddressException) IOException(java.io.IOException)

Example 7 with ScriptType

use of com.sparrowwallet.drongo.protocol.ScriptType 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 8 with ScriptType

use of com.sparrowwallet.drongo.protocol.ScriptType in project sparrow by sparrowwallet.

the class MessageSignDialog method verifyMessage.

private boolean verifyMessage(ECKey signedMessageKey) throws InvalidAddressException, SignatureException {
    Address providedAddress = getAddress();
    ScriptType scriptType = providedAddress.getScriptType();
    if (scriptType == ScriptType.P2SH) {
        scriptType = ScriptType.P2SH_P2WPKH;
    }
    if (!ScriptType.getScriptTypesForPolicyType(PolicyType.SINGLE).contains(scriptType)) {
        throw new IllegalArgumentException("Only single signature P2PKH, P2SH-P2WPKH or P2WPKH addresses can verify messages.");
    }
    Address signedMessageAddress = scriptType.getAddress(signedMessageKey);
    return providedAddress.equals(signedMessageAddress);
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) Address(com.sparrowwallet.drongo.address.Address)

Example 9 with ScriptType

use of com.sparrowwallet.drongo.protocol.ScriptType in project sparrow by sparrowwallet.

the class FileWalletKeystoreImportPane method getScriptTypeEntry.

private Node getScriptTypeEntry(List<ScriptType> scriptTypes) {
    Label label = new Label("Script Type:");
    HBox fieldBox = new HBox(5);
    fieldBox.setAlignment(Pos.CENTER_RIGHT);
    ComboBox<ScriptType> scriptTypeComboBox = new ComboBox<>(FXCollections.observableArrayList(scriptTypes));
    if (scriptTypes.contains(ScriptType.P2WPKH)) {
        scriptTypeComboBox.setValue(ScriptType.P2WPKH);
    }
    scriptTypeComboBox.setConverter(new StringConverter<>() {

        @Override
        public String toString(ScriptType scriptType) {
            return scriptType == null ? "" : scriptType.getDescription();
        }

        @Override
        public ScriptType fromString(String string) {
            return null;
        }
    });
    scriptTypeComboBox.setMaxWidth(170);
    HelpLabel helpLabel = new HelpLabel();
    helpLabel.setHelpText("P2WPKH is a Native Segwit type and is usually the best choice for new wallets.\nP2SH-P2WPKH is a Wrapped Segwit type and is a reasonable choice for the widest compatibility.\nP2PKH is a Legacy type and should be avoided for new wallets.\nFor existing wallets, be sure to choose the type that matches the wallet you are importing.");
    fieldBox.getChildren().addAll(scriptTypeComboBox, helpLabel);
    Region region = new Region();
    HBox.setHgrow(region, Priority.SOMETIMES);
    Button importFileButton = new Button("Import");
    importFileButton.setOnAction(event -> {
        showHideLink.setVisible(true);
        setExpanded(false);
        try {
            importWallet(scriptTypeComboBox.getValue());
        } catch (ImportException e) {
            log.error("Error importing file", e);
            String errorMessage = e.getMessage();
            if (e.getCause() instanceof JsonParseException) {
                errorMessage = "File was not in JSON format";
            } else if (e.getCause() != null && e.getCause().getMessage() != null && !e.getCause().getMessage().isEmpty()) {
                errorMessage = e.getCause().getMessage();
            }
            setError("Import Error", errorMessage);
            importButton.setDisable(false);
        }
    });
    HBox contentBox = new HBox();
    contentBox.setAlignment(Pos.CENTER_RIGHT);
    contentBox.setSpacing(20);
    contentBox.getChildren().addAll(label, fieldBox, region, importFileButton);
    contentBox.setPadding(new Insets(10, 30, 10, 30));
    contentBox.setPrefHeight(60);
    return contentBox;
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) HBox(javafx.scene.layout.HBox) Insets(javafx.geometry.Insets) ComboBox(javafx.scene.control.ComboBox) Label(javafx.scene.control.Label) JsonParseException(com.google.gson.JsonParseException) ImportException(com.sparrowwallet.sparrow.io.ImportException) Button(javafx.scene.control.Button) Region(javafx.scene.layout.Region)

Example 10 with ScriptType

use of com.sparrowwallet.drongo.protocol.ScriptType in project sparrow by sparrowwallet.

the class FileWalletKeystoreImportPane method importFile.

protected void importFile(String fileName, InputStream inputStream, String password) throws ImportException {
    this.fileName = fileName;
    List<ScriptType> scriptTypes = ScriptType.getAddressableScriptTypes(PolicyType.SINGLE);
    if (wallets != null && !wallets.isEmpty()) {
        if (wallets.size() == 1 && scriptTypes.contains(wallets.get(0).getScriptType())) {
            Wallet wallet = wallets.get(0);
            wallet.setPolicyType(PolicyType.SINGLE);
            wallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE, wallet.getScriptType(), wallet.getKeystores(), null));
            wallet.setName(importer.getName());
            EventManager.get().post(new WalletImportEvent(wallets.get(0)));
        } else {
            scriptTypes.retainAll(wallets.stream().map(Wallet::getScriptType).collect(Collectors.toList()));
            if (scriptTypes.isEmpty()) {
                throw new ImportException("No singlesig script types present in QR code");
            }
        }
    } else {
        try {
            fileBytes = ByteStreams.toByteArray(inputStream);
        } catch (IOException e) {
            throw new ImportException("Could not read file", e);
        }
    }
    setContent(getScriptTypeEntry(scriptTypes));
    setExpanded(true);
    importButton.setDisable(true);
}
Also used : ImportException(com.sparrowwallet.sparrow.io.ImportException) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) WalletImportEvent(com.sparrowwallet.sparrow.event.WalletImportEvent) Wallet(com.sparrowwallet.drongo.wallet.Wallet) IOException(java.io.IOException)

Aggregations

ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)18 Wallet (com.sparrowwallet.drongo.wallet.Wallet)6 Keystore (com.sparrowwallet.drongo.wallet.Keystore)5 KeyDerivation (com.sparrowwallet.drongo.KeyDerivation)4 ImportException (com.sparrowwallet.sparrow.io.ImportException)4 PolicyType (com.sparrowwallet.drongo.policy.PolicyType)3 WalletImportEvent (com.sparrowwallet.sparrow.event.WalletImportEvent)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 SecureString (com.sparrowwallet.drongo.SecureString)2 Address (com.sparrowwallet.drongo.address.Address)2 InvalidAddressException (com.sparrowwallet.drongo.address.InvalidAddressException)2 ECKey (com.sparrowwallet.drongo.crypto.ECKey)2 InputStreamReader (java.io.InputStreamReader)2 List (java.util.List)2 Subscribe (com.google.common.eventbus.Subscribe)1 HostAndPort (com.google.common.net.HostAndPort)1 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonParseException (com.google.gson.JsonParseException)1