Update auto-generated bindings to LDK 0.0.121
[ldk-c-bindings] / lightning-c-bindings / src / lightning / offers / refund.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 //! Data structures and encoding for refunds.
10 //!
11 //! A [`Refund`] is an \"offer for money\" and is typically constructed by a merchant and presented
12 //! directly to the customer. The recipient responds with a [`Bolt12Invoice`] to be paid.
13 //!
14 //! This is an [`InvoiceRequest`] produced *not* in response to an [`Offer`].
15 //!
16 //! [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
17 //! [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
18 //! [`Offer`]: crate::offers::offer::Offer
19 //!
20 //! # Example
21 //!
22 //! ```
23 //! extern crate bitcoin;
24 //! extern crate core;
25 //! extern crate lightning;
26 //!
27 //! use core::convert::TryFrom;
28 //! use core::time::Duration;
29 //!
30 //! use bitcoin::network::constants::Network;
31 //! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
32 //! use lightning::offers::parse::Bolt12ParseError;
33 //! use lightning::offers::refund::{Refund, RefundBuilder};
34 //! use lightning::util::ser::{Readable, Writeable};
35 //!
36 //! # use lightning::blinded_path::BlindedPath;
37 //! # #[cfg(feature = \"std\")]
38 //! # use std::time::SystemTime;
39 //! #
40 //! # fn create_blinded_path() -> BlindedPath { unimplemented!() }
41 //! # fn create_another_blinded_path() -> BlindedPath { unimplemented!() }
42 //! #
43 //! # #[cfg(feature = \"std\")]
44 //! # fn build() -> Result<(), Bolt12ParseError> {
45 //! let secp_ctx = Secp256k1::new();
46 //! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
47 //! let pubkey = PublicKey::from(keys);
48 //!
49 //! let expiration = SystemTime::now() + Duration::from_secs(24 * 60 * 60);
50 //! let refund = RefundBuilder::new(\"coffee, large\".to_string(), vec![1; 32], pubkey, 20_000)?
51 //!     .absolute_expiry(expiration.duration_since(SystemTime::UNIX_EPOCH).unwrap())
52 //!     .issuer(\"Foo Bar\".to_string())
53 //!     .path(create_blinded_path())
54 //!     .path(create_another_blinded_path())
55 //!     .chain(Network::Bitcoin)
56 //!     .payer_note(\"refund for order #12345\".to_string())
57 //!     .build()?;
58 //!
59 //! // Encode as a bech32 string for use in a QR code.
60 //! let encoded_refund = refund.to_string();
61 //!
62 //! // Parse from a bech32 string after scanning from a QR code.
63 //! let refund = encoded_refund.parse::<Refund>()?;
64 //!
65 //! // Encode refund as raw bytes.
66 //! let mut bytes = Vec::new();
67 //! refund.write(&mut bytes).unwrap();
68 //!
69 //! // Decode raw bytes into an refund.
70 //! let refund = Refund::try_from(bytes)?;
71 //! # Ok(())
72 //! # }
73 //! ```
74 //!
75 //! # Note
76 //!
77 //! If constructing a [`Refund`] for use with a [`ChannelManager`], use
78 //! [`ChannelManager::create_refund_builder`] instead of [`RefundBuilder::new`].
79 //!
80 //! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
81 //! [`ChannelManager::create_refund_builder`]: crate::ln::channelmanager::ChannelManager::create_refund_builder
82
83 use alloc::str::FromStr;
84 use alloc::string::String;
85 use core::ffi::c_void;
86 use core::convert::Infallible;
87 use bitcoin::hashes::Hash;
88 use crate::c_types::*;
89 #[cfg(feature="no-std")]
90 use alloc::{vec::Vec, boxed::Box};
91
92
93 use lightning::offers::refund::Refund as nativeRefundImport;
94 pub(crate) type nativeRefund = nativeRefundImport;
95
96 /// A `Refund` is a request to send an [`Bolt12Invoice`] without a preceding [`Offer`].
97 ///
98 /// Typically, after an invoice is paid, the recipient may publish a refund allowing the sender to
99 /// recoup their funds. A refund may be used more generally as an \"offer for money\", such as with a
100 /// bitcoin ATM.
101 ///
102 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
103 /// [`Offer`]: crate::offers::offer::Offer
104 #[must_use]
105 #[repr(C)]
106 pub struct Refund {
107         /// A pointer to the opaque Rust object.
108
109         /// Nearly everywhere, inner must be non-null, however in places where
110         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
111         pub inner: *mut nativeRefund,
112         /// Indicates that this is the only struct which contains the same pointer.
113
114         /// Rust functions which take ownership of an object provided via an argument require
115         /// this to be true and invalidate the object pointed to by inner.
116         pub is_owned: bool,
117 }
118
119 impl Drop for Refund {
120         fn drop(&mut self) {
121                 if self.is_owned && !<*mut nativeRefund>::is_null(self.inner) {
122                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
123                 }
124         }
125 }
126 /// Frees any resources used by the Refund, if is_owned is set and inner is non-NULL.
127 #[no_mangle]
128 pub extern "C" fn Refund_free(this_obj: Refund) { }
129 #[allow(unused)]
130 /// Used only if an object of this type is returned as a trait impl by a method
131 pub(crate) extern "C" fn Refund_free_void(this_ptr: *mut c_void) {
132         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRefund) };
133 }
134 #[allow(unused)]
135 impl Refund {
136         pub(crate) fn get_native_ref(&self) -> &'static nativeRefund {
137                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
138         }
139         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRefund {
140                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
141         }
142         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
143         pub(crate) fn take_inner(mut self) -> *mut nativeRefund {
144                 assert!(self.is_owned);
145                 let ret = ObjOps::untweak_ptr(self.inner);
146                 self.inner = core::ptr::null_mut();
147                 ret
148         }
149 }
150 impl Clone for Refund {
151         fn clone(&self) -> Self {
152                 Self {
153                         inner: if <*mut nativeRefund>::is_null(self.inner) { core::ptr::null_mut() } else {
154                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
155                         is_owned: true,
156                 }
157         }
158 }
159 #[allow(unused)]
160 /// Used only if an object of this type is returned as a trait impl by a method
161 pub(crate) extern "C" fn Refund_clone_void(this_ptr: *const c_void) -> *mut c_void {
162         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeRefund)).clone() })) as *mut c_void
163 }
164 #[no_mangle]
165 /// Creates a copy of the Refund
166 pub extern "C" fn Refund_clone(orig: &Refund) -> Refund {
167         orig.clone()
168 }
169 /// Get a string which allows debug introspection of a Refund object
170 pub extern "C" fn Refund_debug_str_void(o: *const c_void) -> Str {
171         alloc::format!("{:?}", unsafe { o as *const crate::lightning::offers::refund::Refund }).into()}
172 /// A complete description of the purpose of the refund. Intended to be displayed to the user
173 /// but with the caveat that it has not been verified in any way.
174 #[must_use]
175 #[no_mangle]
176 pub extern "C" fn Refund_description(this_arg: &crate::lightning::offers::refund::Refund) -> crate::lightning::util::string::PrintableString {
177         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.description();
178         crate::lightning::util::string::PrintableString { inner: ObjOps::heap_alloc(ret), is_owned: true }
179 }
180
181 /// Duration since the Unix epoch when an invoice should no longer be sent.
182 ///
183 /// If `None`, the refund does not expire.
184 #[must_use]
185 #[no_mangle]
186 pub extern "C" fn Refund_absolute_expiry(this_arg: &crate::lightning::offers::refund::Refund) -> crate::c_types::derived::COption_u64Z {
187         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.absolute_expiry();
188         let mut local_ret = if ret.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { ret.unwrap().as_secs() }) };
189         local_ret
190 }
191
192 /// Whether the refund has expired.
193 #[must_use]
194 #[no_mangle]
195 pub extern "C" fn Refund_is_expired(this_arg: &crate::lightning::offers::refund::Refund) -> bool {
196         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_expired();
197         ret
198 }
199
200 /// Whether the refund has expired given the duration since the Unix epoch.
201 #[must_use]
202 #[no_mangle]
203 pub extern "C" fn Refund_is_expired_no_std(this_arg: &crate::lightning::offers::refund::Refund, mut duration_since_epoch: u64) -> bool {
204         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_expired_no_std(core::time::Duration::from_secs(duration_since_epoch));
205         ret
206 }
207
208 /// The issuer of the refund, possibly beginning with `user@domain` or `domain`. Intended to be
209 /// displayed to the user but with the caveat that it has not been verified in any way.
210 ///
211 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
212 #[must_use]
213 #[no_mangle]
214 pub extern "C" fn Refund_issuer(this_arg: &crate::lightning::offers::refund::Refund) -> crate::lightning::util::string::PrintableString {
215         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.issuer();
216         let mut local_ret = crate::lightning::util::string::PrintableString { inner: if ret.is_none() { core::ptr::null_mut() } else {  { ObjOps::heap_alloc((ret.unwrap())) } }, is_owned: true };
217         local_ret
218 }
219
220 /// Paths to the sender originating from publicly reachable nodes. Blinded paths provide sender
221 /// privacy by obfuscating its node id.
222 #[must_use]
223 #[no_mangle]
224 pub extern "C" fn Refund_paths(this_arg: &crate::lightning::offers::refund::Refund) -> crate::c_types::derived::CVec_BlindedPathZ {
225         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.paths();
226         let mut local_ret_clone = Vec::new(); local_ret_clone.extend_from_slice(ret); let mut ret = local_ret_clone; let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
227         local_ret.into()
228 }
229
230 /// An unpredictable series of bytes, typically containing information about the derivation of
231 /// [`payer_id`].
232 ///
233 /// [`payer_id`]: Self::payer_id
234 #[must_use]
235 #[no_mangle]
236 pub extern "C" fn Refund_payer_metadata(this_arg: &crate::lightning::offers::refund::Refund) -> crate::c_types::u8slice {
237         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.payer_metadata();
238         let mut local_ret = crate::c_types::u8slice::from_slice(ret);
239         local_ret
240 }
241
242 /// A chain that the refund is valid for.
243 #[must_use]
244 #[no_mangle]
245 pub extern "C" fn Refund_chain(this_arg: &crate::lightning::offers::refund::Refund) -> crate::c_types::ThirtyTwoBytes {
246         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.chain();
247         crate::c_types::ThirtyTwoBytes { data: *ret.as_ref() }
248 }
249
250 /// The amount to refund in msats (i.e., the minimum lightning-payable unit for [`chain`]).
251 ///
252 /// [`chain`]: Self::chain
253 #[must_use]
254 #[no_mangle]
255 pub extern "C" fn Refund_amount_msats(this_arg: &crate::lightning::offers::refund::Refund) -> u64 {
256         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.amount_msats();
257         ret
258 }
259
260 /// Features pertaining to requesting an invoice.
261 #[must_use]
262 #[no_mangle]
263 pub extern "C" fn Refund_features(this_arg: &crate::lightning::offers::refund::Refund) -> crate::lightning::ln::features::InvoiceRequestFeatures {
264         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.features();
265         crate::lightning::ln::features::InvoiceRequestFeatures { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const lightning::ln::features::InvoiceRequestFeatures<>) as *mut _) }, is_owned: false }
266 }
267
268 /// The quantity of an item that refund is for.
269 #[must_use]
270 #[no_mangle]
271 pub extern "C" fn Refund_quantity(this_arg: &crate::lightning::offers::refund::Refund) -> crate::c_types::derived::COption_u64Z {
272         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.quantity();
273         let mut local_ret = if ret.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { ret.unwrap() }) };
274         local_ret
275 }
276
277 /// A public node id to send to in the case where there are no [`paths`]. Otherwise, a possibly
278 /// transient pubkey.
279 ///
280 /// [`paths`]: Self::paths
281 #[must_use]
282 #[no_mangle]
283 pub extern "C" fn Refund_payer_id(this_arg: &crate::lightning::offers::refund::Refund) -> crate::c_types::PublicKey {
284         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.payer_id();
285         crate::c_types::PublicKey::from_rust(&ret)
286 }
287
288 /// Payer provided note to include in the invoice.
289 ///
290 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
291 #[must_use]
292 #[no_mangle]
293 pub extern "C" fn Refund_payer_note(this_arg: &crate::lightning::offers::refund::Refund) -> crate::lightning::util::string::PrintableString {
294         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.payer_note();
295         let mut local_ret = crate::lightning::util::string::PrintableString { inner: if ret.is_none() { core::ptr::null_mut() } else {  { ObjOps::heap_alloc((ret.unwrap())) } }, is_owned: true };
296         local_ret
297 }
298
299 #[no_mangle]
300 /// Serialize the Refund object into a byte array which can be read by Refund_read
301 pub extern "C" fn Refund_write(obj: &crate::lightning::offers::refund::Refund) -> crate::c_types::derived::CVec_u8Z {
302         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
303 }
304 #[allow(unused)]
305 pub(crate) extern "C" fn Refund_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
306         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRefund) })
307 }
308 #[no_mangle]
309 /// Read a Refund object from a string
310 pub extern "C" fn Refund_from_str(s: crate::c_types::Str) -> crate::c_types::derived::CResult_RefundBolt12ParseErrorZ {
311         match lightning::offers::refund::Refund::from_str(s.into_str()) {
312                 Ok(r) => {
313                         crate::c_types::CResultTempl::ok(
314                                 crate::lightning::offers::refund::Refund { inner: ObjOps::heap_alloc(r), is_owned: true }
315                         )
316                 },
317                 Err(e) => {
318                         crate::c_types::CResultTempl::err(
319                                 crate::lightning::offers::parse::Bolt12ParseError { inner: ObjOps::heap_alloc(e), is_owned: true }
320                         )
321                 },
322         }.into()
323 }