URI: 
       tfix #6125: detect self-payments - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 984da7515aa0fb3b263d7d0621515bf2ec18dc1f
   DIR parent 250c99d5b268821fcb4c4a2aaabdb42331919414
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Wed,  6 May 2020 12:32:33 +0200
       
       fix #6125: detect self-payments
       
       Diffstat:
         M electrum/gui/kivy/uix/dialogs/ligh… |       6 +++---
         M electrum/lnchannel.py               |       4 ++--
         M electrum/lnworker.py                |      29 ++++++++++++-----------------
       
       3 files changed, 17 insertions(+), 22 deletions(-)
       ---
   DIR diff --git a/electrum/gui/kivy/uix/dialogs/lightning_tx_dialog.py b/electrum/gui/kivy/uix/dialogs/lightning_tx_dialog.py
       t@@ -98,7 +98,6 @@ class LightningTxDialog(Factory.Popup):
                self.app = app  # type: ElectrumWindow
                self.wallet = self.app.wallet
                self._action_button_fn = lambda btn: None
       -        self.is_sent = bool(tx_item['direction'] == 'sent')
                self.description = tx_item['label']
                self.timestamp = tx_item['timestamp']
                self.date_str = datetime.fromtimestamp(self.timestamp).isoformat(' ')[:-3]
       t@@ -106,6 +105,7 @@ class LightningTxDialog(Factory.Popup):
                self.payment_hash = tx_item['payment_hash']
                self.preimage = tx_item['preimage']
                format_amount = self.app.format_amount_and_units
       -        self.amount_str = format_amount(self.amount)
       -        if self.is_sent:
       +        self.is_sent = self.amount < 0
       +        self.amount_str = format_amount(-self.amount if self.is_sent else self.amount)
       +        if tx_item.get('fee_msat'):
                    self.fee_str = format_amount(Decimal(tx_item['fee_msat']) / 1000)
   DIR diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py
       t@@ -631,14 +631,14 @@ class Channel(AbstractChannel):
                return out
        
            def get_settled_payments(self):
       -        out = {}
       +        out = defaultdict(list)
                for subject in LOCAL, REMOTE:
                    log = self.hm.log[subject]
                    for htlc_id, htlc in log.get('adds', {}).items():
                        if htlc_id in log.get('settles',{}):
                            direction = SENT if subject is LOCAL else RECEIVED
                            rhash = bh2u(htlc.payment_hash)
       -                    out[rhash] = (self.channel_id, htlc, direction)
       +                    out[rhash].append((self.channel_id, htlc, direction))
                return out
        
            def open_with_first_pcp(self, remote_pcp: bytes, remote_sig: bytes) -> None:
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -593,7 +593,7 @@ class LNWallet(LNWorker):
                for chan in self.channels.values():
                    d = chan.get_settled_payments()
                    for k, v in d.items():
       -                out[k].append(v)
       +                out[k] += v
                return out
        
            def get_lightning_history(self):
       t@@ -601,26 +601,21 @@ class LNWallet(LNWorker):
                for key, plist in self.get_settled_payments().items():
                    if len(plist) == 0:
                        continue
       -            elif len(plist) == 1:
       -                chan_id, htlc, _direction = plist[0]
       -                direction = 'sent' if _direction == SENT else 'received'
       -                amount_msat = int(_direction) * htlc.amount_msat
       -                timestamp = htlc.timestamp
       +            payment_hash = bytes.fromhex(key)
       +            info = self.get_payment_info(payment_hash)
       +            timestamp = min([htlc.timestamp for chan_id, htlc, _direction in plist])
       +            amount_msat = 0
       +            fee_msat = None
       +            for chan_id, htlc, _direction in plist:
       +                amount_msat += int(_direction) * htlc.amount_msat
       +                if _direction == SENT and info and info.amount:
       +                    fee_msat = (fee_msat or 0) - info.amount*1000 - amount_msat
       +            if info is not None:
                        label = self.wallet.get_label(key)
       -                if _direction == SENT:
       -                    info = self.get_payment_info(bfh(key))
       -                    fee_msat = - info.amount*1000 - amount_msat if info and info.amount else None
       -                else:
       -                    fee_msat = None
       +                direction = ('sent' if info.direction == SENT else 'received') if len(plist)==1 else 'self-payment'
                    else:
       -                # assume forwarding
                        direction = 'forwarding'
       -                amount_msat = sum([int(_direction) * htlc.amount_msat for chan_id, htlc, _direction in plist])
                        label = _('Forwarding')
       -                timestamp = min([htlc.timestamp for chan_id, htlc, _direction in plist])
       -                fee_msat = None # fixme
       -
       -            payment_hash = bytes.fromhex(key)
                    preimage = self.get_preimage(payment_hash).hex()
                    item = {
                        'type': 'payment',