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 //! Structs and enums useful for constructing and reading an onion message packet.
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};
21 use lightning::onion_message::packet::Packet as nativePacketImport;
22 pub(crate) type nativePacket = nativePacketImport;
24 /// Packet of hop data for next peer
28 /// A pointer to the opaque Rust object.
30 /// Nearly everywhere, inner must be non-null, however in places where
31 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
32 pub inner: *mut nativePacket,
33 /// Indicates that this is the only struct which contains the same pointer.
35 /// Rust functions which take ownership of an object provided via an argument require
36 /// this to be true and invalidate the object pointed to by inner.
40 impl Drop for Packet {
42 if self.is_owned && !<*mut nativePacket>::is_null(self.inner) {
43 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
47 /// Frees any resources used by the Packet, if is_owned is set and inner is non-NULL.
49 pub extern "C" fn Packet_free(this_obj: Packet) { }
51 /// Used only if an object of this type is returned as a trait impl by a method
52 pub(crate) extern "C" fn Packet_free_void(this_ptr: *mut c_void) {
53 let _ = unsafe { Box::from_raw(this_ptr as *mut nativePacket) };
57 pub(crate) fn get_native_ref(&self) -> &'static nativePacket {
58 unsafe { &*ObjOps::untweak_ptr(self.inner) }
60 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePacket {
61 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
63 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
64 pub(crate) fn take_inner(mut self) -> *mut nativePacket {
65 assert!(self.is_owned);
66 let ret = ObjOps::untweak_ptr(self.inner);
67 self.inner = core::ptr::null_mut();
71 /// Bolt 04 version number
73 pub extern "C" fn Packet_get_version(this_ptr: &Packet) -> u8 {
74 let mut inner_val = &mut this_ptr.get_native_mut_ref().version;
77 /// Bolt 04 version number
79 pub extern "C" fn Packet_set_version(this_ptr: &mut Packet, mut val: u8) {
80 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.version = val;
82 /// A random sepc256k1 point, used to build the ECDH shared secret to decrypt hop_data
84 pub extern "C" fn Packet_get_public_key(this_ptr: &Packet) -> crate::c_types::PublicKey {
85 let mut inner_val = &mut this_ptr.get_native_mut_ref().public_key;
86 crate::c_types::PublicKey::from_rust(&inner_val)
88 /// A random sepc256k1 point, used to build the ECDH shared secret to decrypt hop_data
90 pub extern "C" fn Packet_set_public_key(this_ptr: &mut Packet, mut val: crate::c_types::PublicKey) {
91 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.public_key = val.into_rust();
93 /// Encrypted payload for the next hop
95 /// Returns a copy of the field.
97 pub extern "C" fn Packet_get_hop_data(this_ptr: &Packet) -> crate::c_types::derived::CVec_u8Z {
98 let mut inner_val = this_ptr.get_native_mut_ref().hop_data.clone();
99 let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { item }); };
100 local_inner_val.into()
102 /// Encrypted payload for the next hop
104 pub extern "C" fn Packet_set_hop_data(this_ptr: &mut Packet, mut val: crate::c_types::derived::CVec_u8Z) {
105 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
106 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.hop_data = local_val;
108 /// HMAC to verify the integrity of hop_data
110 pub extern "C" fn Packet_get_hmac(this_ptr: &Packet) -> *const [u8; 32] {
111 let mut inner_val = &mut this_ptr.get_native_mut_ref().hmac;
114 /// HMAC to verify the integrity of hop_data
116 pub extern "C" fn Packet_set_hmac(this_ptr: &mut Packet, mut val: crate::c_types::ThirtyTwoBytes) {
117 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.hmac = val.data;
119 /// Constructs a new Packet given each field
122 pub extern "C" fn Packet_new(mut version_arg: u8, mut public_key_arg: crate::c_types::PublicKey, mut hop_data_arg: crate::c_types::derived::CVec_u8Z, mut hmac_arg: crate::c_types::ThirtyTwoBytes) -> Packet {
123 let mut local_hop_data_arg = Vec::new(); for mut item in hop_data_arg.into_rust().drain(..) { local_hop_data_arg.push( { item }); };
124 Packet { inner: ObjOps::heap_alloc(nativePacket {
125 version: version_arg,
126 public_key: public_key_arg.into_rust(),
127 hop_data: local_hop_data_arg,
131 impl Clone for Packet {
132 fn clone(&self) -> Self {
134 inner: if <*mut nativePacket>::is_null(self.inner) { core::ptr::null_mut() } else {
135 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
141 /// Used only if an object of this type is returned as a trait impl by a method
142 pub(crate) extern "C" fn Packet_clone_void(this_ptr: *const c_void) -> *mut c_void {
143 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePacket)).clone() })) as *mut c_void
146 /// Creates a copy of the Packet
147 pub extern "C" fn Packet_clone(orig: &Packet) -> Packet {
150 /// Checks if two Packets contain equal inner contents.
151 /// This ignores pointers and is_owned flags and looks at the values in fields.
152 /// Two objects with NULL inner values will be considered "equal" here.
154 pub extern "C" fn Packet_eq(a: &Packet, b: &Packet) -> bool {
155 if a.inner == b.inner { return true; }
156 if a.inner.is_null() || b.inner.is_null() { return false; }
157 if a.get_native_ref() == b.get_native_ref() { true } else { false }
160 /// Serialize the Packet object into a byte array which can be read by Packet_read
161 pub extern "C" fn Packet_write(obj: &crate::lightning::onion_message::packet::Packet) -> crate::c_types::derived::CVec_u8Z {
162 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
165 pub(crate) extern "C" fn Packet_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
166 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativePacket) })
168 /// The contents of an onion message. In the context of offers, this would be the invoice, invoice
169 /// request, or invoice error.
173 pub enum OnionMessageContents {
174 /// A message related to BOLT 12 Offers.
176 crate::lightning::onion_message::offers::OffersMessage),
177 /// A custom onion message specified by the user.
179 crate::lightning::onion_message::packet::CustomOnionMessageContents),
181 use lightning::onion_message::packet::OnionMessageContents as OnionMessageContentsImport;
182 pub(crate) type nativeOnionMessageContents = OnionMessageContentsImport<crate::lightning::onion_message::packet::CustomOnionMessageContents>;
184 impl OnionMessageContents {
186 pub(crate) fn to_native(&self) -> nativeOnionMessageContents {
188 OnionMessageContents::Offers (ref a, ) => {
189 let mut a_nonref = Clone::clone(a);
190 nativeOnionMessageContents::Offers (
191 a_nonref.into_native(),
194 OnionMessageContents::Custom (ref a, ) => {
195 let mut a_nonref = Clone::clone(a);
196 nativeOnionMessageContents::Custom (
203 pub(crate) fn into_native(self) -> nativeOnionMessageContents {
205 OnionMessageContents::Offers (mut a, ) => {
206 nativeOnionMessageContents::Offers (
210 OnionMessageContents::Custom (mut a, ) => {
211 nativeOnionMessageContents::Custom (
218 pub(crate) fn from_native(native: &nativeOnionMessageContents) -> Self {
220 nativeOnionMessageContents::Offers (ref a, ) => {
221 let mut a_nonref = Clone::clone(a);
222 OnionMessageContents::Offers (
223 crate::lightning::onion_message::offers::OffersMessage::native_into(a_nonref),
226 nativeOnionMessageContents::Custom (ref a, ) => {
227 let mut a_nonref = Clone::clone(a);
228 OnionMessageContents::Custom (
229 Into::into(a_nonref),
235 pub(crate) fn native_into(native: nativeOnionMessageContents) -> Self {
237 nativeOnionMessageContents::Offers (mut a, ) => {
238 OnionMessageContents::Offers (
239 crate::lightning::onion_message::offers::OffersMessage::native_into(a),
242 nativeOnionMessageContents::Custom (mut a, ) => {
243 OnionMessageContents::Custom (
250 /// Frees any resources used by the OnionMessageContents
252 pub extern "C" fn OnionMessageContents_free(this_ptr: OnionMessageContents) { }
253 /// Creates a copy of the OnionMessageContents
255 pub extern "C" fn OnionMessageContents_clone(orig: &OnionMessageContents) -> OnionMessageContents {
259 /// Utility method to constructs a new Offers-variant OnionMessageContents
260 pub extern "C" fn OnionMessageContents_offers(a: crate::lightning::onion_message::offers::OffersMessage) -> OnionMessageContents {
261 OnionMessageContents::Offers(a, )
264 /// Utility method to constructs a new Custom-variant OnionMessageContents
265 pub extern "C" fn OnionMessageContents_custom(a: crate::lightning::onion_message::packet::CustomOnionMessageContents) -> OnionMessageContents {
266 OnionMessageContents::Custom(a, )
268 /// The contents of a custom onion message.
270 pub struct CustomOnionMessageContents {
271 /// An opaque pointer which is passed to your function implementations as an argument.
272 /// This has no meaning in the LDK, and can be NULL or any other value.
273 pub this_arg: *mut c_void,
274 /// Returns the TLV type identifying the message contents. MUST be >= 64.
275 pub tlv_type: extern "C" fn (this_arg: *const c_void) -> u64,
276 /// Serialize the object into a byte array
277 pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
278 /// Called, if set, after this CustomOnionMessageContents has been cloned into a duplicate object.
279 /// The new CustomOnionMessageContents is provided, and should be mutated as needed to perform a
280 /// deep copy of the object pointed to by this_arg or avoid any double-freeing.
281 pub cloned: Option<extern "C" fn (new_CustomOnionMessageContents: &mut CustomOnionMessageContents)>,
282 /// Frees any resources associated with this object given its this_arg pointer.
283 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
284 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
286 unsafe impl Send for CustomOnionMessageContents {}
287 unsafe impl Sync for CustomOnionMessageContents {}
288 pub(crate) fn CustomOnionMessageContents_clone_fields(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents {
289 CustomOnionMessageContents {
290 this_arg: orig.this_arg,
291 tlv_type: Clone::clone(&orig.tlv_type),
292 write: Clone::clone(&orig.write),
293 cloned: Clone::clone(&orig.cloned),
294 free: Clone::clone(&orig.free),
297 impl lightning::util::ser::Writeable for CustomOnionMessageContents {
298 fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
299 let vec = (self.write)(self.this_arg);
300 w.write_all(vec.as_slice())
304 /// Creates a copy of a CustomOnionMessageContents
305 pub extern "C" fn CustomOnionMessageContents_clone(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents {
306 let mut res = CustomOnionMessageContents_clone_fields(orig);
307 if let Some(f) = orig.cloned { (f)(&mut res) };
310 impl Clone for CustomOnionMessageContents {
311 fn clone(&self) -> Self {
312 CustomOnionMessageContents_clone(self)
316 use lightning::onion_message::packet::CustomOnionMessageContents as rustCustomOnionMessageContents;
317 impl rustCustomOnionMessageContents for CustomOnionMessageContents {
318 fn tlv_type(&self) -> u64 {
319 let mut ret = (self.tlv_type)(self.this_arg);
324 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
325 // directly as a Deref trait in higher-level structs:
326 impl core::ops::Deref for CustomOnionMessageContents {
328 fn deref(&self) -> &Self {
332 impl core::ops::DerefMut for CustomOnionMessageContents {
333 fn deref_mut(&mut self) -> &mut Self {
337 /// Calls the free function if one is set
339 pub extern "C" fn CustomOnionMessageContents_free(this_ptr: CustomOnionMessageContents) { }
340 impl Drop for CustomOnionMessageContents {
342 if let Some(f) = self.free {