Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions contrib/pyln-proto/pyln/proto/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,15 @@ def encode(self, privkey):
data += tagged_bytes('p', self.paymenthash)
tags_set = set()

if self.route_hints is not None:
# `route_hints` is a list with one `RouteHintSet` per `r` field,
# the hops of that route in order. A bare `RouteHintSet` is still
# accepted and written like before, one `r` field per hint.
if isinstance(self.route_hints, RouteHintSet):
for rh in self.route_hints.route_hints:
data += tagged_bytes('r', rh.to_bytes())
elif self.route_hints is not None:
for route in self.route_hints:
data += tagged_bytes('r', route.to_bytes())

for k, v in self.tags:

Expand Down Expand Up @@ -334,7 +340,11 @@ def decode(cls, b):
data_length = len(tagdata) / 5

if tag == 'r':
inv.route_hints = RouteHintSet.from_bytes(trim_to_bytes(tagdata))
# Each `r` field is one route and its entries are the hops
# along it, so keep them apart: one RouteHintSet per field.
if inv.route_hints is None:
inv.route_hints = []
inv.route_hints.append(RouteHintSet.from_bytes(trim_to_bytes(tagdata)))
continue
if data_length != 82:
inv.unknown_tags.append((tag, tagdata))
Expand Down
91 changes: 91 additions & 0 deletions contrib/pyln-proto/tests/test_invoice.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pyln.proto import Invoice
from pyln.proto.invoice import RouteHint, RouteHintSet, pull_tagged, u5_to_bitarray
from pyln.proto.bech32 import bech32_decode
from decimal import Decimal
from bitstring import ConstBitStream
import binascii
Expand All @@ -25,3 +27,92 @@ def test_encode():
privkey = 'c28a9f80738f770d527803a566cf6fc3edf6cea586c4fc4a5223a5ad797e1ac3'
i = inv.encode(privkey)
assert(i == 'lnbc1u1p0zyt04pp5wcnjhxu4k98td0kw8ng9zqrd3246cc7r559a063tk5mp9v9fxf9sdpqw3jhxazlwpshjhmjda6hgetzdahhxapjxqyjw5q0s43wfg4f6yl200hc805kc9u97dp7m6j98fz33uzf4t6kp73trcz8fxkrpass063j2j4quxjr4th2r72lk27tm2aw383zgnl8zpgajsq5kmll8')


PRIVKEY = 'c28a9f80738f770d527803a566cf6fc3edf6cea586c4fc4a5223a5ad797e1ac3'


def make_hint(n):
rh = RouteHint()
rh.pubkey = bytes([2] + [n] * 32)
rh.short_channel_id = n
rh.fee_base_msat = 1000
rh.fee_proportional_millionths = 10
rh.cltv_expiry_delta = 6
return rh


def make_route(*hops):
route = RouteHintSet()
for n in hops:
route.add(make_hint(n))
return route


def r_fields(bolt11):
"""Byte length of every `r` field in the invoice, in order."""
_, words = bech32_decode(bolt11)
data = ConstBitStream(u5_to_bitarray(words))[:-65 * 8]
data.pos = 35
lengths = []
while data.pos != data.len:
tag, tagdata, data = pull_tagged(data)
if tag == 'r':
lengths.append(len(tagdata) // 8)
return lengths


def scids(inv):
return [[rh.short_channel_id for rh in route.route_hints] for route in inv.route_hints]


def test_decode_multiple_route_hints():
"""Every `r` field is its own route, so decoding must keep all of them."""
inv = Invoice(paymenthash=bytes(32), tags=[('d', 'two hints')], date=1579298293)
inv.route_hints = [make_route(1), make_route(2)]

bolt11 = inv.encode(PRIVKEY)
assert r_fields(bolt11) == [RouteHint.length, RouteHint.length]

decoded = Invoice.decode(bolt11)
assert scids(decoded) == [[1], [2]]
assert decoded.encode(PRIVKEY) == bolt11


def test_route_hint_boundaries():
"""One 2-hop `r` field is not the same as two 1-hop `r` fields."""
one_route = Invoice(paymenthash=bytes(32), tags=[('d', 'hints')], date=1579298293)
one_route.route_hints = [make_route(1, 2)]
two_routes = Invoice(paymenthash=bytes(32), tags=[('d', 'hints')], date=1579298293)
two_routes.route_hints = [make_route(1), make_route(2)]

b1 = one_route.encode(PRIVKEY)
b2 = two_routes.encode(PRIVKEY)
assert r_fields(b1) == [2 * RouteHint.length]
assert r_fields(b2) == [RouteHint.length, RouteHint.length]

d1 = Invoice.decode(b1)
d2 = Invoice.decode(b2)
assert scids(d1) == [[1, 2]]
assert scids(d2) == [[1], [2]]
assert d1.encode(PRIVKEY) == b1
assert d2.encode(PRIVKEY) == b2


def test_encode_bare_route_hint_set():
"""A bare RouteHintSet still gives one `r` field per hint."""
inv = Invoice(paymenthash=bytes(32), tags=[('d', 'hints')], date=1579298293)
inv.route_hints = make_route(1, 2)

bolt11 = inv.encode(PRIVKEY)
assert r_fields(bolt11) == [RouteHint.length, RouteHint.length]
assert scids(Invoice.decode(bolt11)) == [[1], [2]]


def test_decode_existing_two_hop_route():
"""The invoice from test_decode carries a single `r` field with two hops."""
i = 'lnbcrt1u1p0zyt04pp5wcnjhxu4k98td0kw8ng9zqrd3246cc7r559a063tk5mp9v9fxf9sdpqw3jhxazlwpshjhmjda6hgetzdahhxapjxqyjw5qcqp9sp5asxa9pwxt6yuse5egtcna8gezazr657chz72qfzztsthxwnwj0yqr9yqdwjkyvjm7apxnssu4qgwhfkd67ghs6n6k48v6uqczgt88p6tky96qqqdcqqqqgqqyqqqqlgqqqqqzsqqcpc9njea0cche7cgemu9c6lyv55hxvjem9f2jgle799d3kt9kw7rxgqqphqqqqzqqqsqqqraqqqqqq2qqrq9qy9qsqfm47uq6ny374m22dxw7p6j8c0khj4tspjcj78l33vf6qv8grhknsmw6slxxucpvxv5s9464qfng8324sagn8g8ng3uuh4d2vdpnmsdgqyqhn4k'
assert r_fields(i) == [2 * RouteHint.length]
inv = Invoice.decode(i)
assert len(inv.route_hints) == 1
assert len(inv.route_hints[0].route_hints) == 2