From 11b6e0bdb25d72929a6368991150fc00ab0eb2b3 Mon Sep 17 00:00:00 2001 From: Xavier Fiechter <31884704+xavierfiechter@users.noreply.github.com> Date: Fri, 15 Sep 2023 09:44:45 +0200 Subject: [PATCH] Introduced support for backward-compatible BIP-329 import and BIP-329 export. --- electrum/gui/qt/main_window.py | 5 ++--- electrum/gui/qt/util.py | 13 ++++++++----- electrum/wallet.py | 15 +++++++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index 16e1ebcf095d..dbbfcb40074c 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -2430,10 +2430,10 @@ def do_export_privkeys(self, fileName, pklist, is_csv): def do_import_labels(self): def on_import(): self.need_update.set() - import_meta_gui(self, _('labels'), self.wallet.import_labels, on_import) + import_meta_gui(self, _('labels'), self.wallet.import_labels, on_import, file_type="jsonp|json") def do_export_labels(self): - export_meta_gui(self, _('labels'), self.wallet.export_labels) + export_meta_gui(self, _('labels'), self.wallet.export_labels, file_type="jsonp") def import_invoices(self): import_meta_gui(self, _('invoices'), self.wallet.import_invoices, self.send_tab.invoice_list.update) @@ -2856,4 +2856,3 @@ def on_swap_result(self, txid): else: msg += _("Lightning funds were not received.") self.show_error_signal.emit(msg) - diff --git a/electrum/gui/qt/util.py b/electrum/gui/qt/util.py index 97490987cad3..5ae6cc4ea2f2 100644 --- a/electrum/gui/qt/util.py +++ b/electrum/gui/qt/util.py @@ -1029,8 +1029,11 @@ def onFileAdded(self, fn): raise NotImplementedError() -def import_meta_gui(electrum_window: 'ElectrumWindow', title, importer, on_success): - filter_ = "JSON (*.json);;All files (*)" +def import_meta_gui(electrum_window: 'ElectrumWindow', title, importer, on_success, file_type="json"): + if file_type == "json": + filter_ = "JSON (*.json);;All files (*)" + elif file_type == "jsonp|json": + filter_ = "JSONP (*.jsonp);;JSON (*.json);;All files (*)" filename = getOpenFileName( parent=electrum_window, title=_("Open {} file").format(title), @@ -1048,12 +1051,12 @@ def import_meta_gui(electrum_window: 'ElectrumWindow', title, importer, on_succe on_success() -def export_meta_gui(electrum_window: 'ElectrumWindow', title, exporter): - filter_ = "JSON (*.json);;All files (*)" +def export_meta_gui(electrum_window: 'ElectrumWindow', title, exporter, file_type="json"): + filter_ = "{} (*.{});;All files (*)".format(file_type.upper(), file_type) filename = getSaveFileName( parent=electrum_window, title=_("Select file to save your {}").format(title), - filename='electrum_{}.json'.format(title), + filename='electrum_{}.{}'.format(title, file_type), filter=filter_, config=electrum_window.config, ) diff --git a/electrum/wallet.py b/electrum/wallet.py index 996e191a4ebe..eab37766f1d3 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -51,6 +51,7 @@ from .i18n import _ from .bip32 import BIP32Node, convert_bip32_intpath_to_strpath, convert_bip32_strpath_to_intpath +from .bip329 import export_bip329_labels, import_bip329_labels, is_json_file from .crypto import sha256 from . import util from .util import (NotEnoughFunds, UserCancelled, profiler, OldTaskGroup, ignore_exceptions, @@ -636,12 +637,18 @@ def set_label(self, name: str, text: str = None) -> bool: return changed def import_labels(self, path): - data = read_json_file(path) - for key, value in data.items(): - self.set_label(key, value) + if is_json_file(path): + # legacy format + data = read_json_file(path) + for key, value in data.items(): + self.set_label(key, value) + else: + import_bip329_labels(path=path, wallet=self) def export_labels(self, path): - write_json_file(path, self.get_all_labels()) + """ """ + export_bip329_labels(path=path, wallet=self) + def set_fiat_value(self, txid, ccy, text, fx, value_sat): if not self.db.get_transaction(txid):