6 namespace org { namespace ldk { namespace structs {
10 * A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
11 * used to retrieve invoices and fulfill invoice requests from [offers]. Currently, only sending
12 * and receiving custom onion messages is supported.
17 * # extern crate bitcoin;
18 * # use bitcoin::hashes::_export::_core::time::Duration;
19 * # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
20 * # use lightning::blinded_path::BlindedPath;
21 * # use lightning::sign::KeysManager;
22 * # use lightning::ln::peer_handler::IgnoringMessageHandler;
23 * # use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessenger, OnionMessagePath};
24 * # use lightning::onion_message::packet::{CustomOnionMessageContents, OnionMessageContents};
25 * # use lightning::util::logger::{Logger, Record};
26 * # use lightning::util::ser::{Writeable, Writer};
27 * # use lightning::io;
28 * # use std::sync::Arc;
29 * # struct FakeLogger;
30 * # impl Logger for FakeLogger {
31 * # fn log(&self, record: &Record) { unimplemented!() }
33 * # struct FakeMessageRouter {}
34 * # impl MessageRouter for FakeMessageRouter {
35 * # fn find_path(&self, sender: PublicKey, peers: Vec<PublicKey>, destination: Destination) -> Result<OnionMessagePath, ()> {
39 * # let seed = [42u8; 32];
40 * # let time = Duration::from_secs(123456);
41 * # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
42 * # let logger = Arc::new(FakeLogger {});
43 * # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
44 * # let secp_ctx = Secp256k1::new();
45 * # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
46 * # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
47 * # let destination_node_id = hop_node_id1;
48 * # let message_router = Arc::new(FakeMessageRouter {});
49 * # let custom_message_handler = IgnoringMessageHandler {};
50 * # let offers_message_handler = IgnoringMessageHandler {};
51 * Create the onion messenger. This must use the same `keys_manager` as is passed to your
53 * let onion_messenger = OnionMessenger::new(
54 * &keys_manager, &keys_manager, logger, message_router, &offers_message_handler,
55 * &custom_message_handler
59 * # struct YourCustomMessage {}
60 * impl Writeable for YourCustomMessage {
61 * \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
63 * \t\t// Write your custom onion message to `w`
66 * impl CustomOnionMessageContents for YourCustomMessage {
67 * \tfn tlv_type(&self) -> u64 {
68 * \t\t# let your_custom_message_type = 42;
69 * \t\tyour_custom_message_type
72 * Send a custom onion message to a node id.
73 * let path = OnionMessagePath {
74 * \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
75 * \tdestination: Destination::Node(destination_node_id),
77 * let reply_path = None;
78 * # let your_custom_message = YourCustomMessage {};
79 * let message = OnionMessageContents::Custom(your_custom_message);
80 * onion_messenger.send_onion_message(path, message, reply_path);
82 * Create a blinded path to yourself, for someone to send an onion message to.
83 * # let your_node_id = hop_node_id1;
84 * let hops = [hop_node_id3, hop_node_id4, your_node_id];
85 * let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
87 * Send a custom onion message to a blinded path.
88 * let path = OnionMessagePath {
89 * \tintermediate_nodes: vec![hop_node_id1, hop_node_id2],
90 * \tdestination: Destination::BlindedPath(blinded_path),
92 * let reply_path = None;
93 * # let your_custom_message = YourCustomMessage {};
94 * let message = OnionMessageContents::Custom(your_custom_message);
95 * onion_messenger.send_onion_message(path, message, reply_path);
98 * [offers]: <https://github.com/lightning/bolts/pull/798>
99 * [`OnionMessenger`]: crate::onion_message::OnionMessenger
101 public class OnionMessenger : CommonBase {
102 internal OnionMessenger(object _dummy, long ptr) : base(ptr) { }
104 if (ptr != 0) { bindings.OnionMessenger_free(ptr); }
108 * Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
109 * their respective handlers.
111 public static OnionMessenger of(org.ldk.structs.EntropySource entropy_source, org.ldk.structs.NodeSigner node_signer, org.ldk.structs.Logger logger, org.ldk.structs.MessageRouter message_router, org.ldk.structs.OffersMessageHandler offers_handler, org.ldk.structs.CustomOnionMessageHandler custom_handler) {
112 long ret = bindings.OnionMessenger_new(entropy_source.ptr, node_signer.ptr, logger.ptr, message_router.ptr, offers_handler.ptr, custom_handler.ptr);
113 GC.KeepAlive(entropy_source);
114 GC.KeepAlive(node_signer);
115 GC.KeepAlive(logger);
116 GC.KeepAlive(message_router);
117 GC.KeepAlive(offers_handler);
118 GC.KeepAlive(custom_handler);
119 if (ret >= 0 && ret <= 4096) { return null; }
120 org.ldk.structs.OnionMessenger ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessenger(null, ret); }
121 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
122 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(entropy_source); };
123 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(node_signer); };
124 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
125 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(message_router); };
126 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(offers_handler); };
127 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(custom_handler); };
132 * Send an onion message with contents `message` to the destination of `path`.
134 * See [`OnionMessenger`] for example usage.
136 * Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
138 public Result_NoneSendErrorZ send_onion_message(org.ldk.structs.OnionMessagePath path, org.ldk.structs.OnionMessageContents message, org.ldk.structs.BlindedPath reply_path) {
139 long ret = bindings.OnionMessenger_send_onion_message(this.ptr, path == null ? 0 : path.ptr, message.ptr, reply_path == null ? 0 : reply_path.ptr);
142 GC.KeepAlive(message);
143 GC.KeepAlive(reply_path);
144 if (ret >= 0 && ret <= 4096) { return null; }
145 Result_NoneSendErrorZ ret_hu_conv = Result_NoneSendErrorZ.constr_from_ptr(ret);
146 if (this != null) { this.ptrs_to.AddLast(path); };
147 if (this != null) { this.ptrs_to.AddLast(message); };
148 if (this != null) { this.ptrs_to.AddLast(reply_path); };
153 * Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
154 * This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
156 public OnionMessageHandler as_OnionMessageHandler() {
157 long ret = bindings.OnionMessenger_as_OnionMessageHandler(this.ptr);
159 if (ret >= 0 && ret <= 4096) { return null; }
160 OnionMessageHandler ret_hu_conv = new OnionMessageHandler(null, ret);
161 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
166 * Constructs a new OnionMessageProvider which calls the relevant methods on this_arg.
167 * This copies the `inner` pointer in this_arg and thus the returned OnionMessageProvider must be freed before this_arg is
169 public OnionMessageProvider as_OnionMessageProvider() {
170 long ret = bindings.OnionMessenger_as_OnionMessageProvider(this.ptr);
172 if (ret >= 0 && ret <= 4096) { return null; }
173 OnionMessageProvider ret_hu_conv = new OnionMessageProvider(null, ret);
174 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };