Search in sources :

Example 11 with KeyDerivation

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

the class ColdcardMultisig method importWallet.

@Override
public Wallet importWallet(InputStream inputStream, String password) throws ImportException {
    Wallet wallet = new Wallet();
    wallet.setPolicyType(PolicyType.MULTI);
    int threshold = 2;
    ScriptType scriptType = ScriptType.P2SH;
    String derivation = null;
    try {
        List<String> lines = CharStreams.readLines(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        for (String line : lines) {
            line = line.trim();
            if (line.isEmpty()) {
                continue;
            }
            String[] keyValue = line.split(":");
            if (keyValue.length == 2) {
                String key = keyValue[0].trim();
                String value = keyValue[1].trim();
                switch(key) {
                    case "Name":
                        wallet.setName(value.trim());
                        break;
                    case "Policy":
                        threshold = Integer.parseInt(value.split(" ")[0]);
                        break;
                    case "Derivation":
                    case "# derivation":
                        derivation = value;
                        break;
                    case "Format":
                        scriptType = ScriptType.valueOf(value.replace("P2WSH-P2SH", "P2SH_P2WSH"));
                        break;
                    default:
                        if (key.length() == 8 && Utils.isHex(key)) {
                            Keystore keystore = new Keystore("Coldcard");
                            keystore.setSource(KeystoreSource.HW_AIRGAPPED);
                            keystore.setWalletModel(WalletModel.COLDCARD);
                            keystore.setKeyDerivation(new KeyDerivation(key, derivation));
                            keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(value));
                            wallet.makeLabelsUnique(keystore);
                            wallet.getKeystores().add(keystore);
                        }
                }
            }
        }
        Policy policy = Policy.getPolicy(PolicyType.MULTI, scriptType, wallet.getKeystores(), threshold);
        wallet.setDefaultPolicy(policy);
        wallet.setScriptType(scriptType);
        if (!wallet.isValid()) {
            throw new IllegalStateException("This file does not describe a valid wallet. " + getKeystoreImportDescription());
        }
        return wallet;
    } catch (Exception e) {
        throw new ImportException("Error importing " + getName() + " wallet", e);
    }
}
Also used : Policy(com.sparrowwallet.drongo.policy.Policy) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) Wallet(com.sparrowwallet.drongo.wallet.Wallet) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) Keystore(com.sparrowwallet.drongo.wallet.Keystore)

Example 12 with KeyDerivation

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

the class ColdcardSinglesig method getKeystore.

@Override
public Keystore getKeystore(ScriptType scriptType, InputStream inputStream, String password) throws ImportException {
    try {
        Gson gson = new Gson();
        Type stringStringMap = new TypeToken<Map<String, JsonElement>>() {
        }.getType();
        Map<String, JsonElement> map = gson.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), stringStringMap);
        if (map.get("xfp") == null) {
            throw new ImportException("File was not a valid " + getName() + " wallet export");
        }
        String masterFingerprint = map.get("xfp").getAsString();
        for (String key : map.keySet()) {
            if (key.startsWith("bip")) {
                ColdcardKeystore ck = gson.fromJson(map.get(key), ColdcardKeystore.class);
                if (ck.name != null) {
                    ScriptType ckScriptType = ScriptType.valueOf(ck.name.replace("p2wpkh-p2sh", "p2sh_p2wpkh").replace("p2sh-p2wpkh", "p2sh_p2wpkh").toUpperCase());
                    if (ckScriptType.equals(scriptType)) {
                        Keystore keystore = new Keystore();
                        keystore.setLabel(getName());
                        keystore.setSource(KeystoreSource.HW_AIRGAPPED);
                        keystore.setWalletModel(WalletModel.COLDCARD);
                        keystore.setKeyDerivation(new KeyDerivation(masterFingerprint, ck.deriv));
                        keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(ck.xpub));
                        return keystore;
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new ImportException("Error getting " + getName() + " keystore", e);
    }
    throw new ImportException("Correct derivation not found for script type: " + scriptType);
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) InputStreamReader(java.io.InputStreamReader) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) Gson(com.google.gson.Gson) PolicyType(com.sparrowwallet.drongo.policy.PolicyType) Type(java.lang.reflect.Type) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) JsonElement(com.google.gson.JsonElement) Map(java.util.Map)

Example 13 with KeyDerivation

use of com.sparrowwallet.drongo.KeyDerivation 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)

Example 14 with KeyDerivation

use of com.sparrowwallet.drongo.KeyDerivation in project drongo by sparrowwallet.

the class PSBTEntry method parseTaprootKeyDerivation.

public static Map<KeyDerivation, List<Sha256Hash>> parseTaprootKeyDerivation(byte[] data) throws PSBTParseException {
    if (data.length < 1) {
        throw new PSBTParseException("Invalid taproot key derivation: no bytes");
    }
    VarInt varInt = new VarInt(data, 0);
    int offset = varInt.getOriginalSizeInBytes();
    if (data.length < offset + (varInt.value * 32)) {
        throw new PSBTParseException("Invalid taproot key derivation: not enough bytes for leaf hashes");
    }
    List<Sha256Hash> leafHashes = new ArrayList<>();
    for (int i = 0; i < varInt.value; i++) {
        leafHashes.add(Sha256Hash.wrap(Arrays.copyOfRange(data, offset + (i * 32), offset + (i * 32) + 32)));
    }
    KeyDerivation keyDerivation = parseKeyDerivation(Arrays.copyOfRange(data, offset + (leafHashes.size() * 32), data.length));
    return Map.of(keyDerivation, leafHashes);
}
Also used : VarInt(com.sparrowwallet.drongo.protocol.VarInt) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) Sha256Hash(com.sparrowwallet.drongo.protocol.Sha256Hash) ArrayList(java.util.ArrayList)

Example 15 with KeyDerivation

use of com.sparrowwallet.drongo.KeyDerivation in project drongo by sparrowwallet.

the class Keystore method rederiveKeystoreFromMaster.

private static void rederiveKeystoreFromMaster(Keystore keystore, List<ChildNumber> derivation) throws MnemonicException {
    ExtendedKey xprv = keystore.getExtendedMasterPrivateKey();
    String masterFingerprint = Utils.bytesToHex(xprv.getKey().getFingerprint());
    DeterministicKey derivedKey = xprv.getKey(derivation);
    DeterministicKey derivedKeyPublicOnly = derivedKey.dropPrivateBytes().dropParent();
    ExtendedKey xpub = new ExtendedKey(derivedKeyPublicOnly, derivedKey.getParentFingerprint(), derivation.isEmpty() ? ChildNumber.ZERO : derivation.get(derivation.size() - 1));
    keystore.setSource(KeystoreSource.SW_SEED);
    keystore.setWalletModel(WalletModel.SPARROW);
    keystore.setKeyDerivation(new KeyDerivation(masterFingerprint, KeyDerivation.writePath(derivation)));
    keystore.setExtendedPublicKey(ExtendedKey.fromDescriptor(xpub.toString()));
    int account = ScriptType.getScriptTypesForPolicyType(PolicyType.SINGLE).stream().mapToInt(scriptType -> scriptType.getAccount(keystore.getKeyDerivation().getDerivationPath())).filter(idx -> idx > -1).findFirst().orElse(0);
    List<ChildNumber> bip47Derivation = KeyDerivation.getBip47Derivation(account);
    DeterministicKey bip47Key = xprv.getKey(bip47Derivation);
    ExtendedKey bip47ExtendedPrivateKey = new ExtendedKey(bip47Key, bip47Key.getParentFingerprint(), bip47Derivation.get(bip47Derivation.size() - 1));
    keystore.setBip47ExtendedPrivateKey(ExtendedKey.fromDescriptor(bip47ExtendedPrivateKey.toString()));
}
Also used : Logger(org.slf4j.Logger) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey) PaymentAddress(com.sparrowwallet.drongo.bip47.PaymentAddress) LoggerFactory(org.slf4j.LoggerFactory) com.sparrowwallet.drongo.crypto(com.sparrowwallet.drongo.crypto) PolicyType(com.sparrowwallet.drongo.policy.PolicyType) ArrayList(java.util.ArrayList) List(java.util.List) KeyPurpose(com.sparrowwallet.drongo.KeyPurpose) Utils(com.sparrowwallet.drongo.Utils) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) PaymentCode(com.sparrowwallet.drongo.bip47.PaymentCode) KeyDerivation(com.sparrowwallet.drongo.KeyDerivation) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey)

Aggregations

KeyDerivation (com.sparrowwallet.drongo.KeyDerivation)17 ExtendedKey (com.sparrowwallet.drongo.ExtendedKey)8 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)4 Keystore (com.sparrowwallet.drongo.wallet.Keystore)4 InputStreamReader (java.io.InputStreamReader)4 PolicyType (com.sparrowwallet.drongo.policy.PolicyType)3 Test (org.junit.Test)3 Gson (com.google.gson.Gson)2 KeyPurpose (com.sparrowwallet.drongo.KeyPurpose)2 Wallet (com.sparrowwallet.drongo.wallet.Wallet)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 JsonElement (com.google.gson.JsonElement)1 OutputDescriptor (com.sparrowwallet.drongo.OutputDescriptor)1 SecureString (com.sparrowwallet.drongo.SecureString)1 Utils (com.sparrowwallet.drongo.Utils)1 Address (com.sparrowwallet.drongo.address.Address)1 PaymentAddress (com.sparrowwallet.drongo.bip47.PaymentAddress)1 PaymentCode (com.sparrowwallet.drongo.bip47.PaymentCode)1 com.sparrowwallet.drongo.crypto (com.sparrowwallet.drongo.crypto)1