From ed65f335bd44c3430581c47e78db0a041540aa5c Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 15 Jul 2022 18:26:13 +0200 Subject: [PATCH] wallet_db upgrade: fix possible corruption of invoice amounts see https://github.com/spesmilo/electrum/pull/7774 --- electrum/wallet.py | 2 ++ electrum/wallet_db.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/electrum/wallet.py b/electrum/wallet.py index 92028dfdd..94ae280c7 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -943,6 +943,7 @@ class Abstract_Wallet(ABC, Logger, EventListener): amount_msat = '!' break else: + assert isinstance(x.value, int), f"{x.value!r}" amount_msat += x.value * 1000 timestamp = None exp = None @@ -2439,6 +2440,7 @@ class Abstract_Wallet(ABC, Logger, EventListener): def create_request(self, amount_sat: int, message: str, exp_delay: int, address: str): # for receiving amount_sat = amount_sat or 0 + assert isinstance(amount_sat, int), f"{amount_sat!r}" exp_delay = exp_delay or 0 timestamp = int(time.time()) fallback_address = address if self.config.get('bolt11_fallback', True) else None diff --git a/electrum/wallet_db.py b/electrum/wallet_db.py index 01c85534d..9fae80a5a 100644 --- a/electrum/wallet_db.py +++ b/electrum/wallet_db.py @@ -52,7 +52,7 @@ if TYPE_CHECKING: OLD_SEED_VERSION = 4 # electrum versions < 2.0 NEW_SEED_VERSION = 11 # electrum versions >= 2.0 -FINAL_SEED_VERSION = 47 # electrum >= 2.7 will set this to prevent +FINAL_SEED_VERSION = 48 # electrum >= 2.7 will set this to prevent # old versions from overwriting new format @@ -196,6 +196,7 @@ class WalletDB(JsonDB): self._convert_version_45() self._convert_version_46() self._convert_version_47() + self._convert_version_48() self.put('seed_version', FINAL_SEED_VERSION) # just to be sure self._after_upgrade_tasks() @@ -942,6 +943,16 @@ class WalletDB(JsonDB): del requests[key] self.data['seed_version'] = 47 + def _convert_version_48(self): + # fix possible corruption of invoice amounts, see #7774 + if not self._is_upgrade_method_needed(47, 47): + return + invoices = self.data.get('invoices', {}) + for key, item in list(invoices.items()): + if item['amount_msat'] == 1000 * "!": + item['amount_msat'] = "!" + self.data['seed_version'] = 48 + def _convert_imported(self): if not self._is_upgrade_method_needed(0, 13): return