Search in sources :

Example 6 with OutputDescriptor

use of com.sparrowwallet.drongo.OutputDescriptor in project sparrow by sparrowwallet.

the class SpecterDIY method getKeystore.

@Override
public Keystore getKeystore(ScriptType scriptType, InputStream inputStream, String password) throws ImportException {
    try {
        String text = CharStreams.toString(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        String outputDesc = "sh(" + text + ")";
        OutputDescriptor outputDescriptor = OutputDescriptor.getOutputDescriptor(outputDesc);
        Wallet wallet = outputDescriptor.toWallet();
        if (wallet.getKeystores().size() != 1) {
            throw new ImportException("Could not determine keystore from import");
        }
        Keystore keystore = wallet.getKeystores().get(0);
        keystore.setLabel(getName());
        keystore.setWalletModel(getWalletModel());
        keystore.setSource(KeystoreSource.HW_AIRGAPPED);
        return keystore;
    } catch (IOException e) {
        throw new ImportException("Error getting " + getName() + " keystore", e);
    }
}
Also used : Keystore(com.sparrowwallet.drongo.wallet.Keystore) OutputDescriptor(com.sparrowwallet.drongo.OutputDescriptor) Wallet(com.sparrowwallet.drongo.wallet.Wallet)

Example 7 with OutputDescriptor

use of com.sparrowwallet.drongo.OutputDescriptor in project sparrow by sparrowwallet.

the class SpecterDesktop method importWallet.

@Override
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
    try {
        Gson gson = new Gson();
        SpecterWallet specterWallet = gson.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), SpecterWallet.class);
        if (specterWallet.descriptor != null) {
            OutputDescriptor outputDescriptor = OutputDescriptor.getOutputDescriptor(specterWallet.descriptor);
            Wallet wallet = outputDescriptor.toWallet();
            wallet.setName(specterWallet.label);
            if (specterWallet.devices != null && specterWallet.devices.size() == wallet.getKeystores().size()) {
                boolean uniqueLabels = specterWallet.devices.stream().map(SpecterWalletDevice::getLabel).distinct().count() == specterWallet.devices.size();
                for (int i = 0; i < specterWallet.devices.size(); i++) {
                    SpecterWalletDevice device = specterWallet.devices.get(i);
                    Keystore keystore = wallet.getKeystores().get(i);
                    keystore.setLabel(device.getLabel() + (uniqueLabels ? "" : " " + i));
                    WalletModel walletModel = device.getWalletModel();
                    if (walletModel != null) {
                        keystore.setWalletModel(walletModel);
                        if (walletModel == WalletModel.TREZOR_1 || walletModel == WalletModel.TREZOR_T || walletModel == WalletModel.KEEPKEY || walletModel == WalletModel.LEDGER_NANO_S || walletModel == WalletModel.LEDGER_NANO_X || walletModel == WalletModel.BITBOX_02 || walletModel == WalletModel.COLDCARD) {
                            keystore.setSource(KeystoreSource.HW_USB);
                        } else if (walletModel == WalletModel.BITCOIN_CORE) {
                            keystore.setSource(KeystoreSource.SW_WATCH);
                        } else {
                            keystore.setSource(KeystoreSource.HW_AIRGAPPED);
                        }
                    }
                }
            }
            try {
                wallet.checkWallet();
            } catch (InvalidWalletException e) {
                throw new ImportException("Imported " + getName() + " wallet was invalid: " + e.getMessage());
            }
            return wallet;
        }
    } catch (Exception e) {
        throw new ImportException("Error importing " + getName() + " wallet", e);
    }
    throw new ImportException("File was not a valid " + getName() + " wallet");
}
Also used : InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) OutputDescriptor(com.sparrowwallet.drongo.OutputDescriptor)

Example 8 with OutputDescriptor

use of com.sparrowwallet.drongo.OutputDescriptor in project sparrow by sparrowwallet.

the class Descriptor method exportWallet.

@Override
public void exportWallet(Wallet wallet, OutputStream outputStream) throws ExportException {
    try {
        OutputDescriptor outputDescriptor = OutputDescriptor.getOutputDescriptor(wallet, KeyPurpose.DEFAULT_PURPOSES, null);
        String outputDescriptorString = outputDescriptor.toString(true);
        outputStream.write(outputDescriptorString.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
    } catch (Exception e) {
        throw new ExportException("Error writing output descriptor", e);
    }
}
Also used : OutputDescriptor(com.sparrowwallet.drongo.OutputDescriptor)

Example 9 with OutputDescriptor

use of com.sparrowwallet.drongo.OutputDescriptor in project sparrow by sparrowwallet.

the class KeystoneSinglesig method getKeystore.

@Override
public Keystore getKeystore(ScriptType scriptType, InputStream inputStream, String password) throws ImportException {
    try {
        String outputDescriptor = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
        OutputDescriptor descriptor = OutputDescriptor.getOutputDescriptor(outputDescriptor);
        if (descriptor.isMultisig()) {
            throw new IllegalArgumentException("Output descriptor describes a multisig wallet");
        }
        if (descriptor.getScriptType() != scriptType) {
            throw new IllegalArgumentException("Output descriptor describes a " + descriptor.getScriptType().getDescription() + " wallet");
        }
        ExtendedKey xpub = descriptor.getSingletonExtendedPublicKey();
        KeyDerivation keyDerivation = descriptor.getKeyDerivation(xpub);
        Keystore keystore = new Keystore();
        keystore.setLabel(getName());
        keystore.setSource(KeystoreSource.HW_AIRGAPPED);
        keystore.setWalletModel(WalletModel.KEYSTONE);
        keystore.setKeyDerivation(keyDerivation);
        keystore.setExtendedPublicKey(xpub);
        return keystore;
    } catch (IllegalArgumentException e) {
        throw new ImportException("Error getting " + getName() + " keystore - not an output descriptor", e);
    } catch (Exception e) {
        throw new ImportException("Error getting " + getName() + " keystore", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputDescriptor(com.sparrowwallet.drongo.OutputDescriptor) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) BufferedReader(java.io.BufferedReader) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey)

Aggregations

OutputDescriptor (com.sparrowwallet.drongo.OutputDescriptor)9 Wallet (com.sparrowwallet.drongo.wallet.Wallet)5 Gson (com.google.gson.Gson)2 KeyPurpose (com.sparrowwallet.drongo.KeyPurpose)2 AppServices (com.sparrowwallet.sparrow.AppServices)2 EventManager (com.sparrowwallet.sparrow.EventManager)2 com.sparrowwallet.sparrow.event (com.sparrowwallet.sparrow.event)2 Device (com.sparrowwallet.sparrow.io.Device)2 Hwi (com.sparrowwallet.sparrow.io.Hwi)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStreamReader (java.io.InputStreamReader)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Platform (javafx.application.Platform)2 Subscribe (com.google.common.eventbus.Subscribe)1 HostAndPort (com.google.common.net.HostAndPort)1 SerializedName (com.google.gson.annotations.SerializedName)1 BarcodeFormat (com.google.zxing.BarcodeFormat)1 EncodeHintType (com.google.zxing.EncodeHintType)1 MatrixToImageConfig (com.google.zxing.client.j2se.MatrixToImageConfig)1