<commit_before>/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.btc.wallet;

import bisq.core.dao.state.DaoStateService;
import bisq.core.dao.state.model.blockchain.TxOutputKey;

import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionConfidence;
import org.bitcoinj.core.TransactionOutput;

import javax.inject.Inject;

import lombok.extern.slf4j.Slf4j;

/**
 * We use a specialized version of the CoinSelector based on the DefaultCoinSelector implementation.
 * We lookup for spendable outputs which matches our address of our address.
 */
@Slf4j
public class NonBsqCoinSelector extends BisqDefaultCoinSelector {
    private DaoStateService daoStateService;

    @Inject
    public NonBsqCoinSelector(DaoStateService daoStateService) {
        super(true);
        this.daoStateService = daoStateService;
    }

    @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;

        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.
        boolean outputIsNotInBsqState = !daoStateService.existsTxOutput(key);
        return outputIsNotInBsqState || daoStateService.getBtcTxOutput(key).isPresent();
    }
}
<commit_msg>Don't support spending of pending BTC utxos in the BSQ wallet.<commit_after>/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.btc.wallet;

import bisq.core.dao.state.DaoStateService;
import bisq.core.dao.state.model.blockchain.TxOutputKey;

import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionConfidence;
import org.bitcoinj.core.TransactionOutput;

import javax.inject.Inject;

import lombok.extern.slf4j.Slf4j;

/**
 * We use a specialized version of the CoinSelector based on the DefaultCoinSelector implementation.
 * We lookup for spendable outputs which matches our address of our address.
 */
@Slf4j
public class NonBsqCoinSelector extends BisqDefaultCoinSelector {
    private DaoStateService daoStateService;

    @Inject
    public NonBsqCoinSelector(DaoStateService daoStateService) {
        super(false);
        this.daoStateService = daoStateService;
    }

    @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 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.
        boolean outputIsNotInBsqState = !daoStateService.existsTxOutput(key);
        return outputIsNotInBsqState || daoStateService.getBtcTxOutput(key).isPresent();
    }
}

{'fraction_non_alphanumeric': 0.06865848832607835, 'fraction_numerical': 0.0015829046299960427, 'mean_word_length': 3.6935933147632314, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3401718', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1357, 'n_words': 514}