106169c2d2988ec398c2ef170baae9b04f854adb
[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 }