Search in sources :

Example 1 with ScriptType

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

the class AppServices method selectWallet.

private static Wallet selectWallet(ScriptType scriptType, String actionDescription) {
    Wallet wallet = null;
    List<Wallet> wallets = get().getOpenWallets().keySet().stream().filter(w -> scriptType == null || w.getScriptType() == scriptType).collect(Collectors.toList());
    if (wallets.isEmpty()) {
        showErrorDialog("No wallet available", "Open a" + (scriptType == null ? "" : " " + scriptType.getDescription()) + " wallet to " + actionDescription + ".");
    } else if (wallets.size() == 1) {
        wallet = wallets.iterator().next();
    } else {
        ChoiceDialog<Wallet> walletChoiceDialog = new ChoiceDialog<>(wallets.iterator().next(), wallets);
        walletChoiceDialog.setTitle("Choose Wallet");
        walletChoiceDialog.setHeaderText("Choose a wallet to " + actionDescription);
        Image image = new Image("/image/sparrow-small.png");
        walletChoiceDialog.getDialogPane().setGraphic(new ImageView(image));
        setStageIcon(walletChoiceDialog.getDialogPane().getScene().getWindow());
        moveToActiveWindowScreen(walletChoiceDialog);
        Optional<Wallet> optWallet = walletChoiceDialog.showAndWait();
        if (optWallet.isPresent()) {
            wallet = optWallet.get();
        }
    }
    return wallet;
}
Also used : Wallet(com.sparrowwallet.drongo.wallet.Wallet) javafx.scene.control(javafx.scene.control) LoggerFactory(org.slf4j.LoggerFactory) Parent(javafx.scene.Parent) Task(javafx.concurrent.Task) java.net(java.net) Matcher(java.util.regex.Matcher) SorobanServices(com.sparrowwallet.sparrow.soroban.SorobanServices) TrayManager(com.sparrowwallet.sparrow.control.TrayManager) com.sparrowwallet.sparrow.net(com.sparrowwallet.sparrow.net) ScheduledService(javafx.concurrent.ScheduledService) Font(javafx.scene.text.Font) Screen(javafx.stage.Screen) KeyEvent(java.awt.event.KeyEvent) Tor(org.berndpruenster.netlayer.tor.Tor) Collectors(java.util.stream.Collectors) KeystoreSource(com.sparrowwallet.drongo.wallet.KeystoreSource) ZoneId(java.time.ZoneId) BitcoinURI(com.sparrowwallet.drongo.uri.BitcoinURI) Platform(javafx.application.Platform) BooleanProperty(javafx.beans.property.BooleanProperty) Duration(javafx.util.Duration) List(java.util.List) Network(com.sparrowwallet.drongo.Network) Pattern(java.util.regex.Pattern) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) Address(com.sparrowwallet.drongo.address.Address) BlockHeader(com.sparrowwallet.drongo.protocol.BlockHeader) Worker(javafx.concurrent.Worker) Scene(javafx.scene.Scene) java.util(java.util) LocalDateTime(java.time.LocalDateTime) TextUtils(com.sparrowwallet.sparrow.control.TextUtils) FXMLLoader(javafx.fxml.FXMLLoader) HyperlinkLabel(org.controlsfx.control.HyperlinkLabel) Subscribe(com.google.common.eventbus.Subscribe) Transaction(com.sparrowwallet.drongo.protocol.Transaction) KeyCode(javafx.scene.input.KeyCode) PayNymService(com.sparrowwallet.sparrow.paynym.PayNymService) Dialog(javafx.scene.control.Dialog) Label(javafx.scene.control.Label) Logger(org.slf4j.Logger) PSBT(com.sparrowwallet.drongo.psbt.PSBT) BlockTransactionHashIndex(com.sparrowwallet.drongo.wallet.BlockTransactionHashIndex) OpenFilesHandler(java.awt.desktop.OpenFilesHandler) Node(javafx.scene.Node) com.sparrowwallet.sparrow.event(com.sparrowwallet.sparrow.event) IOException(java.io.IOException) WhirlpoolServices(com.sparrowwallet.sparrow.whirlpool.WhirlpoolServices) HostAndPort(com.google.common.net.HostAndPort) File(java.io.File) OpenURIHandler(java.awt.desktop.OpenURIHandler) java.awt(java.awt) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) ChronoUnit(java.time.temporal.ChronoUnit) Stage(javafx.stage.Stage) com.sparrowwallet.sparrow.io(com.sparrowwallet.sparrow.io) ImageView(javafx.scene.image.ImageView) ObservableValue(javafx.beans.value.ObservableValue) Window(javafx.stage.Window) WalletTransaction(com.sparrowwallet.drongo.wallet.WalletTransaction) ChangeListener(javafx.beans.value.ChangeListener) Image(javafx.scene.image.Image) Wallet(com.sparrowwallet.drongo.wallet.Wallet) ImageView(javafx.scene.image.ImageView) Image(javafx.scene.image.Image)

Example 2 with ScriptType

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

the class MessageSignDialog method signUnencryptedKeystore.

private void signUnencryptedKeystore(Wallet decryptedWallet) {
    try {
        Keystore keystore = decryptedWallet.getKeystores().get(0);
        ECKey privKey = keystore.getKey(walletNode);
        ScriptType scriptType = electrumSignatureFormat ? ScriptType.P2PKH : decryptedWallet.getScriptType();
        String signatureText = privKey.signMessage(message.getText().trim(), scriptType);
        signature.clear();
        signature.appendText(signatureText);
        privKey.clear();
    } catch (Exception e) {
        log.error("Could not sign message", e);
        AppServices.showErrorDialog("Could not sign message", e.getMessage());
    }
}
Also used : Keystore(com.sparrowwallet.drongo.wallet.Keystore) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) ECKey(com.sparrowwallet.drongo.crypto.ECKey) SecureString(com.sparrowwallet.drongo.SecureString) InvalidAddressException(com.sparrowwallet.drongo.address.InvalidAddressException) SignatureException(java.security.SignatureException)

Example 3 with ScriptType

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

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

the class CaravanMultisig method importWallet.

@Override
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
    try {
        InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        CaravanFile cf = JsonPersistence.getGson().fromJson(reader, CaravanFile.class);
        Wallet wallet = new Wallet();
        wallet.setName(cf.name);
        wallet.setPolicyType(PolicyType.MULTI);
        ScriptType scriptType = ScriptType.valueOf(cf.addressType.replace('-', '_'));
        for (ExtPublicKey extKey : cf.extendedPublicKeys) {
            Keystore keystore = new Keystore(extKey.name);
            try {
                keystore.setKeyDerivation(new KeyDerivation(extKey.xfp, extKey.bip32Path));
            } catch (NumberFormatException e) {
                keystore.setKeyDerivation(new KeyDerivation(extKey.xfp, scriptType.getDefaultDerivationPath()));
            }
            keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(extKey.xpub));
            WalletModel walletModel = WalletModel.fromType(extKey.method);
            if (walletModel == null) {
                keystore.setWalletModel(WalletModel.SPARROW);
                keystore.setSource(KeystoreSource.SW_WATCH);
            } else {
                keystore.setWalletModel(walletModel);
                keystore.setSource(KeystoreSource.HW_USB);
            }
            wallet.getKeystores().add(keystore);
        }
        wallet.setScriptType(scriptType);
        wallet.setDefaultPolicy(Policy.getPolicy(PolicyType.MULTI, scriptType, wallet.getKeystores(), cf.quorum.requiredSigners));
        return wallet;
    } catch (Exception e) {
        throw new ImportException("Error importing " + getName() + " wallet", e);
    }
}
Also used : Keystore(com.sparrowwallet.drongo.wallet.Keystore) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) WalletModel(com.sparrowwallet.drongo.wallet.WalletModel) InputStreamReader(java.io.InputStreamReader) Wallet(com.sparrowwallet.drongo.wallet.Wallet) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation)

Example 5 with ScriptType

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

the class Soroban method setHDWallet.

public void setHDWallet(Wallet wallet) {
    if (wallet.isEncrypted()) {
        throw new IllegalStateException("Wallet cannot be encrypted");
    }
    try {
        Keystore keystore = wallet.getKeystores().get(0);
        ScriptType scriptType = wallet.getScriptType();
        int purpose = scriptType.getDefaultDerivation().get(0).num();
        List<String> words = keystore.getSeed().getMnemonicCode();
        String passphrase = keystore.getSeed().getPassphrase().asString();
        byte[] seed = hdWalletFactory.computeSeedFromWords(words);
        hdWallet = new HD_Wallet(purpose, new ArrayList<>(words), sorobanServer.getParams(), seed, passphrase);
        bip47Account = wallet.isMasterWallet() ? wallet.getAccountIndex() : wallet.getMasterWallet().getAccountIndex();
    } catch (Exception e) {
        throw new IllegalStateException("Could not create Soroban HD wallet ", e);
    }
}
Also used : Keystore(com.sparrowwallet.drongo.wallet.Keystore) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) HD_Wallet(com.samourai.wallet.hd.HD_Wallet)

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