pyln-proto: Added a couple of utilities to manage onions and zbase32

This commit is contained in:
Christian Decker
2020-01-20 14:03:33 +01:00
committed by Rusty Russell
parent d36af2c340
commit d3f6ebf911
6 changed files with 425 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
from binascii import unhexlify
from pyln.proto import onion
def test_legacy_payload():
legacy = unhexlify(
b'00000067000001000100000000000003e800000075000000000000000000000000'
)
payload = onion.OnionPayload.from_bytes(legacy)
assert(payload.to_bytes(include_realm=True) == legacy)
def test_tlv_payload():
tlv = unhexlify(
b'58fe020c21160c48656c6c6f20776f726c6421fe020c21184076e8acd54afbf2361'
b'0b7166ba689afcc9e8ec3c44e442e765012dfc1d299958827d0205f7e4e1a12620e'
b'7fc8ce1c7d3651acefde899c33f12b6958d3304106a0'
)
payload = onion.OnionPayload.from_bytes(tlv)
assert(payload.to_bytes() == tlv)
fields = payload.fields
assert(len(fields) == 2)
assert(isinstance(fields[0], onion.TextField))
assert(fields[0].typenum == 34349334 and fields[0].value == "Hello world!")
assert(fields[1].typenum == 34349336 and fields[1].value == unhexlify(
b'76e8acd54afbf23610b7166ba689afcc9e8ec3c44e442e765012dfc1d299958827d'
b'0205f7e4e1a12620e7fc8ce1c7d3651acefde899c33f12b6958d3304106a0'
))
assert(payload.to_bytes() == tlv)

View File

@@ -0,0 +1,30 @@
from binascii import hexlify, unhexlify
from pyln.proto import zbase32
from pyln.proto.primitives import ShortChannelId
def test_short_channel_id():
num = 618150934845652992
b = unhexlify(b'08941d00090d0000')
s = '562205x2317x0'
s1 = ShortChannelId.from_int(num)
s2 = ShortChannelId.from_str(s)
s3 = ShortChannelId.from_bytes(b)
expected = ShortChannelId(block=562205, txnum=2317, outnum=0)
assert(s1 == expected)
assert(s2 == expected)
assert(s3 == expected)
assert(expected.to_bytes() == b)
assert(str(expected) == s)
assert(expected.to_int() == num)
def test_zbase32():
zb32 = b'd75qtmgijm79rpooshmgzjwji9gj7dsdat8remuskyjp9oq1ugkaoj6orbxzhuo4njtyh96e3aq84p1tiuz77nchgxa1s4ka4carnbiy'
b = zbase32.decode(zb32)
assert(hexlify(b) == b'1f76e8acd54afbf23610b7166ba689afcc9e8ec3c44e442e765012dfc1d299958827d0205f7e4e1a12620e7fc8ce1c7d3651acefde899c33f12b6958d3304106a0')
enc = zbase32.encode(b)
assert(enc == zb32)