diff --git a/common/src/main/proto/pb.proto b/common/src/main/proto/pb.proto index 43911ea5f75..b8c9a2b4c5d 100644 --- a/common/src/main/proto/pb.proto +++ b/common/src/main/proto/pb.proto @@ -722,8 +722,14 @@ message AccountAgeWitness { } message SignedWitness { - bool signed_by_arbitrator = 1; - bytes witness_hash = 2; + enum VerificationMethod { + PB_ERROR = 0; + ARBITRATOR = 1; + TRADE = 2; + } + + VerificationMethod verification_method = 1; + bytes account_age_witness_hash = 2; bytes signature = 3; bytes signer_pub_key = 4; bytes witness_owner_pub_key = 5; diff --git a/core/src/main/java/bisq/core/account/sign/SignedWitness.java b/core/src/main/java/bisq/core/account/sign/SignedWitness.java index 111bb195cbd..e5af940470c 100644 --- a/core/src/main/java/bisq/core/account/sign/SignedWitness.java +++ b/core/src/main/java/bisq/core/account/sign/SignedWitness.java @@ -26,6 +26,7 @@ import bisq.common.app.Capabilities; import bisq.common.app.Capability; import bisq.common.crypto.Hash; +import bisq.common.proto.ProtoUtil; import bisq.common.proto.persistable.PersistableEnvelope; import bisq.common.util.Utilities; @@ -46,10 +47,24 @@ @Value public class SignedWitness implements LazyProcessedPayload, PersistableNetworkPayload, PersistableEnvelope, DateTolerantPayload, CapabilityRequiringPayload { + + public enum VerificationMethod { + ARBITRATOR, + TRADE; + + public static SignedWitness.VerificationMethod fromProto(protobuf.SignedWitness.VerificationMethod method) { + return ProtoUtil.enumFromProto(SignedWitness.VerificationMethod.class, method.name()); + } + + public static protobuf.SignedWitness.VerificationMethod toProtoMessage(SignedWitness.VerificationMethod method) { + return protobuf.SignedWitness.VerificationMethod.valueOf(method.name()); + } + } + private static final long TOLERANCE = TimeUnit.DAYS.toMillis(1); - private final boolean signedByArbitrator; - private final byte[] witnessHash; + private final VerificationMethod verificationMethod; + private final byte[] accountAgeWitnessHash; private final byte[] signature; private final byte[] signerPubKey; private final byte[] witnessOwnerPubKey; @@ -58,15 +73,15 @@ public class SignedWitness implements LazyProcessedPayload, PersistableNetworkPa transient private final byte[] hash; - public SignedWitness(boolean signedByArbitrator, - byte[] witnessHash, + public SignedWitness(VerificationMethod verificationMethod, + byte[] accountAgeWitnessHash, byte[] signature, byte[] signerPubKey, byte[] witnessOwnerPubKey, long date, long tradeAmount) { - this.signedByArbitrator = signedByArbitrator; - this.witnessHash = witnessHash.clone(); + this.verificationMethod = verificationMethod; + this.accountAgeWitnessHash = accountAgeWitnessHash.clone(); this.signature = signature.clone(); this.signerPubKey = signerPubKey.clone(); this.witnessOwnerPubKey = witnessOwnerPubKey.clone(); @@ -80,7 +95,7 @@ public SignedWitness(boolean signedByArbitrator, // same peer to add more security as if that one would be colluding it would be not detected anyway. The total // number of signed trades with different peers is still available and can be considered more valuable data for // security. - byte[] data = Utilities.concatenateByteArrays(witnessHash, signature); + byte[] data = Utilities.concatenateByteArrays(accountAgeWitnessHash, signature); data = Utilities.concatenateByteArrays(data, signerPubKey); hash = Hash.getSha256Ripemd160hash(data); } @@ -93,8 +108,8 @@ public SignedWitness(boolean signedByArbitrator, @Override public protobuf.PersistableNetworkPayload toProtoMessage() { final protobuf.SignedWitness.Builder builder = protobuf.SignedWitness.newBuilder() - .setSignedByArbitrator(signedByArbitrator) - .setWitnessHash(ByteString.copyFrom(witnessHash)) + .setVerificationMethod(VerificationMethod.toProtoMessage(verificationMethod)) + .setAccountAgeWitnessHash(ByteString.copyFrom(accountAgeWitnessHash)) .setSignature(ByteString.copyFrom(signature)) .setSignerPubKey(ByteString.copyFrom(signerPubKey)) .setWitnessOwnerPubKey(ByteString.copyFrom(witnessOwnerPubKey)) @@ -108,8 +123,9 @@ protobuf.SignedWitness toProtoSignedWitness() { } public static SignedWitness fromProto(protobuf.SignedWitness proto) { - return new SignedWitness(proto.getSignedByArbitrator(), - proto.getWitnessHash().toByteArray(), + return new SignedWitness( + SignedWitness.VerificationMethod.fromProto(proto.getVerificationMethod()), + proto.getAccountAgeWitnessHash().toByteArray(), proto.getSignature().toByteArray(), proto.getSignerPubKey().toByteArray(), proto.getWitnessOwnerPubKey().toByteArray(), @@ -124,7 +140,7 @@ public static SignedWitness fromProto(protobuf.SignedWitness proto) { @Override public boolean isDateInTolerance(Clock clock) { - // We don't allow older or newer then 1 day. + // We don't allow older or newer than 1 day. // Preventing forward dating is also important to protect against a sophisticated attack return Math.abs(new Date().getTime() - date) <= TOLERANCE; } @@ -145,6 +161,9 @@ public byte[] getHash() { return hash; } + public boolean isSignedByArbitrator() { + return verificationMethod == VerificationMethod.ARBITRATOR; + } /////////////////////////////////////////////////////////////////////////////////////////// // Getters @@ -157,8 +176,8 @@ P2PDataStorage.ByteArray getHashAsByteArray() { @Override public String toString() { return "SignedWitness{" + - ",\n signedByArbitrator=" + signedByArbitrator + - ",\n witnessHash=" + Utilities.bytesAsHexString(witnessHash) + + ",\n verificationMethod=" + verificationMethod + + ",\n witnessHash=" + Utilities.bytesAsHexString(accountAgeWitnessHash) + ",\n signature=" + Utilities.bytesAsHexString(signature) + ",\n signerPubKey=" + Utilities.bytesAsHexString(signerPubKey) + ",\n witnessOwnerPubKey=" + Utilities.bytesAsHexString(witnessOwnerPubKey) + diff --git a/core/src/main/java/bisq/core/account/sign/SignedWitnessService.java b/core/src/main/java/bisq/core/account/sign/SignedWitnessService.java index f847a3d13e6..6991b86b0d9 100644 --- a/core/src/main/java/bisq/core/account/sign/SignedWitnessService.java +++ b/core/src/main/java/bisq/core/account/sign/SignedWitnessService.java @@ -142,14 +142,14 @@ public SignedWitness signAccountAgeWitness(Coin tradeAmount, AccountAgeWitness accountAgeWitness, ECKey key, PublicKey peersPubKey) { - if (isValidAccountAgeWitness(accountAgeWitness)) { + if (isSignedAccountAgeWitness(accountAgeWitness)) { log.warn("Arbitrator trying to sign already signed accountagewitness {}", accountAgeWitness.toString()); return null; } String accountAgeWitnessHashAsHex = Utilities.encodeToHex(accountAgeWitness.getHash()); String signatureBase64 = key.signMessage(accountAgeWitnessHashAsHex); - SignedWitness signedWitness = new SignedWitness(true, + SignedWitness signedWitness = new SignedWitness(SignedWitness.VerificationMethod.ARBITRATOR, accountAgeWitness.getHash(), signatureBase64.getBytes(Charsets.UTF_8), key.getPubKey(), @@ -165,13 +165,13 @@ public SignedWitness signAccountAgeWitness(Coin tradeAmount, public SignedWitness signAccountAgeWitness(Coin tradeAmount, AccountAgeWitness accountAgeWitness, PublicKey peersPubKey) throws CryptoException { - if (isValidAccountAgeWitness(accountAgeWitness)) { + if (isSignedAccountAgeWitness(accountAgeWitness)) { log.warn("Trader trying to sign already signed accountagewitness {}", accountAgeWitness.toString()); return null; } byte[] signature = Sig.sign(keyRing.getSignatureKeyPair().getPrivate(), accountAgeWitness.getHash()); - SignedWitness signedWitness = new SignedWitness(false, + SignedWitness signedWitness = new SignedWitness(SignedWitness.VerificationMethod.TRADE, accountAgeWitness.getHash(), signature, keyRing.getSignatureKeyPair().getPublic().getEncoded(), @@ -193,7 +193,7 @@ public boolean verifySignature(SignedWitness signedWitness) { private boolean verifySignatureWithECKey(SignedWitness signedWitness) { try { - String message = Utilities.encodeToHex(signedWitness.getWitnessHash()); + String message = Utilities.encodeToHex(signedWitness.getAccountAgeWitnessHash()); String signatureBase64 = new String(signedWitness.getSignature(), Charsets.UTF_8); ECKey key = ECKey.fromPublicOnly(signedWitness.getSignerPubKey()); if (arbitratorManager.isPublicKeyInList(Utilities.encodeToHex(key.getPubKey()))) { @@ -213,7 +213,7 @@ private boolean verifySignatureWithECKey(SignedWitness signedWitness) { private boolean verifySignatureWithDSAKey(SignedWitness signedWitness) { try { PublicKey signaturePubKey = Sig.getPublicKeyFromBytes(signedWitness.getSignerPubKey()); - Sig.verify(signaturePubKey, signedWitness.getWitnessHash(), signedWitness.getSignature()); + Sig.verify(signaturePubKey, signedWitness.getAccountAgeWitnessHash(), signedWitness.getSignature()); return true; } catch (CryptoException e) { log.warn("verifySignature signedWitness failed. signedWitness={}", signedWitness); @@ -224,7 +224,7 @@ private boolean verifySignatureWithDSAKey(SignedWitness signedWitness) { private Set getSignedWitnessSet(AccountAgeWitness accountAgeWitness) { return signedWitnessMap.values().stream() - .filter(e -> Arrays.equals(e.getWitnessHash(), accountAgeWitness.getHash())) + .filter(e -> Arrays.equals(e.getAccountAgeWitnessHash(), accountAgeWitness.getHash())) .collect(Collectors.toSet()); } @@ -232,7 +232,7 @@ private Set getSignedWitnessSet(AccountAgeWitness accountAgeWitne public Set getArbitratorsSignedWitnessSet(AccountAgeWitness accountAgeWitness) { return signedWitnessMap.values().stream() .filter(SignedWitness::isSignedByArbitrator) - .filter(e -> Arrays.equals(e.getWitnessHash(), accountAgeWitness.getHash())) + .filter(e -> Arrays.equals(e.getAccountAgeWitnessHash(), accountAgeWitness.getHash())) .collect(Collectors.toSet()); } @@ -240,7 +240,7 @@ public Set getArbitratorsSignedWitnessSet(AccountAgeWitness accou public Set getTrustedPeerSignedWitnessSet(AccountAgeWitness accountAgeWitness) { return signedWitnessMap.values().stream() .filter(e -> !e.isSignedByArbitrator()) - .filter(e -> Arrays.equals(e.getWitnessHash(), accountAgeWitness.getHash())) + .filter(e -> Arrays.equals(e.getAccountAgeWitnessHash(), accountAgeWitness.getHash())) .collect(Collectors.toSet()); } @@ -254,18 +254,27 @@ private Set getSignedWitnessSetByOwnerPubKey(byte[] ownerPubKey, .collect(Collectors.toSet()); } + public boolean isSignedAccountAgeWitness(AccountAgeWitness accountAgeWitness) { + return isSignerAccountAgeWitness(accountAgeWitness, new Date().getTime() + SIGNER_AGE); + } + + public boolean isSignerAccountAgeWitness(AccountAgeWitness accountAgeWitness) { + return isSignerAccountAgeWitness(accountAgeWitness, new Date().getTime()); + } + /** - * Checks whether the accountAgeWitness has a valid signature from a peer/arbitrator. + * Checks whether the accountAgeWitness has a valid signature from a peer/arbitrator and is allowed to sign + * other accounts. * * @param accountAgeWitness accountAgeWitness - * @return true if accountAgeWitness is valid, false otherwise. + * @param time time of signing + * @return true if accountAgeWitness is allowed to sign at time, false otherwise. */ - public boolean isValidAccountAgeWitness(AccountAgeWitness accountAgeWitness) { + private boolean isSignerAccountAgeWitness(AccountAgeWitness accountAgeWitness, long time) { Stack excludedPubKeys = new Stack<>(); - long now = new Date().getTime(); Set signedWitnessSet = getSignedWitnessSet(accountAgeWitness); for (SignedWitness signedWitness : signedWitnessSet) { - if (isValidSignedWitnessInternal(signedWitness, now, excludedPubKeys)) { + if (isValidSignerWitnessInternal(signedWitness, time, excludedPubKeys)) { return true; } } @@ -281,7 +290,7 @@ public boolean isValidAccountAgeWitness(AccountAgeWitness accountAgeWitness) { * @param excludedPubKeys stack to prevent recursive loops * @return true if signedWitness is valid, false otherwise. */ - private boolean isValidSignedWitnessInternal(SignedWitness signedWitness, + private boolean isValidSignerWitnessInternal(SignedWitness signedWitness, long childSignedWitnessDateMillis, Stack excludedPubKeys) { if (!verifySignature(signedWitness)) { @@ -303,7 +312,7 @@ private boolean isValidSignedWitnessInternal(SignedWitness signedWitness, // Iterate over signedWitness signers Set signerSignedWitnessSet = getSignedWitnessSetByOwnerPubKey(signedWitness.getSignerPubKey(), excludedPubKeys); for (SignedWitness signerSignedWitness : signerSignedWitnessSet) { - if (isValidSignedWitnessInternal(signerSignedWitness, signedWitness.getDate(), excludedPubKeys)) { + if (isValidSignerWitnessInternal(signerSignedWitness, signedWitness.getDate(), excludedPubKeys)) { return true; } } diff --git a/core/src/main/java/bisq/core/account/witness/AccountAgeWitnessService.java b/core/src/main/java/bisq/core/account/witness/AccountAgeWitnessService.java index 89cc9074af4..470b5bd0617 100644 --- a/core/src/main/java/bisq/core/account/witness/AccountAgeWitnessService.java +++ b/core/src/main/java/bisq/core/account/witness/AccountAgeWitnessService.java @@ -619,7 +619,7 @@ public List getTraderPaymentAccounts(long safeDate, PaymentMetho .filter(this::isBuyerWinner) .flatMap(this::getTraderData) .filter(traderDataItem -> - !signedWitnessService.isValidAccountAgeWitness(traderDataItem.getAccountAgeWitness())) + !signedWitnessService.isSignedAccountAgeWitness(traderDataItem.getAccountAgeWitness())) .filter(traderDataItem -> traderDataItem.getAccountAgeWitness().getDate() < safeDate) .distinct() .collect(Collectors.toList()); @@ -662,28 +662,20 @@ private Stream getTraderData(Dispute dispute) { return Stream.of(buyerData, sellerData); } - // Check if my account has a signed witness - public boolean myHasSignedWitness(PaymentAccountPayload paymentAccountPayload) { - return signedWitnessService.isValidAccountAgeWitness(getMyWitness(paymentAccountPayload)); - } - public boolean hasSignedWitness(Offer offer) { return findWitness(offer) - .map(signedWitnessService::isValidAccountAgeWitness) + .map(signedWitnessService::isSignedAccountAgeWitness) .orElse(false); } public boolean peerHasSignedWitness(Trade trade) { return findTradePeerWitness(trade) - .map(signedWitnessService::isValidAccountAgeWitness) + .map(signedWitnessService::isSignedAccountAgeWitness) .orElse(false); } public boolean accountIsSigner(AccountAgeWitness accountAgeWitness) { - if (signedWitnessService.isSignedByArbitrator(accountAgeWitness)) { - return true; - } - return getWitnessSignAge(accountAgeWitness, new Date()) > SignedWitnessService.SIGNER_AGE; + return signedWitnessService.isSignerAccountAgeWitness(accountAgeWitness); } public SignState getSignState(Offer offer) { diff --git a/core/src/main/java/bisq/core/app/BisqSetup.java b/core/src/main/java/bisq/core/app/BisqSetup.java index 95aa8cfcf29..9562a23b2fb 100644 --- a/core/src/main/java/bisq/core/app/BisqSetup.java +++ b/core/src/main/java/bisq/core/app/BisqSetup.java @@ -775,7 +775,7 @@ private boolean isSignedWitnessOfMineWithState(PersistableNetworkPayload payload // Check if new signed witness is for one of my own accounts return user.getPaymentAccounts().stream() .filter(a -> PaymentMethod.hasChargebackRisk(a.getPaymentMethod(), a.getTradeCurrencies())) - .filter(a -> Arrays.equals(((SignedWitness) payload).getWitnessHash(), + .filter(a -> Arrays.equals(((SignedWitness) payload).getAccountAgeWitnessHash(), accountAgeWitnessService.getMyWitness(a.getPaymentAccountPayload()).getHash())) .anyMatch(a -> accountAgeWitnessService.getSignState(accountAgeWitnessService.getMyWitness( a.getPaymentAccountPayload())).equals(state)); diff --git a/core/src/test/java/bisq/core/account/sign/SignedWitnessServiceTest.java b/core/src/test/java/bisq/core/account/sign/SignedWitnessServiceTest.java index 711bbb7eba5..e5a58a94cbd 100644 --- a/core/src/test/java/bisq/core/account/sign/SignedWitnessServiceTest.java +++ b/core/src/test/java/bisq/core/account/sign/SignedWitnessServiceTest.java @@ -40,6 +40,8 @@ import org.junit.Before; import org.junit.Test; +import static bisq.core.account.sign.SignedWitness.VerificationMethod.ARBITRATOR; +import static bisq.core.account.sign.SignedWitness.VerificationMethod.TRADE; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.any; @@ -112,51 +114,51 @@ public void setup() throws Exception { @Test public void testIsValidAccountAgeWitnessOk() { - SignedWitness sw1 = new SignedWitness(true, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); - SignedWitness sw2 = new SignedWitness(false, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); - SignedWitness sw3 = new SignedWitness(false, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); + SignedWitness sw1 = new SignedWitness(ARBITRATOR, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); + SignedWitness sw2 = new SignedWitness(TRADE, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); + SignedWitness sw3 = new SignedWitness(TRADE, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); signedWitnessService.addToMap(sw1); signedWitnessService.addToMap(sw2); signedWitnessService.addToMap(sw3); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew1)); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew2)); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew3)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew1)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew2)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew3)); } @Test public void testIsValidAccountAgeWitnessArbitratorSignatureProblem() { signature1 = new byte[]{1, 2, 3}; - SignedWitness sw1 = new SignedWitness(true, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); - SignedWitness sw2 = new SignedWitness(false, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); - SignedWitness sw3 = new SignedWitness(false, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); + SignedWitness sw1 = new SignedWitness(ARBITRATOR, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); + SignedWitness sw2 = new SignedWitness(TRADE, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); + SignedWitness sw3 = new SignedWitness(TRADE, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); signedWitnessService.addToMap(sw1); signedWitnessService.addToMap(sw2); signedWitnessService.addToMap(sw3); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew1)); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew2)); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew3)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew1)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew2)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew3)); } @Test public void testIsValidAccountAgeWitnessPeerSignatureProblem() { signature2 = new byte[]{1, 2, 3}; - SignedWitness sw1 = new SignedWitness(true, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); - SignedWitness sw2 = new SignedWitness(false, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); - SignedWitness sw3 = new SignedWitness(false, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); + SignedWitness sw1 = new SignedWitness(ARBITRATOR, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); + SignedWitness sw2 = new SignedWitness(TRADE, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); + SignedWitness sw3 = new SignedWitness(TRADE, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); signedWitnessService.addToMap(sw1); signedWitnessService.addToMap(sw2); signedWitnessService.addToMap(sw3); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew1)); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew2)); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew3)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew1)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew2)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew3)); } @Test @@ -167,17 +169,17 @@ public void testIsValidSelfSignatureOk() throws Exception { signature2 = Sig.sign(peer1KeyPair.getPrivate(), Utilities.encodeToHex(account2DataHash).getBytes(Charsets.UTF_8)); signature3 = Sig.sign(peer1KeyPair.getPrivate(), Utilities.encodeToHex(account3DataHash).getBytes(Charsets.UTF_8)); - SignedWitness sw1 = new SignedWitness(true, account1DataHash, signature1, signer1PubKey, signer2PubKey, date1, tradeAmount1); - SignedWitness sw2 = new SignedWitness(false, account2DataHash, signature2, signer2PubKey, signer2PubKey, date2, tradeAmount2); - SignedWitness sw3 = new SignedWitness(false, account3DataHash, signature3, signer2PubKey, signer2PubKey, date3, tradeAmount3); + SignedWitness sw1 = new SignedWitness(ARBITRATOR, account1DataHash, signature1, signer1PubKey, signer2PubKey, date1, tradeAmount1); + SignedWitness sw2 = new SignedWitness(TRADE, account2DataHash, signature2, signer2PubKey, signer2PubKey, date2, tradeAmount2); + SignedWitness sw3 = new SignedWitness(TRADE, account3DataHash, signature3, signer2PubKey, signer2PubKey, date3, tradeAmount3); signedWitnessService.addToMap(sw1); signedWitnessService.addToMap(sw2); signedWitnessService.addToMap(sw3); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew1)); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew2)); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew3)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew1)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew2)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew3)); } @Test @@ -192,51 +194,51 @@ public void testIsValidSimpleLoopSignatureProblem() throws Exception { signature2 = Sig.sign(peer1KeyPair.getPrivate(), Utilities.encodeToHex(account2DataHash).getBytes(Charsets.UTF_8)); signature3 = Sig.sign(peer2KeyPair.getPrivate(), Utilities.encodeToHex(account3DataHash).getBytes(Charsets.UTF_8)); - SignedWitness sw1 = new SignedWitness(true, account1DataHash, signature1, signer1PubKey, user1PubKey, date1, tradeAmount1); - SignedWitness sw2 = new SignedWitness(false, account2DataHash, signature2, user1PubKey, user2PubKey, date2, tradeAmount2); - SignedWitness sw3 = new SignedWitness(false, account3DataHash, signature3, user2PubKey, user1PubKey, date3, tradeAmount3); + SignedWitness sw1 = new SignedWitness(ARBITRATOR, account1DataHash, signature1, signer1PubKey, user1PubKey, date1, tradeAmount1); + SignedWitness sw2 = new SignedWitness(TRADE, account2DataHash, signature2, user1PubKey, user2PubKey, date2, tradeAmount2); + SignedWitness sw3 = new SignedWitness(TRADE, account3DataHash, signature3, user2PubKey, user1PubKey, date3, tradeAmount3); signedWitnessService.addToMap(sw1); signedWitnessService.addToMap(sw2); signedWitnessService.addToMap(sw3); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew1)); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew2)); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew3)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew1)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew2)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew3)); } @Test public void testIsValidAccountAgeWitnessDateTooSoonProblem() { date3 = getTodayMinusNDays(SIGN_AGE_2 - 1); - SignedWitness sw1 = new SignedWitness(true, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); - SignedWitness sw2 = new SignedWitness(false, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); - SignedWitness sw3 = new SignedWitness(false, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); + SignedWitness sw1 = new SignedWitness(ARBITRATOR, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); + SignedWitness sw2 = new SignedWitness(TRADE, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); + SignedWitness sw3 = new SignedWitness(TRADE, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); signedWitnessService.addToMap(sw1); signedWitnessService.addToMap(sw2); signedWitnessService.addToMap(sw3); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew1)); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew2)); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew3)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew1)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew2)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew3)); } @Test public void testIsValidAccountAgeWitnessDateTooLateProblem() { date3 = getTodayMinusNDays(3); - SignedWitness sw1 = new SignedWitness(true, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); - SignedWitness sw2 = new SignedWitness(false, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); - SignedWitness sw3 = new SignedWitness(false, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); + SignedWitness sw1 = new SignedWitness(ARBITRATOR, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); + SignedWitness sw2 = new SignedWitness(TRADE, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); + SignedWitness sw3 = new SignedWitness(TRADE, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); signedWitnessService.addToMap(sw1); signedWitnessService.addToMap(sw2); signedWitnessService.addToMap(sw3); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew1)); - assertTrue(signedWitnessService.isValidAccountAgeWitness(aew2)); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew3)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew1)); + assertTrue(signedWitnessService.isSignerAccountAgeWitness(aew2)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew3)); } @@ -278,15 +280,15 @@ public void testIsValidAccountAgeWitnessEndlessLoop() throws Exception { long tradeAmount2 = 1001; long tradeAmount3 = 1001; - SignedWitness sw1 = new SignedWitness(false, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); - SignedWitness sw2 = new SignedWitness(false, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); - SignedWitness sw3 = new SignedWitness(false, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); + SignedWitness sw1 = new SignedWitness(TRADE, account1DataHash, signature1, signer1PubKey, witnessOwner1PubKey, date1, tradeAmount1); + SignedWitness sw2 = new SignedWitness(TRADE, account2DataHash, signature2, signer2PubKey, witnessOwner2PubKey, date2, tradeAmount2); + SignedWitness sw3 = new SignedWitness(TRADE, account3DataHash, signature3, signer3PubKey, witnessOwner3PubKey, date3, tradeAmount3); signedWitnessService.addToMap(sw1); signedWitnessService.addToMap(sw2); signedWitnessService.addToMap(sw3); - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew3)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew3)); } @@ -318,10 +320,10 @@ public void testIsValidAccountAgeWitnessLongLoop() throws Exception { } byte[] witnessOwnerPubKey = Sig.getPublicKeyBytes(signedKeyPair.getPublic()); long date = getTodayMinusNDays((iterations - i) * (SignedWitnessService.SIGNER_AGE_DAYS + 1)); - SignedWitness sw = new SignedWitness(i == 0, accountDataHash, signature, signerPubKey, witnessOwnerPubKey, date, tradeAmount1); + SignedWitness sw = new SignedWitness(i == 0 ? ARBITRATOR : TRADE, accountDataHash, signature, signerPubKey, witnessOwnerPubKey, date, tradeAmount1); signedWitnessService.addToMap(sw); } - assertFalse(signedWitnessService.isValidAccountAgeWitness(aew)); + assertFalse(signedWitnessService.isSignerAccountAgeWitness(aew)); } diff --git a/core/src/test/java/bisq/core/account/sign/SignedWitnessTest.java b/core/src/test/java/bisq/core/account/sign/SignedWitnessTest.java index 305a539151c..7b4381c81d9 100644 --- a/core/src/test/java/bisq/core/account/sign/SignedWitnessTest.java +++ b/core/src/test/java/bisq/core/account/sign/SignedWitnessTest.java @@ -13,6 +13,8 @@ import org.junit.Before; import org.junit.Test; +import static bisq.core.account.sign.SignedWitness.VerificationMethod.ARBITRATOR; +import static bisq.core.account.sign.SignedWitness.VerificationMethod.TRADE; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -33,17 +35,17 @@ public void setUp() { @Test public void testProtoRoundTrip() { - SignedWitness signedWitness = new SignedWitness(true, witnessHash, witnessHashSignature, arbitrator1Key.getPubKey(), witnessOwner1PubKey, Instant.now().getEpochSecond(), 100); + SignedWitness signedWitness = new SignedWitness(ARBITRATOR, witnessHash, witnessHashSignature, arbitrator1Key.getPubKey(), witnessOwner1PubKey, Instant.now().getEpochSecond(), 100); assertEquals(signedWitness, SignedWitness.fromProto(signedWitness.toProtoMessage().getSignedWitness())); } @Test public void isImmutable() { byte[] signerPubkey = arbitrator1Key.getPubKey(); - SignedWitness signedWitness = new SignedWitness(true, witnessHash, witnessHashSignature, signerPubkey, witnessOwner1PubKey, Instant.now().getEpochSecond(), 100); - byte[] originalWitnessHash = signedWitness.getWitnessHash().clone(); + SignedWitness signedWitness = new SignedWitness(TRADE, witnessHash, witnessHashSignature, signerPubkey, witnessOwner1PubKey, Instant.now().getEpochSecond(), 100); + byte[] originalWitnessHash = signedWitness.getAccountAgeWitnessHash().clone(); witnessHash[0] += 1; - assertArrayEquals(originalWitnessHash, signedWitness.getWitnessHash()); + assertArrayEquals(originalWitnessHash, signedWitness.getAccountAgeWitnessHash()); byte[] originalWitnessHashSignature = signedWitness.getSignature().clone(); witnessHashSignature[0] += 1;