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 //! Tagged hashes for use in signature calculation and verification.
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};
21 use lightning::offers::merkle::TaggedHash as nativeTaggedHashImport;
22 pub(crate) type nativeTaggedHash = nativeTaggedHashImport;
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].
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
31 pub struct TaggedHash {
32 /// A pointer to the opaque Rust object.
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.
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.
44 impl Drop for TaggedHash {
46 if self.is_owned && !<*mut nativeTaggedHash>::is_null(self.inner) {
47 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
51 /// Frees any resources used by the TaggedHash, if is_owned is set and inner is non-NULL.
53 pub extern "C" fn TaggedHash_free(this_obj: TaggedHash) { }
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) };
61 pub(crate) fn get_native_ref(&self) -> &'static nativeTaggedHash {
62 unsafe { &*ObjOps::untweak_ptr(self.inner) }
64 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeTaggedHash {
65 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
75 impl Clone for TaggedHash {
76 fn clone(&self) -> 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()) },
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
90 /// Creates a copy of the TaggedHash
91 pub extern "C" fn TaggedHash_clone(orig: &TaggedHash) -> TaggedHash {
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.
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();
105 /// Returns the tag used in the tagged hash.
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();
113 /// Returns the merkle root used in the tagged hash.
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() }
121 /// Error when signing messages.
126 /// User-defined error when signing the message.
128 /// Error when verifying the produced signature using the given pubkey.
130 crate::c_types::Secp256k1Error),
132 use lightning::offers::merkle::SignError as SignErrorImport;
133 pub(crate) type nativeSignError = SignErrorImport;
137 pub(crate) fn to_native(&self) -> nativeSignError {
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(),
149 pub(crate) fn into_native(self) -> nativeSignError {
151 SignError::Signing => nativeSignError::Signing,
152 SignError::Verification (mut a, ) => {
153 nativeSignError::Verification (
160 pub(crate) fn from_native(native: &SignErrorImport) -> Self {
161 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeSignError) };
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),
173 pub(crate) fn native_into(native: nativeSignError) -> Self {
175 nativeSignError::Signing => SignError::Signing,
176 nativeSignError::Verification (mut a, ) => {
177 SignError::Verification (
178 crate::c_types::Secp256k1Error::from_rust(a),
184 /// Frees any resources used by the SignError
186 pub extern "C" fn SignError_free(this_ptr: SignError) { }
187 /// Creates a copy of the SignError
189 pub extern "C" fn SignError_clone(orig: &SignError) -> SignError {
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
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) };
203 /// Utility method to constructs a new Signing-variant SignError
204 pub extern "C" fn SignError_signing() -> SignError {
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, )
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()}