electrum

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

commit d96b80ad1f40a2ec3f099f963e35f097464a7e92
parent 309aca69b848a2fbdd14098213a13a53151413f0
Author: Janus <ysangkok@gmail.com>
Date:   Wed,  2 May 2018 18:24:40 +0200

lnbase: make function for building htlc_tx depending on if it is for_us/we_receive

Diffstat:
Mlib/lnbase.py | 73++++++++++++++++++++++++++++++++++++++-----------------------------------
1 file changed, 38 insertions(+), 35 deletions(-)

diff --git a/lib/lnbase.py b/lib/lnbase.py @@ -425,6 +425,40 @@ def make_received_htlc(revocation_pubkey, remote_htlcpubkey, local_htlcpubkey, p + bitcoin.add_number_to_script(cltv_expiry) \ + bytes([opcodes.OP_CLTV, opcodes.OP_DROP, opcodes.OP_CHECKSIG, opcodes.OP_ENDIF, opcodes.OP_ENDIF]) +def make_htlc_tx_with_open_channel(chan, pcp, for_us, we_receive, amount_msat, cltv_expiry, payment_hash, commit, original_htlc_output_index): + conf = chan.local_config if for_us else chan.remote_config + other_conf = chan.local_config if not for_us else chan.remote_config + + revocation_pubkey = derive_blinded_pubkey(other_conf.revocation_basepoint.pubkey, pcp) + delayedpubkey = derive_pubkey(conf.delayed_basepoint.pubkey, pcp) + other_revocation_pubkey = derive_blinded_pubkey(other_conf.revocation_basepoint.pubkey, pcp) + other_htlc_pubkey = derive_pubkey(other_conf.htlc_basepoint.pubkey, pcp) + htlc_pubkey = derive_pubkey(conf.htlc_basepoint.pubkey, pcp) + # HTLC-success for the HTLC spending from a received HTLC output + # if we do not receive, and the commitment tx is not for us, they receive, so it is also an HTLC-success + is_htlc_success = for_us == we_receive + htlc_tx_output = make_htlc_tx_output( + amount_msat = amount_msat, + local_feerate = chan.constraints.feerate, + revocationpubkey=revocation_pubkey, + local_delayedpubkey=delayedpubkey, + success = is_htlc_success, + to_self_delay = other_conf.to_self_delay) + if is_htlc_success: + preimage_script = make_received_htlc(other_revocation_pubkey, other_htlc_pubkey, htlc_pubkey, payment_hash, cltv_expiry) + else: + preimage_script = make_offered_htlc(other_revocation_pubkey, other_htlc_pubkey, htlc_pubkey, payment_hash) + htlc_tx_inputs = make_htlc_tx_inputs( + commit.txid(), commit.htlc_output_indices[original_htlc_output_index], + revocationpubkey=revocation_pubkey, + local_delayedpubkey=delayedpubkey, + amount_msat=amount_msat, + witness_script=bh2u(preimage_script)) + if is_htlc_success: + cltv_expiry = 0 + htlc_tx = make_htlc_tx(cltv_expiry, inputs=htlc_tx_inputs, output=htlc_tx_output) + return htlc_tx + def make_commitment_using_open_channel(chan, ctn, for_us, pcp, local_sat, remote_sat, htlcs=[]): conf = chan.local_config if for_us else chan.remote_config other_conf = chan.local_config if not for_us else chan.remote_config @@ -952,27 +986,11 @@ class Peer(PrintError): local_last_per_commitment_point = secret_to_pubkey(int.from_bytes( local_last_per_commitment_secret, byteorder="big")) - revocation_pubkey = derive_blinded_pubkey(chan.remote_config.revocation_basepoint.pubkey, local_last_per_commitment_point) - remote_delayedpubkey = derive_pubkey(chan.local_config.delayed_basepoint.pubkey, local_last_per_commitment_point) - htlc_tx_output = make_htlc_tx_output( - amount_msat = amount_msat, - local_feerate = chan.constraints.feerate, - revocationpubkey=revocation_pubkey, - local_delayedpubkey=remote_delayedpubkey, - success = True, # HTLC-success for the HTLC spending from a received HTLC output - to_self_delay = chan.remote_config.to_self_delay) - preimage_script = htlcs_in_local[0][0] - htlc_tx_inputs = make_htlc_tx_inputs( - new_commitment.txid(), new_commitment.htlc_output_indices[0], - revocationpubkey=revocation_pubkey, - local_delayedpubkey=remote_delayedpubkey, - amount_msat=amount_msat, - witness_script=bh2u(preimage_script)) - htlc_tx = make_htlc_tx(0, inputs=htlc_tx_inputs, output=htlc_tx_output) - print("htlc tx preimage", htlc_tx.serialize_preimage(0)) + htlc_tx = make_htlc_tx_with_open_channel(chan, local_last_per_commitment_point, True, True, amount_msat, cltv_expiry, payment_hash, new_commitment, 0) pre_hash = bitcoin.Hash(bfh(htlc_tx.serialize_preimage(0))) remote_htlc_pubkey = derive_pubkey(chan.remote_config.htlc_basepoint.pubkey, local_last_per_commitment_point) - assert bitcoin.verify_signature(remote_htlc_pubkey, commitment_signed_msg["htlc_signature"], pre_hash) + if not bitcoin.verify_signature(remote_htlc_pubkey, commitment_signed_msg["htlc_signature"], pre_hash): + raise Exception("failed verifying signature an HTLC tx spending from one of our commit tx'es HTLC outputs") local_last_pcs_index = 2**48 - chan.local_state.ctn - 1 local_last_per_commitment_secret = get_per_commitment_secret_from_seed(local_per_commitment_secret_seed, local_last_pcs_index) @@ -1000,22 +1018,7 @@ class Peer(PrintError): r, s = sigdecode_der(sig[:-1], SECP256k1.generator.order()) sig_64 = sigencode_string_canonize(r, s, SECP256k1.generator.order()) - remote_delayedpubkey = derive_pubkey(chan.remote_config.delayed_basepoint.pubkey, chan.remote_state.next_per_commitment_point) - htlc_tx_output = make_htlc_tx_output( - amount_msat = amount_msat, - local_feerate = chan.constraints.feerate, - revocationpubkey=revocation_pubkey, - local_delayedpubkey=remote_delayedpubkey, - success = False, # timeout for the one offering an HTLC - to_self_delay = chan.local_config.to_self_delay) - preimage_script = htlcs_in_remote[0][0] - htlc_tx_inputs = make_htlc_tx_inputs( - remote_ctx.txid(), remote_ctx.htlc_output_indices[0], - revocationpubkey=revocation_pubkey, - local_delayedpubkey=remote_delayedpubkey, - amount_msat=amount_msat, - witness_script=bh2u(preimage_script)) - htlc_tx = make_htlc_tx(cltv_expiry, inputs=htlc_tx_inputs, output=htlc_tx_output) + htlc_tx = make_htlc_tx_with_open_channel(chan, chan.remote_state.next_per_commitment_point, False, True, amount_msat, cltv_expiry, payment_hash, remote_ctx, 0) # htlc_sig signs the HTLC transaction that spends from THEIR commitment transaction's offered_htlc output sig = bfh(htlc_tx.sign_txin(0, their_remote_htlc_privkey))