Browse Source

export channel backup from kivy gui

hard-fail-on-bad-server-string
ThomasV 5 years ago
parent
commit
e50f6d29ed
  1. 12
      electrum/gui/kivy/uix/dialogs/lightning_channels.py
  2. 8
      electrum/lnchannel.py
  3. 18
      electrum/lnworker.py
  4. 3
      electrum/tests/test_lnpeer.py

12
electrum/gui/kivy/uix/dialogs/lightning_channels.py

@ -180,6 +180,11 @@ Builder.load_string(r'''
BoxLayout:
size_hint: 1, None
height: '48dp'
Button:
size_hint: 0.5, None
height: '48dp'
text: _('Backup')
on_release: root.export_backup()
Button:
size_hint: 0.5, None
height: '48dp'
@ -284,6 +289,9 @@ class ChannelBackupPopup(Popup):
super(ChannelBackupPopup,self).__init__(**kwargs)
self.chan = chan
self.app = app
self.short_id = format_short_channel_id(chan.short_channel_id)
self.state = chan.get_state_for_GUI()
self.title = _('Channel Backup')
def request_force_close(self):
msg = _('Request force close?')
@ -363,6 +371,10 @@ class ChannelDetailsPopup(Popup):
self.app._trigger_update_history()
self.dismiss()
def export_backup(self):
text = self.app.wallet.lnworker.export_channel_backup(self.chan.channel_id)
self.app.qr_dialog(_("Channel Backup " + self.chan.short_id_for_GUI()), 'channel_backup:'+text)
def force_close(self):
Question(_('Force-close channel?'), self._force_close).open()

8
electrum/lnchannel.py

@ -146,6 +146,8 @@ class AbstractChannel(Logger):
self.logger.debug(f'Setting channel state: {old_state.name} -> {state.name}')
self._state = state
self.storage['state'] = self._state.name
if self.lnworker:
self.lnworker.channel_state_changed(self)
def get_state(self) -> channel_states:
return self._state
@ -567,12 +569,6 @@ class Channel(AbstractChannel):
self.hm.channel_open_finished()
self.peer_state = peer_states.GOOD
def set_state(self, state: channel_states) -> None:
super().set_state(state)
if self.lnworker:
self.lnworker.save_channel(self)
self.lnworker.network.trigger_callback('channel', self)
def get_state_for_GUI(self):
# status displayed in the GUI
cs = self.get_state()

18
electrum/lnworker.py

@ -657,6 +657,10 @@ class LNWallet(LNWorker):
with self.lock:
return {x: y for (x, y) in self.channels.items() if y.node_id == node_id}
def channel_state_changed(self, chan):
self.save_channel(chan)
self.network.trigger_callback('channel', chan)
def save_channel(self, chan):
assert type(chan) is Channel
if chan.config[REMOTE].next_per_commitment_point == chan.config[REMOTE].current_per_commitment_point:
@ -1367,6 +1371,9 @@ class LNBackups(Logger):
for channel_id, cb in self.db.get_dict("channel_backups").items():
self.channel_backups[bfh(channel_id)] = ChannelBackup(cb, sweep_address=self.sweep_address, lnworker=self)
def channel_state_changed(self, chan):
self.network.trigger_callback('channel', chan)
def peer_closed(self, chan):
pass
@ -1390,16 +1397,17 @@ class LNBackups(Logger):
def import_channel_backup(self, encrypted):
xpub = self.wallet.get_fingerprint()
x = pw_decode_bytes(encrypted, xpub, version=PW_HASH_VERSION_LATEST)
cb = ChannelBackupStorage.from_bytes(x)
channel_id = cb.channel_id().hex()
decrypted = pw_decode_bytes(encrypted, xpub, version=PW_HASH_VERSION_LATEST)
cb_storage = ChannelBackupStorage.from_bytes(decrypted)
channel_id = cb_storage.channel_id().hex()
d = self.db.get_dict("channel_backups")
if channel_id in d:
raise Exception('Channel already in wallet')
d[channel_id] = cb
self.channel_backups[bfh(channel_id)] = ChannelBackup(cb, sweep_address=self.sweep_address, lnworker=self)
d[channel_id] = cb_storage
self.channel_backups[bfh(channel_id)] = cb = ChannelBackup(cb_storage, sweep_address=self.sweep_address, lnworker=self)
self.wallet.save_db()
self.network.trigger_callback('channels_updated', self.wallet)
self.lnwatcher.add_channel(cb.funding_outpoint.to_str(), cb.get_funding_address())
def remove_channel_backup(self, channel_id):
d = self.db.get_dict("channel_backups")

3
electrum/tests/test_lnpeer.py

@ -124,6 +124,9 @@ class MockLNWallet(Logger):
if chan.short_channel_id == short_channel_id:
return chan
def channel_state_changed(self, chan):
pass
def save_channel(self, chan):
print("Ignoring channel save")

Loading…
Cancel
Save