@@ -56,8 +56,13 @@ tests.push(async () => {
5656
5757var seed_counter = 0 ;
5858class Node {
59- constructor ( public chan_man : ldk . ChannelManager , public tx_broadcasted : Promise < Uint8Array > , public logger : ldk . Logger ,
60- public node_id : Uint8Array , public node_secret : Uint8Array ) { }
59+ node_id : Uint8Array ;
60+ node_secret : Uint8Array ;
61+ constructor ( public chan_man : ldk . ChannelManager , public tx_broadcasted : Promise < Uint8Array > ,
62+ public logger : ldk . Logger , public keys_interface : ldk . KeysInterface ) {
63+ this . node_id = chan_man . get_our_node_id ( ) ;
64+ this . node_secret = ( keys_interface . get_node_secret ( ldk . Recipient . LDKRecipient_Node ) as ldk . Result_SecretKeyNoneZ_OK ) . res ;
65+ }
6166}
6267function get_chanman ( ) : Node {
6368 const fee_est = ldk . FeeEstimator . new_impl ( {
@@ -98,10 +103,7 @@ function get_chanman(): Node {
98103 const params = ldk . ChainParameters . constructor_new ( ldk . Network . LDKNetwork_Testnet , ldk . BestBlock . constructor_from_genesis ( ldk . Network . LDKNetwork_Testnet ) ) ;
99104
100105 const chan_man = ldk . ChannelManager . constructor_new ( fee_est , chain_watch , tx_broadcaster , logger , keys_interface , config , params ) ;
101- return new Node (
102- chan_man , tx_broadcasted , logger , chan_man . get_our_node_id ( ) ,
103- ( keys_interface . get_node_secret ( ldk . Recipient . LDKRecipient_Node ) as ldk . Result_SecretKeyNoneZ_OK ) . res
104- ) ;
106+ return new Node ( chan_man , tx_broadcasted , logger , keys_interface ) ;
105107}
106108
107109function exchange_messages ( a : ldk . ChannelManager , b : ldk . ChannelManager ) {
@@ -275,6 +277,101 @@ tests.push(async () => {
275277 return true ;
276278} ) ;
277279
280+ tests . push ( async ( ) => {
281+ // Test passing onion messages through a custom trait implementation.
282+ const a = get_chanman ( ) ;
283+ const b = get_chanman ( ) ;
284+
285+ const ignorer = ldk . IgnoringMessageHandler . constructor_new ( ) ;
286+
287+ const underlying_om_a = ldk . OnionMessenger . constructor_new ( a . keys_interface , a . logger ) ;
288+ const om_provider_a = {
289+ next_onion_message_for_peer ( peer_node_id : Uint8Array ) : ldk . OnionMessage {
290+ return underlying_om_a . as_OnionMessageProvider ( ) . next_onion_message_for_peer ( peer_node_id ) ;
291+ }
292+ } as ldk . OnionMessageProviderInterface ;
293+ const om_a = ldk . OnionMessageHandler . new_impl ( {
294+ handle_onion_message ( peer_node_id : Uint8Array , msg : ldk . OnionMessage ) {
295+ underlying_om_a . as_OnionMessageHandler ( ) . handle_onion_message ( peer_node_id , msg ) ;
296+ } ,
297+ peer_connected ( their_node_id : Uint8Array , init : ldk . Init ) {
298+ underlying_om_a . as_OnionMessageHandler ( ) . peer_connected ( their_node_id , init ) ;
299+ } ,
300+ peer_disconnected ( their_node_id : Uint8Array , no_connection_possible : boolean ) {
301+ underlying_om_a . as_OnionMessageHandler ( ) . peer_disconnected ( their_node_id , no_connection_possible ) ;
302+ } ,
303+ provided_node_features ( ) : ldk . NodeFeatures {
304+ return underlying_om_a . as_OnionMessageHandler ( ) . provided_node_features ( ) ;
305+ } ,
306+ provided_init_features ( their_node_id : Uint8Array ) : ldk . InitFeatures {
307+ return underlying_om_a . as_OnionMessageHandler ( ) . provided_init_features ( their_node_id ) ;
308+ }
309+ } as ldk . OnionMessageHandlerInterface , om_provider_a ) ;
310+
311+ const om_b = ldk . OnionMessenger . constructor_new ( b . keys_interface , b . logger ) ;
312+
313+ const pm_a = ldk . PeerManager . constructor_new ( a . chan_man . as_ChannelMessageHandler ( ) , ignorer . as_RoutingMessageHandler ( ) , om_a , a . node_secret , 0xdeadbeefn , a . node_secret , a . logger , ignorer . as_CustomMessageHandler ( ) ) ;
314+ const pm_b = ldk . PeerManager . constructor_new ( b . chan_man . as_ChannelMessageHandler ( ) , ignorer . as_RoutingMessageHandler ( ) , om_b . as_OnionMessageHandler ( ) , b . node_secret , 0xdeadbeefn , b . node_secret , b . logger , ignorer . as_CustomMessageHandler ( ) ) ;
315+
316+ var sock_b : ldk . SocketDescriptor ;
317+ const sock_a = ldk . SocketDescriptor . new_impl ( {
318+ send_data ( data : Uint8Array , resume_read : boolean ) : number {
319+ console . assert ( pm_b . read_event ( sock_b , data ) instanceof ldk . Result_boolPeerHandleErrorZ_OK ) ;
320+ return data . length ;
321+ } ,
322+ disconnect_socket ( ) : void {
323+ console . assert ( false ) ;
324+ } ,
325+ eq ( other : ldk . SocketDescriptor ) : boolean {
326+ return other . hash ( ) == this . hash ( ) ;
327+ } ,
328+ hash ( ) : bigint {
329+ return BigInt ( 1 ) ;
330+ }
331+ } as ldk . SocketDescriptorInterface ) ;
332+ sock_b = ldk . SocketDescriptor . new_impl ( {
333+ send_data ( data : Uint8Array , resume_read : boolean ) : number {
334+ console . assert ( pm_a . read_event ( sock_a , data ) instanceof ldk . Result_boolPeerHandleErrorZ_OK ) ;
335+ return data . length ;
336+ } ,
337+ disconnect_socket ( ) : void {
338+ console . assert ( false ) ;
339+ } ,
340+ eq ( other : ldk . SocketDescriptor ) : boolean {
341+ return other . hash ( ) == this . hash ( ) ;
342+ } ,
343+ hash ( ) : bigint {
344+ return BigInt ( 2 ) ;
345+ }
346+ } as ldk . SocketDescriptorInterface ) ;
347+
348+ const v4_netaddr = ldk . NetAddress . constructor_ipv4 ( Uint8Array . from ( [ 42 , 0 , 42 , 1 ] ) , 9735 ) ;
349+ console . assert ( pm_b . new_inbound_connection ( sock_b , ldk . Option_NetAddressZ . constructor_some ( v4_netaddr ) ) instanceof ldk . Result_NonePeerHandleErrorZ_OK ) ;
350+ const init_bytes = pm_a . new_outbound_connection ( b . node_id , sock_a , ldk . Option_NetAddressZ . constructor_none ( ) ) ;
351+ if ( ! ( init_bytes instanceof ldk . Result_CVec_u8ZPeerHandleErrorZ_OK ) ) return false ;
352+ console . assert ( pm_b . read_event ( sock_b , init_bytes . res ) instanceof ldk . Result_boolPeerHandleErrorZ_OK ) ;
353+
354+ console . assert ( pm_a . get_peer_node_ids ( ) . length == 0 ) ;
355+ console . assert ( pm_b . get_peer_node_ids ( ) . length == 0 ) ;
356+
357+ pm_b . process_events ( ) ;
358+ pm_a . process_events ( ) ;
359+ pm_b . process_events ( ) ;
360+
361+ console . assert ( pm_a . get_peer_node_ids ( ) . length == 1 ) ;
362+ console . assert ( pm_b . get_peer_node_ids ( ) . length == 1 ) ;
363+
364+ underlying_om_a . send_onion_message ( [ ] , ldk . Destination . constructor_node ( b . node_id ) , null ) ;
365+ pm_a . process_events ( ) ;
366+ om_b . send_onion_message ( [ ] , ldk . Destination . constructor_node ( a . node_id ) , null ) ;
367+ pm_b . process_events ( ) ;
368+
369+ // TODO: Once OnionMessenger supports actually passing messages up, check that we received the
370+ // messages here.
371+
372+ return true ;
373+ } ) ;
374+
278375tests . push ( async ( ) => {
279376 // Test that we can do basic locking of a NetworkGraph
280377 const genesis_hash = new Uint8Array ( [ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x19 , 0xd6 , 0x68 , 0x9c , 0x08 , 0x5a , 0xe1 , 0x65 , 0x83 , 0x1e , 0x93 , 0x4f , 0xf7 , 0x63 , 0xae , 0x46 , 0xa2 , 0xa6 , 0xc1 , 0x72 , 0xb3 , 0xf1 , 0xb6 , 0x0a , 0x8c , 0xe2 , 0x6f ] ) ;
0 commit comments