Browse Source

lnbase: fix initial commitment transaction

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
ThomasV 7 years ago
parent
commit
5f4328fb3a
  1. 115
      lib/lnbase.py
  2. 8
      lib/tests/test_lnbase.py

115
lib/lnbase.py

@ -368,7 +368,7 @@ def make_received_htlc(revocation_pubkey, remote_htlcpubkey, local_htlcpubkey, p
def make_commitment(ctn, local_funding_pubkey, remote_funding_pubkey, remotepubkey,
payment_basepoint, remote_payment_basepoint, revocation_pubkey, delayed_pubkey,
funding_txid, funding_pos, funding_satoshis,
to_local_msat, to_remote_msat, local_feerate, local_delay, htlcs=[]):
local_amount, remote_amount, to_self_delay, dust_limit_satoshis, htlcs=[]):
pubkeys = sorted([bh2u(local_funding_pubkey), bh2u(remote_funding_pubkey)])
obs = get_obscured_ctn(ctn, payment_basepoint, remote_payment_basepoint)
locktime = (0x20 << 24) + (obs & 0xffffff)
@ -387,18 +387,17 @@ def make_commitment(ctn, local_funding_pubkey, remote_funding_pubkey, remotepubk
'sequence':sequence
}]
# commitment tx outputs
local_script = bytes([opcodes.OP_IF]) + bfh(push_script(bh2u(revocation_pubkey))) + bytes([opcodes.OP_ELSE]) + add_number_to_script(local_delay) \
local_script = bytes([opcodes.OP_IF]) + bfh(push_script(bh2u(revocation_pubkey))) + bytes([opcodes.OP_ELSE]) + add_number_to_script(to_self_delay) \
+ bytes([opcodes.OP_CSV, opcodes.OP_DROP]) + bfh(push_script(bh2u(delayed_pubkey))) + bytes([opcodes.OP_ENDIF, opcodes.OP_CHECKSIG])
local_address = bitcoin.redeem_script_to_address('p2wsh', bh2u(local_script))
fee = local_feerate * overall_weight(len(htlcs)) // 1000
local_amount = to_local_msat // 1000 - fee
remote_address = bitcoin.pubkey_to_address('p2wpkh', bh2u(remotepubkey))
remote_amount = to_remote_msat // 1000
to_local = (bitcoin.TYPE_ADDRESS, local_address, local_amount)
to_remote = (bitcoin.TYPE_ADDRESS, remote_address, remote_amount)
c_outputs = [to_local, to_remote]
for script, msat_amount in htlcs:
c_outputs += [(bitcoin.TYPE_ADDRESS, bitcoin.redeem_script_to_address('p2wsh', bh2u(script)), msat_amount // 1000)]
# trim outputs
c_outputs = list(filter(lambda x:x[2]>= dust_limit_satoshis, c_outputs))
# create commitment tx
tx = Transaction.from_io(c_inputs, c_outputs, locktime=locktime, version=2)
tx.BIP_LI01_sort()
@ -537,13 +536,8 @@ class Peer(PrintError):
self.channel_accepted[payload["temporary_channel_id"]].set_result(payload)
def on_funding_signed(self, payload):
sig = payload['signature']
channel_id = payload['channel_id']
#tx = self.channels[channel_id] # FIXME
#self.network.broadcast(tx)
def on_funding_signed(self, payload):
self.funding_signed[payload["temporary_channel_id"]].set_result(payload)
channel_id = int.from_bytes(payload['channel_id'], byteorder="big")
self.funding_signed[channel_id].set_result(payload)
def on_funding_locked(self, payload):
pass
@ -623,9 +617,16 @@ class Peer(PrintError):
revocation_basepoint, revocation_privkey = next(keys)
htlc_basepoint, htlc_privkey = next(keys)
delayed_payment_basepoint, delayed_privkey = next(keys)
funding_satoshis = 20000
base_secret = 0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
per_commitment_secret = 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100
# amounts
funding_satoshis = 200000
push_msat = 0
local_feerate = 20000
dust_limit_satoshis = 10
to_self_delay = 144
ctn = 0
#
base_point = secret_to_pubkey(base_secret)
per_commitment_point = secret_to_pubkey(per_commitment_secret)
msg = gen_msg(
@ -633,13 +634,17 @@ class Peer(PrintError):
temporary_channel_id=temp_channel_id,
chain_hash=bytes.fromhex(rev_hex(constants.net.GENESIS)),
funding_satoshis=funding_satoshis,
push_msat=push_msat,
dust_limit_satoshis=dust_limit_satoshis,
feerate_per_kw=local_feerate,
max_accepted_htlcs=5,
funding_pubkey=funding_pubkey,
revocation_basepoint=revocation_basepoint,
htlc_basepoint=htlc_basepoint,
payment_basepoint=base_point,
delayed_payment_basepoint=delayed_payment_basepoint,
first_per_commitment_point=per_commitment_point
first_per_commitment_point=per_commitment_point,
to_self_delay=to_self_delay
)
self.channel_accepted[temp_channel_id] = asyncio.Future()
self.send_message(msg)
@ -649,44 +654,72 @@ class Peer(PrintError):
del self.channel_accepted[temp_channel_id]
remote_per_commitment_point = payload['first_per_commitment_point']
remote_funding_pubkey = payload["funding_pubkey"]
remote_delay = int.from_bytes(payload['to_self_delay'], byteorder="big")
remote_dust_limit_satoshis = int.from_bytes(payload['dust_limit_satoshis'], byteorder="big")
remote_revocation_basepoint = payload['revocation_basepoint']
remote_payment_basepoint = payload['payment_basepoint']
remote_delayed_payment_basepoint = payload['delayed_payment_basepoint']
self.print_error('remote dust limit', remote_dust_limit_satoshis)
self.print_error('remote delay', remote_delay)
# create funding tx
pubkeys = sorted([bh2u(funding_pubkey), bh2u(remote_funding_pubkey)])
redeem_script = transaction.multisig_script(pubkeys, 2)
funding_address = bitcoin.redeem_script_to_address('p2wsh', redeem_script)
funding_output = (bitcoin.TYPE_ADDRESS, funding_address, funding_satoshis)
funding_tx = wallet.mktx([funding_output], None, config, 1000)
funding_txid = funding_tx.txid()
funding_index = funding_tx.outputs().index(funding_output)
remote_payment_basepoint = payload['payment_basepoint']
localpubkey = derive_pubkey(base_point, per_commitment_point)
localprivkey = derive_privkey(base_secret, per_commitment_point)
self.print_error('localpubkey', binascii.hexlify(localpubkey))
# derive keys
localpubkey = derive_pubkey(base_point, remote_per_commitment_point)
localprivkey = derive_privkey(base_secret, remote_per_commitment_point)
remotepubkey = derive_pubkey(remote_payment_basepoint, per_commitment_point)
revocation_pubkey = derive_blinded_pubkey(revocation_basepoint, remote_per_commitment_point)
self.print_error('revocation_pubkey', binascii.hexlify(revocation_pubkey))
local_delayedpubkey = derive_pubkey(delayed_payment_basepoint, per_commitment_point)
self.print_error('local_delayedpubkey', binascii.hexlify(local_delayedpubkey))
remotepubkey = derive_pubkey(remote_payment_basepoint, remote_per_commitment_point)
self.print_error('remotepubkey', binascii.hexlify(remotepubkey))
ctn = 0
c_tx = make_commitment(
remote_revocation_pubkey = derive_blinded_pubkey(remote_revocation_basepoint, per_commitment_point)
local_delayedpubkey = derive_pubkey(delayed_payment_basepoint, remote_per_commitment_point)
remote_delayedpubkey = derive_pubkey(remote_delayed_payment_basepoint, per_commitment_point)
# compute amounts
htlcs = []
fee = local_feerate * overall_weight(len(htlcs)) // 1000
to_local_msat = funding_satoshis*1000 - push_msat
to_remote_msat = push_msat
local_amount = to_local_msat // 1000 - fee
remote_amount = to_remote_msat // 1000
# remote commitment transaction
remote_ctx = make_commitment(
ctn,
funding_pubkey, remote_funding_pubkey, remotepubkey,
base_point, remote_payment_basepoint,
revocation_pubkey, local_delayedpubkey,
funding_tx.txid(), funding_index, funding_satoshis,
funding_satoshis*1000, 0, 20000, 144)
c_tx.sign({bh2u(funding_pubkey): (funding_privkey, True)})
remote_funding_pubkey, funding_pubkey, # will be sorted
localpubkey, # used in to_remote
base_point, remote_payment_basepoint, # used by obscured ctn
revocation_pubkey, remote_delayedpubkey, # used by to_local script
funding_txid, funding_index, funding_satoshis,
remote_amount, local_amount, remote_delay, remote_dust_limit_satoshis)
remote_ctx.sign({bh2u(funding_pubkey): (funding_privkey, True)})
sig_index = pubkeys.index(bh2u(funding_pubkey))
sig = bytes.fromhex(c_tx.inputs()[0]["signatures"][sig_index])
self.print_error('sig', len(sig))
sig = bytes(sig[:len(sig)-1])
r, s = sigdecode_der(sig, SECP256k1.generator.order())
sig = sigencode_string_canonize(r, s, SECP256k1.generator.order())
self.print_error('canonical signature', len(sig))
self.funding_signed[temp_channel_id] = asyncio.Future()
self.send_message(gen_msg("funding_created", temporary_channel_id=temp_channel_id, funding_txid=bytes.fromhex(funding_tx.txid()), funding_output_index=funding_index, signature=sig))
sig = bytes.fromhex(remote_ctx.inputs()[0]["signatures"][sig_index])
r, s = sigdecode_der(sig[:-1], SECP256k1.generator.order())
sig_64 = sigencode_string_canonize(r, s, SECP256k1.generator.order())
funding_txid = bytes.fromhex(funding_txid)[::-1]
channel_id = int.from_bytes(funding_txid, byteorder="big") ^ funding_index
self.send_message(gen_msg("funding_created", temporary_channel_id=temp_channel_id, funding_txid=funding_txid, funding_output_index=funding_index, signature=sig_64))
self.funding_signed[channel_id] = asyncio.Future()
try:
funding_signed = await self.funding_signed[temp_channel_id]
payload = await self.funding_signed[channel_id]
finally:
del self.funding_signed[temp_channel_id]
del self.funding_signed[channel_id]
self.print_error('received funding_signed')
remote_sig = payload['signature']
# todo: check signature agains local ctx
local_ctx = make_commitment(
ctn,
funding_pubkey, remote_funding_pubkey,
remotepubkey,
base_point, remote_payment_basepoint,
revocation_pubkey, local_delayedpubkey,
funding_txid, funding_index, funding_satoshis,
local_amount, remote_amount, to_self_delay, dust_limit_satoshis)
# broadcast funding tx
#self.network.broadcast(funding_tx)
self.print_error('Done')
# replacement for lightningCall

8
lib/tests/test_lnbase.py

@ -4,7 +4,7 @@ import unittest
from lib.util import bh2u, bfh
from lib.lnbase import make_commitment, get_obscured_ctn, Peer, make_offered_htlc, make_received_htlc, make_htlc_tx
from lib.lnbase import secret_to_pubkey, derive_pubkey, derive_privkey, derive_blinded_pubkey
from lib.lnbase import secret_to_pubkey, derive_pubkey, derive_privkey, derive_blinded_pubkey, overall_weight
from lib.lnbase import make_htlc_tx_output, make_htlc_tx_inputs
from lib.transaction import Transaction
from lib import bitcoin
@ -45,12 +45,16 @@ class Test_LNBase(unittest.TestCase):
# to_remote amount 3000000 P2WPKH(0394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b)
remote_signature = "3045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c0"
# local_signature = 3044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c3836939
htlcs=[]
fee = local_feerate_per_kw * overall_weight(len(htlcs)) // 1000
local_amount = to_local_msat // 1000 - fee
remote_amount = to_remote_msat // 1000
our_commit_tx = make_commitment(commitment_number,
local_funding_pubkey, remote_funding_pubkey, remotepubkey,
local_payment_basepoint, remote_payment_basepoint,
local_revocation_pubkey, local_delayedpubkey,
funding_tx_id, funding_output_index, funding_amount_satoshi,
to_local_msat, to_remote_msat, local_feerate_per_kw, local_delay)
local_amount, remote_amount, local_delay, local_dust_limit_satoshi)
self.sign_and_insert_remote_sig(our_commit_tx, remote_signature)
ref_commit_tx_str = '02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311054a56a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c383693901483045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220'
self.assertEqual(str(our_commit_tx), ref_commit_tx_str)

Loading…
Cancel
Save