def to_ltc(self, type: str) -> str: if isinstance(self.key, (Secp256k1Priv, Secp256k1Pub)): if self.version == Version.PRIVATE: return litecoin.serialize_privkey(self.key.get_private_bytes(), True, type) else: return litecoin.pubkey_to_address(type, self.key.get_public_bytes().hex()) else: raise ValueError("Can only derive LTC from Secp256k1")
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=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=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()) alice._ignore_max_htlc_value = True bob._ignore_max_htlc_value = True return alice, bob
def test_close_upfront_shutdown_script(self): alice_channel, bob_channel = create_test_channels() # create upfront shutdown script for bob, alice doesn't use upfront # shutdown script bob_uss_pub = lnutil.privkey_to_pubkey(os.urandom(32)) bob_uss_addr = bitcoin.pubkey_to_address('p2wpkh', bh2u(bob_uss_pub)) bob_uss = bfh(bitcoin.address_to_script(bob_uss_addr)) # bob commits to close to bob_uss alice_channel.config[ HTLCOwner.REMOTE].upfront_shutdown_script = bob_uss # but bob closes to some receiving address, which we achieve by not # setting the upfront shutdown script in the channel config bob_channel.config[HTLCOwner.LOCAL].upfront_shutdown_script = b'' p1, p2, w1, w2, q1, q2 = self.prepare_peers(alice_channel, bob_channel) w1.network.config.set_key('dynamic_fees', False) w2.network.config.set_key('dynamic_fees', False) w1.network.config.set_key('fee_per_kb', 5000) w2.network.config.set_key('fee_per_kb', 1000) async def test(): async def close(): await asyncio.wait_for(p1.initialized, 1) await asyncio.wait_for(p2.initialized, 1) # bob closes channel with different shutdown script await p1.close_channel(alice_channel.channel_id) gath.cancel() async def main_loop(peer): async with peer.taskgroup as group: await group.spawn(peer._message_loop()) await group.spawn(peer.htlc_switch()) coros = [close(), main_loop(p1), main_loop(p2)] gath = asyncio.gather(*coros) await gath with self.assertRaises(UpfrontShutdownScriptViolation): run(test()) # bob sends the same upfront_shutdown_script has he announced alice_channel.config[ HTLCOwner.REMOTE].upfront_shutdown_script = bob_uss bob_channel.config[HTLCOwner.LOCAL].upfront_shutdown_script = bob_uss p1, p2, w1, w2, q1, q2 = self.prepare_peers(alice_channel, bob_channel) w1.network.config.set_key('dynamic_fees', False) w2.network.config.set_key('dynamic_fees', False) w1.network.config.set_key('fee_per_kb', 5000) w2.network.config.set_key('fee_per_kb', 1000) async def test(): async def close(): await asyncio.wait_for(p1.initialized, 1) await asyncio.wait_for(p2.initialized, 1) await p1.close_channel(alice_channel.channel_id) gath.cancel() async def main_loop(peer): async with peer.taskgroup as group: await group.spawn(peer._message_loop()) await group.spawn(peer.htlc_switch()) coros = [close(), main_loop(p1), main_loop(p2)] gath = asyncio.gather(*coros) await gath with self.assertRaises(concurrent.futures.CancelledError): run(test())
def to_ltc(self, type: str) -> str: if self.version == Version.PRIVATE: return litecoin.serialize_privkey(self.key.get_private_bytes(), True, type) else: return litecoin.pubkey_to_address(type, self.key.get_public_bytes().hex())