Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protection tools to offer #5051

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b013503
Do not shutDown with CloseConnectionReason.PEER_BANNED if we received a
chimp1984 Jan 1, 2021
456d1f8
Improve logs, optimize stream
chimp1984 Jan 1, 2021
a7127d4
Improve logging of received data
chimp1984 Jan 1, 2021
ac8711c
Add NetworkFilter, remove BanList
chimp1984 Jan 1, 2021
1ef086a
Make code more readable
chimp1984 Jan 1, 2021
79a0874
Add support to filter manager for network wide banned nodes
chimp1984 Jan 1, 2021
e868050
Add checks if peer is banned at send msg and InputHandlers
chimp1984 Jan 1, 2021
fbfd95b
Refactor sendMessage method: Return early
chimp1984 Jan 1, 2021
de5b69a
Refactor sendMessage method: Inline debug value
chimp1984 Jan 1, 2021
e73b6b0
If we select TransferWise we switch to show all
chimp1984 Jan 2, 2021
82644fc
Add option in preferences to hide payment methods which are not part …
chimp1984 Jan 2, 2021
039d8fb
Extract methods for show all and edit entries.
chimp1984 Jan 2, 2021
645594f
Add toggle for filtering offers which can be taken with users accounts
chimp1984 Jan 2, 2021
b0f54d4
Add null checks
chimp1984 Jan 3, 2021
9a47a83
Add denyApiTaker entry to extra field map.
chimp1984 Jan 3, 2021
7a8e863
Add denyApiTaker to Preferences
chimp1984 Jan 3, 2021
aaef64b
Add denyApiTaker toggle to PreferencesView
chimp1984 Jan 3, 2021
78282af
Add isApiUser to OfferAvailabilityRequest
chimp1984 Jan 3, 2021
0ae5006
Add UNCONF_TX_LIMIT_HIT to AvailabilityResult
chimp1984 Jan 3, 2021
18e4a39
Add support for isApiUser and check in OpenOfferManager
chimp1984 Jan 3, 2021
400a736
Refactor checks for take-able offers
chimp1984 Jan 3, 2021
2a94810
Add `getOffersAvailableForTaker` method
chimp1984 Jan 3, 2021
dacdd8f
Add disableApi field to filter
chimp1984 Jan 3, 2021
7a76035
Add comment, remove pointless code.
chimp1984 Jan 3, 2021
e2d386e
Rename bannedNodeAddress to nodeAddressesBannedFromTrading
chimp1984 Jan 3, 2021
9d3fd03
Rename isApiUser to isTakerApiUser
chimp1984 Jan 4, 2021
a49505d
Add Result.API_DISABLED
chimp1984 Jan 4, 2021
76022c0
Add missing enum entries to protobuf
chimp1984 Jan 4, 2021
2695415
Remove OfferPayload.DENY_API_TAKER and use preferences
chimp1984 Jan 4, 2021
d402496
Update devMode price (BTC increased 3 time since last update!)
chimp1984 Jan 4, 2021
c68c0ed
Remove DENIED_API_TAKER enum.
chimp1984 Jan 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/src/main/java/bisq/common/proto/ProtoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -105,4 +106,7 @@ public static List<String> protocolStringListToList(ProtocolStringList protocolS
return CollectionUtils.isEmpty(protocolStringList) ? new ArrayList<>() : new ArrayList<>(protocolStringList);
}

public static Set<String> protocolStringListToSet(ProtocolStringList protocolStringList) {
return CollectionUtils.isEmpty(protocolStringList) ? new HashSet<>() : new HashSet<>(protocolStringList);
}
}
10 changes: 10 additions & 0 deletions core/src/main/java/bisq/core/api/CoreApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ public List<Offer> getOffers(String direction, String currencyCode) {
return coreOffersService.getOffers(direction, currencyCode);
}

/**
* @param direction The offer direction
* @param currencyCode The offer currency
* @return Returns the offers which can be taken
*/
List<Offer> getOffersAvailableForTaker(String direction, String currencyCode) {
return coreOffersService.getOffersAvailableForTaker(direction, currencyCode, true);
}

public void createAnPlaceOffer(String currencyCode,
String directionAsString,
String priceAsString,
Expand Down Expand Up @@ -202,6 +211,7 @@ public void takeOffer(String offerId,
coreTradesService.takeOffer(offer,
paymentAccountId,
takerFeeCurrencyCode,
true,
resultHandler);
}

Expand Down
15 changes: 14 additions & 1 deletion core/src/main/java/bisq/core/api/CoreOffersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.core.offer.CreateOfferService;
import bisq.core.offer.Offer;
import bisq.core.offer.OfferBookService;
import bisq.core.offer.OfferFilter;
import bisq.core.offer.OfferUtil;
import bisq.core.offer.OpenOfferManager;
import bisq.core.payment.PaymentAccount;
Expand Down Expand Up @@ -58,27 +59,33 @@ class CoreOffersService {
private final OpenOfferManager openOfferManager;
private final OfferUtil offerUtil;
private final User user;
private final OfferFilter offerFilter;

@Inject
public CoreOffersService(CreateOfferService createOfferService,
OfferBookService offerBookService,
OpenOfferManager openOfferManager,
OfferUtil offerUtil,
User user) {
User user,
OfferFilter offerFilter) {
this.createOfferService = createOfferService;
this.offerBookService = offerBookService;
this.openOfferManager = openOfferManager;
this.offerUtil = offerUtil;
this.user = user;
this.offerFilter = offerFilter;
}

// TODO should we add a check for offerFilter.canTakeOffer?
Offer getOffer(String id) {
return offerBookService.getOffers().stream()
.filter(o -> o.getId().equals(id))
.findAny().orElseThrow(() ->
new IllegalStateException(format("offer with id '%s' not found", id)));
}

// TODO returns all offers also those which cannot be taken. Should we use the filter from
// getOffersAvailableForTaker here and remove the getOffersAvailableForTaker method?
List<Offer> getOffers(String direction, String currencyCode) {
List<Offer> offers = offerBookService.getOffers().stream()
.filter(o -> {
Expand All @@ -99,6 +106,12 @@ List<Offer> getOffers(String direction, String currencyCode) {
return offers;
}

List<Offer> getOffersAvailableForTaker(String direction, String currencyCode, boolean isTakerApiUser) {
return getOffers(direction, currencyCode).stream()
.filter(offer -> offerFilter.canTakeOffer(offer, isTakerApiUser).isValid())
.collect(Collectors.toList());
}

// Create and place new offer.
void createAndPlaceOffer(String currencyCode,
String directionAsString,
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/bisq/core/api/CoreTradesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public CoreTradesService(CoreWalletsService coreWalletsService,
void takeOffer(Offer offer,
String paymentAccountId,
String takerFeeCurrencyCode,
boolean isTakerApiUser,
Consumer<Trade> resultHandler) {
coreWalletsService.verifyWalletsAreAvailable();
coreWalletsService.verifyEncryptedWalletIsUnlocked();
Expand All @@ -108,6 +109,7 @@ void takeOffer(Offer offer,
offer,
paymentAccountId,
useSavingsWallet,
isTakerApiUser,
resultHandler::accept,
errorMessage -> {
log.error(errorMessage);
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/bisq/core/app/CoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import bisq.core.btc.BitcoinModule;
import bisq.core.dao.DaoModule;
import bisq.core.filter.FilterModule;
import bisq.core.network.CoreNetworkFilter;
import bisq.core.network.p2p.seed.DefaultSeedNodeRepository;
import bisq.core.offer.OfferModule;
import bisq.core.presentation.CorePresentationModule;
Expand All @@ -35,6 +36,7 @@
import bisq.network.crypto.EncryptionServiceModule;
import bisq.network.p2p.P2PModule;
import bisq.network.p2p.network.BridgeAddressProvider;
import bisq.network.p2p.network.NetworkFilter;
import bisq.network.p2p.seed.SeedNodeRepository;

import bisq.common.app.AppModule;
Expand All @@ -44,6 +46,8 @@
import bisq.common.proto.network.NetworkProtoResolver;
import bisq.common.proto.persistable.PersistenceProtoResolver;

import com.google.inject.Singleton;

import java.io.File;

import static bisq.common.config.Config.*;
Expand All @@ -62,6 +66,7 @@ protected void configure() {
bind(BridgeAddressProvider.class).to(Preferences.class);

bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class);
bind(NetworkFilter.class).to(CoreNetworkFilter.class).in(Singleton.class);

bind(File.class).annotatedWith(named(STORAGE_DIR)).toInstance(config.storageDir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.core.btc.BitcoinModule;
import bisq.core.dao.DaoModule;
import bisq.core.filter.FilterModule;
import bisq.core.network.CoreNetworkFilter;
import bisq.core.network.p2p.seed.DefaultSeedNodeRepository;
import bisq.core.offer.OfferModule;
import bisq.core.proto.network.CoreNetworkProtoResolver;
Expand All @@ -33,6 +34,7 @@
import bisq.network.crypto.EncryptionServiceModule;
import bisq.network.p2p.P2PModule;
import bisq.network.p2p.network.BridgeAddressProvider;
import bisq.network.p2p.network.NetworkFilter;
import bisq.network.p2p.seed.SeedNodeRepository;

import bisq.common.ClockWatcher;
Expand Down Expand Up @@ -73,6 +75,7 @@ protected void configure() {
bind(TorSetup.class).in(Singleton.class);

bind(SeedNodeRepository.class).to(DefaultSeedNodeRepository.class).in(Singleton.class);
bind(NetworkFilter.class).to(CoreNetworkFilter.class).in(Singleton.class);

bind(File.class).annotatedWith(named(STORAGE_DIR)).toInstance(config.storageDir);
bind(File.class).annotatedWith(named(KEY_STORAGE_DIR)).toInstance(config.keyStorageDir);
Expand Down
57 changes: 40 additions & 17 deletions core/src/main/java/bisq/core/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -47,7 +48,7 @@
@Value
public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
private final List<String> bannedOfferIds;
private final List<String> bannedNodeAddress;
private final List<String> nodeAddressesBannedFromTrading;
private final List<String> bannedAutoConfExplorers;
private final List<PaymentAccountFilter> bannedPaymentAccounts;
private final List<String> bannedCurrencies;
Expand Down Expand Up @@ -91,10 +92,14 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
// added at v1.3.8
private final boolean disableAutoConf;

// added at v1.5.5
private final Set<String> nodeAddressesBannedFromNetwork;
private final boolean disableApi;

// After we have created the signature from the filter data we clone it and apply the signature
static Filter cloneWithSig(Filter filter, String signatureAsBase64) {
return new Filter(filter.getBannedOfferIds(),
filter.getBannedNodeAddress(),
filter.getNodeAddressesBannedFromTrading(),
filter.getBannedPaymentAccounts(),
filter.getBannedCurrencies(),
filter.getBannedPaymentMethods(),
Expand All @@ -117,13 +122,15 @@ static Filter cloneWithSig(Filter filter, String signatureAsBase64) {
filter.getSignerPubKeyAsHex(),
filter.getBannedPrivilegedDevPubKeys(),
filter.isDisableAutoConf(),
filter.getBannedAutoConfExplorers());
filter.getBannedAutoConfExplorers(),
filter.getNodeAddressesBannedFromNetwork(),
filter.isDisableApi());
}

// Used for signature verification as we created the sig without the signatureAsBase64 field we set it to null again
static Filter cloneWithoutSig(Filter filter) {
return new Filter(filter.getBannedOfferIds(),
filter.getBannedNodeAddress(),
filter.getNodeAddressesBannedFromTrading(),
filter.getBannedPaymentAccounts(),
filter.getBannedCurrencies(),
filter.getBannedPaymentMethods(),
Expand All @@ -146,11 +153,13 @@ static Filter cloneWithoutSig(Filter filter) {
filter.getSignerPubKeyAsHex(),
filter.getBannedPrivilegedDevPubKeys(),
filter.isDisableAutoConf(),
filter.getBannedAutoConfExplorers());
filter.getBannedAutoConfExplorers(),
filter.getNodeAddressesBannedFromNetwork(),
filter.isDisableApi());
}

public Filter(List<String> bannedOfferIds,
List<String> bannedNodeAddress,
List<String> nodeAddressesBannedFromTrading,
List<PaymentAccountFilter> bannedPaymentAccounts,
List<String> bannedCurrencies,
List<String> bannedPaymentMethods,
Expand All @@ -170,9 +179,11 @@ public Filter(List<String> bannedOfferIds,
String signerPubKeyAsHex,
List<String> bannedPrivilegedDevPubKeys,
boolean disableAutoConf,
List<String> bannedAutoConfExplorers) {
List<String> bannedAutoConfExplorers,
Set<String> nodeAddressesBannedFromNetwork,
boolean disableApi) {
this(bannedOfferIds,
bannedNodeAddress,
nodeAddressesBannedFromTrading,
bannedPaymentAccounts,
bannedCurrencies,
bannedPaymentMethods,
Expand All @@ -195,7 +206,9 @@ public Filter(List<String> bannedOfferIds,
signerPubKeyAsHex,
bannedPrivilegedDevPubKeys,
disableAutoConf,
bannedAutoConfExplorers);
bannedAutoConfExplorers,
nodeAddressesBannedFromNetwork,
disableApi);
}


Expand All @@ -205,7 +218,7 @@ public Filter(List<String> bannedOfferIds,

@VisibleForTesting
public Filter(List<String> bannedOfferIds,
List<String> bannedNodeAddress,
List<String> nodeAddressesBannedFromTrading,
List<PaymentAccountFilter> bannedPaymentAccounts,
List<String> bannedCurrencies,
List<String> bannedPaymentMethods,
Expand All @@ -228,9 +241,11 @@ public Filter(List<String> bannedOfferIds,
String signerPubKeyAsHex,
List<String> bannedPrivilegedDevPubKeys,
boolean disableAutoConf,
List<String> bannedAutoConfExplorers) {
List<String> bannedAutoConfExplorers,
Set<String> nodeAddressesBannedFromNetwork,
boolean disableApi) {
this.bannedOfferIds = bannedOfferIds;
this.bannedNodeAddress = bannedNodeAddress;
this.nodeAddressesBannedFromTrading = nodeAddressesBannedFromTrading;
this.bannedPaymentAccounts = bannedPaymentAccounts;
this.bannedCurrencies = bannedCurrencies;
this.bannedPaymentMethods = bannedPaymentMethods;
Expand All @@ -254,6 +269,8 @@ public Filter(List<String> bannedOfferIds,
this.bannedPrivilegedDevPubKeys = bannedPrivilegedDevPubKeys;
this.disableAutoConf = disableAutoConf;
this.bannedAutoConfExplorers = bannedAutoConfExplorers;
this.nodeAddressesBannedFromNetwork = nodeAddressesBannedFromNetwork;
this.disableApi = disableApi;

// ownerPubKeyBytes can be null when called from tests
if (ownerPubKeyBytes != null) {
Expand All @@ -270,7 +287,7 @@ public protobuf.StoragePayload toProtoMessage() {
.collect(Collectors.toList());

protobuf.Filter.Builder builder = protobuf.Filter.newBuilder().addAllBannedOfferIds(bannedOfferIds)
.addAllBannedNodeAddress(bannedNodeAddress)
.addAllNodeAddressesBannedFromTrading(nodeAddressesBannedFromTrading)
.addAllBannedPaymentAccounts(paymentAccountFilterList)
.addAllBannedCurrencies(bannedCurrencies)
.addAllBannedPaymentMethods(bannedPaymentMethods)
Expand All @@ -291,7 +308,9 @@ public protobuf.StoragePayload toProtoMessage() {
.setCreationDate(creationDate)
.addAllBannedPrivilegedDevPubKeys(bannedPrivilegedDevPubKeys)
.setDisableAutoConf(disableAutoConf)
.addAllBannedAutoConfExplorers(bannedAutoConfExplorers);
.addAllBannedAutoConfExplorers(bannedAutoConfExplorers)
.addAllNodeAddressesBannedFromNetwork(nodeAddressesBannedFromNetwork)
.setDisableApi(disableApi);

Optional.ofNullable(signatureAsBase64).ifPresent(builder::setSignatureAsBase64);
Optional.ofNullable(extraDataMap).ifPresent(builder::putAllExtraData);
Expand All @@ -306,7 +325,7 @@ public static Filter fromProto(protobuf.Filter proto) {


return new Filter(ProtoUtil.protocolStringListToList(proto.getBannedOfferIdsList()),
ProtoUtil.protocolStringListToList(proto.getBannedNodeAddressList()),
ProtoUtil.protocolStringListToList(proto.getNodeAddressesBannedFromTradingList()),
bannedPaymentAccountsList,
ProtoUtil.protocolStringListToList(proto.getBannedCurrenciesList()),
ProtoUtil.protocolStringListToList(proto.getBannedPaymentMethodsList()),
Expand All @@ -329,7 +348,9 @@ public static Filter fromProto(protobuf.Filter proto) {
proto.getSignerPubKeyAsHex(),
ProtoUtil.protocolStringListToList(proto.getBannedPrivilegedDevPubKeysList()),
proto.getDisableAutoConf(),
ProtoUtil.protocolStringListToList(proto.getBannedAutoConfExplorersList())
ProtoUtil.protocolStringListToList(proto.getBannedAutoConfExplorersList()),
ProtoUtil.protocolStringListToSet(proto.getNodeAddressesBannedFromNetworkList()),
proto.getDisableApi()
);
}

Expand All @@ -347,7 +368,7 @@ public long getTTL() {
public String toString() {
return "Filter{" +
"\n bannedOfferIds=" + bannedOfferIds +
",\n bannedNodeAddress=" + bannedNodeAddress +
",\n nodeAddressesBannedFromTrading=" + nodeAddressesBannedFromTrading +
",\n bannedAutoConfExplorers=" + bannedAutoConfExplorers +
",\n bannedPaymentAccounts=" + bannedPaymentAccounts +
",\n bannedCurrencies=" + bannedCurrencies +
Expand All @@ -372,6 +393,8 @@ public String toString() {
",\n extraDataMap=" + extraDataMap +
",\n ownerPubKey=" + ownerPubKey +
",\n disableAutoConf=" + disableAutoConf +
",\n nodeAddressesBannedFromNetwork=" + nodeAddressesBannedFromNetwork +
",\n disableApi=" + disableApi +
"\n}";
}
}
11 changes: 10 additions & 1 deletion core/src/main/java/bisq/core/filter/FilterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.P2PService;
import bisq.network.p2p.P2PServiceListener;
import bisq.network.p2p.network.NetworkFilter;
import bisq.network.p2p.storage.HashMapChangedListener;
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;

Expand Down Expand Up @@ -115,6 +116,7 @@ public FilterManager(P2PService p2PService,
Preferences preferences,
Config config,
ProvidersRepository providersRepository,
NetworkFilter networkFilter,
@Named(Config.IGNORE_DEV_MSG) boolean ignoreDevMsg,
@Named(Config.USE_DEV_PRIVILEGE_KEYS) boolean useDevPrivilegeKeys) {
this.p2PService = p2PService;
Expand All @@ -131,6 +133,7 @@ public FilterManager(P2PService p2PService,
"029340c3e7d4bb0f9e651b5f590b434fecb6175aeaa57145c7804ff05d210e534f",
"034dc7530bf66ffd9580aa98031ea9a18ac2d269f7c56c0e71eca06105b9ed69f9");

networkFilter.setBannedNodeFunction(this::isNodeAddressBannedFromNetwork);
}


Expand Down Expand Up @@ -394,7 +397,13 @@ public boolean isOfferIdBanned(String offerId) {

public boolean isNodeAddressBanned(NodeAddress nodeAddress) {
return getFilter() != null &&
getFilter().getBannedNodeAddress().stream()
getFilter().getNodeAddressesBannedFromTrading().stream()
.anyMatch(e -> e.equals(nodeAddress.getFullAddress()));
}

public boolean isNodeAddressBannedFromNetwork(NodeAddress nodeAddress) {
return getFilter() != null &&
getFilter().getNodeAddressesBannedFromNetwork().stream()
.anyMatch(e -> e.equals(nodeAddress.getFullAddress()));
}

Expand Down
Loading