URI: 
       texport channel backup from kivy gui - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit e50f6d29ed9c558845531a4abac0304564095efd
   DIR parent 74517c88ad3f425003e99f8d97a64b1dafb9473d
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Fri, 10 Apr 2020 19:50:20 +0200
       
       export channel backup from kivy gui
       
       Diffstat:
         M electrum/gui/kivy/uix/dialogs/ligh… |      12 ++++++++++++
         M electrum/lnchannel.py               |       8 ++------
         M electrum/lnworker.py                |      18 +++++++++++++-----
         M electrum/tests/test_lnpeer.py       |       3 +++
       
       4 files changed, 30 insertions(+), 11 deletions(-)
       ---
   DIR diff --git a/electrum/gui/kivy/uix/dialogs/lightning_channels.py b/electrum/gui/kivy/uix/dialogs/lightning_channels.py
       t@@ -183,6 +183,11 @@ Builder.load_string(r'''
                    Button:
                        size_hint: 0.5, None
                        height: '48dp'
       +                text: _('Backup')
       +                on_release: root.export_backup()
       +            Button:
       +                size_hint: 0.5, None
       +                height: '48dp'
                        text: _('Close')
                        on_release: root.close()
                        disabled: root.is_closed
       t@@ -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?')
       t@@ -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()
        
   DIR diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py
       t@@ -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
       t@@ -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()
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -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:
       t@@ -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
        
       t@@ -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")
   DIR diff --git a/electrum/tests/test_lnpeer.py b/electrum/tests/test_lnpeer.py
       t@@ -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")