Пример #1
0
 def test_per_commitment_secret_from_seed(self):
     self.assertEqual(
         0x02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148.
         to_bytes(byteorder="big", length=32),
         get_per_commitment_secret_from_seed(
             0x0000000000000000000000000000000000000000000000000000000000000000
             .to_bytes(byteorder="big", length=32), 281474976710655))
     self.assertEqual(
         0x7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc.
         to_bytes(byteorder="big", length=32),
         get_per_commitment_secret_from_seed(
             0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
             .to_bytes(byteorder="big", length=32), 281474976710655))
     self.assertEqual(
         0x56f4008fb007ca9acf0e15b054d5c9fd12ee06cea347914ddbaed70d1c13a528.
         to_bytes(byteorder="big", length=32),
         get_per_commitment_secret_from_seed(
             0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
             .to_bytes(byteorder="big", length=32), 0xaaaaaaaaaaa))
     self.assertEqual(
         0x9015daaeb06dba4ccc05b91b2f73bd54405f2be9f217fbacd3c5ac2e62327d31.
         to_bytes(byteorder="big", length=32),
         get_per_commitment_secret_from_seed(
             0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
             .to_bytes(byteorder="big", length=32), 0x555555555555))
     self.assertEqual(
         0x915c75942a26bb3a433a8ce2cb0427c29ec6c1775cfc78328b57f6ba7bfeaa9c.
         to_bytes(byteorder="big", length=32),
         get_per_commitment_secret_from_seed(
             0x0101010101010101010101010101010101010101010101010101010101010101
             .to_bytes(byteorder="big", length=32), 1))
Пример #2
0
 def test_shachain_produce_consume(self):
     seed = bitcoin.sha256(b"shachaintest")
     consumer = RevocationStore(StoredDict({}, None, []))
     for i in range(10000):
         secret = get_per_commitment_secret_from_seed(seed, RevocationStore.START_INDEX - i)
         try:
             consumer.add_next_entry(secret)
         except Exception as e:
             raise Exception("iteration " + str(i) + ": " + str(e))
         if i % 1000 == 0:
             c1 = consumer
             s1 = json.dumps(c1.storage, cls=MyEncoder)
             c2 = RevocationStore(StoredDict(json.loads(s1), None, []))
             s2 = json.dumps(c2.storage, cls=MyEncoder)
             self.assertEqual(s1, s2)
Пример #3
0
 def test_shachain_produce_consume(self):
     seed = bitcoin.sha256(b"shachaintest")
     consumer = RevocationStore()
     for i in range(10000):
         secret = get_per_commitment_secret_from_seed(
             seed, RevocationStore.START_INDEX - i)
         try:
             consumer.add_next_entry(secret)
         except Exception as e:
             raise Exception("iteration " + str(i) + ": " + str(e))
         if i % 1000 == 0:
             self.assertEqual(
                 consumer.serialize(),
                 RevocationStore.from_json_obj(
                     json.loads(json.dumps(
                         consumer.serialize()))).serialize())
Пример #4
0
def create_test_channels(*, feerate=6000, local_msat=None, remote_msat=None,
                         alice_name="alice", bob_name="bob",
                         alice_pubkey=b"\x01"*33, bob_pubkey=b"\x02"*33, random_seed=None):
    if random_seed is None:  # needed for deterministic randomness
        random_seed = os.urandom(32)
    random_gen = PRNG(random_seed)
    funding_txid = binascii.hexlify(random_gen.get_bytes(32)).decode("ascii")
    funding_index = 0
    funding_sat = ((local_msat + remote_msat) // 1000) if local_msat is not None and remote_msat is not None else (bitcoin.COIN * 10)
    local_amount = local_msat if local_msat is not None else (funding_sat * 1000 // 2)
    remote_amount = remote_msat if remote_msat is not None else (funding_sat * 1000 // 2)
    alice_raw = [bip32("m/" + str(i)) for i in range(5)]
    bob_raw = [bip32("m/" + str(i)) for i in range(5,11)]
    alice_privkeys = [lnutil.Keypair(lnutil.privkey_to_pubkey(x), x) for x in alice_raw]
    bob_privkeys = [lnutil.Keypair(lnutil.privkey_to_pubkey(x), x) for x in bob_raw]
    alice_pubkeys = [lnutil.OnlyPubkeyKeypair(x.pubkey) for x in alice_privkeys]
    bob_pubkeys = [lnutil.OnlyPubkeyKeypair(x.pubkey) for x in bob_privkeys]

    alice_seed = random_gen.get_bytes(32)
    bob_seed = random_gen.get_bytes(32)

    alice_first = lnutil.secret_to_pubkey(
        int.from_bytes(lnutil.get_per_commitment_secret_from_seed(
            alice_seed, lnutil.RevocationStore.START_INDEX), "big"))
    bob_first = lnutil.secret_to_pubkey(
        int.from_bytes(lnutil.get_per_commitment_secret_from_seed(
            bob_seed, lnutil.RevocationStore.START_INDEX), "big"))

    alice, bob = (
        lnchannel.Channel(
            create_channel_state(
                funding_txid, funding_index, funding_sat, True, local_amount,
                remote_amount, alice_privkeys, bob_pubkeys, alice_seed, None,
                bob_first, other_node_id=bob_pubkey, l_dust=200, r_dust=1300,
                l_csv=5, r_csv=4
            ),
            name=f"{alice_name}->{bob_name}",
            initial_feerate=feerate),
        lnchannel.Channel(
            create_channel_state(
                funding_txid, funding_index, funding_sat, False, remote_amount,
                local_amount, bob_privkeys, alice_pubkeys, bob_seed, None,
                alice_first, other_node_id=alice_pubkey, l_dust=1300, r_dust=200,
                l_csv=4, r_csv=5
            ),
            name=f"{bob_name}->{alice_name}",
            initial_feerate=feerate)
    )

    alice.hm.log[LOCAL]['ctn'] = 0
    bob.hm.log[LOCAL]['ctn'] = 0

    alice._state = ChannelState.OPEN
    bob._state = ChannelState.OPEN

    a_out = alice.get_latest_commitment(LOCAL).outputs()
    b_out = bob.get_next_commitment(REMOTE).outputs()
    assert a_out == b_out, "\n" + pformat((a_out, b_out))

    sig_from_bob, a_htlc_sigs = bob.sign_next_commitment()
    sig_from_alice, b_htlc_sigs = alice.sign_next_commitment()

    assert len(a_htlc_sigs) == 0
    assert len(b_htlc_sigs) == 0

    alice.open_with_first_pcp(bob_first, sig_from_bob)
    bob.open_with_first_pcp(alice_first, sig_from_alice)

    alice_second = lnutil.secret_to_pubkey(int.from_bytes(lnutil.get_per_commitment_secret_from_seed(alice_seed, lnutil.RevocationStore.START_INDEX - 1), "big"))
    bob_second = lnutil.secret_to_pubkey(int.from_bytes(lnutil.get_per_commitment_secret_from_seed(bob_seed, lnutil.RevocationStore.START_INDEX - 1), "big"))

    # from funding_locked:
    alice.config[REMOTE].next_per_commitment_point = bob_second
    bob.config[REMOTE].next_per_commitment_point = alice_second

    alice._fallback_sweep_address = bitcoin.pubkey_to_address('p2wpkh', alice.config[LOCAL].payment_basepoint.pubkey.hex())
    bob._fallback_sweep_address = bitcoin.pubkey_to_address('p2wpkh', bob.config[LOCAL].payment_basepoint.pubkey.hex())

    return alice, bob
Пример #5
0
def create_test_channels(feerate=6000, local=None, remote=None):
    funding_txid = binascii.hexlify(b"\x01" * 32).decode("ascii")
    funding_index = 0
    funding_sat = ((local + remote) //
                   1000) if local is not None and remote is not None else (
                       bitcoin.COIN * 10)
    local_amount = local if local is not None else (funding_sat * 1000 // 2)
    remote_amount = remote if remote is not None else (funding_sat * 1000 // 2)
    alice_raw = [bip32("m/" + str(i)) for i in range(5)]
    bob_raw = [bip32("m/" + str(i)) for i in range(5, 11)]
    alice_privkeys = [
        lnutil.Keypair(lnutil.privkey_to_pubkey(x), x) for x in alice_raw
    ]
    bob_privkeys = [
        lnutil.Keypair(lnutil.privkey_to_pubkey(x), x) for x in bob_raw
    ]
    alice_pubkeys = [
        lnutil.OnlyPubkeyKeypair(x.pubkey) for x in alice_privkeys
    ]
    bob_pubkeys = [lnutil.OnlyPubkeyKeypair(x.pubkey) for x in bob_privkeys]

    alice_seed = b"\x01" * 32
    bob_seed = b"\x02" * 32

    alice_first = lnutil.secret_to_pubkey(
        int.from_bytes(
            lnutil.get_per_commitment_secret_from_seed(
                alice_seed, lnutil.RevocationStore.START_INDEX), "big"))
    bob_first = lnutil.secret_to_pubkey(
        int.from_bytes(
            lnutil.get_per_commitment_secret_from_seed(
                bob_seed, lnutil.RevocationStore.START_INDEX), "big"))

    alice, bob = (lnchannel.Channel(create_channel_state(funding_txid,
                                                         funding_index,
                                                         funding_sat,
                                                         True,
                                                         local_amount,
                                                         remote_amount,
                                                         alice_privkeys,
                                                         bob_pubkeys,
                                                         alice_seed,
                                                         None,
                                                         bob_first,
                                                         b"\x02" * 33,
                                                         l_dust=200,
                                                         r_dust=1300,
                                                         l_csv=5,
                                                         r_csv=4),
                                    name="alice",
                                    initial_feerate=feerate),
                  lnchannel.Channel(create_channel_state(funding_txid,
                                                         funding_index,
                                                         funding_sat,
                                                         False,
                                                         remote_amount,
                                                         local_amount,
                                                         bob_privkeys,
                                                         alice_pubkeys,
                                                         bob_seed,
                                                         None,
                                                         alice_first,
                                                         b"\x01" * 33,
                                                         l_dust=1300,
                                                         r_dust=200,
                                                         l_csv=4,
                                                         r_csv=5),
                                    name="bob",
                                    initial_feerate=feerate))

    alice.hm.log[LOCAL]['ctn'] = 0
    bob.hm.log[LOCAL]['ctn'] = 0

    alice.set_state('OPEN')
    bob.set_state('OPEN')

    a_out = alice.get_latest_commitment(LOCAL).outputs()
    b_out = bob.get_next_commitment(REMOTE).outputs()
    assert a_out == b_out, "\n" + pformat((a_out, b_out))

    sig_from_bob, a_htlc_sigs = bob.sign_next_commitment()
    sig_from_alice, b_htlc_sigs = alice.sign_next_commitment()

    assert len(a_htlc_sigs) == 0
    assert len(b_htlc_sigs) == 0

    alice.config[LOCAL] = alice.config[LOCAL]._replace(
        current_commitment_signature=sig_from_bob)
    bob.config[LOCAL] = bob.config[LOCAL]._replace(
        current_commitment_signature=sig_from_alice)

    alice_second = lnutil.secret_to_pubkey(
        int.from_bytes(
            lnutil.get_per_commitment_secret_from_seed(
                alice_seed, lnutil.RevocationStore.START_INDEX - 1), "big"))
    bob_second = lnutil.secret_to_pubkey(
        int.from_bytes(
            lnutil.get_per_commitment_secret_from_seed(
                bob_seed, lnutil.RevocationStore.START_INDEX - 1), "big"))

    alice.config[REMOTE] = alice.config[REMOTE]._replace(
        next_per_commitment_point=bob_second,
        current_per_commitment_point=bob_first)
    bob.config[REMOTE] = bob.config[REMOTE]._replace(
        next_per_commitment_point=alice_second,
        current_per_commitment_point=alice_first)

    alice.hm.channel_open_finished()
    bob.hm.channel_open_finished()

    return alice, bob