From 296e4f98cb6231ed2226e72d95c1317981a9d008 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sat, 24 Oct 2020 17:26:10 -0300 Subject: [PATCH 1/2] Replace static TradeUtil with singleton TradeUtil The API is going to need some desktop trade utilities, which should be shared between :desktop and :core.api. --- .../java/bisq/core/trade/TradeManager.java | 5 +- .../trade/{TradeUtils.java => TradeUtil.java} | 60 ++++++++++++++----- .../trade/failed/FailedTradesManager.java | 7 ++- 3 files changed, 55 insertions(+), 17 deletions(-) rename core/src/main/java/bisq/core/trade/{TradeUtils.java => TradeUtil.java} (57%) 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 57% rename from core/src/main/java/bisq/core/trade/TradeUtils.java rename to core/src/main/java/bisq/core/trade/TradeUtil.java index 10a5da8fdf8..001dbd21217 100644 --- a/core/src/main/java/bisq/core/trade/TradeUtils.java +++ b/core/src/main/java/bisq/core/trade/TradeUtil.java @@ -1,18 +1,18 @@ /* * This file is part of Bisq. * - * Bisq is free software: you can redistribute it and/or modify it + * 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 + * 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 . + * along with bisq. If not, see . */ package bisq.core.trade; @@ -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"; } From ccd3c99f2e5b9cf276e6b70bceb34e73fb0a6858 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sat, 24 Oct 2020 17:45:01 -0300 Subject: [PATCH 2/2] Fix comment typos --- core/src/main/java/bisq/core/trade/TradeUtil.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/bisq/core/trade/TradeUtil.java b/core/src/main/java/bisq/core/trade/TradeUtil.java index 001dbd21217..c93e5e2be3b 100644 --- a/core/src/main/java/bisq/core/trade/TradeUtil.java +++ b/core/src/main/java/bisq/core/trade/TradeUtil.java @@ -1,18 +1,18 @@ /* * This file is part of Bisq. * - * bisq is free software: you can redistribute it and/or modify it + * 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 + * 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 . + * along with Bisq. If not, see . */ package bisq.core.trade;