Add auto-generated bindings of `lightning_transaction_sync`
[ldk-c-bindings] / lightning-c-bindings / src / lightning / ln / types.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 //! Various wrapper types (most around 32-byte arrays) for use in lightning.
10
11 use alloc::str::FromStr;
12 use alloc::string::String;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
19
20
21 use lightning::ln::types::ChannelId as nativeChannelIdImport;
22 pub(crate) type nativeChannelId = nativeChannelIdImport;
23
24 /// A unique 32-byte identifier for a channel.
25 /// Depending on how the ID is generated, several varieties are distinguished
26 /// (but all are stored as 32 bytes):
27 ///   _v1_ and _temporary_.
28 /// A _v1_ channel ID is generated based on funding tx outpoint (txid & index).
29 /// A _temporary_ ID is generated randomly.
30 /// (Later revocation-point-based _v2_ is a possibility.)
31 /// The variety (context) is not stored, it is relevant only at creation.
32 #[must_use]
33 #[repr(C)]
34 pub struct ChannelId {
35         /// A pointer to the opaque Rust object.
36
37         /// Nearly everywhere, inner must be non-null, however in places where
38         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
39         pub inner: *mut nativeChannelId,
40         /// Indicates that this is the only struct which contains the same pointer.
41
42         /// Rust functions which take ownership of an object provided via an argument require
43         /// this to be true and invalidate the object pointed to by inner.
44         pub is_owned: bool,
45 }
46
47 impl Drop for ChannelId {
48         fn drop(&mut self) {
49                 if self.is_owned && !<*mut nativeChannelId>::is_null(self.inner) {
50                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
51                 }
52         }
53 }
54 /// Frees any resources used by the ChannelId, if is_owned is set and inner is non-NULL.
55 #[no_mangle]
56 pub extern "C" fn ChannelId_free(this_obj: ChannelId) { }
57 #[allow(unused)]
58 /// Used only if an object of this type is returned as a trait impl by a method
59 pub(crate) extern "C" fn ChannelId_free_void(this_ptr: *mut c_void) {
60         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeChannelId) };
61 }
62 #[allow(unused)]
63 impl ChannelId {
64         pub(crate) fn get_native_ref(&self) -> &'static nativeChannelId {
65                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
66         }
67         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeChannelId {
68                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
69         }
70         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
71         pub(crate) fn take_inner(mut self) -> *mut nativeChannelId {
72                 assert!(self.is_owned);
73                 let ret = ObjOps::untweak_ptr(self.inner);
74                 self.inner = core::ptr::null_mut();
75                 ret
76         }
77 }
78 #[no_mangle]
79 pub extern "C" fn ChannelId_get_a(this_ptr: &ChannelId) -> *const [u8; 32] {
80         let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
81         inner_val
82 }
83 #[no_mangle]
84 pub extern "C" fn ChannelId_set_a(this_ptr: &mut ChannelId, mut val: crate::c_types::ThirtyTwoBytes) {
85         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.data;
86 }
87 /// Constructs a new ChannelId given each field
88 #[must_use]
89 #[no_mangle]
90 pub extern "C" fn ChannelId_new(mut a_arg: crate::c_types::ThirtyTwoBytes) -> ChannelId {
91         ChannelId { inner: ObjOps::heap_alloc(lightning::ln::types::ChannelId (
92                 a_arg.data,
93         )), is_owned: true }
94 }
95 impl Clone for ChannelId {
96         fn clone(&self) -> Self {
97                 Self {
98                         inner: if <*mut nativeChannelId>::is_null(self.inner) { core::ptr::null_mut() } else {
99                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
100                         is_owned: true,
101                 }
102         }
103 }
104 #[allow(unused)]
105 /// Used only if an object of this type is returned as a trait impl by a method
106 pub(crate) extern "C" fn ChannelId_clone_void(this_ptr: *const c_void) -> *mut c_void {
107         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeChannelId)).clone() })) as *mut c_void
108 }
109 #[no_mangle]
110 /// Creates a copy of the ChannelId
111 pub extern "C" fn ChannelId_clone(orig: &ChannelId) -> ChannelId {
112         orig.clone()
113 }
114 /// Get a string which allows debug introspection of a ChannelId object
115 pub extern "C" fn ChannelId_debug_str_void(o: *const c_void) -> Str {
116         alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::types::ChannelId }).into()}
117 /// Checks if two ChannelIds contain equal inner contents.
118 /// This ignores pointers and is_owned flags and looks at the values in fields.
119 /// Two objects with NULL inner values will be considered "equal" here.
120 #[no_mangle]
121 pub extern "C" fn ChannelId_eq(a: &ChannelId, b: &ChannelId) -> bool {
122         if a.inner == b.inner { return true; }
123         if a.inner.is_null() || b.inner.is_null() { return false; }
124         if a.get_native_ref() == b.get_native_ref() { true } else { false }
125 }
126 /// Generates a non-cryptographic 64-bit hash of the ChannelId.
127 #[no_mangle]
128 pub extern "C" fn ChannelId_hash(o: &ChannelId) -> u64 {
129         if o.inner.is_null() { return 0; }
130         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
131         #[allow(deprecated)]
132         let mut hasher = core::hash::SipHasher::new();
133         core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
134         core::hash::Hasher::finish(&hasher)
135 }
136 /// Create _v1_ channel ID based on a funding TX ID and output index
137 #[must_use]
138 #[no_mangle]
139 pub extern "C" fn ChannelId_v1_from_funding_txid(txid: *const [u8; 32], mut output_index: u16) -> crate::lightning::ln::types::ChannelId {
140         let mut ret = lightning::ln::types::ChannelId::v1_from_funding_txid(unsafe { &*txid}, output_index);
141         crate::lightning::ln::types::ChannelId { inner: ObjOps::heap_alloc(ret), is_owned: true }
142 }
143
144 /// Create _v1_ channel ID from a funding tx outpoint
145 #[must_use]
146 #[no_mangle]
147 pub extern "C" fn ChannelId_v1_from_funding_outpoint(mut outpoint: crate::lightning::chain::transaction::OutPoint) -> crate::lightning::ln::types::ChannelId {
148         let mut ret = lightning::ln::types::ChannelId::v1_from_funding_outpoint(*unsafe { Box::from_raw(outpoint.take_inner()) });
149         crate::lightning::ln::types::ChannelId { inner: ObjOps::heap_alloc(ret), is_owned: true }
150 }
151
152 /// Create a _temporary_ channel ID randomly, based on an entropy source.
153 #[must_use]
154 #[no_mangle]
155 pub extern "C" fn ChannelId_temporary_from_entropy_source(entropy_source: &crate::lightning::sign::EntropySource) -> crate::lightning::ln::types::ChannelId {
156         let mut ret = lightning::ln::types::ChannelId::temporary_from_entropy_source(entropy_source);
157         crate::lightning::ln::types::ChannelId { inner: ObjOps::heap_alloc(ret), is_owned: true }
158 }
159
160 /// Generic constructor; create a new channel ID from the provided data.
161 /// Use a more specific `*_from_*` constructor when possible.
162 #[must_use]
163 #[no_mangle]
164 pub extern "C" fn ChannelId_from_bytes(mut data: crate::c_types::ThirtyTwoBytes) -> crate::lightning::ln::types::ChannelId {
165         let mut ret = lightning::ln::types::ChannelId::from_bytes(data.data);
166         crate::lightning::ln::types::ChannelId { inner: ObjOps::heap_alloc(ret), is_owned: true }
167 }
168
169 /// Create a channel ID consisting of all-zeros data (e.g. when uninitialized or a placeholder).
170 #[must_use]
171 #[no_mangle]
172 pub extern "C" fn ChannelId_new_zero() -> crate::lightning::ln::types::ChannelId {
173         let mut ret = lightning::ln::types::ChannelId::new_zero();
174         crate::lightning::ln::types::ChannelId { inner: ObjOps::heap_alloc(ret), is_owned: true }
175 }
176
177 /// Check whether ID is consisting of all zeros (uninitialized)
178 #[must_use]
179 #[no_mangle]
180 pub extern "C" fn ChannelId_is_zero(this_arg: &crate::lightning::ln::types::ChannelId) -> bool {
181         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_zero();
182         ret
183 }
184
185 /// Create _v2_ channel ID by concatenating the holder revocation basepoint with the counterparty
186 /// revocation basepoint and hashing the result. The basepoints will be concatenated in increasing
187 /// sorted order.
188 #[must_use]
189 #[no_mangle]
190 pub extern "C" fn ChannelId_v2_from_revocation_basepoints(ours: &crate::lightning::ln::channel_keys::RevocationBasepoint, theirs: &crate::lightning::ln::channel_keys::RevocationBasepoint) -> crate::lightning::ln::types::ChannelId {
191         let mut ret = lightning::ln::types::ChannelId::v2_from_revocation_basepoints(ours.get_native_ref(), theirs.get_native_ref());
192         crate::lightning::ln::types::ChannelId { inner: ObjOps::heap_alloc(ret), is_owned: true }
193 }
194
195 /// Create temporary _v2_ channel ID by concatenating a zeroed out basepoint with the holder
196 /// revocation basepoint and hashing the result.
197 #[must_use]
198 #[no_mangle]
199 pub extern "C" fn ChannelId_temporary_v2_from_revocation_basepoint(our_revocation_basepoint: &crate::lightning::ln::channel_keys::RevocationBasepoint) -> crate::lightning::ln::types::ChannelId {
200         let mut ret = lightning::ln::types::ChannelId::temporary_v2_from_revocation_basepoint(our_revocation_basepoint.get_native_ref());
201         crate::lightning::ln::types::ChannelId { inner: ObjOps::heap_alloc(ret), is_owned: true }
202 }
203
204 #[no_mangle]
205 /// Serialize the ChannelId object into a byte array which can be read by ChannelId_read
206 pub extern "C" fn ChannelId_write(obj: &crate::lightning::ln::types::ChannelId) -> crate::c_types::derived::CVec_u8Z {
207         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
208 }
209 #[allow(unused)]
210 pub(crate) extern "C" fn ChannelId_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
211         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeChannelId) })
212 }
213 #[no_mangle]
214 /// Read a ChannelId from a byte array, created by ChannelId_write
215 pub extern "C" fn ChannelId_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ChannelIdDecodeErrorZ {
216         let res: Result<lightning::ln::types::ChannelId, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
217         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::types::ChannelId { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
218         local_res
219 }