1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Creating blinded paths and related utilities live here.
11 use alloc::str::FromStr;
12 use alloc::string::String;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
23 use alloc::str::FromStr;
24 use alloc::string::String;
25 use core::ffi::c_void;
26 use core::convert::Infallible;
27 use bitcoin::hashes::Hash;
28 use crate::c_types::*;
29 #[cfg(feature="no-std")]
30 use alloc::{vec::Vec, boxed::Box};
35 use alloc::str::FromStr;
36 use alloc::string::String;
37 use core::ffi::c_void;
38 use core::convert::Infallible;
39 use bitcoin::hashes::Hash;
40 use crate::c_types::*;
41 #[cfg(feature="no-std")]
42 use alloc::{vec::Vec, boxed::Box};
45 /// The next hop to forward an onion message along its path.
47 /// Note that payment blinded paths always specify their next hop using an explicit node id.
51 pub enum NextMessageHop {
52 /// The node id of the next hop.
54 crate::c_types::PublicKey),
55 /// The short channel id leading to the next hop.
59 use lightning::blinded_path::NextMessageHop as NextMessageHopImport;
60 pub(crate) type nativeNextMessageHop = NextMessageHopImport;
64 pub(crate) fn to_native(&self) -> nativeNextMessageHop {
66 NextMessageHop::NodeId (ref a, ) => {
67 let mut a_nonref = Clone::clone(a);
68 nativeNextMessageHop::NodeId (
72 NextMessageHop::ShortChannelId (ref a, ) => {
73 let mut a_nonref = Clone::clone(a);
74 nativeNextMessageHop::ShortChannelId (
81 pub(crate) fn into_native(self) -> nativeNextMessageHop {
83 NextMessageHop::NodeId (mut a, ) => {
84 nativeNextMessageHop::NodeId (
88 NextMessageHop::ShortChannelId (mut a, ) => {
89 nativeNextMessageHop::ShortChannelId (
96 pub(crate) fn from_native(native: &NextMessageHopImport) -> Self {
97 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeNextMessageHop) };
99 nativeNextMessageHop::NodeId (ref a, ) => {
100 let mut a_nonref = Clone::clone(a);
101 NextMessageHop::NodeId (
102 crate::c_types::PublicKey::from_rust(&a_nonref),
105 nativeNextMessageHop::ShortChannelId (ref a, ) => {
106 let mut a_nonref = Clone::clone(a);
107 NextMessageHop::ShortChannelId (
114 pub(crate) fn native_into(native: nativeNextMessageHop) -> Self {
116 nativeNextMessageHop::NodeId (mut a, ) => {
117 NextMessageHop::NodeId (
118 crate::c_types::PublicKey::from_rust(&a),
121 nativeNextMessageHop::ShortChannelId (mut a, ) => {
122 NextMessageHop::ShortChannelId (
129 /// Frees any resources used by the NextMessageHop
131 pub extern "C" fn NextMessageHop_free(this_ptr: NextMessageHop) { }
132 /// Creates a copy of the NextMessageHop
134 pub extern "C" fn NextMessageHop_clone(orig: &NextMessageHop) -> NextMessageHop {
138 /// Used only if an object of this type is returned as a trait impl by a method
139 pub(crate) extern "C" fn NextMessageHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
140 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const NextMessageHop)).clone() })) as *mut c_void
143 /// Used only if an object of this type is returned as a trait impl by a method
144 pub(crate) extern "C" fn NextMessageHop_free_void(this_ptr: *mut c_void) {
145 let _ = unsafe { Box::from_raw(this_ptr as *mut NextMessageHop) };
148 /// Utility method to constructs a new NodeId-variant NextMessageHop
149 pub extern "C" fn NextMessageHop_node_id(a: crate::c_types::PublicKey) -> NextMessageHop {
150 NextMessageHop::NodeId(a, )
153 /// Utility method to constructs a new ShortChannelId-variant NextMessageHop
154 pub extern "C" fn NextMessageHop_short_channel_id(a: u64) -> NextMessageHop {
155 NextMessageHop::ShortChannelId(a, )
157 /// Get a string which allows debug introspection of a NextMessageHop object
158 pub extern "C" fn NextMessageHop_debug_str_void(o: *const c_void) -> Str {
159 alloc::format!("{:?}", unsafe { o as *const crate::lightning::blinded_path::NextMessageHop }).into()}
160 /// Generates a non-cryptographic 64-bit hash of the NextMessageHop.
162 pub extern "C" fn NextMessageHop_hash(o: &NextMessageHop) -> u64 {
163 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
165 let mut hasher = core::hash::SipHasher::new();
166 core::hash::Hash::hash(&o.to_native(), &mut hasher);
167 core::hash::Hasher::finish(&hasher)
169 /// Checks if two NextMessageHops contain equal inner contents.
170 /// This ignores pointers and is_owned flags and looks at the values in fields.
172 pub extern "C" fn NextMessageHop_eq(a: &NextMessageHop, b: &NextMessageHop) -> bool {
173 if &a.to_native() == &b.to_native() { true } else { false }
176 use lightning::blinded_path::BlindedPath as nativeBlindedPathImport;
177 pub(crate) type nativeBlindedPath = nativeBlindedPathImport;
179 /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
180 /// identity of the recipient.
183 pub struct BlindedPath {
184 /// A pointer to the opaque Rust object.
186 /// Nearly everywhere, inner must be non-null, however in places where
187 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
188 pub inner: *mut nativeBlindedPath,
189 /// Indicates that this is the only struct which contains the same pointer.
191 /// Rust functions which take ownership of an object provided via an argument require
192 /// this to be true and invalidate the object pointed to by inner.
196 impl Drop for BlindedPath {
198 if self.is_owned && !<*mut nativeBlindedPath>::is_null(self.inner) {
199 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
203 /// Frees any resources used by the BlindedPath, if is_owned is set and inner is non-NULL.
205 pub extern "C" fn BlindedPath_free(this_obj: BlindedPath) { }
207 /// Used only if an object of this type is returned as a trait impl by a method
208 pub(crate) extern "C" fn BlindedPath_free_void(this_ptr: *mut c_void) {
209 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedPath) };
213 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedPath {
214 unsafe { &*ObjOps::untweak_ptr(self.inner) }
216 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedPath {
217 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
219 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
220 pub(crate) fn take_inner(mut self) -> *mut nativeBlindedPath {
221 assert!(self.is_owned);
222 let ret = ObjOps::untweak_ptr(self.inner);
223 self.inner = core::ptr::null_mut();
227 /// To send to a blinded path, the sender first finds a route to the unblinded
228 /// `introduction_node`, which can unblind its [`encrypted_payload`] to find out the onion
229 /// message or payment's next hop and forward it along.
231 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
233 pub extern "C" fn BlindedPath_get_introduction_node(this_ptr: &BlindedPath) -> crate::lightning::blinded_path::IntroductionNode {
234 let mut inner_val = &mut this_ptr.get_native_mut_ref().introduction_node;
235 crate::lightning::blinded_path::IntroductionNode::from_native(inner_val)
237 /// To send to a blinded path, the sender first finds a route to the unblinded
238 /// `introduction_node`, which can unblind its [`encrypted_payload`] to find out the onion
239 /// message or payment's next hop and forward it along.
241 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
243 pub extern "C" fn BlindedPath_set_introduction_node(this_ptr: &mut BlindedPath, mut val: crate::lightning::blinded_path::IntroductionNode) {
244 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.introduction_node = val.into_native();
246 /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
247 /// message or payment.
249 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
251 pub extern "C" fn BlindedPath_get_blinding_point(this_ptr: &BlindedPath) -> crate::c_types::PublicKey {
252 let mut inner_val = &mut this_ptr.get_native_mut_ref().blinding_point;
253 crate::c_types::PublicKey::from_rust(&inner_val)
255 /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
256 /// message or payment.
258 /// [`encrypted_payload`]: BlindedHop::encrypted_payload
260 pub extern "C" fn BlindedPath_set_blinding_point(this_ptr: &mut BlindedPath, mut val: crate::c_types::PublicKey) {
261 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.blinding_point = val.into_rust();
263 /// The hops composing the blinded path.
265 pub extern "C" fn BlindedPath_get_blinded_hops(this_ptr: &BlindedPath) -> crate::c_types::derived::CVec_BlindedHopZ {
266 let mut inner_val = &mut this_ptr.get_native_mut_ref().blinded_hops;
267 let mut local_inner_val = Vec::new(); for item in inner_val.iter() { local_inner_val.push( { crate::lightning::blinded_path::BlindedHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner((item as *const lightning::blinded_path::BlindedHop<>) as *mut _) }, is_owned: false } }); };
268 local_inner_val.into()
270 /// The hops composing the blinded path.
272 pub extern "C" fn BlindedPath_set_blinded_hops(this_ptr: &mut BlindedPath, mut val: crate::c_types::derived::CVec_BlindedHopZ) {
273 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
274 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.blinded_hops = local_val;
276 /// Constructs a new BlindedPath given each field
279 pub extern "C" fn BlindedPath_new(mut introduction_node_arg: crate::lightning::blinded_path::IntroductionNode, mut blinding_point_arg: crate::c_types::PublicKey, mut blinded_hops_arg: crate::c_types::derived::CVec_BlindedHopZ) -> BlindedPath {
280 let mut local_blinded_hops_arg = Vec::new(); for mut item in blinded_hops_arg.into_rust().drain(..) { local_blinded_hops_arg.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
281 BlindedPath { inner: ObjOps::heap_alloc(nativeBlindedPath {
282 introduction_node: introduction_node_arg.into_native(),
283 blinding_point: blinding_point_arg.into_rust(),
284 blinded_hops: local_blinded_hops_arg,
287 impl Clone for BlindedPath {
288 fn clone(&self) -> Self {
290 inner: if <*mut nativeBlindedPath>::is_null(self.inner) { core::ptr::null_mut() } else {
291 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
297 /// Used only if an object of this type is returned as a trait impl by a method
298 pub(crate) extern "C" fn BlindedPath_clone_void(this_ptr: *const c_void) -> *mut c_void {
299 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeBlindedPath)).clone() })) as *mut c_void
302 /// Creates a copy of the BlindedPath
303 pub extern "C" fn BlindedPath_clone(orig: &BlindedPath) -> BlindedPath {
306 /// Get a string which allows debug introspection of a BlindedPath object
307 pub extern "C" fn BlindedPath_debug_str_void(o: *const c_void) -> Str {
308 alloc::format!("{:?}", unsafe { o as *const crate::lightning::blinded_path::BlindedPath }).into()}
309 /// Generates a non-cryptographic 64-bit hash of the BlindedPath.
311 pub extern "C" fn BlindedPath_hash(o: &BlindedPath) -> u64 {
312 if o.inner.is_null() { return 0; }
313 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
315 let mut hasher = core::hash::SipHasher::new();
316 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
317 core::hash::Hasher::finish(&hasher)
319 /// Checks if two BlindedPaths contain equal inner contents.
320 /// This ignores pointers and is_owned flags and looks at the values in fields.
321 /// Two objects with NULL inner values will be considered "equal" here.
323 pub extern "C" fn BlindedPath_eq(a: &BlindedPath, b: &BlindedPath) -> bool {
324 if a.inner == b.inner { return true; }
325 if a.inner.is_null() || b.inner.is_null() { return false; }
326 if a.get_native_ref() == b.get_native_ref() { true } else { false }
328 /// The unblinded node in a [`BlindedPath`].
332 pub enum IntroductionNode {
333 /// The node id of the introduction node.
335 crate::c_types::PublicKey),
336 /// The short channel id of the channel leading to the introduction node. The [`Direction`]
337 /// identifies which side of the channel is the introduction node.
338 DirectedShortChannelId(
339 crate::lightning::blinded_path::Direction,
342 use lightning::blinded_path::IntroductionNode as IntroductionNodeImport;
343 pub(crate) type nativeIntroductionNode = IntroductionNodeImport;
345 impl IntroductionNode {
347 pub(crate) fn to_native(&self) -> nativeIntroductionNode {
349 IntroductionNode::NodeId (ref a, ) => {
350 let mut a_nonref = Clone::clone(a);
351 nativeIntroductionNode::NodeId (
352 a_nonref.into_rust(),
355 IntroductionNode::DirectedShortChannelId (ref a, ref b, ) => {
356 let mut a_nonref = Clone::clone(a);
357 let mut b_nonref = Clone::clone(b);
358 nativeIntroductionNode::DirectedShortChannelId (
359 a_nonref.into_native(),
366 pub(crate) fn into_native(self) -> nativeIntroductionNode {
368 IntroductionNode::NodeId (mut a, ) => {
369 nativeIntroductionNode::NodeId (
373 IntroductionNode::DirectedShortChannelId (mut a, mut b, ) => {
374 nativeIntroductionNode::DirectedShortChannelId (
382 pub(crate) fn from_native(native: &IntroductionNodeImport) -> Self {
383 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeIntroductionNode) };
385 nativeIntroductionNode::NodeId (ref a, ) => {
386 let mut a_nonref = Clone::clone(a);
387 IntroductionNode::NodeId (
388 crate::c_types::PublicKey::from_rust(&a_nonref),
391 nativeIntroductionNode::DirectedShortChannelId (ref a, ref b, ) => {
392 let mut a_nonref = Clone::clone(a);
393 let mut b_nonref = Clone::clone(b);
394 IntroductionNode::DirectedShortChannelId (
395 crate::lightning::blinded_path::Direction::native_into(a_nonref),
402 pub(crate) fn native_into(native: nativeIntroductionNode) -> Self {
404 nativeIntroductionNode::NodeId (mut a, ) => {
405 IntroductionNode::NodeId (
406 crate::c_types::PublicKey::from_rust(&a),
409 nativeIntroductionNode::DirectedShortChannelId (mut a, mut b, ) => {
410 IntroductionNode::DirectedShortChannelId (
411 crate::lightning::blinded_path::Direction::native_into(a),
418 /// Frees any resources used by the IntroductionNode
420 pub extern "C" fn IntroductionNode_free(this_ptr: IntroductionNode) { }
421 /// Creates a copy of the IntroductionNode
423 pub extern "C" fn IntroductionNode_clone(orig: &IntroductionNode) -> IntroductionNode {
427 /// Used only if an object of this type is returned as a trait impl by a method
428 pub(crate) extern "C" fn IntroductionNode_clone_void(this_ptr: *const c_void) -> *mut c_void {
429 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const IntroductionNode)).clone() })) as *mut c_void
432 /// Used only if an object of this type is returned as a trait impl by a method
433 pub(crate) extern "C" fn IntroductionNode_free_void(this_ptr: *mut c_void) {
434 let _ = unsafe { Box::from_raw(this_ptr as *mut IntroductionNode) };
437 /// Utility method to constructs a new NodeId-variant IntroductionNode
438 pub extern "C" fn IntroductionNode_node_id(a: crate::c_types::PublicKey) -> IntroductionNode {
439 IntroductionNode::NodeId(a, )
442 /// Utility method to constructs a new DirectedShortChannelId-variant IntroductionNode
443 pub extern "C" fn IntroductionNode_directed_short_channel_id(a: crate::lightning::blinded_path::Direction,b: u64) -> IntroductionNode {
444 IntroductionNode::DirectedShortChannelId(a, b, )
446 /// Get a string which allows debug introspection of a IntroductionNode object
447 pub extern "C" fn IntroductionNode_debug_str_void(o: *const c_void) -> Str {
448 alloc::format!("{:?}", unsafe { o as *const crate::lightning::blinded_path::IntroductionNode }).into()}
449 /// Generates a non-cryptographic 64-bit hash of the IntroductionNode.
451 pub extern "C" fn IntroductionNode_hash(o: &IntroductionNode) -> u64 {
452 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
454 let mut hasher = core::hash::SipHasher::new();
455 core::hash::Hash::hash(&o.to_native(), &mut hasher);
456 core::hash::Hasher::finish(&hasher)
458 /// Checks if two IntroductionNodes contain equal inner contents.
459 /// This ignores pointers and is_owned flags and looks at the values in fields.
461 pub extern "C" fn IntroductionNode_eq(a: &IntroductionNode, b: &IntroductionNode) -> bool {
462 if &a.to_native() == &b.to_native() { true } else { false }
464 /// The side of a channel that is the [`IntroductionNode`] in a [`BlindedPath`]. [BOLT 7] defines
465 /// which nodes is which in the [`ChannelAnnouncement`] message.
467 /// [BOLT 7]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#the-channel_announcement-message
468 /// [`ChannelAnnouncement`]: crate::ln::msgs::ChannelAnnouncement
473 /// The lesser node id when compared lexicographically in ascending order.
475 /// The greater node id when compared lexicographically in ascending order.
478 use lightning::blinded_path::Direction as DirectionImport;
479 pub(crate) type nativeDirection = DirectionImport;
483 pub(crate) fn to_native(&self) -> nativeDirection {
485 Direction::NodeOne => nativeDirection::NodeOne,
486 Direction::NodeTwo => nativeDirection::NodeTwo,
490 pub(crate) fn into_native(self) -> nativeDirection {
492 Direction::NodeOne => nativeDirection::NodeOne,
493 Direction::NodeTwo => nativeDirection::NodeTwo,
497 pub(crate) fn from_native(native: &DirectionImport) -> Self {
498 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeDirection) };
500 nativeDirection::NodeOne => Direction::NodeOne,
501 nativeDirection::NodeTwo => Direction::NodeTwo,
505 pub(crate) fn native_into(native: nativeDirection) -> Self {
507 nativeDirection::NodeOne => Direction::NodeOne,
508 nativeDirection::NodeTwo => Direction::NodeTwo,
512 /// Creates a copy of the Direction
514 pub extern "C" fn Direction_clone(orig: &Direction) -> Direction {
518 /// Used only if an object of this type is returned as a trait impl by a method
519 pub(crate) extern "C" fn Direction_clone_void(this_ptr: *const c_void) -> *mut c_void {
520 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const Direction)).clone() })) as *mut c_void
523 /// Used only if an object of this type is returned as a trait impl by a method
524 pub(crate) extern "C" fn Direction_free_void(this_ptr: *mut c_void) {
525 let _ = unsafe { Box::from_raw(this_ptr as *mut Direction) };
528 /// Utility method to constructs a new NodeOne-variant Direction
529 pub extern "C" fn Direction_node_one() -> Direction {
532 /// Utility method to constructs a new NodeTwo-variant Direction
533 pub extern "C" fn Direction_node_two() -> Direction {
535 /// Get a string which allows debug introspection of a Direction object
536 pub extern "C" fn Direction_debug_str_void(o: *const c_void) -> Str {
537 alloc::format!("{:?}", unsafe { o as *const crate::lightning::blinded_path::Direction }).into()}
538 /// Generates a non-cryptographic 64-bit hash of the Direction.
540 pub extern "C" fn Direction_hash(o: &Direction) -> u64 {
541 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
543 let mut hasher = core::hash::SipHasher::new();
544 core::hash::Hash::hash(&o.to_native(), &mut hasher);
545 core::hash::Hasher::finish(&hasher)
547 /// Checks if two Directions contain equal inner contents.
548 /// This ignores pointers and is_owned flags and looks at the values in fields.
550 pub extern "C" fn Direction_eq(a: &Direction, b: &Direction) -> bool {
551 if &a.to_native() == &b.to_native() { true } else { false }
553 /// An interface for looking up the node id of a channel counterparty for the purpose of forwarding
554 /// an [`OnionMessage`].
556 /// [`OnionMessage`]: crate::ln::msgs::OnionMessage
558 pub struct NodeIdLookUp {
559 /// An opaque pointer which is passed to your function implementations as an argument.
560 /// This has no meaning in the LDK, and can be NULL or any other value.
561 pub this_arg: *mut c_void,
562 /// Returns the node id of the forwarding node's channel counterparty with `short_channel_id`.
564 /// Here, the forwarding node is referring to the node of the [`OnionMessenger`] parameterized
565 /// by the [`NodeIdLookUp`] and the counterparty to one of that node's peers.
567 /// [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
569 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
570 pub next_node_id: extern "C" fn (this_arg: *const c_void, short_channel_id: u64) -> crate::c_types::PublicKey,
571 /// Frees any resources associated with this object given its this_arg pointer.
572 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
573 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
575 unsafe impl Send for NodeIdLookUp {}
576 unsafe impl Sync for NodeIdLookUp {}
578 pub(crate) fn NodeIdLookUp_clone_fields(orig: &NodeIdLookUp) -> NodeIdLookUp {
580 this_arg: orig.this_arg,
581 next_node_id: Clone::clone(&orig.next_node_id),
582 free: Clone::clone(&orig.free),
586 use lightning::blinded_path::NodeIdLookUp as rustNodeIdLookUp;
587 impl rustNodeIdLookUp for NodeIdLookUp {
588 fn next_node_id(&self, mut short_channel_id: u64) -> Option<bitcoin::secp256k1::PublicKey> {
589 let mut ret = (self.next_node_id)(self.this_arg, short_channel_id);
590 let mut local_ret = if ret.is_null() { None } else { Some( { ret.into_rust() }) };
595 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
596 // directly as a Deref trait in higher-level structs:
597 impl core::ops::Deref for NodeIdLookUp {
599 fn deref(&self) -> &Self {
603 impl core::ops::DerefMut for NodeIdLookUp {
604 fn deref_mut(&mut self) -> &mut Self {
608 /// Calls the free function if one is set
610 pub extern "C" fn NodeIdLookUp_free(this_ptr: NodeIdLookUp) { }
611 impl Drop for NodeIdLookUp {
613 if let Some(f) = self.free {
619 use lightning::blinded_path::EmptyNodeIdLookUp as nativeEmptyNodeIdLookUpImport;
620 pub(crate) type nativeEmptyNodeIdLookUp = nativeEmptyNodeIdLookUpImport;
622 /// A [`NodeIdLookUp`] that always returns `None`.
625 pub struct EmptyNodeIdLookUp {
626 /// A pointer to the opaque Rust object.
628 /// Nearly everywhere, inner must be non-null, however in places where
629 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
630 pub inner: *mut nativeEmptyNodeIdLookUp,
631 /// Indicates that this is the only struct which contains the same pointer.
633 /// Rust functions which take ownership of an object provided via an argument require
634 /// this to be true and invalidate the object pointed to by inner.
638 impl Drop for EmptyNodeIdLookUp {
640 if self.is_owned && !<*mut nativeEmptyNodeIdLookUp>::is_null(self.inner) {
641 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
645 /// Frees any resources used by the EmptyNodeIdLookUp, if is_owned is set and inner is non-NULL.
647 pub extern "C" fn EmptyNodeIdLookUp_free(this_obj: EmptyNodeIdLookUp) { }
649 /// Used only if an object of this type is returned as a trait impl by a method
650 pub(crate) extern "C" fn EmptyNodeIdLookUp_free_void(this_ptr: *mut c_void) {
651 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeEmptyNodeIdLookUp) };
654 impl EmptyNodeIdLookUp {
655 pub(crate) fn get_native_ref(&self) -> &'static nativeEmptyNodeIdLookUp {
656 unsafe { &*ObjOps::untweak_ptr(self.inner) }
658 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeEmptyNodeIdLookUp {
659 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
661 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
662 pub(crate) fn take_inner(mut self) -> *mut nativeEmptyNodeIdLookUp {
663 assert!(self.is_owned);
664 let ret = ObjOps::untweak_ptr(self.inner);
665 self.inner = core::ptr::null_mut();
669 /// Constructs a new EmptyNodeIdLookUp given each field
672 pub extern "C" fn EmptyNodeIdLookUp_new() -> EmptyNodeIdLookUp {
673 EmptyNodeIdLookUp { inner: ObjOps::heap_alloc(nativeEmptyNodeIdLookUp {
676 impl From<nativeEmptyNodeIdLookUp> for crate::lightning::blinded_path::NodeIdLookUp {
677 fn from(obj: nativeEmptyNodeIdLookUp) -> Self {
678 let rust_obj = crate::lightning::blinded_path::EmptyNodeIdLookUp { inner: ObjOps::heap_alloc(obj), is_owned: true };
679 let mut ret = EmptyNodeIdLookUp_as_NodeIdLookUp(&rust_obj);
680 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn
681 core::mem::forget(rust_obj);
682 ret.free = Some(EmptyNodeIdLookUp_free_void);
686 /// Constructs a new NodeIdLookUp which calls the relevant methods on this_arg.
687 /// This copies the `inner` pointer in this_arg and thus the returned NodeIdLookUp must be freed before this_arg is
689 pub extern "C" fn EmptyNodeIdLookUp_as_NodeIdLookUp(this_arg: &EmptyNodeIdLookUp) -> crate::lightning::blinded_path::NodeIdLookUp {
690 crate::lightning::blinded_path::NodeIdLookUp {
691 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
693 next_node_id: EmptyNodeIdLookUp_NodeIdLookUp_next_node_id,
698 extern "C" fn EmptyNodeIdLookUp_NodeIdLookUp_next_node_id(this_arg: *const c_void, mut short_channel_id: u64) -> crate::c_types::PublicKey {
699 let mut ret = <nativeEmptyNodeIdLookUp as lightning::blinded_path::NodeIdLookUp>::next_node_id(unsafe { &mut *(this_arg as *mut nativeEmptyNodeIdLookUp) }, short_channel_id);
700 let mut local_ret = if ret.is_none() { crate::c_types::PublicKey::null() } else { { crate::c_types::PublicKey::from_rust(&(ret.unwrap())) } };
705 use lightning::blinded_path::BlindedHop as nativeBlindedHopImport;
706 pub(crate) type nativeBlindedHop = nativeBlindedHopImport;
708 /// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to
709 /// be encoded in the sender's onion packet. These hops cannot be identified by outside observers
710 /// and thus can be used to hide the identity of the recipient.
713 pub struct BlindedHop {
714 /// A pointer to the opaque Rust object.
716 /// Nearly everywhere, inner must be non-null, however in places where
717 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
718 pub inner: *mut nativeBlindedHop,
719 /// Indicates that this is the only struct which contains the same pointer.
721 /// Rust functions which take ownership of an object provided via an argument require
722 /// this to be true and invalidate the object pointed to by inner.
726 impl Drop for BlindedHop {
728 if self.is_owned && !<*mut nativeBlindedHop>::is_null(self.inner) {
729 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
733 /// Frees any resources used by the BlindedHop, if is_owned is set and inner is non-NULL.
735 pub extern "C" fn BlindedHop_free(this_obj: BlindedHop) { }
737 /// Used only if an object of this type is returned as a trait impl by a method
738 pub(crate) extern "C" fn BlindedHop_free_void(this_ptr: *mut c_void) {
739 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeBlindedHop) };
743 pub(crate) fn get_native_ref(&self) -> &'static nativeBlindedHop {
744 unsafe { &*ObjOps::untweak_ptr(self.inner) }
746 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeBlindedHop {
747 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
749 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
750 pub(crate) fn take_inner(mut self) -> *mut nativeBlindedHop {
751 assert!(self.is_owned);
752 let ret = ObjOps::untweak_ptr(self.inner);
753 self.inner = core::ptr::null_mut();
757 /// The blinded node id of this hop in a [`BlindedPath`].
759 pub extern "C" fn BlindedHop_get_blinded_node_id(this_ptr: &BlindedHop) -> crate::c_types::PublicKey {
760 let mut inner_val = &mut this_ptr.get_native_mut_ref().blinded_node_id;
761 crate::c_types::PublicKey::from_rust(&inner_val)
763 /// The blinded node id of this hop in a [`BlindedPath`].
765 pub extern "C" fn BlindedHop_set_blinded_node_id(this_ptr: &mut BlindedHop, mut val: crate::c_types::PublicKey) {
766 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.blinded_node_id = val.into_rust();
768 /// The encrypted payload intended for this hop in a [`BlindedPath`].
770 /// Returns a copy of the field.
772 pub extern "C" fn BlindedHop_get_encrypted_payload(this_ptr: &BlindedHop) -> crate::c_types::derived::CVec_u8Z {
773 let mut inner_val = this_ptr.get_native_mut_ref().encrypted_payload.clone();
774 let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { item }); };
775 local_inner_val.into()
777 /// The encrypted payload intended for this hop in a [`BlindedPath`].
779 pub extern "C" fn BlindedHop_set_encrypted_payload(this_ptr: &mut BlindedHop, mut val: crate::c_types::derived::CVec_u8Z) {
780 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
781 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.encrypted_payload = local_val;
783 /// Constructs a new BlindedHop given each field
786 pub extern "C" fn BlindedHop_new(mut blinded_node_id_arg: crate::c_types::PublicKey, mut encrypted_payload_arg: crate::c_types::derived::CVec_u8Z) -> BlindedHop {
787 let mut local_encrypted_payload_arg = Vec::new(); for mut item in encrypted_payload_arg.into_rust().drain(..) { local_encrypted_payload_arg.push( { item }); };
788 BlindedHop { inner: ObjOps::heap_alloc(nativeBlindedHop {
789 blinded_node_id: blinded_node_id_arg.into_rust(),
790 encrypted_payload: local_encrypted_payload_arg,
793 impl Clone for BlindedHop {
794 fn clone(&self) -> Self {
796 inner: if <*mut nativeBlindedHop>::is_null(self.inner) { core::ptr::null_mut() } else {
797 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
803 /// Used only if an object of this type is returned as a trait impl by a method
804 pub(crate) extern "C" fn BlindedHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
805 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeBlindedHop)).clone() })) as *mut c_void
808 /// Creates a copy of the BlindedHop
809 pub extern "C" fn BlindedHop_clone(orig: &BlindedHop) -> BlindedHop {
812 /// Get a string which allows debug introspection of a BlindedHop object
813 pub extern "C" fn BlindedHop_debug_str_void(o: *const c_void) -> Str {
814 alloc::format!("{:?}", unsafe { o as *const crate::lightning::blinded_path::BlindedHop }).into()}
815 /// Generates a non-cryptographic 64-bit hash of the BlindedHop.
817 pub extern "C" fn BlindedHop_hash(o: &BlindedHop) -> u64 {
818 if o.inner.is_null() { return 0; }
819 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
821 let mut hasher = core::hash::SipHasher::new();
822 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
823 core::hash::Hasher::finish(&hasher)
825 /// Checks if two BlindedHops contain equal inner contents.
826 /// This ignores pointers and is_owned flags and looks at the values in fields.
827 /// Two objects with NULL inner values will be considered "equal" here.
829 pub extern "C" fn BlindedHop_eq(a: &BlindedHop, b: &BlindedHop) -> bool {
830 if a.inner == b.inner { return true; }
831 if a.inner.is_null() || b.inner.is_null() { return false; }
832 if a.get_native_ref() == b.get_native_ref() { true } else { false }
834 /// Create a one-hop blinded path for a message.
837 pub extern "C" fn BlindedPath_one_hop_for_message(mut recipient_node_id: crate::c_types::PublicKey, mut entropy_source: crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_BlindedPathNoneZ {
838 let mut ret = lightning::blinded_path::BlindedPath::one_hop_for_message(recipient_node_id.into_rust(), entropy_source, secp256k1::global::SECP256K1);
839 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
843 /// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
844 /// pubkey in `node_pks` will be the destination node.
846 /// Errors if no hops are provided or if `node_pk`(s) are invalid.
849 pub extern "C" fn BlindedPath_new_for_message(mut node_pks: crate::c_types::derived::CVec_PublicKeyZ, mut entropy_source: crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_BlindedPathNoneZ {
850 let mut local_node_pks = Vec::new(); for mut item in node_pks.into_rust().drain(..) { local_node_pks.push( { item.into_rust() }); };
851 let mut ret = lightning::blinded_path::BlindedPath::new_for_message(&local_node_pks[..], entropy_source, secp256k1::global::SECP256K1);
852 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
856 /// Create a one-hop blinded path for a payment.
859 pub extern "C" fn BlindedPath_one_hop_for_payment(mut payee_node_id: crate::c_types::PublicKey, mut payee_tlvs: crate::lightning::blinded_path::payment::ReceiveTlvs, mut min_final_cltv_expiry_delta: u16, mut entropy_source: crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_C2Tuple_BlindedPayInfoBlindedPathZNoneZ {
860 let mut ret = lightning::blinded_path::BlindedPath::one_hop_for_payment(payee_node_id.into_rust(), *unsafe { Box::from_raw(payee_tlvs.take_inner()) }, min_final_cltv_expiry_delta, entropy_source, secp256k1::global::SECP256K1);
861 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let (mut orig_ret_0_0, mut orig_ret_0_1) = o; let mut local_ret_0 = (crate::lightning::offers::invoice::BlindedPayInfo { inner: ObjOps::heap_alloc(orig_ret_0_0), is_owned: true }, crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(orig_ret_0_1), is_owned: true }).into(); local_ret_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
865 /// Create a blinded path for a payment, to be forwarded along `intermediate_nodes`.
868 /// * a provided node id is invalid
869 /// * [`BlindedPayInfo`] calculation results in an integer overflow
870 /// * any unknown features are required in the provided [`ForwardTlvs`]
872 /// [`ForwardTlvs`]: crate::blinded_path::payment::ForwardTlvs
875 pub extern "C" fn BlindedPath_new_for_payment(mut intermediate_nodes: crate::c_types::derived::CVec_ForwardNodeZ, mut payee_node_id: crate::c_types::PublicKey, mut payee_tlvs: crate::lightning::blinded_path::payment::ReceiveTlvs, mut htlc_maximum_msat: u64, mut min_final_cltv_expiry_delta: u16, mut entropy_source: crate::lightning::sign::EntropySource) -> crate::c_types::derived::CResult_C2Tuple_BlindedPayInfoBlindedPathZNoneZ {
876 let mut local_intermediate_nodes = Vec::new(); for mut item in intermediate_nodes.into_rust().drain(..) { local_intermediate_nodes.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
877 let mut ret = lightning::blinded_path::BlindedPath::new_for_payment(local_intermediate_nodes, payee_node_id.into_rust(), *unsafe { Box::from_raw(payee_tlvs.take_inner()) }, htlc_maximum_msat, min_final_cltv_expiry_delta, entropy_source, secp256k1::global::SECP256K1);
878 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let (mut orig_ret_0_0, mut orig_ret_0_1) = o; let mut local_ret_0 = (crate::lightning::offers::invoice::BlindedPayInfo { inner: ObjOps::heap_alloc(orig_ret_0_0), is_owned: true }, crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(orig_ret_0_1), is_owned: true }).into(); local_ret_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() };
882 /// Returns the introduction [`NodeId`] of the blinded path, if it is publicly reachable (i.e.,
883 /// it is found in the network graph).
885 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
888 pub extern "C" fn BlindedPath_public_introduction_node_id(this_arg: &crate::lightning::blinded_path::BlindedPath, network_graph: &crate::lightning::routing::gossip::ReadOnlyNetworkGraph) -> crate::lightning::routing::gossip::NodeId {
889 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.public_introduction_node_id(network_graph.get_native_ref());
890 let mut local_ret = crate::lightning::routing::gossip::NodeId { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning::routing::gossip::NodeId<>) as *mut _ }, is_owned: false };
895 /// Serialize the BlindedPath object into a byte array which can be read by BlindedPath_read
896 pub extern "C" fn BlindedPath_write(obj: &crate::lightning::blinded_path::BlindedPath) -> crate::c_types::derived::CVec_u8Z {
897 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
900 pub(crate) extern "C" fn BlindedPath_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
901 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedPath) })
904 /// Read a BlindedPath from a byte array, created by BlindedPath_write
905 pub extern "C" fn BlindedPath_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedPathDecodeErrorZ {
906 let res: Result<lightning::blinded_path::BlindedPath, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
907 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
911 /// Serialize the BlindedHop object into a byte array which can be read by BlindedHop_read
912 pub extern "C" fn BlindedHop_write(obj: &crate::lightning::blinded_path::BlindedHop) -> crate::c_types::derived::CVec_u8Z {
913 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
916 pub(crate) extern "C" fn BlindedHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
917 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeBlindedHop) })
920 /// Read a BlindedHop from a byte array, created by BlindedHop_write
921 pub extern "C" fn BlindedHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_BlindedHopDecodeErrorZ {
922 let res: Result<lightning::blinded_path::BlindedHop, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
923 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::blinded_path::BlindedHop { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };