Update CI/Cargo.toml references to 0.0.122
[ldk-c-bindings] / lightning-c-bindings / src / lightning / ln / script.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 //! Abstractions for scripts used in the Lightning Network.
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::script::ShutdownScript as nativeShutdownScriptImport;
22 pub(crate) type nativeShutdownScript = nativeShutdownScriptImport;
23
24 /// A script pubkey for shutting down a channel as defined by [BOLT #2].
25 ///
26 /// [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md
27 #[must_use]
28 #[repr(C)]
29 pub struct ShutdownScript {
30         /// A pointer to the opaque Rust object.
31
32         /// Nearly everywhere, inner must be non-null, however in places where
33         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
34         pub inner: *mut nativeShutdownScript,
35         /// Indicates that this is the only struct which contains the same pointer.
36
37         /// Rust functions which take ownership of an object provided via an argument require
38         /// this to be true and invalidate the object pointed to by inner.
39         pub is_owned: bool,
40 }
41
42 impl Drop for ShutdownScript {
43         fn drop(&mut self) {
44                 if self.is_owned && !<*mut nativeShutdownScript>::is_null(self.inner) {
45                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
46                 }
47         }
48 }
49 /// Frees any resources used by the ShutdownScript, if is_owned is set and inner is non-NULL.
50 #[no_mangle]
51 pub extern "C" fn ShutdownScript_free(this_obj: ShutdownScript) { }
52 #[allow(unused)]
53 /// Used only if an object of this type is returned as a trait impl by a method
54 pub(crate) extern "C" fn ShutdownScript_free_void(this_ptr: *mut c_void) {
55         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeShutdownScript) };
56 }
57 #[allow(unused)]
58 impl ShutdownScript {
59         pub(crate) fn get_native_ref(&self) -> &'static nativeShutdownScript {
60                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
61         }
62         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeShutdownScript {
63                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
64         }
65         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
66         pub(crate) fn take_inner(mut self) -> *mut nativeShutdownScript {
67                 assert!(self.is_owned);
68                 let ret = ObjOps::untweak_ptr(self.inner);
69                 self.inner = core::ptr::null_mut();
70                 ret
71         }
72 }
73 impl Clone for ShutdownScript {
74         fn clone(&self) -> Self {
75                 Self {
76                         inner: if <*mut nativeShutdownScript>::is_null(self.inner) { core::ptr::null_mut() } else {
77                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
78                         is_owned: true,
79                 }
80         }
81 }
82 #[allow(unused)]
83 /// Used only if an object of this type is returned as a trait impl by a method
84 pub(crate) extern "C" fn ShutdownScript_clone_void(this_ptr: *const c_void) -> *mut c_void {
85         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeShutdownScript)).clone() })) as *mut c_void
86 }
87 #[no_mangle]
88 /// Creates a copy of the ShutdownScript
89 pub extern "C" fn ShutdownScript_clone(orig: &ShutdownScript) -> ShutdownScript {
90         orig.clone()
91 }
92 /// Checks if two ShutdownScripts contain equal inner contents.
93 /// This ignores pointers and is_owned flags and looks at the values in fields.
94 /// Two objects with NULL inner values will be considered "equal" here.
95 #[no_mangle]
96 pub extern "C" fn ShutdownScript_eq(a: &ShutdownScript, b: &ShutdownScript) -> bool {
97         if a.inner == b.inner { return true; }
98         if a.inner.is_null() || b.inner.is_null() { return false; }
99         if a.get_native_ref() == b.get_native_ref() { true } else { false }
100 }
101
102 use lightning::ln::script::InvalidShutdownScript as nativeInvalidShutdownScriptImport;
103 pub(crate) type nativeInvalidShutdownScript = nativeInvalidShutdownScriptImport;
104
105 /// An error occurring when converting from [`ScriptBuf`] to [`ShutdownScript`].
106 #[must_use]
107 #[repr(C)]
108 pub struct InvalidShutdownScript {
109         /// A pointer to the opaque Rust object.
110
111         /// Nearly everywhere, inner must be non-null, however in places where
112         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
113         pub inner: *mut nativeInvalidShutdownScript,
114         /// Indicates that this is the only struct which contains the same pointer.
115
116         /// Rust functions which take ownership of an object provided via an argument require
117         /// this to be true and invalidate the object pointed to by inner.
118         pub is_owned: bool,
119 }
120
121 impl Drop for InvalidShutdownScript {
122         fn drop(&mut self) {
123                 if self.is_owned && !<*mut nativeInvalidShutdownScript>::is_null(self.inner) {
124                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
125                 }
126         }
127 }
128 /// Frees any resources used by the InvalidShutdownScript, if is_owned is set and inner is non-NULL.
129 #[no_mangle]
130 pub extern "C" fn InvalidShutdownScript_free(this_obj: InvalidShutdownScript) { }
131 #[allow(unused)]
132 /// Used only if an object of this type is returned as a trait impl by a method
133 pub(crate) extern "C" fn InvalidShutdownScript_free_void(this_ptr: *mut c_void) {
134         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInvalidShutdownScript) };
135 }
136 #[allow(unused)]
137 impl InvalidShutdownScript {
138         pub(crate) fn get_native_ref(&self) -> &'static nativeInvalidShutdownScript {
139                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
140         }
141         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInvalidShutdownScript {
142                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
143         }
144         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
145         pub(crate) fn take_inner(mut self) -> *mut nativeInvalidShutdownScript {
146                 assert!(self.is_owned);
147                 let ret = ObjOps::untweak_ptr(self.inner);
148                 self.inner = core::ptr::null_mut();
149                 ret
150         }
151 }
152 /// The script that did not meet the requirements from [BOLT #2].
153 ///
154 /// [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md
155 #[no_mangle]
156 pub extern "C" fn InvalidShutdownScript_get_script(this_ptr: &InvalidShutdownScript) -> crate::c_types::derived::CVec_u8Z {
157         let mut inner_val = &mut this_ptr.get_native_mut_ref().script;
158         inner_val.as_bytes().to_vec().into()
159 }
160 /// The script that did not meet the requirements from [BOLT #2].
161 ///
162 /// [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md
163 #[no_mangle]
164 pub extern "C" fn InvalidShutdownScript_set_script(this_ptr: &mut InvalidShutdownScript, mut val: crate::c_types::derived::CVec_u8Z) {
165         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.script = ::bitcoin::blockdata::script::ScriptBuf::from(val.into_rust());
166 }
167 /// Constructs a new InvalidShutdownScript given each field
168 #[must_use]
169 #[no_mangle]
170 pub extern "C" fn InvalidShutdownScript_new(mut script_arg: crate::c_types::derived::CVec_u8Z) -> InvalidShutdownScript {
171         InvalidShutdownScript { inner: ObjOps::heap_alloc(nativeInvalidShutdownScript {
172                 script: ::bitcoin::blockdata::script::ScriptBuf::from(script_arg.into_rust()),
173         }), is_owned: true }
174 }
175 impl Clone for InvalidShutdownScript {
176         fn clone(&self) -> Self {
177                 Self {
178                         inner: if <*mut nativeInvalidShutdownScript>::is_null(self.inner) { core::ptr::null_mut() } else {
179                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
180                         is_owned: true,
181                 }
182         }
183 }
184 #[allow(unused)]
185 /// Used only if an object of this type is returned as a trait impl by a method
186 pub(crate) extern "C" fn InvalidShutdownScript_clone_void(this_ptr: *const c_void) -> *mut c_void {
187         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeInvalidShutdownScript)).clone() })) as *mut c_void
188 }
189 #[no_mangle]
190 /// Creates a copy of the InvalidShutdownScript
191 pub extern "C" fn InvalidShutdownScript_clone(orig: &InvalidShutdownScript) -> InvalidShutdownScript {
192         orig.clone()
193 }
194 /// Get a string which allows debug introspection of a InvalidShutdownScript object
195 pub extern "C" fn InvalidShutdownScript_debug_str_void(o: *const c_void) -> Str {
196         alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::script::InvalidShutdownScript }).into()}
197 #[no_mangle]
198 /// Serialize the ShutdownScript object into a byte array which can be read by ShutdownScript_read
199 pub extern "C" fn ShutdownScript_write(obj: &crate::lightning::ln::script::ShutdownScript) -> crate::c_types::derived::CVec_u8Z {
200         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
201 }
202 #[allow(unused)]
203 pub(crate) extern "C" fn ShutdownScript_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
204         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeShutdownScript) })
205 }
206 #[no_mangle]
207 /// Read a ShutdownScript from a byte array, created by ShutdownScript_write
208 pub extern "C" fn ShutdownScript_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ShutdownScriptDecodeErrorZ {
209         let res: Result<lightning::ln::script::ShutdownScript, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
210         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { 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() };
211         local_res
212 }
213 /// Generates a P2WPKH script pubkey from the given [`WPubkeyHash`].
214 #[must_use]
215 #[no_mangle]
216 pub extern "C" fn ShutdownScript_new_p2wpkh(pubkey_hash: *const [u8; 20]) -> crate::lightning::ln::script::ShutdownScript {
217         let mut ret = lightning::ln::script::ShutdownScript::new_p2wpkh(&bitcoin::hash_types::WPubkeyHash::from_raw_hash(bitcoin::hashes::Hash::from_byte_array(unsafe { *pubkey_hash }.clone())));
218         crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(ret), is_owned: true }
219 }
220
221 /// Generates a P2WSH script pubkey from the given [`WScriptHash`].
222 #[must_use]
223 #[no_mangle]
224 pub extern "C" fn ShutdownScript_new_p2wsh(script_hash: *const [u8; 32]) -> crate::lightning::ln::script::ShutdownScript {
225         let mut ret = lightning::ln::script::ShutdownScript::new_p2wsh(&bitcoin::hash_types::WScriptHash::from_raw_hash(bitcoin::hashes::Hash::from_byte_array(unsafe { *script_hash }.clone())));
226         crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(ret), is_owned: true }
227 }
228
229 /// Generates a witness script pubkey from the given segwit version and program.
230 ///
231 /// Note for version-zero witness scripts you must use [`ShutdownScript::new_p2wpkh`] or
232 /// [`ShutdownScript::new_p2wsh`] instead.
233 ///
234 /// # Errors
235 ///
236 /// This function may return an error if `program` is invalid for the segwit `version`.
237 #[must_use]
238 #[no_mangle]
239 pub extern "C" fn ShutdownScript_new_witness_program(mut witness_program: crate::c_types::WitnessProgram) -> crate::c_types::derived::CResult_ShutdownScriptInvalidShutdownScriptZ {
240         let mut ret = lightning::ln::script::ShutdownScript::new_witness_program(&witness_program.into_bitcoin());
241         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::script::InvalidShutdownScript { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
242         local_ret
243 }
244
245 /// Converts the shutdown script into the underlying [`ScriptBuf`].
246 #[must_use]
247 #[no_mangle]
248 pub extern "C" fn ShutdownScript_into_inner(mut this_arg: crate::lightning::ln::script::ShutdownScript) -> crate::c_types::derived::CVec_u8Z {
249         let mut ret = (*unsafe { Box::from_raw(this_arg.take_inner()) }).into_inner();
250         ret.to_bytes().into()
251 }
252
253 /// Returns the [`PublicKey`] used for a P2WPKH shutdown script if constructed directly from it.
254 ///
255 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
256 #[must_use]
257 #[no_mangle]
258 pub extern "C" fn ShutdownScript_as_legacy_pubkey(this_arg: &crate::lightning::ln::script::ShutdownScript) -> crate::c_types::PublicKey {
259         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.as_legacy_pubkey();
260         let mut local_ret = if ret.is_none() { crate::c_types::PublicKey::null() } else {  { crate::c_types::PublicKey::from_rust(&(ret.unwrap())) } };
261         local_ret
262 }
263
264 /// Returns whether the shutdown script is compatible with the features as defined by BOLT #2.
265 ///
266 /// Specifically, checks for compliance with feature `option_shutdown_anysegwit`.
267 #[must_use]
268 #[no_mangle]
269 pub extern "C" fn ShutdownScript_is_compatible(this_arg: &crate::lightning::ln::script::ShutdownScript, features: &crate::lightning::ln::features::InitFeatures) -> bool {
270         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.is_compatible(features.get_native_ref());
271         ret
272 }
273