Update auto-generated bindings for LDK 0.0.115
[ldk-c-bindings] / lightning-c-bindings / src / lightning / offers / offer.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 `offer` messages.
10 //!
11 //! An [`Offer`] represents an \"offer to be paid.\" It is typically constructed by a merchant and
12 //! published as a QR code to be scanned by a customer. The customer uses the offer to request an
13 //! invoice from the merchant to be paid.
14 //!
15 //! ```
16 //! extern crate bitcoin;
17 //! extern crate core;
18 //! extern crate lightning;
19 //!
20 //! use core::convert::TryFrom;
21 //! use core::num::NonZeroU64;
22 //! use core::time::Duration;
23 //!
24 //! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
25 //! use lightning::offers::offer::{Offer, OfferBuilder, Quantity};
26 //! use lightning::offers::parse::ParseError;
27 //! use lightning::util::ser::{Readable, Writeable};
28 //!
29 //! # use lightning::blinded_path::BlindedPath;
30 //! # #[cfg(feature = \"std\")]
31 //! # use std::time::SystemTime;
32 //! #
33 //! # fn create_blinded_path() -> BlindedPath { unimplemented!() }
34 //! # fn create_another_blinded_path() -> BlindedPath { unimplemented!() }
35 //! #
36 //! # #[cfg(feature = \"std\")]
37 //! # fn build() -> Result<(), ParseError> {
38 //! let secp_ctx = Secp256k1::new();
39 //! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
40 //! let pubkey = PublicKey::from(keys);
41 //!
42 //! let expiration = SystemTime::now() + Duration::from_secs(24 * 60 * 60);
43 //! let offer = OfferBuilder::new(\"coffee, large\".to_string(), pubkey)
44 //!     .amount_msats(20_000)
45 //!     .supported_quantity(Quantity::Unbounded)
46 //!     .absolute_expiry(expiration.duration_since(SystemTime::UNIX_EPOCH).unwrap())
47 //!     .issuer(\"Foo Bar\".to_string())
48 //!     .path(create_blinded_path())
49 //!     .path(create_another_blinded_path())
50 //!     .build()?;
51 //!
52 //! // Encode as a bech32 string for use in a QR code.
53 //! let encoded_offer = offer.to_string();
54 //!
55 //! // Parse from a bech32 string after scanning from a QR code.
56 //! let offer = encoded_offer.parse::<Offer>()?;
57 //!
58 //! // Encode offer as raw bytes.
59 //! let mut bytes = Vec::new();
60 //! offer.write(&mut bytes).unwrap();
61 //!
62 //! // Decode raw bytes into an offer.
63 //! let offer = Offer::try_from(bytes)?;
64 //! # Ok(())
65 //! # }
66 //! ```
67
68 use alloc::str::FromStr;
69 use core::ffi::c_void;
70 use core::convert::Infallible;
71 use bitcoin::hashes::Hash;
72 use crate::c_types::*;
73 #[cfg(feature="no-std")]
74 use alloc::{vec::Vec, boxed::Box};
75
76
77 use lightning::offers::offer::Offer as nativeOfferImport;
78 pub(crate) type nativeOffer = nativeOfferImport;
79
80 /// An `Offer` is a potentially long-lived proposal for payment of a good or service.
81 ///
82 /// An offer is a precursor to an [`InvoiceRequest`]. A merchant publishes an offer from which a
83 /// customer may request an [`Invoice`] for a specific quantity and using an amount sufficient to
84 /// cover that quantity (i.e., at least `quantity * amount`). See [`Offer::amount`].
85 ///
86 /// Offers may be denominated in currency other than bitcoin but are ultimately paid using the
87 /// latter.
88 ///
89 /// Through the use of [`BlindedPath`]s, offers provide recipient privacy.
90 ///
91 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
92 /// [`Invoice`]: crate::offers::invoice::Invoice
93 #[must_use]
94 #[repr(C)]
95 pub struct Offer {
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 nativeOffer,
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 Offer {
109         fn drop(&mut self) {
110                 if self.is_owned && !<*mut nativeOffer>::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 Offer, if is_owned is set and inner is non-NULL.
116 #[no_mangle]
117 pub extern "C" fn Offer_free(this_obj: Offer) { }
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 Offer_free_void(this_ptr: *mut c_void) {
121         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeOffer) };
122 }
123 #[allow(unused)]
124 impl Offer {
125         pub(crate) fn get_native_ref(&self) -> &'static nativeOffer {
126                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
127         }
128         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeOffer {
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 nativeOffer {
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 Offer {
140         fn clone(&self) -> Self {
141                 Self {
142                         inner: if <*mut nativeOffer>::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 Offer_clone_void(this_ptr: *const c_void) -> *mut c_void {
151         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeOffer)).clone() })) as *mut c_void
152 }
153 #[no_mangle]
154 /// Creates a copy of the Offer
155 pub extern "C" fn Offer_clone(orig: &Offer) -> Offer {
156         orig.clone()
157 }
158 /// The chains that may be used when paying a requested invoice (e.g., bitcoin mainnet).
159 /// Payments must be denominated in units of the minimal lightning-payable unit (e.g., msats)
160 /// for the selected chain.
161 #[must_use]
162 #[no_mangle]
163 pub extern "C" fn Offer_chains(this_arg: &crate::lightning::offers::offer::Offer) -> crate::c_types::derived::CVec_ChainHashZ {
164         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.chains();
165         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::c_types::ThirtyTwoBytes { data: item.to_bytes() } }); };
166         local_ret.into()
167 }
168
169 /// Returns whether the given chain is supported by the offer.
170 #[must_use]
171 #[no_mangle]
172 pub extern "C" fn Offer_supports_chain(this_arg: &crate::lightning::offers::offer::Offer, mut chain: crate::c_types::ThirtyTwoBytes) -> bool {
173         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.supports_chain(::bitcoin::blockdata::constants::ChainHash::from(&chain.data[..]));
174         ret
175 }
176
177 /// Opaque bytes set by the originator. Useful for authentication and validating fields since it
178 /// is reflected in `invoice_request` messages along with all the other fields from the `offer`.
179 #[must_use]
180 #[no_mangle]
181 pub extern "C" fn Offer_metadata(this_arg: &crate::lightning::offers::offer::Offer) -> crate::c_types::derived::COption_CVec_u8ZZ {
182         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.metadata();
183         let mut local_ret = if ret.is_none() { crate::c_types::derived::COption_CVec_u8ZZ::None } else { crate::c_types::derived::COption_CVec_u8ZZ::Some(/* WARNING: CLONING CONVERSION HERE! &Option<Enum> is otherwise un-expressable. */ { let mut local_ret_0 = Vec::new(); for mut item in (*ret.as_ref().unwrap()).clone().drain(..) { local_ret_0.push( { item }); }; local_ret_0.into() }) };
184         local_ret
185 }
186
187 /// The minimum amount required for a successful payment of a single item.
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 Offer_amount(this_arg: &crate::lightning::offers::offer::Offer) -> crate::lightning::offers::offer::Amount {
193         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.amount();
194         let mut local_ret = crate::lightning::offers::offer::Amount { inner: unsafe { (if ret.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (ret.unwrap()) }) } as *const lightning::offers::offer::Amount<>) as *mut _ }, is_owned: false };
195         local_ret
196 }
197
198 /// A complete description of the purpose of the payment. Intended to be displayed to the user
199 /// but with the caveat that it has not been verified in any way.
200 #[must_use]
201 #[no_mangle]
202 pub extern "C" fn Offer_description(this_arg: &crate::lightning::offers::offer::Offer) -> crate::lightning::util::string::PrintableString {
203         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.description();
204         crate::lightning::util::string::PrintableString { inner: ObjOps::heap_alloc(ret), is_owned: true }
205 }
206
207 /// Features pertaining to the offer.
208 #[must_use]
209 #[no_mangle]
210 pub extern "C" fn Offer_features(this_arg: &crate::lightning::offers::offer::Offer) -> crate::lightning::ln::features::OfferFeatures {
211         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.features();
212         crate::lightning::ln::features::OfferFeatures { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const lightning::ln::features::OfferFeatures<>) as *mut _) }, is_owned: false }
213 }
214
215 /// Duration since the Unix epoch when an invoice should no longer be requested.
216 ///
217 /// If `None`, the offer does not expire.
218 #[must_use]
219 #[no_mangle]
220 pub extern "C" fn Offer_absolute_expiry(this_arg: &crate::lightning::offers::offer::Offer) -> crate::c_types::derived::COption_DurationZ {
221         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.absolute_expiry();
222         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() }) };
223         local_ret
224 }
225
226 /// Whether the offer has expired.
227 #[must_use]
228 #[no_mangle]
229 pub extern "C" fn Offer_is_expired(this_arg: &crate::lightning::offers::offer::Offer) -> bool {
230         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_expired();
231         ret
232 }
233
234 /// The issuer of the offer, possibly beginning with `user@domain` or `domain`. Intended to be
235 /// displayed to the user but with the caveat that it has not been verified in any way.
236 ///
237 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
238 #[must_use]
239 #[no_mangle]
240 pub extern "C" fn Offer_issuer(this_arg: &crate::lightning::offers::offer::Offer) -> crate::lightning::util::string::PrintableString {
241         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.issuer();
242         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 };
243         local_ret
244 }
245
246 /// Paths to the recipient originating from publicly reachable nodes. Blinded paths provide
247 /// recipient privacy by obfuscating its node id.
248 #[must_use]
249 #[no_mangle]
250 pub extern "C" fn Offer_paths(this_arg: &crate::lightning::offers::offer::Offer) -> crate::c_types::derived::CVec_BlindedPathZ {
251         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.paths();
252         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 } }); };
253         local_ret.into()
254 }
255
256 /// The quantity of items supported.
257 #[must_use]
258 #[no_mangle]
259 pub extern "C" fn Offer_supported_quantity(this_arg: &crate::lightning::offers::offer::Offer) -> crate::lightning::offers::offer::Quantity {
260         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.supported_quantity();
261         crate::lightning::offers::offer::Quantity { inner: ObjOps::heap_alloc(ret), is_owned: true }
262 }
263
264 /// Returns whether the given quantity is valid for the offer.
265 #[must_use]
266 #[no_mangle]
267 pub extern "C" fn Offer_is_valid_quantity(this_arg: &crate::lightning::offers::offer::Offer, mut quantity: u64) -> bool {
268         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_valid_quantity(quantity);
269         ret
270 }
271
272 /// Returns whether a quantity is expected in an [`InvoiceRequest`] for the offer.
273 ///
274 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
275 #[must_use]
276 #[no_mangle]
277 pub extern "C" fn Offer_expects_quantity(this_arg: &crate::lightning::offers::offer::Offer) -> bool {
278         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.expects_quantity();
279         ret
280 }
281
282 /// The public key used by the recipient to sign invoices.
283 #[must_use]
284 #[no_mangle]
285 pub extern "C" fn Offer_signing_pubkey(this_arg: &crate::lightning::offers::offer::Offer) -> crate::c_types::PublicKey {
286         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.signing_pubkey();
287         crate::c_types::PublicKey::from_rust(&ret)
288 }
289
290 #[no_mangle]
291 /// Serialize the Offer object into a byte array which can be read by Offer_read
292 pub extern "C" fn Offer_write(obj: &crate::lightning::offers::offer::Offer) -> crate::c_types::derived::CVec_u8Z {
293         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
294 }
295 #[no_mangle]
296 pub(crate) extern "C" fn Offer_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
297         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeOffer) })
298 }
299
300 use lightning::offers::offer::Amount as nativeAmountImport;
301 pub(crate) type nativeAmount = nativeAmountImport;
302
303 /// The minimum amount required for an item in an [`Offer`], denominated in either bitcoin or
304 /// another currency.
305 #[must_use]
306 #[repr(C)]
307 pub struct Amount {
308         /// A pointer to the opaque Rust object.
309
310         /// Nearly everywhere, inner must be non-null, however in places where
311         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
312         pub inner: *mut nativeAmount,
313         /// Indicates that this is the only struct which contains the same pointer.
314
315         /// Rust functions which take ownership of an object provided via an argument require
316         /// this to be true and invalidate the object pointed to by inner.
317         pub is_owned: bool,
318 }
319
320 impl Drop for Amount {
321         fn drop(&mut self) {
322                 if self.is_owned && !<*mut nativeAmount>::is_null(self.inner) {
323                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
324                 }
325         }
326 }
327 /// Frees any resources used by the Amount, if is_owned is set and inner is non-NULL.
328 #[no_mangle]
329 pub extern "C" fn Amount_free(this_obj: Amount) { }
330 #[allow(unused)]
331 /// Used only if an object of this type is returned as a trait impl by a method
332 pub(crate) extern "C" fn Amount_free_void(this_ptr: *mut c_void) {
333         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeAmount) };
334 }
335 #[allow(unused)]
336 impl Amount {
337         pub(crate) fn get_native_ref(&self) -> &'static nativeAmount {
338                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
339         }
340         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeAmount {
341                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
342         }
343         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
344         pub(crate) fn take_inner(mut self) -> *mut nativeAmount {
345                 assert!(self.is_owned);
346                 let ret = ObjOps::untweak_ptr(self.inner);
347                 self.inner = core::ptr::null_mut();
348                 ret
349         }
350 }
351 impl Clone for Amount {
352         fn clone(&self) -> Self {
353                 Self {
354                         inner: if <*mut nativeAmount>::is_null(self.inner) { core::ptr::null_mut() } else {
355                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
356                         is_owned: true,
357                 }
358         }
359 }
360 #[allow(unused)]
361 /// Used only if an object of this type is returned as a trait impl by a method
362 pub(crate) extern "C" fn Amount_clone_void(this_ptr: *const c_void) -> *mut c_void {
363         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeAmount)).clone() })) as *mut c_void
364 }
365 #[no_mangle]
366 /// Creates a copy of the Amount
367 pub extern "C" fn Amount_clone(orig: &Amount) -> Amount {
368         orig.clone()
369 }
370
371 use lightning::offers::offer::Quantity as nativeQuantityImport;
372 pub(crate) type nativeQuantity = nativeQuantityImport;
373
374 /// Quantity of items supported by an [`Offer`].
375 #[must_use]
376 #[repr(C)]
377 pub struct Quantity {
378         /// A pointer to the opaque Rust object.
379
380         /// Nearly everywhere, inner must be non-null, however in places where
381         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
382         pub inner: *mut nativeQuantity,
383         /// Indicates that this is the only struct which contains the same pointer.
384
385         /// Rust functions which take ownership of an object provided via an argument require
386         /// this to be true and invalidate the object pointed to by inner.
387         pub is_owned: bool,
388 }
389
390 impl Drop for Quantity {
391         fn drop(&mut self) {
392                 if self.is_owned && !<*mut nativeQuantity>::is_null(self.inner) {
393                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
394                 }
395         }
396 }
397 /// Frees any resources used by the Quantity, if is_owned is set and inner is non-NULL.
398 #[no_mangle]
399 pub extern "C" fn Quantity_free(this_obj: Quantity) { }
400 #[allow(unused)]
401 /// Used only if an object of this type is returned as a trait impl by a method
402 pub(crate) extern "C" fn Quantity_free_void(this_ptr: *mut c_void) {
403         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeQuantity) };
404 }
405 #[allow(unused)]
406 impl Quantity {
407         pub(crate) fn get_native_ref(&self) -> &'static nativeQuantity {
408                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
409         }
410         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeQuantity {
411                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
412         }
413         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
414         pub(crate) fn take_inner(mut self) -> *mut nativeQuantity {
415                 assert!(self.is_owned);
416                 let ret = ObjOps::untweak_ptr(self.inner);
417                 self.inner = core::ptr::null_mut();
418                 ret
419         }
420 }
421 impl Clone for Quantity {
422         fn clone(&self) -> Self {
423                 Self {
424                         inner: if <*mut nativeQuantity>::is_null(self.inner) { core::ptr::null_mut() } else {
425                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
426                         is_owned: true,
427                 }
428         }
429 }
430 #[allow(unused)]
431 /// Used only if an object of this type is returned as a trait impl by a method
432 pub(crate) extern "C" fn Quantity_clone_void(this_ptr: *const c_void) -> *mut c_void {
433         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeQuantity)).clone() })) as *mut c_void
434 }
435 #[no_mangle]
436 /// Creates a copy of the Quantity
437 pub extern "C" fn Quantity_clone(orig: &Quantity) -> Quantity {
438         orig.clone()
439 }