Search in sources :

Example 1 with WalletImportEvent

use of com.sparrowwallet.sparrow.event.WalletImportEvent in project sparrow by sparrowwallet.

the class MnemonicWalletKeystoreImportPane method getScriptTypeEntry.

private Node getScriptTypeEntry() {
    Label label = new Label("Script Type:");
    HBox fieldBox = new HBox(5);
    fieldBox.setAlignment(Pos.CENTER_RIGHT);
    ComboBox<ScriptType> scriptTypeComboBox = new ComboBox<>(FXCollections.observableArrayList(ScriptType.getAddressableScriptTypes(PolicyType.SINGLE)));
    if (scriptTypeComboBox.getItems().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 importMnemonicButton = new Button("Import");
    importMnemonicButton.setOnAction(event -> {
        showHideLink.setVisible(true);
        setExpanded(false);
        try {
            ScriptType scriptType = scriptTypeComboBox.getValue();
            Wallet wallet = getWallet(scriptType, scriptType.getDefaultDerivation());
            EventManager.get().post(new WalletImportEvent(wallet));
        } catch (ImportException e) {
            log.error("Error importing mnemonic", e);
            String errorMessage = e.getMessage();
            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, importMnemonicButton);
    contentBox.setPadding(new Insets(10, 30, 10, 30));
    contentBox.setPrefHeight(60);
    return contentBox;
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) WalletImportEvent(com.sparrowwallet.sparrow.event.WalletImportEvent) HBox(javafx.scene.layout.HBox) Insets(javafx.geometry.Insets) Wallet(com.sparrowwallet.drongo.wallet.Wallet) ImportException(com.sparrowwallet.sparrow.io.ImportException) Region(javafx.scene.layout.Region)

Example 2 with WalletImportEvent

use of com.sparrowwallet.sparrow.event.WalletImportEvent in project sparrow by sparrowwallet.

the class FileWalletImportPane method importFile.

@Override
protected void importFile(String fileName, InputStream inputStream, String password) throws ImportException {
    Wallet wallet = importer.importWallet(inputStream, password);
    if (wallet.getName() == null) {
        wallet.setName(fileName);
    }
    EventManager.get().post(new WalletImportEvent(wallet));
}
Also used : WalletImportEvent(com.sparrowwallet.sparrow.event.WalletImportEvent) Wallet(com.sparrowwallet.drongo.wallet.Wallet)

Example 3 with WalletImportEvent

use of com.sparrowwallet.sparrow.event.WalletImportEvent 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 4 with WalletImportEvent

use of com.sparrowwallet.sparrow.event.WalletImportEvent in project sparrow by sparrowwallet.

the class FileWalletKeystoreImportPane method importWallet.

private void importWallet(ScriptType scriptType) throws ImportException {
    if (wallets != null && !wallets.isEmpty()) {
        Wallet wallet = wallets.stream().filter(wallet1 -> wallet1.getScriptType() == scriptType).findFirst().orElseThrow(ImportException::new);
        wallet.setName(importer.getName());
        wallet.setPolicyType(PolicyType.SINGLE);
        wallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE, wallet.getScriptType(), wallet.getKeystores(), null));
        EventManager.get().post(new WalletImportEvent(wallet));
    } else {
        ByteArrayInputStream bais = new ByteArrayInputStream(fileBytes);
        Keystore keystore = importer.getKeystore(scriptType, bais, "");
        Wallet wallet = new Wallet();
        wallet.setName(Files.getNameWithoutExtension(fileName));
        wallet.setPolicyType(PolicyType.SINGLE);
        wallet.setScriptType(scriptType);
        wallet.getKeystores().add(keystore);
        wallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE, scriptType, wallet.getKeystores(), null));
        EventManager.get().post(new WalletImportEvent(wallet));
    }
}
Also used : ImportException(com.sparrowwallet.sparrow.io.ImportException) Keystore(com.sparrowwallet.drongo.wallet.Keystore) WalletImportEvent(com.sparrowwallet.sparrow.event.WalletImportEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) Wallet(com.sparrowwallet.drongo.wallet.Wallet)

Example 5 with WalletImportEvent

use of com.sparrowwallet.sparrow.event.WalletImportEvent 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

Wallet (com.sparrowwallet.drongo.wallet.Wallet)5 WalletImportEvent (com.sparrowwallet.sparrow.event.WalletImportEvent)5 ImportException (com.sparrowwallet.sparrow.io.ImportException)4 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)3 ChildNumber (com.sparrowwallet.drongo.crypto.ChildNumber)1 Keystore (com.sparrowwallet.drongo.wallet.Keystore)1 MnemonicException (com.sparrowwallet.drongo.wallet.MnemonicException)1 ElectrumServer (com.sparrowwallet.sparrow.net.ElectrumServer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Insets (javafx.geometry.Insets)1 HBox (javafx.scene.layout.HBox)1 Region (javafx.scene.layout.Region)1