diff --git a/core/src/main/java/bisq/core/trade/TradeManager.java b/core/src/main/java/bisq/core/trade/TradeManager.java index 0d8d02a2414..d8278018d56 100644 --- a/core/src/main/java/bisq/core/trade/TradeManager.java +++ b/core/src/main/java/bisq/core/trade/TradeManager.java @@ -120,6 +120,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi private final P2PService p2PService; private final PriceFeedService priceFeedService; private final TradeStatisticsManager tradeStatisticsManager; + private final TradeUtil tradeUtil; @Getter private final ArbitratorManager arbitratorManager; private final MediatorManager mediatorManager; @@ -157,6 +158,7 @@ public TradeManager(User user, P2PService p2PService, PriceFeedService priceFeedService, TradeStatisticsManager tradeStatisticsManager, + TradeUtil tradeUtil, ArbitratorManager arbitratorManager, MediatorManager mediatorManager, ProcessModelServiceProvider processModelServiceProvider, @@ -175,6 +177,7 @@ public TradeManager(User user, this.p2PService = p2PService; this.priceFeedService = priceFeedService; this.tradeStatisticsManager = tradeStatisticsManager; + this.tradeUtil = tradeUtil; this.arbitratorManager = arbitratorManager; this.mediatorManager = mediatorManager; this.processModelServiceProvider = processModelServiceProvider; @@ -634,7 +637,7 @@ private boolean unFailTrade(Trade trade) { // the relevant entries are changed, otherwise it's not added and no address entries are changed private boolean recoverAddresses(Trade trade) { // Find addresses associated with this trade. - var entries = TradeUtils.getAvailableAddresses(trade, btcWalletService, keyRing); + var entries = tradeUtil.getAvailableAddresses(trade); if (entries == null) return false; diff --git a/core/src/main/java/bisq/core/trade/TradeUtils.java b/core/src/main/java/bisq/core/trade/TradeUtil.java similarity index 62% rename from core/src/main/java/bisq/core/trade/TradeUtils.java rename to core/src/main/java/bisq/core/trade/TradeUtil.java index 10a5da8fdf8..c93e5e2be3b 100644 --- a/core/src/main/java/bisq/core/trade/TradeUtils.java +++ b/core/src/main/java/bisq/core/trade/TradeUtil.java @@ -23,20 +23,44 @@ import bisq.common.util.Tuple2; import bisq.common.util.Utilities; +import javax.inject.Inject; +import javax.inject.Singleton; + import java.util.Objects; -public class TradeUtils { +import lombok.extern.slf4j.Slf4j; + +/** + * This class contains trade utility methods. + */ +@Slf4j +@Singleton +public class TradeUtil { + + private final BtcWalletService btcWalletService; + private final KeyRing keyRing; - // Returns if both are AVAILABLE, otherwise null - static Tuple2 getAvailableAddresses(Trade trade, BtcWalletService btcWalletService, - KeyRing keyRing) { - var addresses = getTradeAddresses(trade, btcWalletService, keyRing); + @Inject + public TradeUtil(BtcWalletService btcWalletService, KeyRing keyRing) { + this.btcWalletService = btcWalletService; + this.keyRing = keyRing; + } + + /** + * Returns if and only if both are AVAILABLE, + * otherwise null. + * @param trade the trade being queried for MULTI_SIG, TRADE_PAYOUT addresses + * @return Tuple2 tuple containing MULTI_SIG, TRADE_PAYOUT addresses for trade + */ + public Tuple2 getAvailableAddresses(Trade trade) { + var addresses = getTradeAddresses(trade); if (addresses == null) return null; if (btcWalletService.getAvailableAddressEntries().stream() .noneMatch(e -> Objects.equals(e.getAddressString(), addresses.first))) return null; + if (btcWalletService.getAvailableAddressEntries().stream() .noneMatch(e -> Objects.equals(e.getAddressString(), addresses.second))) return null; @@ -44,18 +68,25 @@ static Tuple2 getAvailableAddresses(Trade trade, BtcWalletServic return new Tuple2<>(addresses.first, addresses.second); } - // Returns addresses as strings if they're known by the wallet - public static Tuple2 getTradeAddresses(Trade trade, BtcWalletService btcWalletService, - KeyRing keyRing) { + /** + * Returns addresses as strings if they're known by the + * wallet. + * @param trade the trade being queried for MULTI_SIG, TRADE_PAYOUT addresses + * @return Tuple2 tuple containing MULTI_SIG, TRADE_PAYOUT addresses for trade + */ + public Tuple2 getTradeAddresses(Trade trade) { var contract = trade.getContract(); if (contract == null) return null; // Get multisig address var isMyRoleBuyer = contract.isMyRoleBuyer(keyRing.getPubKeyRing()); - var multiSigPubKey = isMyRoleBuyer ? contract.getBuyerMultiSigPubKey() : contract.getSellerMultiSigPubKey(); + var multiSigPubKey = isMyRoleBuyer + ? contract.getBuyerMultiSigPubKey() + : contract.getSellerMultiSigPubKey(); if (multiSigPubKey == null) return null; + var multiSigPubKeyString = Utilities.bytesAsHexString(multiSigPubKey); var multiSigAddress = btcWalletService.getAddressEntryListAsImmutableList().stream() .filter(e -> e.getKeyPair().getPublicKeyAsHex().equals(multiSigPubKeyString)) @@ -65,8 +96,9 @@ public static Tuple2 getTradeAddresses(Trade trade, BtcWalletSer return null; // Get payout address - var payoutAddress = isMyRoleBuyer ? - contract.getBuyerPayoutAddressString() : contract.getSellerPayoutAddressString(); + var payoutAddress = isMyRoleBuyer + ? contract.getBuyerPayoutAddressString() + : contract.getSellerPayoutAddressString(); var payoutAddressEntry = btcWalletService.getAddressEntryListAsImmutableList().stream() .filter(e -> Objects.equals(e.getAddressString(), payoutAddress)) .findAny() diff --git a/core/src/main/java/bisq/core/trade/failed/FailedTradesManager.java b/core/src/main/java/bisq/core/trade/failed/FailedTradesManager.java index a584f6b783f..4782ef080d2 100644 --- a/core/src/main/java/bisq/core/trade/failed/FailedTradesManager.java +++ b/core/src/main/java/bisq/core/trade/failed/FailedTradesManager.java @@ -24,7 +24,7 @@ import bisq.core.trade.DumpDelayedPayoutTx; import bisq.core.trade.TradableList; import bisq.core.trade.Trade; -import bisq.core.trade.TradeUtils; +import bisq.core.trade.TradeUtil; import bisq.common.crypto.KeyRing; import bisq.common.persistence.PersistenceManager; @@ -50,6 +50,7 @@ public class FailedTradesManager implements PersistedDataHost { private final PriceFeedService priceFeedService; private final BtcWalletService btcWalletService; private final PersistenceManager> persistenceManager; + private final TradeUtil tradeUtil; private final DumpDelayedPayoutTx dumpDelayedPayoutTx; @Setter private Function unFailTradeCallback; @@ -59,12 +60,14 @@ public FailedTradesManager(KeyRing keyRing, PriceFeedService priceFeedService, BtcWalletService btcWalletService, PersistenceManager> persistenceManager, + TradeUtil tradeUtil, DumpDelayedPayoutTx dumpDelayedPayoutTx) { this.keyRing = keyRing; this.priceFeedService = priceFeedService; this.btcWalletService = btcWalletService; this.dumpDelayedPayoutTx = dumpDelayedPayoutTx; this.persistenceManager = persistenceManager; + this.tradeUtil = tradeUtil; this.persistenceManager.initialize(failedTrades, "FailedTrades", PersistenceManager.Source.PRIVATE); } @@ -127,7 +130,7 @@ public void unFailTrade(Trade trade) { } public String checkUnFail(Trade trade) { - var addresses = TradeUtils.getTradeAddresses(trade, btcWalletService, keyRing); + var addresses = tradeUtil.getTradeAddresses(trade); if (addresses == null) { return "Addresses not found"; }