Skip to content

Commit

Permalink
Apply ExcludeForHash annotation to Filter.
Browse files Browse the repository at this point in the history
By that the newly added fields will not alter the byte array used for the hash and the signature, thus it will not cause issues with removing an old filter which did not inculde those new fields.

Signed-off-by: HenrikJannsen <[email protected]>
  • Loading branch information
HenrikJannsen committed Jun 8, 2024
1 parent d352f1f commit c70cc4a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
28 changes: 26 additions & 2 deletions core/src/main/java/bisq/core/filter/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
import bisq.network.p2p.storage.payload.ExpirablePayload;
import bisq.network.p2p.storage.payload.ProtectedStoragePayload;

import bisq.common.ExcludeForHash;
import bisq.common.ExcludeForHashAwareProto;
import bisq.common.crypto.Sig;
import bisq.common.proto.ProtoUtil;
import bisq.common.proto.network.GetDataResponsePriority;
import bisq.common.util.CollectionUtils;
import bisq.common.util.ExtraDataMapValidator;
import bisq.common.util.Utilities;

import protobuf.StoragePayload;

import com.google.protobuf.ByteString;

import com.google.common.annotations.VisibleForTesting;
Expand All @@ -49,7 +53,7 @@
@Slf4j
@Getter
@EqualsAndHashCode
public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
public final class Filter implements ProtectedStoragePayload, ExpirablePayload, ExcludeForHashAwareProto {
public static final long TTL = TimeUnit.DAYS.toMillis(180);

private final List<String> bannedOfferIds;
Expand Down Expand Up @@ -124,7 +128,9 @@ public final class Filter implements ProtectedStoragePayload, ExpirablePayload {
private final List<PaymentAccountFilter> delayedPayoutPaymentAccounts;

// Added at v 1.9.16
@ExcludeForHash
private final List<String> addedBtcNodes;
@ExcludeForHash
private final List<String> addedSeedNodes;

// After we have created the signature from the filter data we clone it and apply the signature
Expand Down Expand Up @@ -379,6 +385,24 @@ public Filter(List<String> bannedOfferIds,

@Override
public protobuf.StoragePayload toProtoMessage() {
return toProto(false);
}

@Override
public protobuf.StoragePayload toProto(boolean serializeForHash) {
return resolveProto(serializeForHash);
}

@Override
public protobuf.StoragePayload.Builder getBuilder(boolean serializeForHash) {
return StoragePayload.newBuilder().setFilter(toFilterProto(serializeForHash));
}

private protobuf.Filter toFilterProto(boolean serializeForHash) {
return resolveBuilder(getFilterBuilder(serializeForHash), serializeForHash).build();
}

private protobuf.Filter.Builder getFilterBuilder(boolean serializeForHash) {
List<protobuf.PaymentAccountFilter> paymentAccountFilterList = bannedPaymentAccounts.stream()
.map(PaymentAccountFilter::toProtoMessage)
.collect(Collectors.toList());
Expand Down Expand Up @@ -426,7 +450,7 @@ public protobuf.StoragePayload toProtoMessage() {
Optional.ofNullable(signatureAsBase64).ifPresent(builder::setSignatureAsBase64);
Optional.ofNullable(extraDataMap).ifPresent(builder::putAllExtraData);

return protobuf.StoragePayload.newBuilder().setFilter(builder).build();
return builder;
}

public static Filter fromProto(protobuf.Filter proto) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/bisq/core/filter/FilterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,7 @@ private ECKey toECKey(String privKeyString) {
}

private Sha256Hash getSha256Hash(Filter filter) {
byte[] filterData = filter.serializeForHash();
return Sha256Hash.of(filterData);
return Sha256Hash.of(filter.serializeForHash());
}

private String getPubKeyAsHex(ECKey ecKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.filter;

import bisq.common.crypto.Sig;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.Security;

import java.util.Arrays;

import org.mockito.junit.jupiter.MockitoExtension;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(MockitoExtension.class)
public class FilterWithExcludeForHashFieldTests {
static {
Security.addProvider(new BouncyCastleProvider());
}

private final PublicKey ownerPublicKey;

public FilterWithExcludeForHashFieldTests() throws NoSuchAlgorithmException, NoSuchProviderException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(Sig.KEY_ALGO, "BC");
KeyPair ownerKeyPair = keyPairGenerator.generateKeyPair();
ownerPublicKey = ownerKeyPair.getPublic();
}

@Test
void testSerializeForHashAnnotation() {
Filter filterWithoutSig = TestFilter.createFilter(ownerPublicKey, "invalidPubKeyAsHex");
byte[] serializeForHashBytes = filterWithoutSig.serializeForHash();
byte[] serializeBytes = filterWithoutSig.serialize();
assertFalse(Arrays.equals(serializeForHashBytes, serializeBytes));
assertTrue(serializeBytes.length > serializeForHashBytes.length);
}
}
4 changes: 2 additions & 2 deletions core/src/test/java/bisq/core/filter/TestFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public static Filter createFilter(PublicKey ownerPublicKey, String signerPubKeyA
1,
1,
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
List.of("test1.onion:1221"),
List.of("test2.onion:1221")
);
}

Expand Down

0 comments on commit c70cc4a

Please sign in to comment.