|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from io import BytesIO |
| 4 | + |
| 5 | +from commander import Commander |
| 6 | +from pyln.proto.message import Message, MessageNamespace |
| 7 | +from pyln.proto.wire import PrivateKey, PublicKey, connect |
| 8 | + |
| 9 | + |
| 10 | +class PyLNConnect(Commander): |
| 11 | + def set_test_params(self): |
| 12 | + self.num_nodes = 0 |
| 13 | + |
| 14 | + def run_test(self): |
| 15 | + ln = self.lns["tank-0000-ln"] |
| 16 | + ln.createrune() |
| 17 | + uri = ln.uri() |
| 18 | + pk, host = uri.split("@") |
| 19 | + host, port = host.split(":") |
| 20 | + |
| 21 | + ls_privkey = PrivateKey( |
| 22 | + bytes.fromhex("1111111111111111111111111111111111111111111111111111111111111111") |
| 23 | + ) |
| 24 | + remote_pubkey = PublicKey(bytes.fromhex(pk)) |
| 25 | + |
| 26 | + lc = connect(ls_privkey, remote_pubkey, host, port) |
| 27 | + |
| 28 | + # Send an init message, with no global features, and 0b10101010 as local |
| 29 | + # features. |
| 30 | + # From BOLT1: |
| 31 | + # type: 16 (init) |
| 32 | + # data: |
| 33 | + # [u16:gflen] |
| 34 | + # [gflen*byte:globalfeatures] |
| 35 | + # [u16:flen] |
| 36 | + # [flen*byte:features] |
| 37 | + # [init_tlvs:tlvs] |
| 38 | + lc.send_message(b"\x00\x10\x00\x00\x00\x01\xaa") |
| 39 | + |
| 40 | + # Import the BOLT#1 init message namesapce |
| 41 | + ns = MessageNamespace( |
| 42 | + [ |
| 43 | + "msgtype,init,16", |
| 44 | + "msgdata,init,gflen,u16,", |
| 45 | + "msgdata,init,globalfeatures,byte,gflen", |
| 46 | + "msgdata,init,flen,u16,", |
| 47 | + "msgdata,init,features,byte,flen", |
| 48 | + ] |
| 49 | + ) |
| 50 | + # read reply from peer |
| 51 | + msg = lc.read_message() |
| 52 | + self.log.info(f"Got message bytes: {msg.hex()}") |
| 53 | + # interpret reply from peer |
| 54 | + stream = BytesIO(msg) |
| 55 | + msg = Message.read(ns, stream) |
| 56 | + self.log.info(f"Decoded message type: {msg.messagetype} content: {msg.to_py()}") |
| 57 | + |
| 58 | + |
| 59 | +def main(): |
| 60 | + PyLNConnect().main() |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + main() |
0 commit comments