From 6821c582e9ddb5d738cb62d2d49869b7ef237f64 Mon Sep 17 00:00:00 2001 From: chimp1984 Date: Wed, 9 Sep 2020 14:12:03 -0500 Subject: [PATCH] Add check for open offers and remove them if present at restore from seed --- .../resources/i18n/displayStrings.properties | 2 ++ .../bisq/desktop/main/SharedPresentation.java | 26 ++++++++++++++++++- .../content/seedwords/SeedWordsView.java | 22 +++++++++------- .../windows/WalletPasswordWindow.java | 7 +++-- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/core/src/main/resources/i18n/displayStrings.properties b/core/src/main/resources/i18n/displayStrings.properties index c28a2d6526..92aafb0d0a 100644 --- a/core/src/main/resources/i18n/displayStrings.properties +++ b/core/src/main/resources/i18n/displayStrings.properties @@ -3036,6 +3036,8 @@ Ideally you should specify the date your wallet seed was created.\n\n\n\ Are you sure you want to go ahead without specifying a wallet date? seed.restore.success=Wallets restored successfully with the new seed words.\n\nYou need to shut down and restart the application. seed.restore.error=An error occurred when restoring the wallets with seed words.{0} +seed.restore.openOffers.warn=You have open offers which will be removed if you restore from seed words.\n\ + Are you sure that you want to continue? #################################################################### diff --git a/desktop/src/main/java/bisq/desktop/main/SharedPresentation.java b/desktop/src/main/java/bisq/desktop/main/SharedPresentation.java index a99dd1f43f..913529779d 100644 --- a/desktop/src/main/java/bisq/desktop/main/SharedPresentation.java +++ b/desktop/src/main/java/bisq/desktop/main/SharedPresentation.java @@ -22,6 +22,7 @@ import bisq.desktop.main.overlays.popups.Popup; import bisq.core.btc.wallet.WalletsManager; import bisq.core.locale.Res; +import bisq.core.offer.OpenOfferManager; import bisq.common.UserThread; import bisq.common.storage.FileUtil; @@ -30,6 +31,8 @@ import org.bitcoinj.wallet.DeterministicSeed; import java.io.File; +import java.util.concurrent.TimeUnit; + import lombok.extern.slf4j.Slf4j; /** @@ -39,7 +42,28 @@ import lombok.extern.slf4j.Slf4j; */ @Slf4j public class SharedPresentation { - public static void restoreSeedWords(DeterministicSeed seed, WalletsManager walletsManager, File storageDir) { + public static void restoreSeedWords(WalletsManager walletsManager, + OpenOfferManager openOfferManager, + DeterministicSeed seed, + File storageDir) { + if (!openOfferManager.getObservableList().isEmpty()) { + UserThread.runAfter(() -> + new Popup().warning(Res.get("seed.restore.openOffers.warn")) + .actionButtonText(Res.get("shared.yes")) + .onAction(() -> { + openOfferManager.removeAllOpenOffers(() -> { + doRestoreSeedWords(walletsManager, seed, storageDir); + }); + }) + .show(), 100, TimeUnit.MILLISECONDS); + } else { + doRestoreSeedWords(walletsManager, seed, storageDir); + } + } + + private static void doRestoreSeedWords(WalletsManager walletsManager, + DeterministicSeed seed, + File storageDir) { try { File backup = new File(storageDir, "AddressEntryList_backup_pre_wallet_restore_" + System.currentTimeMillis()); FileUtil.copyFile(new File(storageDir, "AddressEntryList"), backup); diff --git a/desktop/src/main/java/bisq/desktop/main/account/content/seedwords/SeedWordsView.java b/desktop/src/main/java/bisq/desktop/main/account/content/seedwords/SeedWordsView.java index bddbf4ea07..ea3186a003 100644 --- a/desktop/src/main/java/bisq/desktop/main/account/content/seedwords/SeedWordsView.java +++ b/desktop/src/main/java/bisq/desktop/main/account/content/seedwords/SeedWordsView.java @@ -27,6 +27,7 @@ import bisq.desktop.util.Layout; import bisq.core.btc.wallet.BtcWalletService; import bisq.core.btc.wallet.WalletsManager; import bisq.core.locale.Res; +import bisq.core.offer.OpenOfferManager; import bisq.core.user.DontShowAgainLookup; import bisq.common.config.Config; @@ -68,6 +69,7 @@ import static javafx.beans.binding.Bindings.createBooleanBinding; @FxmlView public class SeedWordsView extends ActivatableView { private final WalletsManager walletsManager; + private final OpenOfferManager openOfferManager; private final BtcWalletService btcWalletService; private final WalletPasswordWindow walletPasswordWindow; private final File storageDir; @@ -91,10 +93,12 @@ public class SeedWordsView extends ActivatableView { @Inject private SeedWordsView(WalletsManager walletsManager, + OpenOfferManager openOfferManager, BtcWalletService btcWalletService, WalletPasswordWindow walletPasswordWindow, @Named(Config.STORAGE_DIR) File storageDir) { this.walletsManager = walletsManager; + this.openOfferManager = openOfferManager; this.btcWalletService = btcWalletService; this.walletPasswordWindow = walletPasswordWindow; this.storageDir = storageDir; @@ -166,20 +170,18 @@ public class SeedWordsView extends ActivatableView { String key = "showBackupWarningAtSeedPhrase"; if (DontShowAgainLookup.showAgain(key)) { new Popup().warning(Res.get("account.seed.backup.warning")) - .onAction(() -> { - showSeedPhrase(); - }) - .actionButtonText(Res.get("shared.iUnderstand")) - .useIUnderstandButton() - .dontShowAgainId(key) - .hideCloseButton() - .show(); + .onAction(this::showSeedPhrase) + .actionButtonText(Res.get("shared.iUnderstand")) + .useIUnderstandButton() + .dontShowAgainId(key) + .hideCloseButton() + .show(); } else { showSeedPhrase(); } } - public void showSeedPhrase() { + private void showSeedPhrase() { DeterministicSeed keyChainSeed = btcWalletService.getKeyChainSeed(); // wallet creation date is not encrypted walletCreationDate = Instant.ofEpochSecond(walletsManager.getChainSeedCreationTimeSeconds()).atZone(ZoneId.systemDefault()).toLocalDate(); @@ -305,6 +307,6 @@ public class SeedWordsView extends ActivatableView { long date = localDateTime.toEpochSecond(ZoneOffset.UTC); DeterministicSeed seed = new DeterministicSeed(Splitter.on(" ").splitToList(seedWordsTextArea.getText()), null, "", date); - SharedPresentation.restoreSeedWords(seed, walletsManager, storageDir); + SharedPresentation.restoreSeedWords(walletsManager, openOfferManager, seed, storageDir); } } diff --git a/desktop/src/main/java/bisq/desktop/main/overlays/windows/WalletPasswordWindow.java b/desktop/src/main/java/bisq/desktop/main/overlays/windows/WalletPasswordWindow.java index c203666558..640ee6fc62 100644 --- a/desktop/src/main/java/bisq/desktop/main/overlays/windows/WalletPasswordWindow.java +++ b/desktop/src/main/java/bisq/desktop/main/overlays/windows/WalletPasswordWindow.java @@ -30,6 +30,7 @@ import bisq.desktop.util.Transitions; import bisq.core.btc.wallet.WalletsManager; import bisq.core.crypto.ScryptUtil; import bisq.core.locale.Res; +import bisq.core.offer.OpenOfferManager; import bisq.common.UserThread; import bisq.common.config.Config; @@ -88,6 +89,7 @@ import static javafx.beans.binding.Bindings.createBooleanBinding; @Slf4j public class WalletPasswordWindow extends Overlay { private final WalletsManager walletsManager; + private final OpenOfferManager openOfferManager; private File storageDir; private Button unlockButton; @@ -115,8 +117,10 @@ public class WalletPasswordWindow extends Overlay { @Inject private WalletPasswordWindow(WalletsManager walletsManager, + OpenOfferManager openOfferManager, @Named(Config.STORAGE_DIR) File storageDir) { this.walletsManager = walletsManager; + this.openOfferManager = openOfferManager; this.storageDir = storageDir; type = Type.Attention; width = 900; @@ -277,7 +281,6 @@ public class WalletPasswordWindow extends Overlay { gridPane.getChildren().add(headLine2Label); seedWordsTextArea = addTextArea(gridPane, ++rowIndex, Res.get("seed.enterSeedWords"), 5); - ; seedWordsTextArea.setPrefHeight(60); Tuple2 labelDatePickerTuple2 = addTopLabelDatePicker(gridPane, ++rowIndex, @@ -356,6 +359,6 @@ public class WalletPasswordWindow extends Overlay { //TODO Is ZoneOffset correct? long date = value != null ? value.atStartOfDay().toEpochSecond(ZoneOffset.UTC) : 0; DeterministicSeed seed = new DeterministicSeed(Splitter.on(" ").splitToList(seedWordsTextArea.getText()), null, "", date); - SharedPresentation.restoreSeedWords(seed, walletsManager, storageDir); + SharedPresentation.restoreSeedWords(walletsManager, openOfferManager, seed, storageDir); } }