storage: upgrade format version automatically in load_data()

This commit is contained in:
SomberNight 2017-10-12 07:08:00 +02:00
parent 1320b18d7e
commit a5ffa69a3e
3 changed files with 19 additions and 6 deletions

View File

@ -191,7 +191,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
def on_filename(filename):
path = os.path.join(wallet_folder, filename)
try:
self.storage = WalletStorage(path)
self.storage = WalletStorage(path, manual_upgrades=True)
except IOError:
self.storage = None
if self.storage:

View File

@ -210,7 +210,7 @@ class Daemon(DaemonThread):
if path in self.wallets:
wallet = self.wallets[path]
return wallet
storage = WalletStorage(path)
storage = WalletStorage(path, manual_upgrades=True)
if not storage.file_exists():
return
if storage.is_encrypted():
@ -220,7 +220,6 @@ class Daemon(DaemonThread):
if storage.requires_split():
return
if storage.requires_upgrade():
self.print_error('upgrading wallet format')
storage.upgrade()
if storage.get_action():
return

View File

@ -62,8 +62,9 @@ def multisig_type(wallet_type):
class WalletStorage(PrintError):
def __init__(self, path):
def __init__(self, path, manual_upgrades=False):
self.print_error("wallet path", path)
self.manual_upgrades = manual_upgrades
self.lock = threading.RLock()
self.data = {}
self.path = path
@ -74,6 +75,9 @@ class WalletStorage(PrintError):
self.raw = f.read()
if not self.is_encrypted():
self.load_data(self.raw)
else:
# avoid new wallets getting 'upgraded'
self.put('seed_version', FINAL_SEED_VERSION)
def load_data(self, s):
try:
@ -99,6 +103,12 @@ class WalletStorage(PrintError):
l = plugin_loaders.get(t)
if l: l()
if not self.manual_upgrades:
if self.requires_split():
raise BaseException("This wallet has multiple accounts and must be split")
if self.requires_upgrade():
self.upgrade()
def is_encrypted(self):
try:
return base64.b64decode(self.raw)[0:4] == b'BIE1'
@ -155,8 +165,6 @@ class WalletStorage(PrintError):
@profiler
def write(self):
# this ensures that previous versions of electrum won't open the wallet
self.put('seed_version', FINAL_SEED_VERSION)
with self.lock:
self._write()
@ -244,10 +252,14 @@ class WalletStorage(PrintError):
return self.file_exists() and self.get_seed_version() != FINAL_SEED_VERSION
def upgrade(self):
self.print_error('upgrading wallet format')
self.convert_imported()
self.convert_wallet_type()
self.convert_account()
self.convert_version_14()
self.put('seed_version', FINAL_SEED_VERSION)
self.write()
def convert_wallet_type(self):
@ -338,6 +350,8 @@ class WalletStorage(PrintError):
def convert_version_14(self):
# convert imported wallets for 3.0
if self.get_seed_version() >= 14:
return
if self.get('wallet_type') =='imported':
addresses = self.get('addresses')
if type(addresses) is list: