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
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