lightningdevkit
Version:
Lightning Development Kit
108 lines • 5.74 kB
JavaScript
import { Option_CVec_u8ZZ } from '../structs/Option_CVec_u8ZZ.mjs';
import { OnionMessageContents } from '../structs/OnionMessageContents.mjs';
import { Option_C2Tuple_OnionMessageContentsResponseInstructionZZ } from '../structs/Option_C2Tuple_OnionMessageContentsResponseInstructionZZ.mjs';
import { Result_COption_OnionMessageContentsZDecodeErrorZ } from '../structs/Result_COption_OnionMessageContentsZDecodeErrorZ.mjs';
import { TwoTuple_OnionMessageContentsMessageSendInstructionsZ } from '../structs/TwoTuple_OnionMessageContentsMessageSendInstructionsZ.mjs';
import { Responder } from '../structs/Responder.mjs';
import { CommonBase } from './CommonBase.mjs';
import * as bindings from '../bindings.mjs';
class LDKCustomOnionMessageHandlerHolder {
constructor() {
this.held = null;
}
}
/**
* Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`],
* [`SimpleRefOnionMessenger`], or prefer to ignore inbound custom onion messages,
* [`IgnoringMessageHandler`] must be provided to [`OnionMessenger::new`]. Otherwise, a custom
* implementation of this trait must be provided, with [`CustomMessage`] specifying the supported
* message types.
*
* See [`OnionMessenger`] for example usage.
*
* [`IgnoringMessageHandler`]: crate::ln::peer_handler::IgnoringMessageHandler
* [`CustomMessage`]: Self::CustomMessage
*/
export class CustomOnionMessageHandler extends CommonBase {
/* @internal */
constructor(_dummy, ptr) {
super(ptr, bindings.CustomOnionMessageHandler_free);
this.bindings_instance = null;
}
/** Creates a new instance of CustomOnionMessageHandler from a given implementation */
static new_impl(arg) {
const impl_holder = new LDKCustomOnionMessageHandlerHolder();
let structImplementation = {
handle_custom_message(message, context, responder) {
const ret_hu_conv = new OnionMessageContents(null, message);
CommonBase.add_ref_from(ret_hu_conv, this);
const context_hu_conv = Option_CVec_u8ZZ.constr_from_ptr(context);
CommonBase.add_ref_from(context_hu_conv, this);
const responder_hu_conv = new Responder(null, responder);
CommonBase.add_ref_from(responder_hu_conv, this);
const ret = arg.handle_custom_message(ret_hu_conv, context_hu_conv, responder_hu_conv);
const result = ret.clone_ptr();
return result;
},
read_custom_message(message_type, buffer) {
const buffer_conv = bindings.decodeUint8Array(buffer);
const ret = arg.read_custom_message(message_type, buffer_conv);
const result = ret.clone_ptr();
return result;
},
release_pending_custom_messages() {
const ret = arg.release_pending_custom_messages();
const result = bindings.encodeUint64Array(ret.map(ret_conv_55 => ret_conv_55.clone_ptr()));
return result;
},
};
const ptr_idx = bindings.LDKCustomOnionMessageHandler_new(structImplementation);
impl_holder.held = new CustomOnionMessageHandler(null, ptr_idx[0]);
impl_holder.held.instance_idx = ptr_idx[1];
impl_holder.held.bindings_instance = structImplementation;
return impl_holder.held;
}
/**
* Called with the custom message that was received, returning a response to send, if any.
*
* The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
*
* Note that responder (or a relevant inner pointer) may be NULL or all-0s to represent None
*/
handle_custom_message(message, context, responder) {
const ret = bindings.CustomOnionMessageHandler_handle_custom_message(this.ptr, CommonBase.get_ptr_of(message), CommonBase.get_ptr_of(context), responder == null ? 0n : CommonBase.get_ptr_of(responder));
const ret_hu_conv = Option_C2Tuple_OnionMessageContentsResponseInstructionZZ.constr_from_ptr(ret);
CommonBase.add_ref_from(ret_hu_conv, this);
CommonBase.add_ref_from(this, message);
return ret_hu_conv;
}
/**
* Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
* message type is unknown.
*/
read_custom_message(message_type, buffer) {
const ret = bindings.CustomOnionMessageHandler_read_custom_message(this.ptr, message_type, bindings.encodeUint8Array(buffer));
const ret_hu_conv = Result_COption_OnionMessageContentsZDecodeErrorZ.constr_from_ptr(ret);
return ret_hu_conv;
}
/**
* Releases any [`Self::CustomMessage`]s that need to be sent.
*
* Typically, this is used for messages initiating a message flow rather than in response to
* another message. The latter should use the return value of [`Self::handle_custom_message`].
*/
release_pending_custom_messages() {
const ret = bindings.CustomOnionMessageHandler_release_pending_custom_messages(this.ptr);
const ret_conv_55_len = bindings.getArrayLength(ret);
const ret_conv_55_arr = new Array(ret_conv_55_len).fill(null);
for (var d = 0; d < ret_conv_55_len; d++) {
const ret_conv_55 = bindings.getU64ArrayElem(ret, d);
const ret_conv_55_hu_conv = new TwoTuple_OnionMessageContentsMessageSendInstructionsZ(null, ret_conv_55);
CommonBase.add_ref_from(ret_conv_55_hu_conv, this);
ret_conv_55_arr[d] = ret_conv_55_hu_conv;
}
bindings.freeWasmMemory(ret);
return ret_conv_55_arr;
}
}
//# sourceMappingURL=CustomOnionMessageHandler.mjs.map