From 7a2ca0d9c794d0dc1e3dfe7573b67325cfcfa0a1 Mon Sep 17 00:00:00 2001 From: PiRK Date: Fri, 27 Nov 2020 16:04:21 +0100 Subject: [PATCH 1/2] use the default directory when creating new wallets The previous behavior was to create new wallets in the same directory as the current wallet, even if this wallet was a wallet open in an arbitrary external path. This changes the behavior to use either the wallet path explicitely specified on the command line (-w or --wallet argument) or the default wallet directory in the user's data directory. Remove also a piece of code providing compatiblity with an ancient version of electrum (1.9 released in 2013). Closes #10 Test Plan: Run `electrum-abc`, open a wallet file in an external directory unrelated to electrum-abc (for instance in the Electron Cash data directory), then create a new wallet and verify that it is created in the normal .electrum-abc directory. --- electroncash/simple_config.py | 28 ++++++++++++++++++++-------- electroncash_gui/qt/__init__.py | 10 +++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/electroncash/simple_config.py b/electroncash/simple_config.py index 8f765f885c06..14bd439ce473 100644 --- a/electroncash/simple_config.py +++ b/electroncash/simple_config.py @@ -232,9 +232,28 @@ def save_user_config(self): f.write(s) os.chmod(path, stat.S_IREAD | stat.S_IWRITE) + def get_new_wallet_directory(self): + """Return the path to the directory where new wallets are saved. + + If the program was started with the wallet path (-w or --wallet + argument), the directory of that wallet is used. + Else, the default wallet directory inside the user data directory + is used. + """ + # command line -w option + if self.get('wallet_path'): + return os.path.dirname(os.path.join(self.get('cwd', ''), + self.get('wallet_path'))) + return os.path.join(self.path, "wallets") + def get_wallet_path(self): - """Set the path of the wallet.""" + """Return the path of the current wallet. + On program startup, this is either the wallet path specified on the + command line (-w or --wallet argument), or the wallet used the last + time the program was used, or by default a new wallet named + "default_wallet" in the user directory. + """ # command line -w option if self.get('wallet_path'): return os.path.join(self.get('cwd', ''), self.get('wallet_path')) @@ -248,14 +267,7 @@ def get_wallet_path(self): util.assert_datadir_available(self.path) dirpath = os.path.join(self.path, "wallets") make_dir(dirpath) - new_path = os.path.join(self.path, "wallets", "default_wallet") - - # 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): - os.rename(old_path, new_path) - return new_path def remove_from_recently_open(self, filename): diff --git a/electroncash_gui/qt/__init__.py b/electroncash_gui/qt/__init__.py index 8846a22310be..9f279e72c84d 100644 --- a/electroncash_gui/qt/__init__.py +++ b/electroncash_gui/qt/__init__.py @@ -573,15 +573,15 @@ def create_window_for_wallet(self, wallet): return w def get_wallet_folder(self): - ''' may raise FileNotFoundError ''' + """Get path to directory containing the current wallet. + """ return os.path.dirname(os.path.abspath(self.config.get_wallet_path())) def get_new_wallet_path(self): - ''' may raise FileNotFoundError ''' - wallet_folder = self.get_wallet_folder() + """Return path to a new wallet file to be created.""" + wallet_folder = self.config.get_new_wallet_directory() filename = get_new_wallet_name(wallet_folder) - full_path = os.path.join(wallet_folder, filename) - return full_path + return os.path.join(wallet_folder, filename) def on_focus_change(self, ignored, new_focus_widget): ''' Remember the last wallet window that was activated because From 7a453f380d855d77836023cc77860da4a5eb0f02 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Gasull Date: Thu, 25 Mar 2021 06:08:47 +0000 Subject: [PATCH 2/2] Use either --wallet arg or default wallet dir Backport from Electrum ABC: https://github.com/Bitcoin-ABC/ElectrumABC/pull/15/ --- electroncash/commands.py | 3 ++- electroncash/simple_config.py | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/electroncash/commands.py b/electroncash/commands.py index 99c2202634ae..3e0ee773267b 100644 --- a/electroncash/commands.py +++ b/electroncash/commands.py @@ -28,6 +28,7 @@ import base64 import datetime import json +import os import queue import sys import time @@ -987,7 +988,7 @@ def add_global_options(parser): group.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Show debugging information") group.add_argument("-D", "--dir", dest="electron_cash_path", help="electron cash directory") group.add_argument("-P", "--portable", action="store_true", dest="portable", default=False, help="Use local 'electron_cash_data' directory") - group.add_argument("-w", "--wallet", dest="wallet_path", help="wallet path") + group.add_argument("-w", "--wallet", dest="wallet_path", help="wallet path", type=os.path.abspath) group.add_argument("-wp", "--walletpassword", dest="wallet_password", default=None, help="Supply wallet password") group.add_argument("--testnet", action="store_true", dest="testnet", default=False, help="Use Testnet") group.add_argument("--testnet4", action="store_true", dest="testnet4", default=False, help="Use Testnet4") diff --git a/electroncash/simple_config.py b/electroncash/simple_config.py index 14bd439ce473..d14ee9666355 100644 --- a/electroncash/simple_config.py +++ b/electroncash/simple_config.py @@ -241,10 +241,9 @@ def get_new_wallet_directory(self): is used. """ # command line -w option - if self.get('wallet_path'): - return os.path.dirname(os.path.join(self.get('cwd', ''), - self.get('wallet_path'))) - return os.path.join(self.path, "wallets") + path = self.get('wallet_path') + return os.path.dirname(path) if path else os.path.join(self.path, + "wallets") def get_wallet_path(self): """Return the path of the current wallet. @@ -256,7 +255,7 @@ def get_wallet_path(self): """ # command line -w option if self.get('wallet_path'): - return os.path.join(self.get('cwd', ''), self.get('wallet_path')) + return self.get('wallet_path') # path in config file path = self.get('default_wallet_path')