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