Search in sources :

Example 11 with ScriptType

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

the class DescriptorArea method setWallet.

public void setWallet(Wallet wallet) {
    clear();
    this.wallet = wallet;
    DescriptorContextMenu contextMenu = new DescriptorContextMenu(wallet, this);
    setContextMenu(contextMenu);
    PolicyType policyType = wallet.getPolicyType();
    ScriptType scriptType = wallet.getScriptType();
    List<Keystore> keystores = wallet.getKeystores();
    int threshold = wallet.getDefaultPolicy().getNumSignaturesRequired();
    if (SINGLE.equals(policyType)) {
        append(scriptType.getDescriptor(), "descriptor-text");
        replace(getLength(), getLength(), keystores.get(0).getScriptName(), List.of(keystores.get(0).isValid() ? "descriptor-text" : "descriptor-error", keystores.get(0).getScriptName()));
        append(scriptType.getCloseDescriptor(), "descriptor-text");
    }
    if (MULTI.equals(policyType)) {
        append(scriptType.getDescriptor(), "descriptor-text");
        append(MULTISIG.getDescriptor(), "descriptor-text");
        append(Integer.toString(threshold), "descriptor-text");
        for (Keystore keystore : keystores) {
            append(",", "descriptor-text");
            replace(getLength(), getLength(), keystore.getScriptName(), List.of(keystore.isValid() ? "descriptor-text" : "descriptor-error", keystore.getScriptName()));
        }
        append(MULTISIG.getCloseDescriptor(), "descriptor-text");
        append(scriptType.getCloseDescriptor(), "descriptor-text");
    }
}
Also used : Keystore(com.sparrowwallet.drongo.wallet.Keystore) PolicyType(com.sparrowwallet.drongo.policy.PolicyType) ScriptType(com.sparrowwallet.drongo.protocol.ScriptType)

Example 12 with ScriptType

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

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

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

the class PSBTInput method getHashForSignature.

private Sha256Hash getHashForSignature(Script connectedScript, SigHash localSigHash) {
    Sha256Hash hash;
    ScriptType scriptType = getScriptType();
    if (scriptType == ScriptType.P2TR) {
        List<TransactionOutput> spentUtxos = psbt.getPsbtInputs().stream().map(PSBTInput::getUtxo).collect(Collectors.toList());
        hash = transaction.hashForTaprootSignature(spentUtxos, index, !P2TR.isScriptType(connectedScript), connectedScript, localSigHash, null);
    } else if (Arrays.asList(WITNESS_TYPES).contains(scriptType)) {
        long prevValue = getUtxo().getValue();
        hash = transaction.hashForWitnessSignature(index, connectedScript, prevValue, localSigHash);
    } else {
        hash = transaction.hashForLegacySignature(index, connectedScript, localSigHash);
    }
    return hash;
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType)

Example 15 with ScriptType

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

the class Script method getToAddresses.

/**
 * Gets the destination address from this script, if it's in the required form.
 */
public Address[] getToAddresses() throws NonStandardScriptException {
    for (ScriptType scriptType : SINGLE_HASH_TYPES) {
        if (scriptType.isScriptType(this)) {
            return new Address[] { scriptType.getAddress(scriptType.getHashFromScript(this)) };
        }
    }
    // Special handling for taproot tweaked keys - we don't want to tweak them again
    if (P2TR.isScriptType(this)) {
        return new Address[] { new P2TRAddress(P2TR.getPublicKeyFromScript(this).getPubKeyXCoord()) };
    }
    for (ScriptType scriptType : SINGLE_KEY_TYPES) {
        if (scriptType.isScriptType(this)) {
            return new Address[] { scriptType.getAddress(scriptType.getPublicKeyFromScript(this)) };
        }
    }
    if (MULTISIG.isScriptType(this)) {
        List<Address> addresses = new ArrayList<>();
        ECKey[] pubKeys = MULTISIG.getPublicKeysFromScript(this);
        for (ECKey pubKey : pubKeys) {
            addresses.add(new P2PKAddress(pubKey.getPubKey()));
        }
        return addresses.toArray(new Address[addresses.size()]);
    }
    throw new NonStandardScriptException("Cannot find addresses in non standard script: " + toString());
}
Also used : ScriptType(com.sparrowwallet.drongo.protocol.ScriptType) ArrayList(java.util.ArrayList) ECKey(com.sparrowwallet.drongo.crypto.ECKey)

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