UNPKG

lightningdevkit

Version:
142 lines 6.7 kB
import { Result_NoneNoneZ } from '../structs/Result_NoneNoneZ.mjs'; import { Init } from '../structs/Init.mjs'; import { OnionMessage } from '../structs/OnionMessage.mjs'; import { InitFeatures } from '../structs/InitFeatures.mjs'; import { NodeFeatures } from '../structs/NodeFeatures.mjs'; import { CommonBase } from './CommonBase.mjs'; import * as bindings from '../bindings.mjs'; class LDKOnionMessageHandlerHolder { constructor() { this.held = null; } } /** * A handler for received [`OnionMessage`]s and for providing generated ones to send. */ export class OnionMessageHandler extends CommonBase { /* @internal */ constructor(_dummy, ptr) { super(ptr, bindings.OnionMessageHandler_free); this.bindings_instance = null; } /** Creates a new instance of OnionMessageHandler from a given implementation */ static new_impl(arg) { const impl_holder = new LDKOnionMessageHandlerHolder(); let structImplementation = { handle_onion_message(peer_node_id, msg) { const peer_node_id_conv = bindings.decodeUint8Array(peer_node_id); const msg_hu_conv = new OnionMessage(null, msg); arg.handle_onion_message(peer_node_id_conv, msg_hu_conv); }, next_onion_message_for_peer(peer_node_id) { const peer_node_id_conv = bindings.decodeUint8Array(peer_node_id); const ret = arg.next_onion_message_for_peer(peer_node_id_conv); const result = ret == null ? 0n : ret.clone_ptr(); return result; }, peer_connected(their_node_id, init, inbound) { const their_node_id_conv = bindings.decodeUint8Array(their_node_id); const init_hu_conv = new Init(null, init); const ret = arg.peer_connected(their_node_id_conv, init_hu_conv, inbound); const result = ret.clone_ptr(); return result; }, peer_disconnected(their_node_id) { const their_node_id_conv = bindings.decodeUint8Array(their_node_id); arg.peer_disconnected(their_node_id_conv); }, timer_tick_occurred() { arg.timer_tick_occurred(); }, provided_node_features() { const ret = arg.provided_node_features(); const result = ret.clone_ptr(); return result; }, provided_init_features(their_node_id) { const their_node_id_conv = bindings.decodeUint8Array(their_node_id); const ret = arg.provided_init_features(their_node_id_conv); const result = ret.clone_ptr(); return result; }, }; const ptr_idx = bindings.LDKOnionMessageHandler_new(structImplementation); impl_holder.held = new OnionMessageHandler(null, ptr_idx[0]); impl_holder.held.instance_idx = ptr_idx[1]; impl_holder.held.bindings_instance = structImplementation; return impl_holder.held; } /** * Handle an incoming `onion_message` message from the given peer. */ handle_onion_message(peer_node_id, msg) { bindings.OnionMessageHandler_handle_onion_message(this.ptr, bindings.encodeUint8Array(peer_node_id), CommonBase.get_ptr_of(msg)); CommonBase.add_ref_from(this, msg); } /** * Returns the next pending onion message for the peer with the given node id. * * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None */ next_onion_message_for_peer(peer_node_id) { const ret = bindings.OnionMessageHandler_next_onion_message_for_peer(this.ptr, bindings.encodeUint8Array(peer_node_id)); const ret_hu_conv = new OnionMessage(null, ret); CommonBase.add_ref_from(ret_hu_conv, this); return ret_hu_conv; } /** * Called when a connection is established with a peer. Can be used to track which peers * advertise onion message support and are online. * * May return an `Err(())` if the features the peer supports are not sufficient to communicate * with us. Implementors should be somewhat conservative about doing so, however, as other * message handlers may still wish to communicate with this peer. * * [`Self::peer_disconnected`] will not be called if `Err(())` is returned. */ peer_connected(their_node_id, init, inbound) { const ret = bindings.OnionMessageHandler_peer_connected(this.ptr, bindings.encodeUint8Array(their_node_id), CommonBase.get_ptr_of(init), inbound); const ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret); CommonBase.add_ref_from(this, init); return ret_hu_conv; } /** * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to * drop and refuse to forward onion messages to this peer. */ peer_disconnected(their_node_id) { bindings.OnionMessageHandler_peer_disconnected(this.ptr, bindings.encodeUint8Array(their_node_id)); } /** * Performs actions that should happen roughly every ten seconds after startup. Allows handlers * to drop any buffered onion messages intended for prospective peers. */ timer_tick_occurred() { bindings.OnionMessageHandler_timer_tick_occurred(this.ptr); } /** * Gets the node feature flags which this handler itself supports. All available handlers are * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`] * which are broadcasted in our [`NodeAnnouncement`] message. */ provided_node_features() { const ret = bindings.OnionMessageHandler_provided_node_features(this.ptr); const ret_hu_conv = new NodeFeatures(null, ret); CommonBase.add_ref_from(ret_hu_conv, this); return ret_hu_conv; } /** * Gets the init feature flags which should be sent to the given peer. All available handlers * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`] * which are sent in our [`Init`] message. * * Note that this method is called before [`Self::peer_connected`]. */ provided_init_features(their_node_id) { const ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, bindings.encodeUint8Array(their_node_id)); const ret_hu_conv = new InitFeatures(null, ret); CommonBase.add_ref_from(ret_hu_conv, this); return ret_hu_conv; } } //# sourceMappingURL=OnionMessageHandler.mjs.map