From 786979ec5021be542f47cd54f6d9ebd228c82d17 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Fri, 13 Oct 2023 13:10:56 +0200 Subject: [PATCH 1/4] config: keep wallets directory stable, regardless of wallet opens in other directories --- electrum/gui/qt/main_window.py | 2 +- electrum/simple_config.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index 524401b98..ebf7b7509 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -669,7 +669,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger, QtEventListener): self.recently_visited_menu.setEnabled(bool(len(recent))) def get_wallet_folder(self): - return os.path.dirname(os.path.abspath(self.wallet.storage.path)) + return os.path.abspath(self.config.get_datadir_wallet_path()) def new_wallet(self): try: diff --git a/electrum/simple_config.py b/electrum/simple_config.py index 5f9d462bd..c803f0de2 100644 --- a/electrum/simple_config.py +++ b/electrum/simple_config.py @@ -445,6 +445,7 @@ class SimpleConfig(Logger): new_path = self.get_fallback_wallet_path() + # TODO: this can be removed by now # default path in pre 1.9 versions old_path = os.path.join(self.path, "electrum.dat") if os.path.exists(old_path) and not os.path.exists(new_path): @@ -452,12 +453,14 @@ class SimpleConfig(Logger): return new_path - def get_fallback_wallet_path(self): + def get_datadir_wallet_path(self): util.assert_datadir_available(self.path) dirpath = os.path.join(self.path, "wallets") make_dir(dirpath, allow_symlink=False) - path = os.path.join(self.path, "wallets", "default_wallet") - return path + return dirpath + + def get_fallback_wallet_path(self): + return os.path.join(self.get_datadir_wallet_path(), "default_wallet") def remove_from_recently_open(self, filename): recent = self.RECENTLY_OPEN_WALLET_FILES or [] From 5c96847111db780810f3cf7daa18c96c6a2de5d0 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Fri, 13 Oct 2023 15:15:02 +0200 Subject: [PATCH 2/4] qt: show wallet as relative path if below datadir wallets folder --- electrum/gui/qt/wizard/wallet.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/electrum/gui/qt/wizard/wallet.py b/electrum/gui/qt/wizard/wallet.py index 145d3e339..330fbf236 100644 --- a/electrum/gui/qt/wizard/wallet.py +++ b/electrum/gui/qt/wizard/wallet.py @@ -300,10 +300,22 @@ class WCWalletName(WizardComponent, Logger): temp_storage = None # type: Optional[WalletStorage] wallet_folder = os.path.dirname(path) + def relative_path(path): + new_path = path + try: + datadir_wallet_path = self.wizard.config.get_datadir_wallet_path() + commonpath = os.path.commonpath([path, datadir_wallet_path]) + if commonpath == datadir_wallet_path: + # below datadir_wallet_path, make relative + new_path = os.path.relpath(path, commonpath) + except ValueError: + pass + return new_path + def on_choose(): _path, __ = QFileDialog.getOpenFileName(self, "Select your wallet file", wallet_folder) if _path: - self.name_e.setText(_path) + self.name_e.setText(relative_path(_path)) def on_filename(filename): # FIXME? "filename" might contain ".." (etc) and hence sketchy path traversals are possible @@ -368,7 +380,7 @@ class WCWalletName(WizardComponent, Logger): button_create_new.clicked.connect( lambda: self.name_e.setText(get_new_wallet_name(wallet_folder))) # FIXME get_new_wallet_name might raise self.name_e.textChanged.connect(on_filename) - self.name_e.setText(os.path.basename(path)) + self.name_e.setText(relative_path(path)) def apply(self): if self.wallet_exists: From b59e9089a097937778d01bd3155c2ec45a1a5fda Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Fri, 13 Oct 2023 15:37:21 +0200 Subject: [PATCH 3/4] qt: use datadir wallets folder consistently --- electrum/gui/qt/wizard/wallet.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/electrum/gui/qt/wizard/wallet.py b/electrum/gui/qt/wizard/wallet.py index 330fbf236..1aa9a8d8b 100644 --- a/electrum/gui/qt/wizard/wallet.py +++ b/electrum/gui/qt/wizard/wallet.py @@ -298,14 +298,13 @@ class WCWalletName(WizardComponent, Logger): self.layout().addStretch(1) temp_storage = None # type: Optional[WalletStorage] - wallet_folder = os.path.dirname(path) + datadir_wallet_folder = self.wizard.config.get_datadir_wallet_path() def relative_path(path): new_path = path try: - datadir_wallet_path = self.wizard.config.get_datadir_wallet_path() - commonpath = os.path.commonpath([path, datadir_wallet_path]) - if commonpath == datadir_wallet_path: + commonpath = os.path.commonpath([path, datadir_wallet_folder]) + if commonpath == datadir_wallet_folder: # below datadir_wallet_path, make relative new_path = os.path.relpath(path, commonpath) except ValueError: @@ -313,7 +312,7 @@ class WCWalletName(WizardComponent, Logger): return new_path def on_choose(): - _path, __ = QFileDialog.getOpenFileName(self, "Select your wallet file", wallet_folder) + _path, __ = QFileDialog.getOpenFileName(self, "Select your wallet file", datadir_wallet_folder) if _path: self.name_e.setText(relative_path(_path)) @@ -326,7 +325,7 @@ class WCWalletName(WizardComponent, Logger): self.wallet_is_open = False self.wallet_needs_hw_unlock = False if filename: - _path = os.path.join(wallet_folder, filename) + _path = os.path.join(datadir_wallet_folder, filename) wallet_from_memory = self.wizard._daemon.get_wallet(_path) try: if wallet_from_memory: @@ -378,15 +377,14 @@ class WCWalletName(WizardComponent, Logger): button.clicked.connect(on_choose) button_create_new.clicked.connect( - lambda: self.name_e.setText(get_new_wallet_name(wallet_folder))) # FIXME get_new_wallet_name might raise + lambda: self.name_e.setText(get_new_wallet_name(datadir_wallet_folder))) # FIXME get_new_wallet_name might raise self.name_e.textChanged.connect(on_filename) self.name_e.setText(relative_path(path)) def apply(self): if self.wallet_exists: # use full path - path = self.wizard._path - wallet_folder = os.path.dirname(path) + wallet_folder = self.wizard.config.get_datadir_wallet_path() self.wizard_data['wallet_name'] = os.path.join(wallet_folder, self.name_e.text()) else: self.wizard_data['wallet_name'] = self.name_e.text() From 24323d21a2c965cba2e5571e60163cfd402a5fbd Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Thu, 30 Nov 2023 12:47:28 +0100 Subject: [PATCH 4/4] qt: add note in wizard if wallet path is outside the default wallets folder --- electrum/gui/qt/wizard/wallet.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/electrum/gui/qt/wizard/wallet.py b/electrum/gui/qt/wizard/wallet.py index 1aa9a8d8b..84e34f6b9 100644 --- a/electrum/gui/qt/wizard/wallet.py +++ b/electrum/gui/qt/wizard/wallet.py @@ -363,6 +363,8 @@ class WCWalletName(WizardComponent, Logger): + _("Press 'Finish' to create/focus window.") if msg is None: msg = _('Cannot read file') + if filename and os.path.isabs(relative_path(_path)): + msg += '\n\n' + _('Note: this wallet file is outside the default wallets folder.') msg_label.setText(msg) widget_create_new.setVisible(bool(temp_storage and temp_storage.file_exists())) if user_needs_to_enter_password: