Skip to content

Commit

Permalink
Introduced support for backward-compatible BIP-329 import and BIP-329…
Browse files Browse the repository at this point in the history
… export.
  • Loading branch information
xavierfiechter committed Sep 18, 2023
1 parent bbfe522 commit 11b6e0b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
5 changes: 2 additions & 3 deletions electrum/gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -2856,4 +2856,3 @@ def on_swap_result(self, txid):
else:
msg += _("Lightning funds were not received.")
self.show_error_signal.emit(msg)

13 changes: 8 additions & 5 deletions electrum/gui/qt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
)
Expand Down
15 changes: 11 additions & 4 deletions electrum/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 11b6e0b

Please sign in to comment.