/*
 * 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 be considered nonBSQ as
        // below outputIsNotInBsqState would be true.
        if (parentTransaction.getConfidence().getConfidenceType() != TransactionConfidence.ConfidenceType.BUILDING)
            return false;

        TxOutputKey key = new TxOutputKey(parentTransaction.getTxId().toString(), 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);
    }

    // BTC utxo in the BSQ wallet are usually from rejected comp request so we don't expect dust attack utxos here.
    @Override
    protected boolean isDustAttackUtxo(TransactionOutput output) {
        return false;
    }
}

@@ -806,16 +806,51 @@
 tputKey;
+%0Aimport bisq.core.user.Preferences;
 %0A%0Aimport
@@ -999,24 +999,46 @@
 ct.Inject;%0A%0A
+import lombok.Setter;%0A
 import lombo
@@ -1363,16 +1363,65 @@
 Service;
+%0A    @Setter%0A    private Preferences preferences;
 %0A%0A    @I
@@ -2609,92 +2609,22 @@
  // 
-BTC utxo in the BSQ wallet are usually from rejected comp request so we don't expect
+Prevent to use
  dus
@@ -2641,14 +2641,8 @@
 txos
- here.
 %0A   
@@ -2734,21 +2734,78 @@
  return 
-false
+output.getValue().value %3C preferences.getIgnoreDustThreshold()
 ;%0A    %7D%0A

Implement isDustAttackUtxo protection