Browse Source

simnet/testnet support in bolt11, set max-htlc-value-in-flight

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
Janus 7 years ago
committed by ThomasV
parent
commit
1ffaed718c
  1. 10
      lib/lightning_payencode/lnaddr.py
  2. 9
      lib/lnbase.py
  3. 2
      lib/tests/test_bolt11.py
  4. 28
      lib/tests/test_lnbase_online.py

10
lib/lightning_payencode/lnaddr.py

@ -7,6 +7,7 @@ from ..segwit_addr import bech32_encode, bech32_decode, CHARSET
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from bitstring import BitArray from bitstring import BitArray
from decimal import Decimal from decimal import Decimal
from .. import constants
import bitstring import bitstring
import hashlib import hashlib
@ -230,14 +231,14 @@ def lnencode(addr, privkey):
return bech32_encode(hrp, bitarray_to_u5(data)) return bech32_encode(hrp, bitarray_to_u5(data))
class LnAddr(object): class LnAddr(object):
def __init__(self, paymenthash=None, amount=None, currency='bc', tags=None, date=None): def __init__(self, paymenthash=None, amount=None, currency=None, tags=None, date=None):
self.date = int(time.time()) if not date else int(date) self.date = int(time.time()) if not date else int(date)
self.tags = [] if not tags else tags self.tags = [] if not tags else tags
self.unknown_tags = [] self.unknown_tags = []
self.paymenthash=paymenthash self.paymenthash=paymenthash
self.signature = None self.signature = None
self.pubkey = None self.pubkey = None
self.currency = currency self.currency = constants.net.SEGWIT_HRP if currency is None else currency
self.amount = amount self.amount = amount
def __str__(self): def __str__(self):
@ -247,7 +248,7 @@ class LnAddr(object):
", ".join([k + '=' + str(v) for k, v in self.tags]) ", ".join([k + '=' + str(v) for k, v in self.tags])
) )
def lndecode(a, verbose=False): def lndecode(a, verbose=False, expected_hrp=constants.net.SEGWIT_HRP):
hrp, data = bech32_decode(a, ignore_long_length=True) hrp, data = bech32_decode(a, ignore_long_length=True)
if not hrp: if not hrp:
raise ValueError("Bad bech32 checksum") raise ValueError("Bad bech32 checksum")
@ -258,6 +259,9 @@ def lndecode(a, verbose=False):
if not hrp.startswith('ln'): if not hrp.startswith('ln'):
raise ValueError("Does not start with ln") raise ValueError("Does not start with ln")
if not hrp[2:].startswith(expected_hrp):
raise ValueError("Wrong Lightning invoice HRP " + hrp[2:] + ", should be " + expected_hrp)
data = u5_to_bitarray(data); data = u5_to_bitarray(data);
# Final signature 65 bytes, split it off. # Final signature 65 bytes, split it off.

9
lib/lnbase.py

@ -675,7 +675,7 @@ class Peer(PrintError):
self.writer.close() self.writer.close()
@aiosafe @aiosafe
async def channel_establishment_flow(self, wallet, config): async def channel_establishment_flow(self, wallet, config, funding_satoshis, push_msat):
await self.initialized await self.initialized
temp_channel_id = os.urandom(32) temp_channel_id = os.urandom(32)
keys = get_unused_keys() keys = get_unused_keys()
@ -687,8 +687,6 @@ class Peer(PrintError):
per_commitment_secret_seed = 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100.to_bytes(length=32, byteorder="big") per_commitment_secret_seed = 0x1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100.to_bytes(length=32, byteorder="big")
per_commitment_secret_index = 2**48 - 1 per_commitment_secret_index = 2**48 - 1
# amounts # amounts
funding_satoshis = 200000
push_msat = 0
local_feerate = 20000 local_feerate = 20000
dust_limit_satoshis = 10 dust_limit_satoshis = 10
to_self_delay = 144 to_self_delay = 144
@ -713,7 +711,8 @@ class Peer(PrintError):
payment_basepoint=base_point, payment_basepoint=base_point,
delayed_payment_basepoint=delayed_payment_basepoint, delayed_payment_basepoint=delayed_payment_basepoint,
first_per_commitment_point=per_commitment_point_first, first_per_commitment_point=per_commitment_point_first,
to_self_delay=to_self_delay to_self_delay=to_self_delay,
max_htlc_value_in_flight_msat=10_000
) )
self.channel_accepted[temp_channel_id] = asyncio.Future() self.channel_accepted[temp_channel_id] = asyncio.Future()
self.send_message(msg) self.send_message(msg)
@ -793,7 +792,7 @@ class Peer(PrintError):
self.local_funding_locked[channel_id] = asyncio.Future() self.local_funding_locked[channel_id] = asyncio.Future()
self.remote_funding_locked[channel_id] = asyncio.Future() self.remote_funding_locked[channel_id] = asyncio.Future()
success, _txid = self.network.broadcast(funding_tx) success, _txid = self.network.broadcast(funding_tx)
assert success assert success, success
# wait until we see confirmations # wait until we see confirmations
def on_network_update(event, *args): def on_network_update(event, *args):
conf = wallet.get_tx_height(funding_txid)[1] conf = wallet.get_tx_height(funding_txid)[1]

2
lib/tests/test_bolt11.py

@ -71,7 +71,7 @@ class TestBolt11(unittest.TestCase):
# Roundtrip # Roundtrip
for t in tests: for t in tests:
o = lndecode(lnencode(t, PRIVKEY)) o = lndecode(lnencode(t, PRIVKEY), False, t.currency)
self.compare(t, o) self.compare(t, o)
def test_n_decoding(self): def test_n_decoding(self):

28
lib/tests/test_lnbase_online.py

@ -1,16 +1,20 @@
import traceback
import sys import sys
import json import json
import binascii import binascii
import asyncio import asyncio
import time import time
from lib.bitcoin import sha256
from decimal import Decimal
from lib.constants import set_testnet, set_simnet from lib.constants import set_testnet, set_simnet
from lib.simple_config import SimpleConfig from lib.simple_config import SimpleConfig
from lib.network import Network from lib.network import Network
from lib.storage import WalletStorage from lib.storage import WalletStorage
from lib.wallet import Wallet from lib.wallet import Wallet
from lib.lnbase import Peer, node_list from lib.lnbase import Peer, node_list
from lib.lightning_payencode.lnaddr import lnencode, LnAddr
import lib.constants as constants
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) > 2: if len(sys.argv) > 2:
@ -38,8 +42,24 @@ if __name__ == "__main__":
# start peer # start peer
peer = Peer(host, port, pubkey, request_initial_sync=False, network=network) peer = Peer(host, port, pubkey, request_initial_sync=False, network=network)
network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), network.asyncio_loop)) network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), network.asyncio_loop))
funding_satoshis = 200000
push_msat = 100000
# run blocking test # run blocking test
coro = peer.channel_establishment_flow(wallet, config) async def async_test():
fut = asyncio.run_coroutine_threadsafe(coro, network.asyncio_loop) RHASH = sha256(bytes.fromhex("01"*32))
while network.asyncio_loop.is_running(): await peer.channel_establishment_flow(wallet, config, funding_satoshis, push_msat)
pay_req = lnencode(LnAddr(RHASH, amount=Decimal("0.00000001")*10, tags=[('d', 'one cup of coffee')]), peer.privkey[:32])
print("payment request", pay_req)
while True:
await asyncio.sleep(1)
fut = asyncio.run_coroutine_threadsafe(async_test(), network.asyncio_loop)
while not fut.done():
time.sleep(1) time.sleep(1)
if fut.exception():
try:
raise fut.exception()
except:
traceback.print_exc()
network.stop()

Loading…
Cancel
Save