URI: 
       tlnbase: fix locktime and nsequence - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 316f9a3954ce93336213c071d17e0694a269bcf6
   DIR parent 814146f099931acf3b3c3da69564e03df4377a6c
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Fri, 13 Apr 2018 17:07:42 +0200
       
       lnbase: fix locktime and nsequence
       
       Diffstat:
         M lib/lnbase.py                       |      22 ++++++++++------------
         M lib/tests/test_lnbase.py            |       5 ++---
       
       2 files changed, 12 insertions(+), 15 deletions(-)
       ---
   DIR diff --git a/lib/lnbase.py b/lib/lnbase.py
       t@@ -248,18 +248,17 @@ def aiosafe(f):
                    traceback.print_exc()
            return f2
        
       -def get_locktime(cn, local, remote):
       -    print_error(len(local), len(remote))
       -    q = local + remote
       -    mask = int.from_bytes(H256(q)[-6:], byteorder="big")
       -    print_error('mask', hex(mask))
       -    obs = cn ^ mask
       -    print_error('obs ', hex(obs))
       -    locktime = (0x20 << 48) + obs
       -    return locktime
       +def get_obscured_ctn(ctn, local, remote):
       +    mask = int.from_bytes(H256(local + remote)[-6:], byteorder="big")
       +    return ctn ^ mask
       +
        
        def make_commitment(local_pubkey, remote_pubkey, payment_pubkey, remote_payment_pubkey, revocation_pubkey, delayed_pubkey, funding_txid, funding_pos, funding_satoshis):
            pubkeys = sorted([bh2u(local_pubkey), bh2u(remote_pubkey)])
       +    obs = get_obscured_ctn(0, payment_pubkey, remote_payment_pubkey)
       +    locktime = (0x20 << 24) + (obs & 0xffffff)
       +    sequence = (0x80 << 24) + (obs >> 24)
       +    print_error('locktime', locktime, hex(locktime))
            # commitment tx input
            c_inputs = [{
                'type': 'p2wsh',
       t@@ -269,7 +268,8 @@ def make_commitment(local_pubkey, remote_pubkey, payment_pubkey, remote_payment_
                'prevout_n': funding_pos,
                'prevout_hash': funding_txid,
                'value': funding_satoshis,
       -        'coinbase': False
       +        'coinbase': False,
       +        'sequence':sequence
            }]
            # commitment tx outputs
            local_script = bytes([opcodes.OP_IF]) + revocation_pubkey + bytes([opcodes.OP_ELSE, opcodes.OP_CSV, opcodes.OP_DROP]) + delayed_pubkey + bytes([opcodes.OP_ENDIF, opcodes.OP_CHECKSIG])
       t@@ -282,8 +282,6 @@ def make_commitment(local_pubkey, remote_pubkey, payment_pubkey, remote_payment_
            # no htlc for the moment
            c_outputs = [to_local, to_remote]
            # create commitment tx
       -    locktime = get_locktime(0, payment_pubkey, remote_payment_pubkey)
       -    print_error('locktime', locktime, hex(locktime))
            tx = Transaction.from_io(c_inputs, c_outputs, locktime=locktime, version=2)
            return tx
        
   DIR diff --git a/lib/tests/test_lnbase.py b/lib/tests/test_lnbase.py
       t@@ -1,7 +1,7 @@
        import json
        import unittest
        from lib.util import bh2u
       -from lib.lnbase import make_commitment, get_locktime
       +from lib.lnbase import make_commitment, get_obscured_ctn
        from lib.transaction import Transaction
        from lib import bitcoin
        
       t@@ -18,7 +18,7 @@ class Test_LNBase(unittest.TestCase):
        
                local_payment_basepoint = bytes.fromhex('034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa')
                remote_payment_basepoint = bytes.fromhex('032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991')
       -        locktime = get_locktime(42, local_payment_basepoint, remote_payment_basepoint)
       +        obs = get_obscured_ctn(42, local_payment_basepoint, remote_payment_basepoint)
        
                local_funding_privkey = bytes.fromhex('30ff4956bbdd3222d44cc5e8a1261dab1e07957bdac5ae88fe3261ef321f374901')
                local_funding_pubkey = bytes.fromhex('023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb')
       t@@ -47,7 +47,6 @@ class Test_LNBase(unittest.TestCase):
                    local_revocation_pubkey, local_delayedpubkey,
                    funding_tx_id, funding_output_index, funding_amount_satoshi)
                our_commit_tx.sign({bh2u(local_funding_pubkey): (local_funding_privkey[:-1], True)})
       -
                ref_commit_tx_str = '02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311054a56a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c383693901483045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220'
                ref_commit_tx = Transaction(ref_commit_tx_str)