Java Code Examples for org.bitcoinj.core.Transaction#getHashAsString()

The following examples show how to use org.bitcoinj.core.Transaction#getHashAsString() . 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: NonBsqCoinSelector.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected boolean isTxOutputSpendable(TransactionOutput output) {
    // output.getParentTransaction() cannot be null as it is checked in calling method
    Transaction parentTransaction = output.getParentTransaction();
    if (parentTransaction == null)
        return false;

    // It is important to not allow pending txs as otherwise unconfirmed BSQ txs would be considered nonBSQ as
    // below outputIsNotInBsqState would be true.
    if (parentTransaction.getConfidence().getConfidenceType() != TransactionConfidence.ConfidenceType.BUILDING)
        return false;

    TxOutputKey key = new TxOutputKey(parentTransaction.getHashAsString(), output.getIndex());
    // It might be that we received BTC in a non-BSQ tx so that will not be stored in out state and not found.
    // So we consider any txOutput which is not in the state as BTC output.
    return !daoStateService.existsTxOutput(key) || daoStateService.isRejectedIssuanceOutput(key);
}
 
Example 2
Source File: UnconfirmedTxOutput.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
public static UnconfirmedTxOutput fromTransactionOutput(TransactionOutput transactionOutput) {
    Transaction parentTransaction = transactionOutput.getParentTransaction();
    if (parentTransaction != null) {
        return new UnconfirmedTxOutput(transactionOutput.getIndex(),
                transactionOutput.getValue().value,
                parentTransaction.getHashAsString());
    } else {
        log.warn("parentTransaction of transactionOutput is null. " +
                        "This must not happen. " +
                        "We could throw an exception as well " +
                        "here but we prefer to be for now more tolerant and just " +
                        "assign the value 0 if that would be the case. transactionOutput={}",
                transactionOutput);
        return new UnconfirmedTxOutput(transactionOutput.getIndex(),
                0,
                "null");
    }
}
 
Example 3
Source File: TxConfidenceListItem.java    From bisq with GNU Affero General Public License v3.0 6 votes vote down vote up
protected TxConfidenceListItem(Transaction transaction,
                               BsqWalletService bsqWalletService) {
    this.bsqWalletService = bsqWalletService;

    txId = transaction.getHashAsString();
    txConfidenceIndicator = new TxConfidenceIndicator();
    txConfidenceIndicator.setId("funds-confidence");
    Tooltip tooltip = new Tooltip();
    txConfidenceIndicator.setProgress(0);
    txConfidenceIndicator.setPrefSize(24, 24);
    txConfidenceIndicator.setTooltip(tooltip);

    txConfidenceListener = new TxConfidenceListener(txId) {
        @Override
        public void onTransactionConfidenceChanged(TransactionConfidence confidence) {
            updateConfidence(confidence, tooltip);
        }
    };
    bsqWalletService.addTxConfidenceListener(txConfidenceListener);
    updateConfidence(bsqWalletService.getConfidenceForTxId(txId), tooltip);
}
 
Example 4
Source File: MyBlindVoteListService.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
public void publishBlindVote(Coin stake, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
    try {
        SecretKey secretKey = BlindVoteConsensus.createSecretKey();
        BallotList sortedBallotList = BlindVoteConsensus.getSortedBallotList(ballotListService);
        byte[] encryptedVotes = getEncryptedVotes(sortedBallotList, secretKey);
        byte[] opReturnData = getOpReturnData(encryptedVotes);
        Coin blindVoteFee = BlindVoteConsensus.getFee(bsqStateService, bsqStateService.getChainHeight());
        Transaction blindVoteTx = getBlindVoteTx(stake, blindVoteFee, opReturnData);
        String blindVoteTxId = blindVoteTx.getHashAsString();

        byte[] encryptedMeritList = getEncryptedMeritList(blindVoteTxId, secretKey);

        // We prefer to not wait for the tx broadcast as if the tx broadcast would fail we still prefer to have our
        // blind vote stored and broadcasted to the p2p network. The tx might get re-broadcasted at a restart and
        // in worst case if it does not succeed the blind vote will be ignored anyway.
        // Inconsistently propagated blind votes in the p2p network could have potentially worse effects.
        BlindVote blindVote = new BlindVote(encryptedVotes, blindVoteTxId, stake.value, encryptedMeritList);
        addBlindVoteToList(blindVote);

        addToP2PNetwork(blindVote, errorMessage -> {
            log.error(errorMessage);
            exceptionHandler.handleException(new PublishToP2PNetworkException(errorMessage));
        });

        // We store our source data for the blind vote in myVoteList
        myVoteListService.createAndAddMyVote(sortedBallotList, secretKey, blindVote);

        publishTx(resultHandler, exceptionHandler, blindVoteTx);
    } catch (CryptoException | TransactionVerificationException | InsufficientMoneyException |
            WalletException | IOException exception) {
        exceptionHandler.handleException(exception);
    }
}
 
Example 5
Source File: TxBroadcastTimeoutException.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @param localTx The tx we sent out
 * @param delay   The timeout delay
 * @param wallet  Wallet is needed if a client is calling wallet.commitTx(tx)
 */
public TxBroadcastTimeoutException(Transaction localTx, int delay, Wallet wallet) {
    super("The transaction was not broadcasted in " + delay +
            " seconds. txId=" + localTx.getHashAsString());
    this.localTx = localTx;
    this.delay = delay;
    this.wallet = wallet;
}
 
Example 6
Source File: TxBroadcastTimeoutException.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param localTx       The tx we sent out
 * @param delay         The timeout delay
 * @param wallet        Wallet is needed if a client is calling wallet.commitTx(tx)
 */
public TxBroadcastTimeoutException(Transaction localTx, int delay, Wallet wallet) {
    super("The transaction was not broadcasted in " + delay +
            "seconds. txId=" + localTx.getHashAsString());
    this.localTx = localTx;
    this.delay = delay;
    this.wallet = wallet;
}
 
Example 7
Source File: TxMalleabilityException.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
public TxMalleabilityException(Transaction localTx, Transaction networkTx) {
    super("The transaction we received from the Bitcoin network has a different txId as the one we broadcasted.\n" +
            "txId of local tx=" + localTx.getHashAsString() +
            ", txId of received tx=" + localTx.getHashAsString());
    this.localTx = localTx;
    this.networkTx = networkTx;
}
 
Example 8
Source File: BsqWalletService.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Coin getValueSentToMeForTransaction(Transaction transaction) throws ScriptException {
    Coin result = Coin.ZERO;
    final String txId = transaction.getHashAsString();
    // We check if we have a matching BSQ tx. We do that call here to avoid repeated calls in the loop.
    Optional<Tx> txOptional = bsqStateService.getTx(txId);
    // We check all the outputs of our tx
    for (int i = 0; i < transaction.getOutputs().size(); i++) {
        TransactionOutput output = transaction.getOutputs().get(i);
        final boolean isConfirmed = output.getParentTransaction() != null &&
                output.getParentTransaction().getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING;
        if (output.isMineOrWatched(wallet)) {
            if (isConfirmed) {
                if (txOptional.isPresent()) {
                    // The index of the BSQ tx outputs are the same like the bitcoinj tx outputs
                    TxOutput txOutput = txOptional.get().getTxOutputs().get(i);
                    if (bsqStateService.isBsqTxOutputType(txOutput)) {
                        //TODO check why values are not the same
                        if (txOutput.getValue() != output.getValue().value) {
                            log.warn("getValueSentToMeForTransaction: Value of BSQ output do not match BitcoinJ tx output. " +
                                            "txOutput.getValue()={}, output.getValue().value={}, txId={}",
                                    txOutput.getValue(), output.getValue().value, txId);
                        }

                        // 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(output.getValue());
            }*/
        }
    }
    return result;
}
 
Example 9
Source File: BsqWalletService.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Coin getValueSentToMeForTransaction(Transaction transaction) throws ScriptException {
    Coin result = Coin.ZERO;
    final String txId = transaction.getHashAsString();
    // We check if we have a matching BSQ tx. We do that call here to avoid repeated calls in the loop.
    Optional<Tx> txOptional = daoStateService.getTx(txId);
    // We check all the outputs of our tx
    for (int i = 0; i < transaction.getOutputs().size(); i++) {
        TransactionOutput output = transaction.getOutputs().get(i);
        final boolean isConfirmed = output.getParentTransaction() != null &&
                output.getParentTransaction().getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.BUILDING;
        if (output.isMineOrWatched(wallet)) {
            if (isConfirmed) {
                if (txOptional.isPresent()) {
                    // The index of the BSQ tx outputs are the same like the bitcoinj tx outputs
                    TxOutput txOutput = txOptional.get().getTxOutputs().get(i);
                    if (daoStateService.isBsqTxOutputType(txOutput)) {
                        //TODO check why values are not the same
                        if (txOutput.getValue() != output.getValue().value) {
                            log.warn("getValueSentToMeForTransaction: Value of BSQ output do not match BitcoinJ tx output. " +
                                            "txOutput.getValue()={}, output.getValue().value={}, txId={}",
                                    txOutput.getValue(), output.getValue().value, txId);
                        }

                        // 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(output.getValue());
            }*/
        }
    }
    return result;
}
 
Example 10
Source File: MyBlindVoteListService.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public void publishBlindVote(Coin stake, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
    try {
        SecretKey secretKey = BlindVoteConsensus.createSecretKey();
        BallotList sortedBallotList = BlindVoteConsensus.getSortedBallotList(ballotListService);
        byte[] encryptedVotes = getEncryptedVotes(sortedBallotList, secretKey);
        byte[] opReturnData = getOpReturnData(encryptedVotes);
        Coin blindVoteFee = BlindVoteConsensus.getFee(daoStateService, daoStateService.getChainHeight());
        Transaction blindVoteTx = getBlindVoteTx(stake, blindVoteFee, opReturnData);
        String blindVoteTxId = blindVoteTx.getHashAsString();

        byte[] encryptedMeritList = getEncryptedMeritList(blindVoteTxId, secretKey);

        // We prefer to not wait for the tx broadcast as if the tx broadcast would fail we still prefer to have our
        // blind vote stored and broadcasted to the p2p network. The tx might get re-broadcasted at a restart and
        // in worst case if it does not succeed the blind vote will be ignored anyway.
        // Inconsistently propagated blind votes in the p2p network could have potentially worse effects.
        BlindVote blindVote = new BlindVote(encryptedVotes,
                blindVoteTxId,
                stake.value,
                encryptedMeritList,
                new Date().getTime(),
                new HashMap<>());
        addBlindVoteToList(blindVote);

        addToP2PNetwork(blindVote, errorMessage -> {
            log.error(errorMessage);
            exceptionHandler.handleException(new PublishToP2PNetworkException(errorMessage));
        });

        // We store our source data for the blind vote in myVoteList
        myVoteListService.createAndAddMyVote(sortedBallotList, secretKey, blindVote);

        publishTx(resultHandler, exceptionHandler, blindVoteTx);
    } catch (CryptoException | TransactionVerificationException | InsufficientMoneyException |
            WalletException | IOException exception) {
        log.error(exception.toString());
        exception.printStackTrace();
        exceptionHandler.handleException(exception);
    }
}
 
Example 11
Source File: TransactionAwareOpenOffer.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public boolean isRelatedToTransaction(Transaction transaction) {
    Offer offer = delegate.getOffer();
    String paymentTxId = offer.getOfferFeePaymentTxId();

    String txId = transaction.getHashAsString();

    return paymentTxId.equals(txId);
}
 
Example 12
Source File: TransactionAwareTrade.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean isRelatedToTransaction(Transaction transaction) {
    String txId = transaction.getHashAsString();

    boolean isTakerOfferFeeTx = txId.equals(trade.getTakerFeeTxId());
    boolean isOfferFeeTx = isOfferFeeTx(txId);
    boolean isDepositTx = isDepositTx(txId);
    boolean isPayoutTx = isPayoutTx(txId);
    boolean isDisputedPayoutTx = isDisputedPayoutTx(txId);
    boolean isDelayedPayoutTx = isDelayedPayoutTx(txId);
    boolean isRefundPayoutTx = isRefundPayoutTx(txId);

    return isTakerOfferFeeTx || isOfferFeeTx || isDepositTx || isPayoutTx ||
            isDisputedPayoutTx || isDelayedPayoutTx || isRefundPayoutTx;
}
 
Example 13
Source File: Trade.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("NullableProblems")
public void setPayoutTx(Transaction payoutTx) {
    this.payoutTx = payoutTx;
    payoutTxId = payoutTx.getHashAsString();
}
 
Example 14
Source File: TxBroadcaster.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void broadcastTx(Wallet wallet, PeerGroup peerGroup, Transaction tx, Callback callback, int delayInSec) {
    Timer timeoutTimer;
    final String txId = tx.getHashAsString();
    if (!broadcastTimerMap.containsKey(txId)) {
        timeoutTimer = UserThread.runAfter(() -> {
            log.warn("Broadcast of tx {} not completed after {} sec.", txId, delayInSec);
            stopAndRemoveTimer(txId);
            UserThread.execute(() -> callback.onTimeout(new TxBroadcastTimeoutException(tx, delayInSec, wallet)));
        }, delayInSec);

        broadcastTimerMap.put(txId, timeoutTimer);
    } else {
        // Would be the wrong way how to use the API (calling 2 times a broadcast with same tx).
        // An arbitrator reported that got the error after a manual payout, need to investigate why...
        stopAndRemoveTimer(txId);
        UserThread.execute(() -> callback.onFailure(new TxBroadcastException("We got broadcastTx called with a tx " +
                "which has an open timeoutTimer. txId=" + txId, txId)));
    }

    // We decided the least risky scenario is to commit the tx to the wallet and broadcast it later.
    // If it's a bsq tx WalletManager.publishAndCommitBsqTx() should have committed the tx to both bsq and btc
    // wallets so the next line causes no effect.
    // If it's a btc tx, the next line adds the tx to the wallet.
    wallet.maybeCommitTx(tx);

    Futures.addCallback(peerGroup.broadcastTransaction(tx).future(), new FutureCallback<>() {
        @Override
        public void onSuccess(@Nullable Transaction result) {
            // We expect that there is still a timeout in our map, otherwise the timeout got triggered
            if (broadcastTimerMap.containsKey(txId)) {
                stopAndRemoveTimer(txId);
                // At regtest we get called immediately back but we want to make sure that the handler is not called
                // before the caller is finished.
                UserThread.execute(() -> callback.onSuccess(tx));
            } else {
                log.warn("We got an onSuccess callback for a broadcast which already triggered the timeout. txId={}", txId);
            }
        }

        @Override
        public void onFailure(@NotNull Throwable throwable) {
            stopAndRemoveTimer(txId);
            UserThread.execute(() -> callback.onFailure(new TxBroadcastException("We got an onFailure from " +
                    "the peerGroup.broadcastTransaction callback.", throwable)));
        }
    });
}
 
Example 15
Source File: Trade.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setPayoutTx(Transaction payoutTx) {
    this.payoutTx = payoutTx;
    payoutTxId = payoutTx.getHashAsString();
}
 
Example 16
Source File: ProcessModel.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setTakeOfferFeeTx(Transaction takeOfferFeeTx) {
    this.takeOfferFeeTx = takeOfferFeeTx;
    takeOfferFeeTxId = takeOfferFeeTx.getHashAsString();
}
 
Example 17
Source File: TxBroadcaster.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void broadcastTx(Wallet wallet, PeerGroup peerGroup, Transaction tx, Callback callback, int delayInSec) {
    Timer timeoutTimer;
    final String txId = tx.getHashAsString();
    if (!broadcastTimerMap.containsKey(txId)) {
        timeoutTimer = UserThread.runAfter(() -> {
            log.warn("Broadcast of tx {} not completed after {} sec.", txId, delayInSec);
            stopAndRemoveTimer(txId);
            UserThread.execute(() -> callback.onTimeout(new TxBroadcastTimeoutException(tx, delayInSec, wallet)));
        }, delayInSec);

        broadcastTimerMap.put(txId, timeoutTimer);
    } else {
        // Would be due a wrong way how to use the API (calling 2 times a broadcast with same tx).
        stopAndRemoveTimer(txId);
        UserThread.execute(() -> callback.onFailure(new TxBroadcastException("We got broadcastTx called with a tx " +
                "which has an open timeoutTimer. txId=" + txId, txId)));
    }

    Futures.addCallback(peerGroup.broadcastTransaction(tx).future(), new FutureCallback<Transaction>() {
        @Override
        public void onSuccess(@Nullable Transaction result) {
            if (result != null) {
                if (txId.equals(result.getHashAsString())) {
                    // We expect that there is still a timeout in our map, otherwise the timeout got triggered
                    if (broadcastTimerMap.containsKey(txId)) {
                        wallet.maybeCommitTx(tx);
                        stopAndRemoveTimer(txId);
                        // At regtest we get called immediately back but we want to make sure that the handler is not called
                        // before the caller is finished.
                        UserThread.execute(() -> callback.onSuccess(tx));
                    } else {
                        stopAndRemoveTimer(txId);
                        log.warn("We got an onSuccess callback for a broadcast which already triggered the timeout.", txId);
                    }
                } else {
                    stopAndRemoveTimer(txId);
                    UserThread.execute(() -> callback.onTxMalleability(new TxMalleabilityException(tx, result)));
                }
            } else {
                stopAndRemoveTimer(txId);
                UserThread.execute(() -> callback.onFailure(new TxBroadcastException("Transaction returned from the " +
                        "broadcastTransaction call back is null.", txId)));
            }
        }

        @Override
        public void onFailure(@NotNull Throwable throwable) {
            stopAndRemoveTimer(txId);
            UserThread.execute(() -> callback.onFailure(new TxBroadcastException("We got an onFailure from " +
                    "the peerGroup.broadcastTransaction callback.", throwable)));
        }
    });
}
 
Example 18
Source File: ProcessModel.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setTakeOfferFeeTx(Transaction takeOfferFeeTx) {
    this.takeOfferFeeTx = takeOfferFeeTx;
    takeOfferFeeTxId = takeOfferFeeTx.getHashAsString();
}
 
Example 19
Source File: TransactionWrapper.java    From thundernetwork with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Instantiates a new transaction wrapper.
 *
 * @param t         the t
 * @param channelId the channel id
 * @param id        the id
 */
public TransactionWrapper (Transaction t, int channelId, int id) {
    this.hash = t.getHashAsString();
    this.channelId = channelId;
    this.data = t.bitcoinSerialize();
    this.id = id;
}
 
Example 20
Source File: TransactionWrapper.java    From thundernetwork with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Instantiates a new transaction wrapper.
 *
 * @param t         the t
 * @param channelId the channel id
 */
public TransactionWrapper (Transaction t, int channelId) {
    this.hash = t.getHashAsString();
    this.channelId = channelId;
    this.data = t.bitcoinSerialize();
}