1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Data structures and encoding for `invoice_request` messages.
11 //! An [`InvoiceRequest`] can be built from a parsed [`Offer`] as an \"offer to be paid\". It is
12 //! typically constructed by a customer and sent to the merchant who had published the corresponding
13 //! offer. The recipient of the request responds with an [`Invoice`].
15 //! For an \"offer for money\" (e.g., refund, ATM withdrawal), where an offer doesn't exist as a
16 //! precursor, see [`Refund`].
18 //! [`Invoice`]: crate::offers::invoice::Invoice
19 //! [`Refund`]: crate::offers::refund::Refund
22 //! extern crate bitcoin;
23 //! extern crate lightning;
25 //! use bitcoin::network::constants::Network;
26 //! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
27 //! use core::convert::Infallible;
28 //! use lightning::ln::features::OfferFeatures;
29 //! use lightning::offers::offer::Offer;
30 //! use lightning::util::ser::Writeable;
32 //! # fn parse() -> Result<(), lightning::offers::parse::ParseError> {
33 //! let secp_ctx = Secp256k1::new();
34 //! let keys = KeyPair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32])?);
35 //! let pubkey = PublicKey::from(keys);
36 //! let mut buffer = Vec::new();
38 //! \"lno1qcp4256ypq\"
39 //! .parse::<Offer>()?
40 //! .request_invoice(vec![42; 64], pubkey)?
41 //! .chain(Network::Testnet)?
42 //! .amount_msats(1000)?
44 //! .payer_note(\"foo\".to_string())
46 //! .sign::<_, Infallible>(|digest| Ok(secp_ctx.sign_schnorr_no_aux_rand(digest, &keys)))
47 //! .expect(\"failed verifying signature\")
48 //! .write(&mut buffer)
54 use alloc::str::FromStr;
55 use core::ffi::c_void;
56 use core::convert::Infallible;
57 use bitcoin::hashes::Hash;
58 use crate::c_types::*;
59 #[cfg(feature="no-std")]
60 use alloc::{vec::Vec, boxed::Box};
63 use lightning::offers::invoice_request::UnsignedInvoiceRequest as nativeUnsignedInvoiceRequestImport;
64 pub(crate) type nativeUnsignedInvoiceRequest = nativeUnsignedInvoiceRequestImport<'static>;
66 /// A semantically valid [`InvoiceRequest`] that hasn't been signed.
69 pub struct UnsignedInvoiceRequest {
70 /// A pointer to the opaque Rust object.
72 /// Nearly everywhere, inner must be non-null, however in places where
73 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
74 pub inner: *mut nativeUnsignedInvoiceRequest,
75 /// Indicates that this is the only struct which contains the same pointer.
77 /// Rust functions which take ownership of an object provided via an argument require
78 /// this to be true and invalidate the object pointed to by inner.
82 impl Drop for UnsignedInvoiceRequest {
84 if self.is_owned && !<*mut nativeUnsignedInvoiceRequest>::is_null(self.inner) {
85 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
89 /// Frees any resources used by the UnsignedInvoiceRequest, if is_owned is set and inner is non-NULL.
91 pub extern "C" fn UnsignedInvoiceRequest_free(this_obj: UnsignedInvoiceRequest) { }
93 /// Used only if an object of this type is returned as a trait impl by a method
94 pub(crate) extern "C" fn UnsignedInvoiceRequest_free_void(this_ptr: *mut c_void) {
95 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeUnsignedInvoiceRequest) };
98 impl UnsignedInvoiceRequest {
99 pub(crate) fn get_native_ref(&self) -> &'static nativeUnsignedInvoiceRequest {
100 unsafe { &*ObjOps::untweak_ptr(self.inner) }
102 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeUnsignedInvoiceRequest {
103 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
105 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
106 pub(crate) fn take_inner(mut self) -> *mut nativeUnsignedInvoiceRequest {
107 assert!(self.is_owned);
108 let ret = ObjOps::untweak_ptr(self.inner);
109 self.inner = core::ptr::null_mut();
114 use lightning::offers::invoice_request::InvoiceRequest as nativeInvoiceRequestImport;
115 pub(crate) type nativeInvoiceRequest = nativeInvoiceRequestImport;
117 /// An `InvoiceRequest` is a request for an [`Invoice`] formulated from an [`Offer`].
119 /// An offer may provide choices such as quantity, amount, chain, features, etc. An invoice request
120 /// specifies these such that its recipient can send an invoice for payment.
122 /// [`Invoice`]: crate::offers::invoice::Invoice
123 /// [`Offer`]: crate::offers::offer::Offer
126 pub struct InvoiceRequest {
127 /// A pointer to the opaque Rust object.
129 /// Nearly everywhere, inner must be non-null, however in places where
130 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
131 pub inner: *mut nativeInvoiceRequest,
132 /// Indicates that this is the only struct which contains the same pointer.
134 /// Rust functions which take ownership of an object provided via an argument require
135 /// this to be true and invalidate the object pointed to by inner.
139 impl Drop for InvoiceRequest {
141 if self.is_owned && !<*mut nativeInvoiceRequest>::is_null(self.inner) {
142 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
146 /// Frees any resources used by the InvoiceRequest, if is_owned is set and inner is non-NULL.
148 pub extern "C" fn InvoiceRequest_free(this_obj: InvoiceRequest) { }
150 /// Used only if an object of this type is returned as a trait impl by a method
151 pub(crate) extern "C" fn InvoiceRequest_free_void(this_ptr: *mut c_void) {
152 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInvoiceRequest) };
155 impl InvoiceRequest {
156 pub(crate) fn get_native_ref(&self) -> &'static nativeInvoiceRequest {
157 unsafe { &*ObjOps::untweak_ptr(self.inner) }
159 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInvoiceRequest {
160 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
162 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
163 pub(crate) fn take_inner(mut self) -> *mut nativeInvoiceRequest {
164 assert!(self.is_owned);
165 let ret = ObjOps::untweak_ptr(self.inner);
166 self.inner = core::ptr::null_mut();
170 impl Clone for InvoiceRequest {
171 fn clone(&self) -> Self {
173 inner: if <*mut nativeInvoiceRequest>::is_null(self.inner) { core::ptr::null_mut() } else {
174 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
180 /// Used only if an object of this type is returned as a trait impl by a method
181 pub(crate) extern "C" fn InvoiceRequest_clone_void(this_ptr: *const c_void) -> *mut c_void {
182 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInvoiceRequest)).clone() })) as *mut c_void
185 /// Creates a copy of the InvoiceRequest
186 pub extern "C" fn InvoiceRequest_clone(orig: &InvoiceRequest) -> InvoiceRequest {
189 /// An unpredictable series of bytes, typically containing information about the derivation of
192 /// [`payer_id`]: Self::payer_id
195 pub extern "C" fn InvoiceRequest_metadata(this_arg: &crate::lightning::offers::invoice_request::InvoiceRequest) -> crate::c_types::u8slice {
196 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.metadata();
197 let mut local_ret = crate::c_types::u8slice::from_slice(ret);
201 /// A chain from [`Offer::chains`] that the offer is valid for.
204 pub extern "C" fn InvoiceRequest_chain(this_arg: &crate::lightning::offers::invoice_request::InvoiceRequest) -> crate::c_types::ThirtyTwoBytes {
205 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.chain();
206 crate::c_types::ThirtyTwoBytes { data: ret.to_bytes() }
209 /// The amount to pay in msats (i.e., the minimum lightning-payable unit for [`chain`]), which
210 /// must be greater than or equal to [`Offer::amount`], converted if necessary.
212 /// [`chain`]: Self::chain
215 pub extern "C" fn InvoiceRequest_amount_msats(this_arg: &crate::lightning::offers::invoice_request::InvoiceRequest) -> crate::c_types::derived::COption_u64Z {
216 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.amount_msats();
217 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() }) };
221 /// Features pertaining to requesting an invoice.
224 pub extern "C" fn InvoiceRequest_features(this_arg: &crate::lightning::offers::invoice_request::InvoiceRequest) -> crate::lightning::ln::features::InvoiceRequestFeatures {
225 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.features();
226 crate::lightning::ln::features::InvoiceRequestFeatures { inner: unsafe { ObjOps::nonnull_ptr_to_inner((ret as *const lightning::ln::features::InvoiceRequestFeatures<>) as *mut _) }, is_owned: false }
229 /// The quantity of the offer's item conforming to [`Offer::is_valid_quantity`].
232 pub extern "C" fn InvoiceRequest_quantity(this_arg: &crate::lightning::offers::invoice_request::InvoiceRequest) -> crate::c_types::derived::COption_u64Z {
233 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.quantity();
234 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() }) };
238 /// A possibly transient pubkey used to sign the invoice request.
241 pub extern "C" fn InvoiceRequest_payer_id(this_arg: &crate::lightning::offers::invoice_request::InvoiceRequest) -> crate::c_types::PublicKey {
242 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.payer_id();
243 crate::c_types::PublicKey::from_rust(&ret)
246 /// A payer-provided note which will be seen by the recipient and reflected back in the invoice
249 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
252 pub extern "C" fn InvoiceRequest_payer_note(this_arg: &crate::lightning::offers::invoice_request::InvoiceRequest) -> crate::lightning::util::string::PrintableString {
253 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.payer_note();
254 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 };
259 /// Serialize the InvoiceRequest object into a byte array which can be read by InvoiceRequest_read
260 pub extern "C" fn InvoiceRequest_write(obj: &crate::lightning::offers::invoice_request::InvoiceRequest) -> crate::c_types::derived::CVec_u8Z {
261 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
264 pub(crate) extern "C" fn InvoiceRequest_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
265 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInvoiceRequest) })