Search in sources :

Example 1 with PSBTInput

use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.

the class PSBT method getPublicCopy.

public PSBT getPublicCopy() {
    try {
        PSBT publicCopy = new PSBT(serialize());
        publicCopy.extendedPublicKeys.clear();
        publicCopy.globalProprietary.clear();
        for (PSBTInput psbtInput : publicCopy.getPsbtInputs()) {
            psbtInput.getDerivedPublicKeys().clear();
            psbtInput.getProprietary().clear();
        }
        for (PSBTOutput psbtOutput : publicCopy.getPsbtOutputs()) {
            psbtOutput.getDerivedPublicKeys().clear();
            psbtOutput.getProprietary().clear();
        }
        return publicCopy;
    } catch (PSBTParseException e) {
        throw new IllegalStateException("Could not parse PSBT", e);
    }
}
Also used : PSBTOutput(com.sparrowwallet.drongo.psbt.PSBTOutput) PSBTInput(com.sparrowwallet.drongo.psbt.PSBTInput)

Example 2 with PSBTInput

use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.

the class PSBT method extractTransaction.

public Transaction extractTransaction() {
    boolean hasWitness = false;
    for (PSBTInput psbtInput : getPsbtInputs()) {
        if (psbtInput.getFinalScriptWitness() != null) {
            hasWitness = true;
        }
    }
    Transaction finalTransaction = new Transaction(transaction.bitcoinSerialize());
    if (hasWitness && !finalTransaction.isSegwit()) {
        finalTransaction.setSegwitFlag(Transaction.DEFAULT_SEGWIT_FLAG);
    }
    for (int i = 0; i < finalTransaction.getInputs().size(); i++) {
        TransactionInput txInput = finalTransaction.getInputs().get(i);
        PSBTInput psbtInput = getPsbtInputs().get(i);
        txInput.setScriptBytes(psbtInput.getFinalScriptSig() == null ? new byte[0] : psbtInput.getFinalScriptSig().getProgram());
        if (hasWitness) {
            if (psbtInput.getFinalScriptWitness() != null) {
                txInput.setWitness(psbtInput.getFinalScriptWitness());
            } else {
                txInput.setWitness(new TransactionWitness(finalTransaction));
            }
        }
    }
    return finalTransaction;
}
Also used : PSBTInput(com.sparrowwallet.drongo.psbt.PSBTInput)

Example 3 with PSBTInput

use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.

the class PSBT method getFee.

public Long getFee() {
    long fee = 0L;
    for (PSBTInput input : psbtInputs) {
        TransactionOutput utxo = input.getUtxo();
        if (utxo != null) {
            fee += utxo.getValue();
        } else {
            log.error("Cannot determine fee - not enough information provided on inputs");
            return null;
        }
    }
    for (int i = 0; i < transaction.getOutputs().size(); i++) {
        TransactionOutput output = transaction.getOutputs().get(i);
        fee -= output.getValue();
    }
    return fee;
}
Also used : PSBTInput(com.sparrowwallet.drongo.psbt.PSBTInput)

Example 4 with PSBTInput

use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.

the class PSBT method parseInputEntries.

private void parseInputEntries(List<List<PSBTEntry>> inputEntryLists, boolean verifySignatures) throws PSBTParseException {
    for (List<PSBTEntry> inputEntries : inputEntryLists) {
        PSBTEntry duplicate = findDuplicateKey(inputEntries);
        if (duplicate != null) {
            throw new PSBTParseException("Found duplicate key for PSBT input: " + Utils.bytesToHex(duplicate.getKey()));
        }
        int inputIndex = this.psbtInputs.size();
        PSBTInput input = new PSBTInput(this, inputEntries, transaction, inputIndex);
        this.psbtInputs.add(input);
    }
    if (verifySignatures) {
        for (PSBTInput input : psbtInputs) {
            boolean verified = input.verifySignatures();
            if (!verified && input.getPartialSignatures().size() > 0) {
                throw new PSBTSignatureException("Unverifiable partial signatures provided");
            }
        }
    }
}
Also used : PSBTEntry(com.sparrowwallet.drongo.psbt.PSBTEntry) PSBTInput(com.sparrowwallet.drongo.psbt.PSBTInput)

Example 5 with PSBTInput

use of com.sparrowwallet.drongo.psbt.PSBTInput in project drongo by sparrowwallet.

the class Wallet method sign.

public void sign(PSBT psbt) throws MnemonicException {
    Map<PSBTInput, WalletNode> signingNodes = getSigningNodes(psbt);
    for (Map.Entry<PSBTInput, WalletNode> signingEntry : signingNodes.entrySet()) {
        Wallet signingWallet = signingEntry.getValue().getWallet();
        for (Keystore keystore : signingWallet.getKeystores()) {
            if (keystore.hasPrivateKey()) {
                ECKey privKey = signingWallet.getScriptType().getOutputKey(keystore.getKey(signingEntry.getValue()));
                PSBTInput psbtInput = signingEntry.getKey();
                if (!psbtInput.isSigned()) {
                    psbtInput.sign(privKey);
                }
            }
        }
    }
}
Also used : ECKey(com.sparrowwallet.drongo.crypto.ECKey) PSBTInput(com.sparrowwallet.drongo.psbt.PSBTInput)

Aggregations

PSBTInput (com.sparrowwallet.drongo.psbt.PSBTInput)17 PSBTOutput (com.sparrowwallet.drongo.psbt.PSBTOutput)6 ECKey (com.sparrowwallet.drongo.crypto.ECKey)5 Address (com.sparrowwallet.drongo.address.Address)3 com.sparrowwallet.drongo.protocol (com.sparrowwallet.drongo.protocol)3 PSBT (com.sparrowwallet.drongo.psbt.PSBT)3 Subscribe (com.google.common.eventbus.Subscribe)2 PSBTEntry (com.sparrowwallet.drongo.psbt.PSBTEntry)2 EventManager (com.sparrowwallet.sparrow.EventManager)2 com.sparrowwallet.sparrow.control (com.sparrowwallet.sparrow.control)2 com.sparrowwallet.sparrow.event (com.sparrowwallet.sparrow.event)2 StandardCharsets (java.nio.charset.StandardCharsets)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 FXML (javafx.fxml.FXML)2 Initializable (javafx.fxml.Initializable)2 javafx.scene.control (javafx.scene.control)2 Field (tornadofx.control.Field)2 Fieldset (tornadofx.control.Fieldset)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 com.sparrowwallet.drongo (com.sparrowwallet.drongo)1