Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning_invoice / utils.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
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.
8
9 //! Convenient utilities to create an invoice.
10
11 use alloc::str::FromStr;
12 use core::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16 #[cfg(feature="no-std")]
17 use alloc::{vec::Vec, boxed::Box};
18
19 /// Utility to construct an invoice. Generally, unless you want to do something like a custom
20 /// cltv_expiry, this is what you should be using to create an invoice. The reason being, this
21 /// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
22 /// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
23 /// that the payment secret is valid when the invoice is paid.
24 #[no_mangle]
25 pub extern "C" fn create_invoice_from_channelmanager(channelmanager: &crate::lightning::ln::channelmanager::ChannelManager, mut keys_manager: crate::lightning::chain::keysinterface::KeysInterface, mut network: crate::lightning_invoice::Currency, mut amt_msat: crate::c_types::derived::COption_u64Z, mut description: crate::c_types::Str) -> crate::c_types::derived::CResult_InvoiceSignOrCreationErrorZ {
26         let mut local_amt_msat = if amt_msat.is_some() { Some( { amt_msat.take() }) } else { None };
27         let mut ret = lightning_invoice::utils::create_invoice_from_channelmanager(channelmanager.get_native_ref(), keys_manager, network.into_native(), local_amt_msat, description.into_string());
28         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning_invoice::Invoice { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::SignOrCreationError::native_into(e) }).into() };
29         local_ret
30 }
31
32
33 use lightning_invoice::utils::DefaultRouter as nativeDefaultRouterImport;
34 pub(crate) type nativeDefaultRouter = nativeDefaultRouterImport<&'static lightning::routing::network_graph::NetworkGraph, crate::lightning::util::logger::Logger>;
35
36 /// A [`Router`] implemented using [`find_route`].
37 #[must_use]
38 #[repr(C)]
39 pub struct DefaultRouter {
40         /// A pointer to the opaque Rust object.
41
42         /// Nearly everywhere, inner must be non-null, however in places where
43         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
44         pub inner: *mut nativeDefaultRouter,
45         /// Indicates that this is the only struct which contains the same pointer.
46
47         /// Rust functions which take ownership of an object provided via an argument require
48         /// this to be true and invalidate the object pointed to by inner.
49         pub is_owned: bool,
50 }
51
52 impl Drop for DefaultRouter {
53         fn drop(&mut self) {
54                 if self.is_owned && !<*mut nativeDefaultRouter>::is_null(self.inner) {
55                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
56                 }
57         }
58 }
59 /// Frees any resources used by the DefaultRouter, if is_owned is set and inner is non-NULL.
60 #[no_mangle]
61 pub extern "C" fn DefaultRouter_free(this_obj: DefaultRouter) { }
62 #[allow(unused)]
63 /// Used only if an object of this type is returned as a trait impl by a method
64 pub(crate) extern "C" fn DefaultRouter_free_void(this_ptr: *mut c_void) {
65         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeDefaultRouter); }
66 }
67 #[allow(unused)]
68 impl DefaultRouter {
69         pub(crate) fn get_native_ref(&self) -> &'static nativeDefaultRouter {
70                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
71         }
72         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeDefaultRouter {
73                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
74         }
75         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
76         pub(crate) fn take_inner(mut self) -> *mut nativeDefaultRouter {
77                 assert!(self.is_owned);
78                 let ret = ObjOps::untweak_ptr(self.inner);
79                 self.inner = core::ptr::null_mut();
80                 ret
81         }
82 }
83 /// Creates a new router using the given [`NetworkGraph`] and  [`Logger`].
84 #[must_use]
85 #[no_mangle]
86 pub extern "C" fn DefaultRouter_new(network_graph: &crate::lightning::routing::network_graph::NetworkGraph, mut logger: crate::lightning::util::logger::Logger) -> DefaultRouter {
87         let mut ret = lightning_invoice::utils::DefaultRouter::new(network_graph.get_native_ref(), logger);
88         DefaultRouter { inner: ObjOps::heap_alloc(ret), is_owned: true }
89 }
90
91 impl From<nativeDefaultRouter> for crate::lightning_invoice::payment::Router {
92         fn from(obj: nativeDefaultRouter) -> Self {
93                 let mut rust_obj = DefaultRouter { inner: ObjOps::heap_alloc(obj), is_owned: true };
94                 let mut ret = DefaultRouter_as_Router(&rust_obj);
95                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
96                 rust_obj.inner = core::ptr::null_mut();
97                 ret.free = Some(DefaultRouter_free_void);
98                 ret
99         }
100 }
101 /// Constructs a new Router which calls the relevant methods on this_arg.
102 /// This copies the `inner` pointer in this_arg and thus the returned Router must be freed before this_arg is
103 #[no_mangle]
104 pub extern "C" fn DefaultRouter_as_Router(this_arg: &DefaultRouter) -> crate::lightning_invoice::payment::Router {
105         crate::lightning_invoice::payment::Router {
106                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
107                 free: None,
108                 find_route: DefaultRouter_Router_find_route,
109         }
110 }
111
112 #[must_use]
113 extern "C" fn DefaultRouter_Router_find_route(this_arg: *const c_void, mut payer: crate::c_types::PublicKey, params: &crate::lightning::routing::router::RouteParameters, _payment_hash: *const [u8; 32], first_hops: *mut crate::c_types::derived::CVec_ChannelDetailsZ, scorer: &crate::lightning::routing::scoring::Score) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
114         let mut local_first_hops_base = if first_hops == core::ptr::null_mut() { None } else { Some( { let mut local_first_hops_0 = Vec::new(); for mut item in unsafe { &mut *first_hops }.as_slice().iter() { local_first_hops_0.push( { item.get_native_ref() }); }; local_first_hops_0 }) }; let mut local_first_hops = local_first_hops_base.as_ref().map(|a| &a[..]);
115         let mut ret = <nativeDefaultRouter as lightning_invoice::payment::Router<_>>::find_route(unsafe { &mut *(this_arg as *mut nativeDefaultRouter) }, &payer.into_rust(), params.get_native_ref(), &::lightning::ln::PaymentHash(unsafe { *_payment_hash }), local_first_hops, scorer);
116         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::Route { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
117         local_ret
118 }
119
120 use crate::lightning::ln::channelmanager::nativeChannelManager as nativeChannelManager;
121 use crate::lightning::ln::channelmanager::ChannelManager;
122 use crate::lightning::ln::channelmanager::ChannelManager_free_void;
123 impl From<nativeChannelManager> for crate::lightning_invoice::payment::Payer {
124         fn from(obj: nativeChannelManager) -> Self {
125                 let mut rust_obj = ChannelManager { inner: ObjOps::heap_alloc(obj), is_owned: true };
126                 let mut ret = ChannelManager_as_Payer(&rust_obj);
127                 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
128                 rust_obj.inner = core::ptr::null_mut();
129                 ret.free = Some(ChannelManager_free_void);
130                 ret
131         }
132 }
133 /// Constructs a new Payer which calls the relevant methods on this_arg.
134 /// This copies the `inner` pointer in this_arg and thus the returned Payer must be freed before this_arg is
135 #[no_mangle]
136 pub extern "C" fn ChannelManager_as_Payer(this_arg: &ChannelManager) -> crate::lightning_invoice::payment::Payer {
137         crate::lightning_invoice::payment::Payer {
138                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
139                 free: None,
140                 node_id: ChannelManager_Payer_node_id,
141                 first_hops: ChannelManager_Payer_first_hops,
142                 send_payment: ChannelManager_Payer_send_payment,
143                 send_spontaneous_payment: ChannelManager_Payer_send_spontaneous_payment,
144                 retry_payment: ChannelManager_Payer_retry_payment,
145                 abandon_payment: ChannelManager_Payer_abandon_payment,
146         }
147 }
148
149 #[must_use]
150 extern "C" fn ChannelManager_Payer_node_id(this_arg: *const c_void) -> crate::c_types::PublicKey {
151         let mut ret = <nativeChannelManager as lightning_invoice::payment::Payer<>>::node_id(unsafe { &mut *(this_arg as *mut nativeChannelManager) }, );
152         crate::c_types::PublicKey::from_rust(&ret)
153 }
154 #[must_use]
155 extern "C" fn ChannelManager_Payer_first_hops(this_arg: *const c_void) -> crate::c_types::derived::CVec_ChannelDetailsZ {
156         let mut ret = <nativeChannelManager as lightning_invoice::payment::Payer<>>::first_hops(unsafe { &mut *(this_arg as *mut nativeChannelManager) }, );
157         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::ln::channelmanager::ChannelDetails { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
158         local_ret.into()
159 }
160 #[must_use]
161 extern "C" fn ChannelManager_Payer_send_payment(this_arg: *const c_void, route: &crate::lightning::routing::router::Route, mut payment_hash: crate::c_types::ThirtyTwoBytes, mut payment_secret: crate::c_types::ThirtyTwoBytes) -> crate::c_types::derived::CResult_PaymentIdPaymentSendFailureZ {
162         let mut local_payment_secret = if payment_secret.data == [0; 32] { None } else { Some( { ::lightning::ln::PaymentSecret(payment_secret.data) }) };
163         let mut ret = <nativeChannelManager as lightning_invoice::payment::Payer<>>::send_payment(unsafe { &mut *(this_arg as *mut nativeChannelManager) }, route.get_native_ref(), ::lightning::ln::PaymentHash(payment_hash.data), &local_payment_secret);
164         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::ThirtyTwoBytes { data: o.0 } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::channelmanager::PaymentSendFailure::native_into(e) }).into() };
165         local_ret
166 }
167 #[must_use]
168 extern "C" fn ChannelManager_Payer_send_spontaneous_payment(this_arg: *const c_void, route: &crate::lightning::routing::router::Route, mut payment_preimage: crate::c_types::ThirtyTwoBytes) -> crate::c_types::derived::CResult_PaymentIdPaymentSendFailureZ {
169         let mut ret = <nativeChannelManager as lightning_invoice::payment::Payer<>>::send_spontaneous_payment(unsafe { &mut *(this_arg as *mut nativeChannelManager) }, route.get_native_ref(), ::lightning::ln::PaymentPreimage(payment_preimage.data));
170         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::ThirtyTwoBytes { data: o.0 } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::channelmanager::PaymentSendFailure::native_into(e) }).into() };
171         local_ret
172 }
173 #[must_use]
174 extern "C" fn ChannelManager_Payer_retry_payment(this_arg: *const c_void, route: &crate::lightning::routing::router::Route, mut payment_id: crate::c_types::ThirtyTwoBytes) -> crate::c_types::derived::CResult_NonePaymentSendFailureZ {
175         let mut ret = <nativeChannelManager as lightning_invoice::payment::Payer<>>::retry_payment(unsafe { &mut *(this_arg as *mut nativeChannelManager) }, route.get_native_ref(), ::lightning::ln::channelmanager::PaymentId(payment_id.data));
176         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::channelmanager::PaymentSendFailure::native_into(e) }).into() };
177         local_ret
178 }
179 extern "C" fn ChannelManager_Payer_abandon_payment(this_arg: *const c_void, mut payment_id: crate::c_types::ThirtyTwoBytes) {
180         <nativeChannelManager as lightning_invoice::payment::Payer<>>::abandon_payment(unsafe { &mut *(this_arg as *mut nativeChannelManager) }, ::lightning::ln::channelmanager::PaymentId(payment_id.data))
181 }
182