electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit e264a21c644c645c21456bee7dc3bf213e7fa8ae
parent 3c34628ffbf6af5bf40d8601d284d0fa5960d4d4
Author: Janus <ysangkok@gmail.com>
Date:   Tue, 24 Apr 2018 18:19:39 +0200

lnbase: derive next keys when making updated local commitment transaction

Diffstat:
Mlib/lnbase.py | 20+++++++++++++++-----
Mlib/tests/test_lnbase_online.py | 4++--
2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/lib/lnbase.py b/lib/lnbase.py @@ -761,6 +761,11 @@ class Peer(PrintError): remote_revocation_basepoint = payload['revocation_basepoint'] remote_payment_basepoint = payload['payment_basepoint'] remote_delayed_payment_basepoint = payload['delayed_payment_basepoint'] + #basepoints = { + # "delayed_payment_basepoint": delayed_payment_basepoint, + # "remote_payment_basepoint": remote_payment_basepoint, + # "remote_revocation_basepoint": remote_revocation_basepoint + #} remote_htlc_basepoint = payload['htlc_basepoint'] remote_htlc_minimum_msat = int.from_bytes(payload['htlc_minimum_msat'], "big") remote_max_htlc_value_in_flight_msat = int.from_bytes(payload['max_htlc_value_in_flight_msat'], "big") @@ -865,9 +870,9 @@ class Peer(PrintError): del self.remote_funding_locked[channel_id] self.print_error('Done waiting for remote_funding_locked', remote_funding_locked_msg) self.commitment_signed[channel_id] = asyncio.Future() - return channel_id, per_commitment_secret_seed, local_ctx_args, remote_funding_pubkey, remote_funding_locked_msg, remote_revocation_basepoint, remote_htlc_basepoint, htlc_basepoint + return channel_id, per_commitment_secret_seed, local_ctx_args, remote_funding_pubkey, remote_funding_locked_msg, remote_revocation_basepoint, remote_htlc_basepoint, htlc_basepoint, delayed_payment_basepoint - async def receive_commitment_revoke_ack(self, channel_id, local_per_commitment_secret_seed, local_last_pcs_index, local_ctx_args, expected_received_sat, remote_funding_pubkey, local_next_commitment_number, remote_next_commitment_point, remote_revocation_basepoint, remote_htlc_basepoint, local_htlc_basepoint): + async def receive_commitment_revoke_ack(self, channel_id, local_per_commitment_secret_seed, local_last_pcs_index, local_ctx_args, expected_received_sat, remote_funding_pubkey, local_next_commitment_number, remote_next_commitment_point, remote_revocation_basepoint, remote_htlc_basepoint, local_htlc_basepoint, delayed_payment_basepoint): try: commitment_signed_msg = await self.commitment_signed[channel_id] finally: @@ -881,9 +886,6 @@ class Peer(PrintError): local_next_per_commitment_secret, byteorder="big")) - local_ctx_args = local_ctx_args._replace(remote_amount = local_ctx_args.remote_amount - expected_received_sat) - local_ctx_args = local_ctx_args._replace(ctn = local_next_commitment_number) - remote_revocation_pubkey = derive_blinded_pubkey(remote_revocation_basepoint, remote_next_commitment_point) remote_htlc_pubkey = derive_pubkey(remote_htlc_basepoint, remote_next_commitment_point) local_htlc_pubkey = derive_pubkey(local_htlc_basepoint, local_next_per_commitment_point) @@ -891,6 +893,12 @@ class Peer(PrintError): cltv_expiry = int.from_bytes(self.unfulfilled_htlcs[0]["cltv_expiry"],"big") amount_msat = int.from_bytes(self.unfulfilled_htlcs[0]["amount_msat"], "big") + local_ctx_args = local_ctx_args._replace(remote_amount = local_ctx_args.remote_amount - expected_received_sat) + local_ctx_args = local_ctx_args._replace(ctn = local_next_commitment_number) + local_ctx_args = local_ctx_args._replace(remote_revocation_pubkey = remote_revocation_pubkey) + local_ctx_args = local_ctx_args._replace(remotepubkey = derive_pubkey(local_ctx_args.remote_payment_basepoint, remote_next_commitment_point)) + local_ctx_args = local_ctx_args._replace(local_delayedpubkey = derive_pubkey(delayed_payment_basepoint, local_next_per_commitment_point)) + # make_received_htlc(revocation_pubkey, remote_htlcpubkey, local_htlcpubkey, payment_hash, cltv_expiry) htlcs = [ ( @@ -903,6 +911,8 @@ class Peer(PrintError): preimage_hex = new_commitment.serialize_preimage(0) print("new commitment tx", new_commitment) print("new commitment tx outputs", new_commitment.outputs()) + for idx, output in enumerate(new_commitment.outputs()): + print("output {}: ".format(idx), bitcoin.address_to_script(output[1] )) pre_hash = bitcoin.Hash(bfh(preimage_hex)) if not bitcoin.verify_signature(remote_funding_pubkey, commitment_signed_msg["signature"], pre_hash): raise Exception('failed verifying signature of updated commitment transaction') diff --git a/lib/tests/test_lnbase_online.py b/lib/tests/test_lnbase_online.py @@ -50,12 +50,12 @@ if __name__ == "__main__": async def async_test(): payment_preimage = bytes.fromhex("01"*32) RHASH = sha256(payment_preimage) - channel_id, per_commitment_secret_seed, local_ctx_args, remote_funding_pubkey, remote_funding_locked_msg, remote_revocation_basepoint, remote_htlc_basepoint, local_htlc_basepoint = await peer.channel_establishment_flow(wallet, config, funding_satoshis, push_msat) + channel_id, per_commitment_secret_seed, local_ctx_args, remote_funding_pubkey, remote_funding_locked_msg, remote_revocation_basepoint, remote_htlc_basepoint, local_htlc_basepoint, delayed_payment_basepoint = await peer.channel_establishment_flow(wallet, config, funding_satoshis, push_msat) expected_received_sat = 400000 pay_req = lnencode(LnAddr(RHASH, amount=Decimal("0.00000001")*expected_received_sat, tags=[('d', 'one cup of coffee')]), peer.privkey[:32]) print("payment request", pay_req) last_pcs_index = 2**48 - 1 - await peer.receive_commitment_revoke_ack(channel_id, per_commitment_secret_seed, last_pcs_index, local_ctx_args, expected_received_sat, remote_funding_pubkey, local_next_commitment_number=1, remote_next_commitment_point=remote_funding_locked_msg["next_per_commitment_point"], remote_revocation_basepoint=remote_revocation_basepoint, remote_htlc_basepoint=remote_htlc_basepoint, local_htlc_basepoint=local_htlc_basepoint) + await peer.receive_commitment_revoke_ack(channel_id, per_commitment_secret_seed, last_pcs_index, local_ctx_args, expected_received_sat, remote_funding_pubkey, local_next_commitment_number=1, remote_next_commitment_point=remote_funding_locked_msg["next_per_commitment_point"], remote_revocation_basepoint=remote_revocation_basepoint, remote_htlc_basepoint=remote_htlc_basepoint, local_htlc_basepoint=local_htlc_basepoint, delayed_payment_basepoint=delayed_payment_basepoint) htlc_id = 0 # TODO should correspond with received htlc (when handling more than just one update) await peer.fulfill_htlc(channel_id, htlc_id, payment_preimage) while True: