electrum

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

commit 1ffaed718c317f9d7bed04824020e84be3c88340
parent fd7469745e56fa53307b48c35b59a77516051b93
Author: Janus <ysangkok@gmail.com>
Date:   Mon, 23 Apr 2018 15:11:56 +0200

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

Diffstat:
Mlib/lightning_payencode/lnaddr.py | 10+++++++---
Mlib/lnbase.py | 9++++-----
Mlib/tests/test_bolt11.py | 2+-
Mlib/tests/test_lnbase_online.py | 28++++++++++++++++++++++++----
4 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/lib/lightning_payencode/lnaddr.py b/lib/lightning_payencode/lnaddr.py @@ -7,6 +7,7 @@ from ..segwit_addr import bech32_encode, bech32_decode, CHARSET from binascii import hexlify, unhexlify from bitstring import BitArray from decimal import Decimal +from .. import constants import bitstring import hashlib @@ -230,14 +231,14 @@ def lnencode(addr, privkey): return bech32_encode(hrp, bitarray_to_u5(data)) 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.tags = [] if not tags else tags self.unknown_tags = [] self.paymenthash=paymenthash self.signature = None self.pubkey = None - self.currency = currency + self.currency = constants.net.SEGWIT_HRP if currency is None else currency self.amount = amount def __str__(self): @@ -247,7 +248,7 @@ class LnAddr(object): ", ".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) if not hrp: raise ValueError("Bad bech32 checksum") @@ -258,6 +259,9 @@ def lndecode(a, verbose=False): if not hrp.startswith('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); # Final signature 65 bytes, split it off. diff --git a/lib/lnbase.py b/lib/lnbase.py @@ -675,7 +675,7 @@ class Peer(PrintError): self.writer.close() @aiosafe - async def channel_establishment_flow(self, wallet, config): + async def channel_establishment_flow(self, wallet, config, funding_satoshis, push_msat): await self.initialized temp_channel_id = os.urandom(32) 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_index = 2**48 - 1 # amounts - funding_satoshis = 200000 - push_msat = 0 local_feerate = 20000 dust_limit_satoshis = 10 to_self_delay = 144 @@ -713,7 +711,8 @@ class Peer(PrintError): payment_basepoint=base_point, delayed_payment_basepoint=delayed_payment_basepoint, 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.send_message(msg) @@ -793,7 +792,7 @@ class Peer(PrintError): self.local_funding_locked[channel_id] = asyncio.Future() self.remote_funding_locked[channel_id] = asyncio.Future() success, _txid = self.network.broadcast(funding_tx) - assert success + assert success, success # wait until we see confirmations def on_network_update(event, *args): conf = wallet.get_tx_height(funding_txid)[1] diff --git a/lib/tests/test_bolt11.py b/lib/tests/test_bolt11.py @@ -71,7 +71,7 @@ class TestBolt11(unittest.TestCase): # Roundtrip for t in tests: - o = lndecode(lnencode(t, PRIVKEY)) + o = lndecode(lnencode(t, PRIVKEY), False, t.currency) self.compare(t, o) def test_n_decoding(self): diff --git a/lib/tests/test_lnbase_online.py b/lib/tests/test_lnbase_online.py @@ -1,16 +1,20 @@ +import traceback import sys import json import binascii import asyncio import time +from lib.bitcoin import sha256 +from decimal import Decimal from lib.constants import set_testnet, set_simnet from lib.simple_config import SimpleConfig from lib.network import Network from lib.storage import WalletStorage from lib.wallet import Wallet from lib.lnbase import Peer, node_list - +from lib.lightning_payencode.lnaddr import lnencode, LnAddr +import lib.constants as constants if __name__ == "__main__": if len(sys.argv) > 2: @@ -38,8 +42,24 @@ if __name__ == "__main__": # start peer peer = Peer(host, port, pubkey, request_initial_sync=False, network=network) network.futures.append(asyncio.run_coroutine_threadsafe(peer.main_loop(), network.asyncio_loop)) + + funding_satoshis = 200000 + push_msat = 100000 + # run blocking test - coro = peer.channel_establishment_flow(wallet, config) - fut = asyncio.run_coroutine_threadsafe(coro, network.asyncio_loop) - while network.asyncio_loop.is_running(): + async def async_test(): + RHASH = sha256(bytes.fromhex("01"*32)) + 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) + if fut.exception(): + try: + raise fut.exception() + except: + traceback.print_exc() + network.stop()