diff --git a/build.gradle b/build.gradle index 7cac2454cb5..9697ea46ae7 100644 --- a/build.gradle +++ b/build.gradle @@ -209,7 +209,6 @@ configure(project(':p2p')) { testCompileOnly "org.projectlombok:lombok:$lombokVersion" testAnnotationProcessor "org.projectlombok:lombok:$lombokVersion" testCompile("org.mockito:mockito-core:$mockitoVersion") - } } diff --git a/common/src/main/java/bisq/common/crypto/KeyRing.java b/common/src/main/java/bisq/common/crypto/KeyRing.java index 7f965d702ae..df457dcb8d5 100644 --- a/common/src/main/java/bisq/common/crypto/KeyRing.java +++ b/common/src/main/java/bisq/common/crypto/KeyRing.java @@ -36,7 +36,7 @@ @EqualsAndHashCode @Slf4j @Singleton -public class KeyRing { +public final class KeyRing { private final KeyPair signatureKeyPair; private final KeyPair encryptionKeyPair; private final PubKeyRing pubKeyRing; diff --git a/common/src/main/java/bisq/common/crypto/PubKeyRingProvider.java b/common/src/main/java/bisq/common/crypto/PubKeyRingProvider.java new file mode 100644 index 00000000000..2432005d7ed --- /dev/null +++ b/common/src/main/java/bisq/common/crypto/PubKeyRingProvider.java @@ -0,0 +1,19 @@ +package bisq.common.crypto; + +import com.google.inject.Inject; +import com.google.inject.Provider; + +public class PubKeyRingProvider implements Provider { + + private PubKeyRing pubKeyRing; + + @Inject + public PubKeyRingProvider(KeyRing keyRing) { + pubKeyRing = keyRing.getPubKeyRing(); + } + + @Override + public PubKeyRing get() { + return pubKeyRing; + } +} diff --git a/core/src/main/java/bisq/core/CoreModule.java b/core/src/main/java/bisq/core/CoreModule.java index adcbf3a163e..b73c25dd2cf 100644 --- a/core/src/main/java/bisq/core/CoreModule.java +++ b/core/src/main/java/bisq/core/CoreModule.java @@ -40,6 +40,8 @@ import bisq.common.CommonOptionKeys; import bisq.common.app.AppModule; import bisq.common.crypto.KeyStorage; +import bisq.common.crypto.PubKeyRing; +import bisq.common.crypto.PubKeyRingProvider; import bisq.common.proto.network.NetworkProtoResolver; import bisq.common.proto.persistable.PersistenceProtoResolver; import bisq.common.storage.Storage; @@ -96,6 +98,7 @@ protected void configure() { install(alertModule()); install(filterModule()); install(corePresentationModule()); + bind(PubKeyRing.class).toProvider(PubKeyRingProvider.class); } private TradeModule tradeModule() { diff --git a/core/src/main/java/bisq/core/app/misc/ModuleForAppWithP2p.java b/core/src/main/java/bisq/core/app/misc/ModuleForAppWithP2p.java index 95e86c611f8..517107e3119 100644 --- a/core/src/main/java/bisq/core/app/misc/ModuleForAppWithP2p.java +++ b/core/src/main/java/bisq/core/app/misc/ModuleForAppWithP2p.java @@ -43,6 +43,8 @@ import bisq.common.app.AppModule; import bisq.common.crypto.KeyRing; import bisq.common.crypto.KeyStorage; +import bisq.common.crypto.PubKeyRing; +import bisq.common.crypto.PubKeyRingProvider; import bisq.common.proto.network.NetworkProtoResolver; import bisq.common.proto.persistable.PersistenceProtoResolver; import bisq.common.storage.Storage; @@ -103,6 +105,8 @@ protected void configure() { install(daoModule()); install(alertModule()); install(filterModule()); + bind(PubKeyRing.class).toProvider(PubKeyRingProvider.class); + } protected void configEnvironment() { diff --git a/core/src/main/java/bisq/core/dao/governance/period/PeriodService.java b/core/src/main/java/bisq/core/dao/governance/period/PeriodService.java index 18ea9b61912..dfe18cb07a5 100644 --- a/core/src/main/java/bisq/core/dao/governance/period/PeriodService.java +++ b/core/src/main/java/bisq/core/dao/governance/period/PeriodService.java @@ -32,7 +32,7 @@ import javax.annotation.Nullable; @Slf4j -public final class PeriodService { +public class PeriodService { private final DaoStateService daoStateService; diff --git a/core/src/main/java/bisq/core/dao/governance/proposal/MyProposalListService.java b/core/src/main/java/bisq/core/dao/governance/proposal/MyProposalListService.java index 0357ce7958e..d0933144c56 100644 --- a/core/src/main/java/bisq/core/dao/governance/proposal/MyProposalListService.java +++ b/core/src/main/java/bisq/core/dao/governance/proposal/MyProposalListService.java @@ -32,7 +32,7 @@ import bisq.common.UserThread; import bisq.common.app.DevEnv; -import bisq.common.crypto.KeyRing; +import bisq.common.crypto.PubKeyRing; import bisq.common.handlers.ErrorMessageHandler; import bisq.common.handlers.ResultHandler; import bisq.common.proto.persistable.PersistedDataHost; @@ -85,14 +85,14 @@ public MyProposalListService(P2PService p2PService, PeriodService periodService, WalletsManager walletsManager, Storage storage, - KeyRing keyRing) { + PubKeyRing pubKeyRing) { this.p2PService = p2PService; this.daoStateService = daoStateService; this.periodService = periodService; this.walletsManager = walletsManager; this.storage = storage; - signaturePubKey = keyRing.getPubKeyRing().getSignaturePubKey(); + signaturePubKey = pubKeyRing.getSignaturePubKey(); numConnectedPeersListener = (observable, oldValue, newValue) -> rePublishMyProposalsOnceWellConnected(); daoStateService.addDaoStateListener(this); @@ -239,8 +239,8 @@ private void persist() { } private boolean canRemoveProposal(Proposal proposal, DaoStateService daoStateService, PeriodService periodService) { - boolean inPhase = periodService.isInPhase(daoStateService.getChainHeight(), DaoPhase.Phase.PROPOSAL); - return isMine(proposal) && inPhase; + boolean inProposalPhase = periodService.isInPhase(daoStateService.getChainHeight(), DaoPhase.Phase.PROPOSAL); + return isMine(proposal) && inProposalPhase; } } diff --git a/core/src/test/java/bisq/core/dao/governance/proposal/MyProposalListServiceTest.java b/core/src/test/java/bisq/core/dao/governance/proposal/MyProposalListServiceTest.java new file mode 100644 index 00000000000..2a80becf2b4 --- /dev/null +++ b/core/src/test/java/bisq/core/dao/governance/proposal/MyProposalListServiceTest.java @@ -0,0 +1,32 @@ +package bisq.core.dao.governance.proposal; + +import bisq.core.btc.wallet.WalletsManager; +import bisq.core.dao.governance.period.PeriodService; +import bisq.core.dao.state.DaoStateService; + +import bisq.network.p2p.P2PService; + +import bisq.common.crypto.PubKeyRing; +import bisq.common.storage.Storage; + +import javafx.beans.property.SimpleIntegerProperty; + +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class MyProposalListServiceTest { + @Test + public void canInstantiate() { + P2PService p2PService = mock(P2PService.class); + when(p2PService.getNumConnectedPeers()).thenReturn(new SimpleIntegerProperty(0)); + Storage storage = mock(Storage.class); + MyProposalListService service = new MyProposalListService(p2PService, + mock(DaoStateService.class), + mock(PeriodService.class), mock(WalletsManager.class), storage, mock(PubKeyRing.class) + ); + } + + +} diff --git a/desktop/src/main/java/bisq/desktop/main/offer/MakerFeeMaker.java b/desktop/src/main/java/bisq/desktop/main/offer/MakerFeeProvider.java similarity index 92% rename from desktop/src/main/java/bisq/desktop/main/offer/MakerFeeMaker.java rename to desktop/src/main/java/bisq/desktop/main/offer/MakerFeeProvider.java index 58149224f10..2243a9e9b06 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/MakerFeeMaker.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/MakerFeeProvider.java @@ -6,7 +6,7 @@ import org.bitcoinj.core.Coin; -public class MakerFeeMaker { +public class MakerFeeProvider { public Coin getMakerFee(BsqWalletService bsqWalletService, Preferences preferences, Coin amount) { return OfferUtil.getMakerFee(bsqWalletService, preferences, amount); } diff --git a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java index aad279b2860..ab9a0f10f17 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/MutableOfferDataModel.java @@ -106,7 +106,7 @@ public abstract class MutableOfferDataModel extends OfferDataModel implements Bs private final TxFeeEstimationService txFeeEstimationService; private final ReferralIdService referralIdService; private final BSFormatter btcFormatter; - private MakerFeeMaker makerFeeMaker; + private MakerFeeProvider makerFeeProvider; private final String offerId; private final BalanceListener btcBalanceListener; private final SetChangeListener paymentAccountsChangeListener; @@ -159,7 +159,7 @@ public MutableOfferDataModel(OpenOfferManager openOfferManager, TxFeeEstimationService txFeeEstimationService, ReferralIdService referralIdService, BSFormatter btcFormatter, - MakerFeeMaker makerFeeMaker) { + MakerFeeProvider makerFeeProvider) { super(btcWalletService); this.openOfferManager = openOfferManager; @@ -175,7 +175,7 @@ public MutableOfferDataModel(OpenOfferManager openOfferManager, this.txFeeEstimationService = txFeeEstimationService; this.referralIdService = referralIdService; this.btcFormatter = btcFormatter; - this.makerFeeMaker = makerFeeMaker; + this.makerFeeProvider = makerFeeProvider; offerId = Utilities.getRandomPrefix(5, 8) + "-" + UUID.randomUUID().toString() + "-" + @@ -805,7 +805,7 @@ public Coin getMakerFee(boolean isCurrencyForMakerFeeBtc) { } public Coin getMakerFee() { - return makerFeeMaker.getMakerFee(bsqWalletService, preferences, amount.get()); + return makerFeeProvider.getMakerFee(bsqWalletService, preferences, amount.get()); } public Coin getMakerFeeInBtc() { diff --git a/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModel.java index 47354790dd2..bed681ae255 100644 --- a/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModel.java @@ -21,7 +21,7 @@ package bisq.desktop.main.offer.createoffer; -import bisq.desktop.main.offer.MakerFeeMaker; +import bisq.desktop.main.offer.MakerFeeProvider; import bisq.desktop.main.offer.MutableOfferDataModel; import bisq.core.account.witness.AccountAgeWitnessService; @@ -65,7 +65,7 @@ public CreateOfferDataModel(OpenOfferManager openOfferManager, TxFeeEstimationService txFeeEstimationService, ReferralIdService referralIdService, BSFormatter btcFormatter, - MakerFeeMaker makerFeeMaker) { + MakerFeeProvider makerFeeProvider) { super(openOfferManager, btcWalletService, bsqWalletService, @@ -80,6 +80,6 @@ public CreateOfferDataModel(OpenOfferManager openOfferManager, txFeeEstimationService, referralIdService, btcFormatter, - makerFeeMaker); + makerFeeProvider); } } diff --git a/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java b/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java index b07eb0f1ba9..a575e5bbddd 100644 --- a/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java +++ b/desktop/src/main/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModel.java @@ -18,7 +18,7 @@ package bisq.desktop.main.portfolio.editoffer; -import bisq.desktop.main.offer.MakerFeeMaker; +import bisq.desktop.main.offer.MakerFeeProvider; import bisq.desktop.main.offer.MutableOfferDataModel; import bisq.core.account.witness.AccountAgeWitnessService; @@ -75,7 +75,7 @@ class EditOfferDataModel extends MutableOfferDataModel { ReferralIdService referralIdService, BSFormatter btcFormatter, CorePersistenceProtoResolver corePersistenceProtoResolver, - MakerFeeMaker makerFeeMaker) { + MakerFeeProvider makerFeeProvider) { super(openOfferManager, btcWalletService, bsqWalletService, @@ -90,7 +90,7 @@ class EditOfferDataModel extends MutableOfferDataModel { txFeeEstimationService, referralIdService, btcFormatter, - makerFeeMaker); + makerFeeProvider); this.corePersistenceProtoResolver = corePersistenceProtoResolver; } diff --git a/desktop/src/test/java/bisq/desktop/GuiceSetupTest.java b/desktop/src/test/java/bisq/desktop/GuiceSetupTest.java index 68feb00202f..ef55e99c39f 100644 --- a/desktop/src/test/java/bisq/desktop/GuiceSetupTest.java +++ b/desktop/src/test/java/bisq/desktop/GuiceSetupTest.java @@ -46,6 +46,7 @@ import bisq.common.ClockWatcher; import bisq.common.crypto.KeyRing; import bisq.common.crypto.KeyStorage; +import bisq.common.crypto.PubKeyRing; import bisq.common.proto.network.NetworkProtoResolver; import bisq.common.proto.persistable.PersistenceProtoResolver; import bisq.common.storage.CorruptedDatabaseFilesHandler; @@ -104,6 +105,7 @@ public void testGuiceSetup() { assertSingleton(TradeLimits.class); assertSingleton(KeyStorage.class); assertSingleton(KeyRing.class); + assertSingleton(PubKeyRing.class); assertSingleton(User.class); assertSingleton(ClockWatcher.class); assertSingleton(Preferences.class); diff --git a/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModelTest.java b/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModelTest.java index 74098b24d2d..e2291be163b 100644 --- a/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModelTest.java +++ b/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferDataModelTest.java @@ -1,6 +1,6 @@ package bisq.desktop.main.offer.createoffer; -import bisq.desktop.main.offer.MakerFeeMaker; +import bisq.desktop.main.offer.MakerFeeProvider; import bisq.core.btc.TxFeeEstimationService; import bisq.core.btc.model.AddressEntry; @@ -36,7 +36,7 @@ public class CreateOfferDataModelTest { private CreateOfferDataModel model; private User user; private Preferences preferences; - private MakerFeeMaker makerFeeMaker; + private MakerFeeProvider makerFeeProvider; @Before public void setUp() { @@ -56,12 +56,12 @@ public void setUp() { when(preferences.isUsePercentageBasedPrice()).thenReturn(true); when(preferences.getBuyerSecurityDepositAsPercent(null)).thenReturn(0.01); - makerFeeMaker = mock(MakerFeeMaker.class); + makerFeeProvider = mock(MakerFeeProvider.class); model = new CreateOfferDataModel(null, btcWalletService, null, preferences, user, null, null, priceFeedService, null, null, feeService, feeEstimationService, - null, null, makerFeeMaker); + null, null, makerFeeProvider); } @Test @@ -78,7 +78,7 @@ public void testUseTradeCurrencySetInOfferViewWhenInPaymentAccountAvailable() { when(user.getPaymentAccounts()).thenReturn(paymentAccounts); when(preferences.getSelectedPaymentAccountForCreateOffer()).thenReturn(revolutAccount); - when(makerFeeMaker.getMakerFee(any(), any(), any())).thenReturn(Coin.ZERO); + when(makerFeeProvider.getMakerFee(any(), any(), any())).thenReturn(Coin.ZERO); model.initWithData(OfferPayload.Direction.BUY, new FiatCurrency("USD")); assertEquals("USD", model.getTradeCurrencyCode().get()); @@ -98,7 +98,7 @@ public void testUseTradeAccountThatMatchesTradeCurrencySetInOffer() { when(user.getPaymentAccounts()).thenReturn(paymentAccounts); when(user.findFirstPaymentAccountWithCurrency(new FiatCurrency("USD"))).thenReturn(zelleAccount); when(preferences.getSelectedPaymentAccountForCreateOffer()).thenReturn(revolutAccount); - when(makerFeeMaker.getMakerFee(any(), any(), any())).thenReturn(Coin.ZERO); + when(makerFeeProvider.getMakerFee(any(), any(), any())).thenReturn(Coin.ZERO); model.initWithData(OfferPayload.Direction.BUY, new FiatCurrency("USD")); assertEquals("USD", model.getTradeCurrencyCode().get()); diff --git a/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferViewModelTest.java b/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferViewModelTest.java index c8e4c37b4c5..6b4aa521165 100644 --- a/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferViewModelTest.java +++ b/desktop/src/test/java/bisq/desktop/main/offer/createoffer/CreateOfferViewModelTest.java @@ -17,7 +17,7 @@ package bisq.desktop.main.offer.createoffer; -import bisq.desktop.main.offer.MakerFeeMaker; +import bisq.desktop.main.offer.MakerFeeProvider; import bisq.desktop.util.validation.AltcoinValidator; import bisq.desktop.util.validation.BtcValidator; import bisq.desktop.util.validation.FiatPriceValidator; @@ -104,7 +104,7 @@ public void setUp() { when(bsqFormatter.formatCoin(any())).thenReturn("0"); when(bsqWalletService.getAvailableConfirmedBalance()).thenReturn(Coin.ZERO); - CreateOfferDataModel dataModel = new CreateOfferDataModel(null, btcWalletService, bsqWalletService, empty, user, null, null, priceFeedService, null, accountAgeWitnessService, feeService, txFeeEstimationService, null, bsFormatter, mock(MakerFeeMaker.class)); + CreateOfferDataModel dataModel = new CreateOfferDataModel(null, btcWalletService, bsqWalletService, empty, user, null, null, priceFeedService, null, accountAgeWitnessService, feeService, txFeeEstimationService, null, bsFormatter, mock(MakerFeeProvider.class)); dataModel.initWithData(OfferPayload.Direction.BUY, new CryptoCurrency("BTC", "bitcoin")); dataModel.activate(); diff --git a/desktop/src/test/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModelTest.java b/desktop/src/test/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModelTest.java index 617f25571bf..ee0dc5a2e97 100644 --- a/desktop/src/test/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModelTest.java +++ b/desktop/src/test/java/bisq/desktop/main/portfolio/editoffer/EditOfferDataModelTest.java @@ -1,6 +1,6 @@ package bisq.desktop.main.portfolio.editoffer; -import bisq.desktop.main.offer.MakerFeeMaker; +import bisq.desktop.main.offer.MakerFeeProvider; import bisq.desktop.util.validation.SecurityDepositValidator; import bisq.core.account.witness.AccountAgeWitnessService; @@ -93,7 +93,7 @@ public void setUp() { btcWalletService, bsqWalletService, empty, user, null, null, priceFeedService, null, accountAgeWitnessService, feeService, null, null, - null, null, mock(MakerFeeMaker.class)); + null, null, mock(MakerFeeProvider.class)); } @Test diff --git a/p2p/src/test/java/bisq/network/p2p/storage/ObsoleteP2PDataStorageTest.java b/p2p/src/test/java/bisq/network/p2p/storage/ObsoleteP2PDataStorageTest.java new file mode 100644 index 00000000000..5cd526aba75 --- /dev/null +++ b/p2p/src/test/java/bisq/network/p2p/storage/ObsoleteP2PDataStorageTest.java @@ -0,0 +1,217 @@ +/* left as documentation +package bisq.network.p2p.storage; + +import bisq.network.crypto.EncryptionService; +import bisq.network.p2p.NodeAddress; +import bisq.network.p2p.TestUtils; +import bisq.network.p2p.network.NetworkNode; +import bisq.network.p2p.peers.Broadcaster; +import bisq.network.p2p.storage.payload.ProtectedStoragePayload; + +import bisq.common.crypto.CryptoException; +import bisq.common.crypto.KeyRing; +import bisq.common.crypto.KeyStorage; +import bisq.common.proto.network.NetworkProtoResolver; +import bisq.common.proto.persistable.PersistenceProtoResolver; +import bisq.common.storage.FileUtil; + +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.SignatureException; +import java.security.cert.CertificateException; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import java.io.File; +import java.io.IOException; + +import java.util.HashSet; +import java.util.Set; + +import lombok.extern.slf4j.Slf4j; + +import mockit.Mocked; +import mockit.integration.junit4.JMockit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.runner.RunWith; + +@Slf4j +@RunWith(JMockit.class) +@Ignore("Use NetworkProtoResolver, PersistenceProtoResolver or ProtoResolver which are all in io.bisq.common.") +public class ObsoleteP2PDataStorageTest { + private final Set seedNodes = new HashSet<>(); + private EncryptionService encryptionService1, encryptionService2; + private P2PDataStorage dataStorage1; + private KeyPair storageSignatureKeyPair1, storageSignatureKeyPair2; + private KeyRing keyRing1, keyRing2; + private ProtectedStoragePayload protectedStoragePayload; + private File dir1; + private File dir2; + + @Mocked + Broadcaster broadcaster; + @Mocked + NetworkNode networkNode; + @Mocked + NetworkProtoResolver networkProtoResolver; + @Mocked + PersistenceProtoResolver persistenceProtoResolver; + + @Before + public void setup() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException { + + dir1 = File.createTempFile("temp_tests1", ""); + //noinspection ResultOfMethodCallIgnored + dir1.delete(); + //noinspection ResultOfMethodCallIgnored + dir1.mkdir(); + dir2 = File.createTempFile("temp_tests2", ""); + //noinspection ResultOfMethodCallIgnored + dir2.delete(); + //noinspection ResultOfMethodCallIgnored + dir2.mkdir(); + + keyRing1 = new KeyRing(new KeyStorage(dir1)); + storageSignatureKeyPair1 = keyRing1.getSignatureKeyPair(); + encryptionService1 = new EncryptionService(keyRing1, TestUtils.getNetworkProtoResolver()); + + // for mailbox + keyRing2 = new KeyRing(new KeyStorage(dir2)); + storageSignatureKeyPair2 = keyRing2.getSignatureKeyPair(); + encryptionService2 = new EncryptionService(keyRing2, TestUtils.getNetworkProtoResolver()); + //dataStorage1 = new P2PDataStorage(broadcaster, networkNode, dir1, persistenceProtoResolver); + } + + @After + public void tearDown() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException { + Path path = Paths.get(TestUtils.test_dummy_dir); + File dir = path.toFile(); + FileUtil.deleteDirectory(dir); + FileUtil.deleteDirectory(dir1); + FileUtil.deleteDirectory(dir2); + } + + /* @Test + public void testProtectedStorageEntryAddAndRemove() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException, NoSuchProviderException { + storagePayload = new AlertPayload(new AlertVO("alert", + false, + "version", + storageSignatureKeyPair1.getPublic().getEncoded(), + "sig", + null)); + + ProtectedStorageEntry data = dataStorage1.getProtectedData(storagePayload, storageSignatureKeyPair1); + assertTrue(dataStorage1.add(data, null, null, true)); + assertEquals(1, dataStorage1.getMap().size()); + + int newSequenceNumber = data.sequenceNumber + 1; + byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(data.getStoragePayload(), newSequenceNumber)); + byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); + ProtectedStorageEntry dataToRemove = new ProtectedStorageEntry(data.getStoragePayload(), data.ownerPubKey, newSequenceNumber, signature); + assertTrue(dataStorage1.remove(dataToRemove, null, true)); + assertEquals(0, dataStorage1.getMap().size()); + } + + @Test + public void testProtectedStorageEntryRoundtrip() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException, NoSuchProviderException { + //mockData = new MockData("mockData", keyRing1.getSignatureKeyPair().getPublic()); + storagePayload = getDummyOffer(); + + ProtectedStorageEntry data = dataStorage1.getProtectedData(storagePayload, storageSignatureKeyPair1); + setSignature(data); + assertTrue(checkSignature(data)); + + ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); + data.toEnvelopeProto().writeTo(byteOutputStream); + + //TODO Use NetworkProtoResolver, PersistenceProtoResolver or ProtoResolver which are all in io.bisq.common. + ProtectedStorageEntry protectedStorageEntry = ProtoBufferUtilities.getProtectedStorageEntry(PB.ProtectedStorageEntry.parseFrom(new ByteArrayInputStream(byteOutputStream.toByteArray()))); + + assertTrue(Arrays.equals(Hash.getHash(data.getStoragePayload()), Hash.getHash(protectedStorageEntry.getStoragePayload()))); + assertTrue(data.equals(protectedStorageEntry)); + assertTrue(checkSignature(protectedStorageEntry)); + }*/ + + //TODO Use NetworkProtoResolver, PersistenceProtoResolver or ProtoResolver which are all in io.bisq.common. + /* @Test + public void testOfferRoundtrip() throws InvalidProtocolBufferException { + OfferPayload offer = getDummyOffer(); + try { + String buffer = JsonFormat.printer().print(offer.toEnvelopeProto().getOfferPayload()); + JsonFormat.Parser parser = JsonFormat.parser(); + PB.OfferPayload.Builder builder = PB.OfferPayload.newBuilder(); + parser.merge(buffer, builder); + assertEquals(offer, ProtoBufferUtilities.getOfferPayload(builder.build())); + } catch (IOException e) { + e.printStackTrace(); + fail(); + } + }*/ + + /* @NotNull + private OfferPayload getDummyOffer() { + NodeAddress nodeAddress = new NodeAddress("host", 1000); + NodeAddress nodeAddress2 = new NodeAddress("host1", 1001); + NodeAddress nodeAddress3 = new NodeAddress("host2", 1002); + NodeAddress nodeAddress4 = new NodeAddress("host3", 1002); + return new OfferPayload("id", + System.currentTimeMillis(), + nodeAddress4, + keyRing1.getPubKeyRing(), + OfferPayload.Direction.BUY, + 1200, + 1.5, + true, + 100, + 50, + "BTC", + "USD", + Lists.newArrayList(nodeAddress, + nodeAddress2, + nodeAddress3), + Lists.newArrayList(nodeAddress, + nodeAddress2, + nodeAddress3), + "SEPA", + "accountid", + "feetxId", + "BE", + Lists.newArrayList("BE", "AU"), + "bankid", + Lists.newArrayList("BANK1", "BANK2"), + "version", + 100, + 100, + 100, + 100, + 1000, + 1000, + 1000, + false, + false, + + 1000, + 1000, + false, + "hash", + null); + } + + private void setSignature(ProtectedStorageEntry entry) throws CryptoException { + int newSequenceNumber = entry.sequenceNumber; + byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(entry.getStoragePayload(), newSequenceNumber)); + byte[] signature = Sig.sign(storageSignatureKeyPair1.getPrivate(), hashOfDataAndSeqNr); + entry.signature = signature; + } + + private boolean checkSignature(ProtectedStorageEntry entry) throws CryptoException { + byte[] hashOfDataAndSeqNr = Hash.getHash(new P2PDataStorage.DataAndSeqNrPair(entry.getStoragePayload(), entry.sequenceNumber)); + return dataStorage1.checkSignature(entry.ownerPubKey, hashOfDataAndSeqNr, entry.signature); +} + }*/