Pin compiler_builtins to 0.1.109 when building std
[ldk-c-bindings] / lightning-c-bindings / src / lightning / offers / merkle.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 //! Tagged hashes for use in signature calculation and verification.
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::offers::merkle::TaggedHash as nativeTaggedHashImport;
22 pub(crate) type nativeTaggedHash = nativeTaggedHashImport;
23
24 /// A hash for use in a specific context by tweaking with a context-dependent tag as per [BIP 340]
25 /// and computed over the merkle root of a TLV stream to sign as defined in [BOLT 12].
26 ///
27 /// [BIP 340]: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
28 /// [BOLT 12]: https://github.com/rustyrussell/lightning-rfc/blob/guilt/offers/12-offer-encoding.md#signature-calculation
29 #[must_use]
30 #[repr(C)]
31 pub struct TaggedHash {
32         /// A pointer to the opaque Rust object.
33
34         /// Nearly everywhere, inner must be non-null, however in places where
35         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
36         pub inner: *mut nativeTaggedHash,
37         /// Indicates that this is the only struct which contains the same pointer.
38
39         /// Rust functions which take ownership of an object provided via an argument require
40         /// this to be true and invalidate the object pointed to by inner.
41         pub is_owned: bool,
42 }
43
44 impl Drop for TaggedHash {
45         fn drop(&mut self) {
46                 if self.is_owned && !<*mut nativeTaggedHash>::is_null(self.inner) {
47                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
48                 }
49         }
50 }
51 /// Frees any resources used by the TaggedHash, if is_owned is set and inner is non-NULL.
52 #[no_mangle]
53 pub extern "C" fn TaggedHash_free(this_obj: TaggedHash) { }
54 #[allow(unused)]
55 /// Used only if an object of this type is returned as a trait impl by a method
56 pub(crate) extern "C" fn TaggedHash_free_void(this_ptr: *mut c_void) {
57         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeTaggedHash) };
58 }
59 #[allow(unused)]
60 impl TaggedHash {
61         pub(crate) fn get_native_ref(&self) -> &'static nativeTaggedHash {
62                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
63         }
64         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeTaggedHash {
65                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
66         }
67         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
68         pub(crate) fn take_inner(mut self) -> *mut nativeTaggedHash {
69                 assert!(self.is_owned);
70                 let ret = ObjOps::untweak_ptr(self.inner);
71                 self.inner = core::ptr::null_mut();
72                 ret
73         }
74 }
75 impl Clone for TaggedHash {
76         fn clone(&self) -> Self {
77                 Self {
78                         inner: if <*mut nativeTaggedHash>::is_null(self.inner) { core::ptr::null_mut() } else {
79                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
80                         is_owned: true,
81                 }
82         }
83 }
84 #[allow(unused)]
85 /// Used only if an object of this type is returned as a trait impl by a method
86 pub(crate) extern "C" fn TaggedHash_clone_void(this_ptr: *const c_void) -> *mut c_void {
87         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeTaggedHash)).clone() })) as *mut c_void
88 }
89 #[no_mangle]
90 /// Creates a copy of the TaggedHash
91 pub extern "C" fn TaggedHash_clone(orig: &TaggedHash) -> TaggedHash {
92         orig.clone()
93 }
94 /// Get a string which allows debug introspection of a TaggedHash object
95 pub extern "C" fn TaggedHash_debug_str_void(o: *const c_void) -> Str {
96         alloc::format!("{:?}", unsafe { o as *const crate::lightning::offers::merkle::TaggedHash }).into()}
97 /// Returns the digest to sign.
98 #[must_use]
99 #[no_mangle]
100 pub extern "C" fn TaggedHash_as_digest(this_arg: &crate::lightning::offers::merkle::TaggedHash) -> *const [u8; 32] {
101         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.as_digest();
102         ret.as_ref()
103 }
104
105 /// Returns the tag used in the tagged hash.
106 #[must_use]
107 #[no_mangle]
108 pub extern "C" fn TaggedHash_tag(this_arg: &crate::lightning::offers::merkle::TaggedHash) -> crate::c_types::Str {
109         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.tag();
110         ret.into()
111 }
112
113 /// Returns the merkle root used in the tagged hash.
114 #[must_use]
115 #[no_mangle]
116 pub extern "C" fn TaggedHash_merkle_root(this_arg: &crate::lightning::offers::merkle::TaggedHash) -> crate::c_types::ThirtyTwoBytes {
117         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.merkle_root();
118         crate::c_types::ThirtyTwoBytes { data: *ret.as_ref() }
119 }
120
121 /// Error when signing messages.
122 #[derive(Clone)]
123 #[must_use]
124 #[repr(C)]
125 pub enum SignError {
126         /// User-defined error when signing the message.
127         Signing,
128         /// Error when verifying the produced signature using the given pubkey.
129         Verification(
130                 crate::c_types::Secp256k1Error),
131 }
132 use lightning::offers::merkle::SignError as SignErrorImport;
133 pub(crate) type nativeSignError = SignErrorImport;
134
135 impl SignError {
136         #[allow(unused)]
137         pub(crate) fn to_native(&self) -> nativeSignError {
138                 match self {
139                         SignError::Signing => nativeSignError::Signing,
140                         SignError::Verification (ref a, ) => {
141                                 let mut a_nonref = Clone::clone(a);
142                                 nativeSignError::Verification (
143                                         a_nonref.into_rust(),
144                                 )
145                         },
146                 }
147         }
148         #[allow(unused)]
149         pub(crate) fn into_native(self) -> nativeSignError {
150                 match self {
151                         SignError::Signing => nativeSignError::Signing,
152                         SignError::Verification (mut a, ) => {
153                                 nativeSignError::Verification (
154                                         a.into_rust(),
155                                 )
156                         },
157                 }
158         }
159         #[allow(unused)]
160         pub(crate) fn from_native(native: &SignErrorImport) -> Self {
161                 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeSignError) };
162                 match native {
163                         nativeSignError::Signing => SignError::Signing,
164                         nativeSignError::Verification (ref a, ) => {
165                                 let mut a_nonref = Clone::clone(a);
166                                 SignError::Verification (
167                                         crate::c_types::Secp256k1Error::from_rust(a_nonref),
168                                 )
169                         },
170                 }
171         }
172         #[allow(unused)]
173         pub(crate) fn native_into(native: nativeSignError) -> Self {
174                 match native {
175                         nativeSignError::Signing => SignError::Signing,
176                         nativeSignError::Verification (mut a, ) => {
177                                 SignError::Verification (
178                                         crate::c_types::Secp256k1Error::from_rust(a),
179                                 )
180                         },
181                 }
182         }
183 }
184 /// Frees any resources used by the SignError
185 #[no_mangle]
186 pub extern "C" fn SignError_free(this_ptr: SignError) { }
187 /// Creates a copy of the SignError
188 #[no_mangle]
189 pub extern "C" fn SignError_clone(orig: &SignError) -> SignError {
190         orig.clone()
191 }
192 #[allow(unused)]
193 /// Used only if an object of this type is returned as a trait impl by a method
194 pub(crate) extern "C" fn SignError_clone_void(this_ptr: *const c_void) -> *mut c_void {
195         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const SignError)).clone() })) as *mut c_void
196 }
197 #[allow(unused)]
198 /// Used only if an object of this type is returned as a trait impl by a method
199 pub(crate) extern "C" fn SignError_free_void(this_ptr: *mut c_void) {
200         let _ = unsafe { Box::from_raw(this_ptr as *mut SignError) };
201 }
202 #[no_mangle]
203 /// Utility method to constructs a new Signing-variant SignError
204 pub extern "C" fn SignError_signing() -> SignError {
205         SignError::Signing}
206 #[no_mangle]
207 /// Utility method to constructs a new Verification-variant SignError
208 pub extern "C" fn SignError_verification(a: crate::c_types::Secp256k1Error) -> SignError {
209         SignError::Verification(a, )
210 }
211 /// Get a string which allows debug introspection of a SignError object
212 pub extern "C" fn SignError_debug_str_void(o: *const c_void) -> Str {
213         alloc::format!("{:?}", unsafe { o as *const crate::lightning::offers::merkle::SignError }).into()}