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