From 4ef8309153bdb6d2dacae353a6cd1e9474972788 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Mon, 25 Sep 2017 09:12:02 -0500 Subject: [PATCH] Add TakerVerifyOffersAgeWitnessHash to taker protocols --- .../PaymentAccountAgeWitnessService.java | 28 ++++++-- .../trade/protocol/BuyerAsTakerProtocol.java | 2 + .../trade/protocol/SellerAsTakerProtocol.java | 2 + .../TakerVerifyOffersAgeWitnessHash.java | 64 +++++++++++++++++++ ...tPaymentAccountAgeWitnessServiceTest.java} | 2 +- 5 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 core/src/main/java/io/bisq/core/trade/protocol/tasks/taker/TakerVerifyOffersAgeWitnessHash.java rename core/src/test/java/io/bisq/core/payment/{PaymentAccountAgeWitnessServiceTest.java => PaymentPaymentAccountAgeWitnessServiceTest.java} (98%) diff --git a/core/src/main/java/io/bisq/core/payment/PaymentAccountAgeWitnessService.java b/core/src/main/java/io/bisq/core/payment/PaymentAccountAgeWitnessService.java index ac1a68b52b..4ebcb0fd41 100644 --- a/core/src/main/java/io/bisq/core/payment/PaymentAccountAgeWitnessService.java +++ b/core/src/main/java/io/bisq/core/payment/PaymentAccountAgeWitnessService.java @@ -22,6 +22,7 @@ import io.bisq.common.crypto.Hash; import io.bisq.common.crypto.KeyRing; import io.bisq.common.crypto.Sig; import io.bisq.common.util.Utilities; +import io.bisq.core.payment.payload.PaymentAccountPayload; import io.bisq.core.trade.Trade; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ArrayUtils; @@ -46,10 +47,7 @@ public class PaymentAccountAgeWitnessService { } public PaymentAccountAgeWitness getPaymentAccountWitness(PaymentAccount paymentAccount, Trade trade) throws CryptoException { - byte[] ageWitnessInputData = paymentAccount.getAgeWitnessInputData(); - byte[] salt = paymentAccount.getSalt(); - final byte[] combined = ArrayUtils.addAll(ageWitnessInputData, salt); - byte[] hash = Sha256Hash.hash(combined); + byte[] hash = getWitnessHash(paymentAccount); byte[] signature = Sig.sign(keyRing.getSignatureKeyPair().getPrivate(), hash); long tradeDate = trade.getTakeOfferDate().getTime(); byte[] hashOfPubKey = Sha256Hash.hash(keyRing.getPubKeyRing().getSignaturePubKeyBytes()); @@ -59,6 +57,16 @@ public class PaymentAccountAgeWitnessService { tradeDate); } + public byte[] getWitnessHash(PaymentAccount paymentAccount) { + return getWitnessHash(paymentAccount.getPaymentAccountPayload(), paymentAccount.getSalt()); + } + + public byte[] getWitnessHash(PaymentAccountPayload paymentAccountPayload, byte[] salt) { + byte[] ageWitnessInputData = paymentAccountPayload.getAgeWitnessInputData(); + final byte[] combined = ArrayUtils.addAll(ageWitnessInputData, salt); + return Sha256Hash.hash(combined); + } + boolean verifyAgeWitness(byte[] peersAgeWitnessInputData, PaymentAccountAgeWitness witness, byte[] peersSalt, @@ -141,4 +149,16 @@ public class PaymentAccountAgeWitnessService { return false; } } + + public boolean verifyOffersAccountAgeWitness(PaymentAccountPayload paymentAccountPayload, + byte[] peersSalt, + byte[] offersWitness) { + byte[] witnessHash = getWitnessHash(paymentAccountPayload, peersSalt); + final boolean result = Arrays.equals(witnessHash, offersWitness); + if (!result) + log.warn("witnessHash is not matching peers offersWitness. " + + "witnessHash={}, offersWitness={}", Utilities.bytesAsHexString(witnessHash), + Utilities.bytesAsHexString(offersWitness)); + return false; + } } diff --git a/core/src/main/java/io/bisq/core/trade/protocol/BuyerAsTakerProtocol.java b/core/src/main/java/io/bisq/core/trade/protocol/BuyerAsTakerProtocol.java index 86b9e250e9..49dfd79ad5 100644 --- a/core/src/main/java/io/bisq/core/trade/protocol/BuyerAsTakerProtocol.java +++ b/core/src/main/java/io/bisq/core/trade/protocol/BuyerAsTakerProtocol.java @@ -27,6 +27,7 @@ import io.bisq.core.trade.messages.PayoutTxPublishedMessage; import io.bisq.core.trade.messages.PublishDepositTxRequest; import io.bisq.core.trade.messages.TradeMessage; import io.bisq.core.trade.protocol.tasks.CheckIfPeerIsBanned; +import io.bisq.core.trade.protocol.tasks.taker.TakerVerifyOffersAgeWitnessHash; import io.bisq.core.trade.protocol.tasks.buyer.BuyerProcessPayoutTxPublishedMessage; import io.bisq.core.trade.protocol.tasks.buyer.BuyerSendCounterCurrencyTransferStartedMessage; import io.bisq.core.trade.protocol.tasks.buyer.BuyerSetupPayoutTxListener; @@ -125,6 +126,7 @@ public class BuyerAsTakerProtocol extends TradeProtocol implements BuyerProtocol TakerProcessPublishDepositTxRequest.class, CheckIfPeerIsBanned.class, TakerVerifyMakerAccount.class, + TakerVerifyOffersAgeWitnessHash.class, TakerVerifyMakerFeePayment.class, TakerVerifyAndSignContract.class, BuyerAsTakerSignAndPublishDepositTx.class, diff --git a/core/src/main/java/io/bisq/core/trade/protocol/SellerAsTakerProtocol.java b/core/src/main/java/io/bisq/core/trade/protocol/SellerAsTakerProtocol.java index f8cb2c59cf..1625cde692 100644 --- a/core/src/main/java/io/bisq/core/trade/protocol/SellerAsTakerProtocol.java +++ b/core/src/main/java/io/bisq/core/trade/protocol/SellerAsTakerProtocol.java @@ -27,6 +27,7 @@ import io.bisq.core.trade.messages.CounterCurrencyTransferStartedMessage; import io.bisq.core.trade.messages.PublishDepositTxRequest; import io.bisq.core.trade.messages.TradeMessage; import io.bisq.core.trade.protocol.tasks.CheckIfPeerIsBanned; +import io.bisq.core.trade.protocol.tasks.taker.TakerVerifyOffersAgeWitnessHash; import io.bisq.core.trade.protocol.tasks.seller.SellerBroadcastPayoutTx; import io.bisq.core.trade.protocol.tasks.seller.SellerProcessCounterCurrencyTransferStartedMessage; import io.bisq.core.trade.protocol.tasks.seller.SellerSendPayoutTxPublishedMessage; @@ -120,6 +121,7 @@ public class SellerAsTakerProtocol extends TradeProtocol implements SellerProtoc TakerProcessPublishDepositTxRequest.class, CheckIfPeerIsBanned.class, TakerVerifyMakerAccount.class, + TakerVerifyOffersAgeWitnessHash.class, TakerVerifyMakerFeePayment.class, TakerVerifyAndSignContract.class, SellerAsTakerSignAndPublishDepositTx.class, diff --git a/core/src/main/java/io/bisq/core/trade/protocol/tasks/taker/TakerVerifyOffersAgeWitnessHash.java b/core/src/main/java/io/bisq/core/trade/protocol/tasks/taker/TakerVerifyOffersAgeWitnessHash.java new file mode 100644 index 0000000000..dba9c59263 --- /dev/null +++ b/core/src/main/java/io/bisq/core/trade/protocol/tasks/taker/TakerVerifyOffersAgeWitnessHash.java @@ -0,0 +1,64 @@ +/* + * 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 . + */ + +package io.bisq.core.trade.protocol.tasks.taker; + +import io.bisq.common.taskrunner.TaskRunner; +import io.bisq.common.util.Utilities; +import io.bisq.core.offer.OfferPayload; +import io.bisq.core.payment.payload.PaymentAccountPayload; +import io.bisq.core.trade.Trade; +import io.bisq.core.trade.protocol.tasks.TradeTask; +import lombok.extern.slf4j.Slf4j; + +import java.util.Map; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +@Slf4j +public class TakerVerifyOffersAgeWitnessHash extends TradeTask { + + @SuppressWarnings({"WeakerAccess", "unused"}) + public TakerVerifyOffersAgeWitnessHash(TaskRunner taskHandler, Trade trade) { + super(taskHandler, trade); + } + + @Override + protected void run() { + try { + runInterceptHook(); + + final Map extraDataMap = trade.getOffer().getExtraDataMap(); + final byte[] accountSalt = processModel.getTradingPeer().getAccountSalt(); + if (extraDataMap != null && + extraDataMap.containsKey(OfferPayload.ACCOUNT_AGE_WITNESS) && + accountSalt != null) { + final String offersWitness = extraDataMap.get(OfferPayload.ACCOUNT_AGE_WITNESS); + final PaymentAccountPayload paymentAccountPayload = checkNotNull(processModel.getTradingPeer().getPaymentAccountPayload() + , "Peers paymentAccountPayload must nto be null"); + checkArgument(processModel.getPaymentAccountAgeWitnessService() + .verifyOffersAccountAgeWitness(paymentAccountPayload, + accountSalt, + Utilities.decodeFromHex(offersWitness)), ""); + } + complete(); + } catch (Throwable t) { + failed(t); + } + } +} diff --git a/core/src/test/java/io/bisq/core/payment/PaymentAccountAgeWitnessServiceTest.java b/core/src/test/java/io/bisq/core/payment/PaymentPaymentAccountAgeWitnessServiceTest.java similarity index 98% rename from core/src/test/java/io/bisq/core/payment/PaymentAccountAgeWitnessServiceTest.java rename to core/src/test/java/io/bisq/core/payment/PaymentPaymentAccountAgeWitnessServiceTest.java index f7f2e17be1..66c618b56c 100644 --- a/core/src/test/java/io/bisq/core/payment/PaymentAccountAgeWitnessServiceTest.java +++ b/core/src/test/java/io/bisq/core/payment/PaymentPaymentAccountAgeWitnessServiceTest.java @@ -41,7 +41,7 @@ import static org.junit.Assert.assertTrue; * along with Bisq. If not, see . */ @Slf4j -public class PaymentAccountAgeWitnessServiceTest { +public class PaymentPaymentAccountAgeWitnessServiceTest { private PublicKey publicKey; private KeyPair keypair;