Java Code Examples for org.bitcoinj.core.TransactionInput#getConnectedOutput()

The following examples show how to use org.bitcoinj.core.TransactionInput#getConnectedOutput() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WalletService.java    From bisq-core with GNU Affero General Public License v3.0 6 votes vote down vote up
protected List<TransactionOutput> getOutputsWithConnectedOutputs(Transaction tx) {
    List<TransactionOutput> transactionOutputs = tx.getOutputs();
    List<TransactionOutput> connectedOutputs = new ArrayList<>();

    // add all connected outputs from any inputs as well
    List<TransactionInput> transactionInputs = tx.getInputs();
    for (TransactionInput transactionInput : transactionInputs) {
        TransactionOutput transactionOutput = transactionInput.getConnectedOutput();
        if (transactionOutput != null) {
            connectedOutputs.add(transactionOutput);
        }
    }

    List<TransactionOutput> mergedOutputs = new ArrayList<>();
    mergedOutputs.addAll(transactionOutputs);
    mergedOutputs.addAll(connectedOutputs);
    return mergedOutputs;
}
 
Example 2
Source File: WalletService.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
protected List<TransactionOutput> getOutputsWithConnectedOutputs(Transaction tx) {
    List<TransactionOutput> transactionOutputs = tx.getOutputs();
    List<TransactionOutput> connectedOutputs = new ArrayList<>();

    // add all connected outputs from any inputs as well
    List<TransactionInput> transactionInputs = tx.getInputs();
    for (TransactionInput transactionInput : transactionInputs) {
        TransactionOutput transactionOutput = transactionInput.getConnectedOutput();
        if (transactionOutput != null) {
            connectedOutputs.add(transactionOutput);
        }
    }

    List<TransactionOutput> mergedOutputs = new ArrayList<>();
    mergedOutputs.addAll(transactionOutputs);
    mergedOutputs.addAll(connectedOutputs);
    return mergedOutputs;
}
 
Example 3
Source File: BsqWalletService.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
public Transaction signTx(Transaction tx) throws WalletException, TransactionVerificationException {
    for (int i = 0; i < tx.getInputs().size(); i++) {
        TransactionInput txIn = tx.getInputs().get(i);
        TransactionOutput connectedOutput = txIn.getConnectedOutput();
        if (connectedOutput != null && connectedOutput.isMine(wallet)) {
            signTransactionInput(wallet, aesKey, tx, txIn, i);
            checkScriptSig(tx, txIn, i);
        }
    }

    for (TransactionOutput txo : tx.getOutputs()) {
        Coin value = txo.getValue();
        // OpReturn outputs have value 0
        if (value.isPositive()) {
            checkArgument(Restrictions.isAboveDust(txo.getValue()),
                    "An output value is below dust limit. Transaction=" + tx);
        }
    }

    checkWalletConsistency(wallet);
    verifyTransaction(tx);
    printTx("BSQ wallet: Signed Tx", tx);
    return tx;
}
 
Example 4
Source File: BsqWalletService.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
public Transaction signTx(Transaction tx) throws WalletException, TransactionVerificationException {
    for (int i = 0; i < tx.getInputs().size(); i++) {
        TransactionInput txIn = tx.getInputs().get(i);
        TransactionOutput connectedOutput = txIn.getConnectedOutput();
        if (connectedOutput != null && connectedOutput.isMine(wallet)) {
            signTransactionInput(wallet, aesKey, tx, txIn, i);
            checkScriptSig(tx, txIn, i);
        }
    }

    checkWalletConsistency(wallet);
    verifyTransaction(tx);
    printTx("BSQ wallet: Signed Tx", tx);
    return tx;
}
 
Example 5
Source File: MissingSigResolutionSigner.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
    if (missingSigsMode == Wallet.MissingSigsMode.USE_OP_ZERO)
        return true;

    int numInputs = propTx.partialTx.getInputs().size();
    byte[] dummySig = TransactionSignature.dummy().encodeToBitcoin();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = propTx.partialTx.getInput(i);
        if (txIn.getConnectedOutput() == null) {
            log.warn("Missing connected output, assuming input {} is already signed.", i);
            continue;
        }

        Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey();
        Script inputScript = txIn.getScriptSig();
        if (ScriptPattern.isPayToScriptHash(scriptPubKey) || ScriptPattern.isSentToMultisig(scriptPubKey)) {
            int sigSuffixCount = ScriptPattern.isPayToScriptHash(scriptPubKey) ? 1 : 0;
            // all chunks except the first one (OP_0) and the last (redeem script) are signatures
            for (int j = 1; j < inputScript.getChunks().size() - sigSuffixCount; j++) {
                ScriptChunk scriptChunk = inputScript.getChunks().get(j);
                if (scriptChunk.equalsOpCode(0)) {
                    if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                        throw new MissingSignatureException();
                    } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                        txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, j - 1));
                    }
                }
            }
        } else {
            if (inputScript.getChunks().get(0).equalsOpCode(0)) {
                if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                    throw new ECKey.MissingPrivateKeyException();
                } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                    txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, 0));
                }
            }
        }
        // TODO handle non-P2SH multisig
    }
    return true;
}
 
Example 6
Source File: BsqWalletService.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Coin getValueSentFromMeForTransaction(Transaction transaction) throws ScriptException {
    Coin result = Coin.ZERO;
    // We check all our inputs and get the connected outputs.
    for (int i = 0; i < transaction.getInputs().size(); i++) {
        TransactionInput input = transaction.getInputs().get(i);
        // We grab the connected output for that input
        TransactionOutput connectedOutput = input.getConnectedOutput();
        if (connectedOutput != null) {
            // We grab the parent tx of the connected output
            final Transaction parentTransaction = connectedOutput.getParentTransaction();
            final boolean isConfirmed = parentTransaction != null &&
                    parentTransaction.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING;
            if (connectedOutput.isMineOrWatched(wallet)) {
                if (isConfirmed) {
                    // We lookup if we have a BSQ tx matching the parent tx
                    // We cannot make that findTx call outside of the loop as the parent tx can change at each iteration
                    Optional<Tx> txOptional = bsqStateService.getTx(parentTransaction.getHash().toString());
                    if (txOptional.isPresent()) {
                        TxOutput txOutput = txOptional.get().getTxOutputs().get(connectedOutput.getIndex());
                        if (bsqStateService.isBsqTxOutputType(txOutput)) {
                            //TODO check why values are not the same
                            if (txOutput.getValue() != connectedOutput.getValue().value)
                                log.warn("getValueSentToMeForTransaction: Value of BSQ output do not match BitcoinJ tx output. " +
                                                "txOutput.getValue()={}, output.getValue().value={}, txId={}",
                                        txOutput.getValue(), connectedOutput.getValue().value, txOptional.get().getId());

                            // If it is a valid BSQ output we add it
                            result = result.add(Coin.valueOf(txOutput.getValue()));
                        }
                    }
                } /*else {
                    // TODO atm we don't display amounts of unconfirmed txs but that might change so we leave that code
                    // if it will be required
                    // If the tx is not confirmed yet we add the value and assume it is a valid BSQ output.
                    result = result.add(connectedOutput.getValue());
                }*/
            }
        }
    }
    return result;
}
 
Example 7
Source File: MissingSigResolutionSigner.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
    if (missingSigsMode == Wallet.MissingSigsMode.USE_OP_ZERO)
        return true;

    int numInputs = propTx.partialTx.getInputs().size();
    byte[] dummySig = TransactionSignature.dummy().encodeToBitcoin();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = propTx.partialTx.getInput(i);
        if (txIn.getConnectedOutput() == null) {
            log.warn("Missing connected output, assuming input {} is already signed.", i);
            continue;
        }

        Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey();
        Script inputScript = txIn.getScriptSig();
        if (scriptPubKey.isPayToScriptHash() || scriptPubKey.isSentToMultiSig()) {
            int sigSuffixCount = scriptPubKey.isPayToScriptHash() ? 1 : 0;
            // all chunks except the first one (OP_0) and the last (redeem script) are signatures
            for (int j = 1; j < inputScript.getChunks().size() - sigSuffixCount; j++) {
                ScriptChunk scriptChunk = inputScript.getChunks().get(j);
                if (scriptChunk.equalsOpCode(0)) {
                    if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                        throw new MissingSignatureException();
                    } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                        txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, j - 1));
                    }
                }
            }
        } else {
            if (inputScript.getChunks().get(0).equalsOpCode(0)) {
                if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                    throw new ECKey.MissingPrivateKeyException();
                } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                    txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, 0));
                }
            }
        }
        // TODO handle non-P2SH multisig
    }
    return true;
}
 
Example 8
Source File: MissingSigResolutionSigner.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) {
    if (missingSigsMode == Wallet.MissingSigsMode.USE_OP_ZERO)
        return true;

    int numInputs = propTx.partialTx.getInputs().size();
    byte[] dummySig = TransactionSignature.dummy().encodeToBitcoin();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = propTx.partialTx.getInput(i);
        if (txIn.getConnectedOutput() == null) {
            log.warn("Missing connected output, assuming input {} is already signed.", i);
            continue;
        }

        Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey();
        Script inputScript = txIn.getScriptSig();
        if (scriptPubKey.isPayToScriptHash() || scriptPubKey.isSentToMultiSig()) {
            int sigSuffixCount = scriptPubKey.isPayToScriptHash() ? 1 : 0;
            // all chunks except the first one (OP_0) and the last (redeem script) are signatures
            for (int j = 1; j < inputScript.getChunks().size() - sigSuffixCount; j++) {
                ScriptChunk scriptChunk = inputScript.getChunks().get(j);
                if (scriptChunk.equalsOpCode(0)) {
                    if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                        throw new MissingSignatureException();
                    } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                        txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, j - 1));
                    }
                }
            }
        } else {
            if (inputScript.getChunks().get(0).equalsOpCode(0)) {
                if (missingSigsMode == Wallet.MissingSigsMode.THROW) {
                    throw new ECKey.MissingPrivateKeyException();
                } else if (missingSigsMode == Wallet.MissingSigsMode.USE_DUMMY_SIG) {
                    txIn.setScriptSig(scriptPubKey.getScriptSigWithSignature(inputScript, dummySig, 0));
                }
            }
        }
        // TODO handle non-P2SH multisig
    }
    return true;
}
 
Example 9
Source File: BsqWalletService.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Coin getValueSentFromMeForTransaction(Transaction transaction) throws ScriptException {
    Coin result = Coin.ZERO;
    // We check all our inputs and get the connected outputs.
    for (int i = 0; i < transaction.getInputs().size(); i++) {
        TransactionInput input = transaction.getInputs().get(i);
        // We grab the connected output for that input
        TransactionOutput connectedOutput = input.getConnectedOutput();
        if (connectedOutput != null) {
            // We grab the parent tx of the connected output
            final Transaction parentTransaction = connectedOutput.getParentTransaction();
            final boolean isConfirmed = parentTransaction != null &&
                    parentTransaction.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING;
            if (connectedOutput.isMineOrWatched(wallet)) {
                if (isConfirmed) {
                    // We lookup if we have a BSQ tx matching the parent tx
                    // We cannot make that findTx call outside of the loop as the parent tx can change at each iteration
                    Optional<Tx> txOptional = daoStateService.getTx(parentTransaction.getHash().toString());
                    if (txOptional.isPresent()) {
                        TxOutput txOutput = txOptional.get().getTxOutputs().get(connectedOutput.getIndex());
                        if (daoStateService.isBsqTxOutputType(txOutput)) {
                            //TODO check why values are not the same
                            if (txOutput.getValue() != connectedOutput.getValue().value)
                                log.warn("getValueSentToMeForTransaction: Value of BSQ output do not match BitcoinJ tx output. " +
                                                "txOutput.getValue()={}, output.getValue().value={}, txId={}",
                                        txOutput.getValue(), connectedOutput.getValue().value, txOptional.get().getId());

                            // If it is a valid BSQ output we add it
                            result = result.add(Coin.valueOf(txOutput.getValue()));
                        }
                    }
                } /*else {
                    // TODO atm we don't display amounts of unconfirmed txs but that might change so we leave that code
                    // if it will be required
                    // If the tx is not confirmed yet we add the value and assume it is a valid BSQ output.
                    result = result.add(connectedOutput.getValue());
                }*/
            }
        }
    }
    return result;
}