[TS] Update tests and node-net for new LDK 0.0.117 API
[ldk-java] / node-net / test / test.mts
1 import * as ldk from "../../ts/index.mjs";
2 import * as node_net from '../net.mjs';
3
4 import * as fs from 'fs';
5
6 const wasm_file = fs.readFileSync('../ts/liblightningjs.wasm');
7 await ldk.initializeWasmFromBinary(wasm_file);
8
9 const logger_a = ldk.Logger.new_impl({
10         log(record: ldk.Record): void {
11                 console.log(record.get_module_path() + ": " + record.get_args());
12         }
13 } as ldk.LoggerInterface);
14 const logger_b = logger_a;
15
16 const node_a_seed = new Uint8Array(32);
17 for (var i = 0; i < 32; i++) node_a_seed[i] = 42;
18 const keys_manager_a = ldk.KeysManager.constructor_new(node_a_seed, 0xdeadbeefn, 0xdeadbeef);
19 // The public key for a secret key of all 42s:
20 const node_a_pk = new Uint8Array([2, 199, 152,  57,  62, 230,  84, 174, 99,
21                                      219,  13,   9, 134, 214, 253,  64, 62,
22                                      214, 150, 255, 176, 173,  44, 221, 102,
23                                      195, 152, 169, 215, 195,  79, 251, 240]);
24
25 const node_b_seed = new Uint8Array(32);
26 for (var i = 0; i < 32; i++) node_b_seed[i] = 43;
27 const keys_manager_b = ldk.KeysManager.constructor_new(node_b_seed, 0xdeadbeefn, 0xdeadbeef);
28
29 const rng_seed = new Uint8Array(32);
30 const routing_handler = ldk.IgnoringMessageHandler.constructor_new().as_RoutingMessageHandler();
31 const chan_handler = ldk.ErroringMessageHandler.constructor_new().as_ChannelMessageHandler();
32 const cust_handler = ldk.IgnoringMessageHandler.constructor_new().as_CustomMessageHandler();
33 const onion_handler = ldk.IgnoringMessageHandler.constructor_new().as_OnionMessageHandler();
34
35 const a_pm = ldk.PeerManager.constructor_new(chan_handler, routing_handler, onion_handler, cust_handler, 0xdeadbeef, rng_seed, logger_a, keys_manager_a.as_NodeSigner());
36 const a_net_handler = new node_net.NodeLDKNet(a_pm);
37 var port = 10000;
38 for (; port < 11000; port++) {
39         try {
40                 // Try ports until we find one we can bind to.
41                 await a_net_handler.bind_listener("127.0.0.1", port);
42                 break;
43         } catch(_) {}
44 }
45
46 const b_pm = ldk.PeerManager.constructor_new(chan_handler, routing_handler, onion_handler, cust_handler, 0xdeadbeef, rng_seed, logger_b, keys_manager_b.as_NodeSigner());
47 const b_net_handler = new node_net.NodeLDKNet(b_pm);
48 await b_net_handler.connect_peer("127.0.0.1", port, node_a_pk);
49
50 try {
51         // Ensure we get an error if we try to bind the same port twice.
52         await a_net_handler.bind_listener("127.0.0.1", port);
53         console.assert(false);
54 } catch(_) {}
55
56 await new Promise<void>(resolve => {
57         // Wait until the peers are connected and have exchanged the initial handshake
58         var timer: ReturnType<typeof setInterval>;
59         timer = setInterval(function() {
60                 if (a_pm.get_peer_node_ids().length == 1 && b_pm.get_peer_node_ids().length == 1) {
61                         resolve();
62                         clearInterval(timer);
63                 }
64         }, 500);
65 });
66
67 b_pm.disconnect_by_node_id(node_a_pk);
68 await new Promise<void>(resolve => {
69         // Wait until A learns the connection is closed from the socket closure
70         var timer: ReturnType<typeof setInterval>;
71         timer = setInterval(function() {
72                 if (a_pm.get_peer_node_ids().length == 0 && b_pm.get_peer_node_ids().length == 0) {
73                         resolve();
74                         clearInterval(timer);
75                 }
76         }, 500);
77 });
78
79 a_net_handler.stop();
80 b_net_handler.stop();
81
82 function arr_eq(a: number[]|Uint8Array, b: number[]|Uint8Array): boolean {
83         return a.length == b.length && a.every((val, idx) => val == b[idx]);
84 }
85
86 const v4_parse = node_net.NodeLDKNet["v4_addr_from_ip"];
87 console.assert((v4_parse("127.0.0.1", 4242) as ldk.SocketAddress_TcpIpV4).port == 4242);
88 console.assert(arr_eq((v4_parse("127.0.0.1", 4242) as ldk.SocketAddress_TcpIpV4).addr, [127,0,0,1]));
89 console.assert(arr_eq((v4_parse("0.0.0.0", 4242) as ldk.SocketAddress_TcpIpV4).addr, [0,0,0,0]));
90
91 const v6_parse = node_net.NodeLDKNet["v6_addr_from_ip"];
92 console.assert((v6_parse("::", 4242) as ldk.SocketAddress_TcpIpV4).port == 4242);
93 console.assert(arr_eq((v6_parse("::", 4242) as ldk.SocketAddress_TcpIpV6).addr,
94         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]));
95 console.assert(arr_eq((v6_parse("fe80::", 4242) as ldk.SocketAddress_TcpIpV6).addr,
96         [0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0]));
97 console.assert(arr_eq((v6_parse("fe80::42", 4242) as ldk.SocketAddress_TcpIpV6).addr,
98         [0xfe,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0x42]));
99 console.assert(arr_eq((v6_parse("fe80:A:b::", 4242) as ldk.SocketAddress_TcpIpV6).addr,
100         [0xfe,0x80,0,0xa,0,0xb,0,0,0,0,0,0,0,0,0,0]));
101 console.assert(arr_eq((v6_parse("2001:1:bad::beef:cafe", 4242) as ldk.SocketAddress_TcpIpV6).addr,
102         [0x20, 0x01, 0, 1, 0xb, 0xad, 0, 0, 0, 0, 0, 0, 0xbe, 0xef, 0xca, 0xfe]));