URI: 
       tfix lnwatcher for channels initiated by remote - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 585905409567245c062a8ead744890fe81f579a4
   DIR parent fb8deecb575140a362e02849175d95a09ff89954
  HTML Author: Janus <ysangkok@gmail.com>
       Date:   Thu,  4 Oct 2018 16:38:59 +0200
       
       fix lnwatcher for channels initiated by remote
       
       Diffstat:
         M electrum/lnutil.py                  |      18 +++++++++---------
         M electrum/lnwatcher.py               |       9 ++++-----
       
       2 files changed, 13 insertions(+), 14 deletions(-)
       ---
   DIR diff --git a/electrum/lnutil.py b/electrum/lnutil.py
       t@@ -398,24 +398,24 @@ def invert_short_channel_id(short_channel_id: bytes) -> (int, int, int):
            oi = int.from_bytes(short_channel_id[6:8], byteorder='big')
            return bh, tpos, oi
        
       -def get_obscured_ctn(ctn: int, local: bytes, remote: bytes) -> int:
       -    mask = int.from_bytes(sha256(local + remote)[-6:], 'big')
       +def get_obscured_ctn(ctn: int, funder: bytes, fundee: bytes) -> int:
       +    mask = int.from_bytes(sha256(funder + fundee)[-6:], 'big')
            return ctn ^ mask
        
       -def extract_ctn_from_tx(tx, txin_index: int, local_payment_basepoint: bytes,
       -                        remote_payment_basepoint: bytes) -> int:
       +def extract_ctn_from_tx(tx, txin_index: int, funder_payment_basepoint: bytes,
       +                        fundee_payment_basepoint: bytes) -> int:
            tx.deserialize()
            locktime = tx.locktime
            sequence = tx.inputs()[txin_index]['sequence']
            obs = ((sequence & 0xffffff) << 24) + (locktime & 0xffffff)
       -    return get_obscured_ctn(obs, local_payment_basepoint, remote_payment_basepoint)
       +    return get_obscured_ctn(obs, funder_payment_basepoint, fundee_payment_basepoint)
        
        def extract_ctn_from_tx_and_chan(tx, chan) -> int:
       -    local_pubkey = chan.local_config.payment_basepoint.pubkey
       -    remote_pubkey = chan.remote_config.payment_basepoint.pubkey
       +    funder_conf = chan.local_config if     chan.constraints.is_initiator else chan.remote_config
       +    fundee_conf = chan.local_config if not chan.constraints.is_initiator else chan.remote_config
            return extract_ctn_from_tx(tx, txin_index=0,
       -                               local_payment_basepoint=local_pubkey,
       -                               remote_payment_basepoint=remote_pubkey)
       +                               funder_payment_basepoint=funder_conf.payment_basepoint.pubkey,
       +                               fundee_payment_basepoint=fundee_conf.payment_basepoint.pubkey)
        
        def overall_weight(num_htlc):
            return 500 + 172 * num_htlc + 224
   DIR diff --git a/electrum/lnwatcher.py b/electrum/lnwatcher.py
       t@@ -265,10 +265,9 @@ class LNWatcher(PrintError):
                ctn = extract_ctn_from_tx_and_chan(ctx, chan)
                latest_ctn_on_channel = chan.local_state.ctn if ours else chan.remote_state.ctn
                last_ctn_watcher_saw = self._get_last_ctn_for_processed_ctx(funding_address, ours)
       -        # TODO make it work when we are not initiator
       -        if chan.constraints.is_initiator and latest_ctn_on_channel + 1 != ctn:
       +        if latest_ctn_on_channel + 1 != ctn:
                    raise Exception('unexpected ctn {}. latest is {}. our ctx: {}'.format(ctn, latest_ctn_on_channel, ours))
       -        if chan.constraints.is_initiator and last_ctn_watcher_saw + 1 != ctn:
       +        if last_ctn_watcher_saw + 1 != ctn:
                    raise Exception('watcher skipping ctns!! ctn {}. last seen {}. our ctx: {}'.format(ctn, last_ctn_watcher_saw, ours))
                #self.print_error("process_new_offchain_ctx. funding {}, ours {}, ctn {}, ctx {}"
                #      .format(chan.funding_outpoint.to_str(), ours, ctn, ctx.txid()))
       t@@ -291,9 +290,9 @@ class LNWatcher(PrintError):
                ctn = extract_ctn_from_tx_and_chan(ctx, chan)
                latest_ctn_on_channel = chan.remote_state.ctn
                last_ctn_watcher_saw = self._get_last_ctn_for_revoked_secret(funding_address)
       -        if chan.constraints.is_initiator and latest_ctn_on_channel != ctn:
       +        if latest_ctn_on_channel != ctn:
                    raise Exception('unexpected ctn {}. latest is {}'.format(ctn, latest_ctn_on_channel))
       -        if chan.constraints.is_initiator and last_ctn_watcher_saw + 1 != ctn:
       +        if last_ctn_watcher_saw + 1 != ctn:
                    raise Exception('watcher skipping ctns!! ctn {}. last seen {}'.format(ctn, last_ctn_watcher_saw))
                sweep_address = self._get_sweep_address_for_chan(chan)
                encumbered_sweeptx = maybe_create_sweeptx_for_their_ctx_to_local(chan, ctx, per_commitment_secret, sweep_address)