diff --git a/noise/noise.py b/noise/noise.py index c19e383..b97e07f 100755 --- a/noise/noise.py +++ b/noise/noise.py @@ -1,16 +1,10 @@ #!/usr/bin/env python3 -from binascii import hexlify, unhexlify -from collections import namedtuple -from io import BytesIO +from binascii import hexlify from onion import OnionPayload from onion import TlvPayload -from primitives import varint_decode, varint_encode from pyln.client import Plugin, RpcError import hashlib import os -import random -import shelve -import string import struct import time import zbase32 @@ -22,6 +16,8 @@ TLV_KEYSEND_PREIMAGE = 5482373484 TLV_NOISE_MESSAGE = 34349334 TLV_NOISE_SIGNATURE = 34349335 TLV_NOISE_TIMESTAMP = 34349343 + + class Message(object): def __init__(self, sender, body, signature, payment=None, id=None): self.id = id @@ -41,6 +37,7 @@ class Message(object): "verified": self.verified, } + class Payment(object): def __init__(self, payment_key, amount): self.payment_key = payment_key @@ -104,7 +101,7 @@ def deliver(node_id, payload, amt, payment_hash, max_attempts=5): first_hop=first_hop, payment_hash=payment_hash, shared_secrets=onion['shared_secrets'] - ) + ) try: plugin.rpc.waitsendpay(payment_hash=payment_hash) return {'route': route, 'payment_hash': payment_hash, 'attempt': attempt} @@ -112,7 +109,7 @@ def deliver(node_id, payload, amt, payment_hash, max_attempts=5): failcode = e.error['data']['failcode'] failingidx = e.error['data']['erring_index'] if failcode == 16399 or failingidx == len(hops): - return {'route': route, 'payment_hash': payment_hash, 'attempt': attempt+1} + return {'route': route, 'payment_hash': payment_hash, 'attempt': attempt + 1} plugin.log("Retrying delivery.") @@ -139,7 +136,7 @@ def sendmsg(node_id, msg, plugin, request, pay=None, **kwargs): # the TLV payload and verification ends up with a bogus sender node_id. sigmsg = hexlify(payload.to_bytes()).decode('ASCII') sig = plugin.rpc.signmessage(sigmsg) - sigcheck = plugin.rpc.checkmessage(sigmsg, sig['zbase']) + plugin.rpc.checkmessage(sigmsg, sig['zbase']) sig = zbase32.decode(sig['zbase']) payload.add_field(TLV_NOISE_SIGNATURE, sig) diff --git a/noise/onion.py b/noise/onion.py index ef7d2ab..cde7e65 100644 --- a/noise/onion.py +++ b/noise/onion.py @@ -40,7 +40,7 @@ class LegacyOnionPayload(OnionPayload): def __init__(self, amt_to_forward, outgoing_cltv_value, short_channel_id=None, padding=None): assert(padding is None or len(padding) == 12) - self.padding = b'\x00'*12 if padding is None else padding + self.padding = b'\x00' * 12 if padding is None else padding if isinstance(amt_to_forward, str): self.amt_to_forward = int(amt_to_forward) diff --git a/noise/test_chat.py b/noise/test_chat.py index b77b69c..b0ff73d 100644 --- a/noise/test_chat.py +++ b/noise/test_chat.py @@ -1,12 +1,12 @@ -from binascii import hexlify from onion import TlvPayload from flaky import flaky from pprint import pprint from pyln.client import RpcError -from pyln.testing.fixtures import * +from pyln.testing.fixtures import * # noqa: F401,F403 from pyln.testing.utils import wait_for import hashlib -import unittest +import os +import pytest import zbase32 @@ -29,10 +29,10 @@ def test_sendmsg_success(node_factory, executor): assert(m1 == m2) assert(m2['sender'] == l1.info['id']) - assert(m2['verified'] == True) + assert(m2['verified'] is True) -@unittest.skipIf(True, "This test is flaky since we cannot force a payment to take a specific route") +@flaky # since we cannot force a payment to take a specific route def test_sendmsg_retry(node_factory, executor): """Fail a sendmsg using a cheap route, and check that it retries. @@ -64,16 +64,16 @@ def test_sendmsg_retry(node_factory, executor): return False return True - wait_for(lambda: [c['active'] for c in l1.rpc.listchannels()['channels']] == [True]*10) + wait_for(lambda: [c['active'] for c in l1.rpc.listchannels()['channels']] == [True] * 10) # Now stop l5 so the first attempt will fail. l5.stop() - recv = executor.submit(l4.rpc.recvmsg) + executor.submit(l4.rpc.recvmsg) send = executor.submit(l1.rpc.sendmsg, l4.info['id'], "Hello world!") # Just making sure our view didn't change since we initiated the attempt - assert([c['active'] for c in l1.rpc.listchannels()['channels']] == [True]*10) + assert([c['active'] for c in l1.rpc.listchannels()['channels']] == [True] * 10) pprint(l1.rpc.listchannels()) l1.daemon.wait_for_log(r'Retrying delivery') @@ -82,7 +82,7 @@ def test_sendmsg_retry(node_factory, executor): assert(sres['attempt'] == 2) pprint(sres) - msg = l4.rpc.recvmsg(last_id=-1) + l4.rpc.recvmsg(last_id=-1) def test_zbase32(): @@ -104,7 +104,7 @@ def test_msg_and_keysend(node_factory, executor): m = l3.rpc.recvmsg(last_id=-1) assert(m['sender'] == l1.info['id']) - assert(m['verified'] == True) + assert(m['verified'] is True) p = m['payment'] assert(p is not None) assert(p['payment_key'] is not None) @@ -121,7 +121,7 @@ def test_forward_ok(node_factory, executor): https://github.com/lightningd/plugins/pull/68#issuecomment-577251902 """ - opts = [{'plugin': plugin}]*3 + opts = [{'plugin': plugin}] * 3 l1, l2, l3 = node_factory.line_graph(3, wait_for_announce=True, opts=opts) recv = executor.submit(l3.rpc.recvmsg) @@ -136,21 +136,21 @@ def test_forward_ok(node_factory, executor): assert(m1 == m2) assert(m2['sender'] == l1.info['id']) - assert(m2['verified'] == True) + assert(m2['verified'] is True) def test_missing_tlv_fields(node_factory): """If we're missing a field we should not crash """ - opts = [{'plugin': plugin}]*2 - l1, l2 = node_factory.line_graph(2, wait_for_announce=True, opts=opts) + opts = [{'plugin': plugin}] * 2 + l1, l2 = node_factory.line_graph(2, wait_for_announce=True, opts=opts) payment_key = os.urandom(32) payment_hash = hashlib.sha256(payment_key).hexdigest() route = l1.rpc.getroute(l2.info['id'], 10, 10)['route'] def send(key, value): - hops = [{"type":"tlv", "pubkey": l2.info['id'], "payload": None}] + hops = [{"type": "tlv", "pubkey": l2.info['id'], "payload": None}] payload = TlvPayload() payload.add_field(key, value) @@ -169,5 +169,5 @@ def test_missing_tlv_fields(node_factory): send(34349334, b'Message body') assert(l2.daemon.wait_for_log(r'Missing message body or signature')) - send(34349335, b'00'*32) + send(34349335, b'00' * 32) assert(l2.daemon.wait_for_log(r'Missing message body or signature')) diff --git a/noise/zbase32.py b/noise/zbase32.py index 68c6bf9..e93ffb6 100644 --- a/noise/zbase32.py +++ b/noise/zbase32.py @@ -21,6 +21,7 @@ zbase32_revchars = [ 255, 255, 255, 255, 255, 255, 255 ] + def bitarray_to_u5(barr): assert len(barr) % 5 == 0 ret = [] @@ -29,17 +30,20 @@ def bitarray_to_u5(barr): ret.append(s.read(5).uint) return ret + def u5_to_bitarray(arr): ret = bitstring.BitArray() for a in arr: ret += bitstring.pack("uint:5", a) return ret + def encode(b): uint5s = bitarray_to_u5(b) res = [zbase32_chars[c] for c in uint5s] return bytes(res) + def decode(b): if isinstance(b, str): b = b.encode('ASCII')