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 //! Keys used to generate commitment transactions.
10 //! See: <https://github.com/lightning/bolts/blob/master/03-transactions.md#keys>
12 use alloc::str::FromStr;
13 use alloc::string::String;
14 use core::ffi::c_void;
15 use core::convert::Infallible;
16 use bitcoin::hashes::Hash;
17 use crate::c_types::*;
18 #[cfg(feature="no-std")]
19 use alloc::{vec::Vec, boxed::Box};
22 use lightning::ln::channel_keys::DelayedPaymentBasepoint as nativeDelayedPaymentBasepointImport;
23 pub(crate) type nativeDelayedPaymentBasepoint = nativeDelayedPaymentBasepointImport;
25 /// Base key used in conjunction with a `per_commitment_point` to generate a [`DelayedPaymentKey`].
27 /// The delayed payment key is used to pay the commitment state broadcaster their
28 /// non-HTLC-encumbered funds after a delay to give their counterparty a chance to punish if the
29 /// state broadcasted was previously revoked.
32 pub struct DelayedPaymentBasepoint {
33 /// A pointer to the opaque Rust object.
35 /// Nearly everywhere, inner must be non-null, however in places where
36 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
37 pub inner: *mut nativeDelayedPaymentBasepoint,
38 /// Indicates that this is the only struct which contains the same pointer.
40 /// Rust functions which take ownership of an object provided via an argument require
41 /// this to be true and invalidate the object pointed to by inner.
45 impl Drop for DelayedPaymentBasepoint {
47 if self.is_owned && !<*mut nativeDelayedPaymentBasepoint>::is_null(self.inner) {
48 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
52 /// Frees any resources used by the DelayedPaymentBasepoint, if is_owned is set and inner is non-NULL.
54 pub extern "C" fn DelayedPaymentBasepoint_free(this_obj: DelayedPaymentBasepoint) { }
56 /// Used only if an object of this type is returned as a trait impl by a method
57 pub(crate) extern "C" fn DelayedPaymentBasepoint_free_void(this_ptr: *mut c_void) {
58 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeDelayedPaymentBasepoint) };
61 impl DelayedPaymentBasepoint {
62 pub(crate) fn get_native_ref(&self) -> &'static nativeDelayedPaymentBasepoint {
63 unsafe { &*ObjOps::untweak_ptr(self.inner) }
65 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeDelayedPaymentBasepoint {
66 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
68 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
69 pub(crate) fn take_inner(mut self) -> *mut nativeDelayedPaymentBasepoint {
70 assert!(self.is_owned);
71 let ret = ObjOps::untweak_ptr(self.inner);
72 self.inner = core::ptr::null_mut();
77 pub extern "C" fn DelayedPaymentBasepoint_get_a(this_ptr: &DelayedPaymentBasepoint) -> crate::c_types::PublicKey {
78 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
79 crate::c_types::PublicKey::from_rust(&inner_val)
82 pub extern "C" fn DelayedPaymentBasepoint_set_a(this_ptr: &mut DelayedPaymentBasepoint, mut val: crate::c_types::PublicKey) {
83 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_rust();
85 /// Constructs a new DelayedPaymentBasepoint given each field
88 pub extern "C" fn DelayedPaymentBasepoint_new(mut a_arg: crate::c_types::PublicKey) -> DelayedPaymentBasepoint {
89 DelayedPaymentBasepoint { inner: ObjOps::heap_alloc(lightning::ln::channel_keys::DelayedPaymentBasepoint (
93 /// Checks if two DelayedPaymentBasepoints contain equal inner contents.
94 /// This ignores pointers and is_owned flags and looks at the values in fields.
95 /// Two objects with NULL inner values will be considered "equal" here.
97 pub extern "C" fn DelayedPaymentBasepoint_eq(a: &DelayedPaymentBasepoint, b: &DelayedPaymentBasepoint) -> bool {
98 if a.inner == b.inner { return true; }
99 if a.inner.is_null() || b.inner.is_null() { return false; }
100 if a.get_native_ref() == b.get_native_ref() { true } else { false }
102 impl Clone for DelayedPaymentBasepoint {
103 fn clone(&self) -> Self {
105 inner: if <*mut nativeDelayedPaymentBasepoint>::is_null(self.inner) { core::ptr::null_mut() } else {
106 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
112 /// Used only if an object of this type is returned as a trait impl by a method
113 pub(crate) extern "C" fn DelayedPaymentBasepoint_clone_void(this_ptr: *const c_void) -> *mut c_void {
114 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeDelayedPaymentBasepoint)).clone() })) as *mut c_void
117 /// Creates a copy of the DelayedPaymentBasepoint
118 pub extern "C" fn DelayedPaymentBasepoint_clone(orig: &DelayedPaymentBasepoint) -> DelayedPaymentBasepoint {
121 /// Get a string which allows debug introspection of a DelayedPaymentBasepoint object
122 pub extern "C" fn DelayedPaymentBasepoint_debug_str_void(o: *const c_void) -> Str {
123 alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::channel_keys::DelayedPaymentBasepoint }).into()}
124 /// Generates a non-cryptographic 64-bit hash of the DelayedPaymentBasepoint.
126 pub extern "C" fn DelayedPaymentBasepoint_hash(o: &DelayedPaymentBasepoint) -> u64 {
127 if o.inner.is_null() { return 0; }
128 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
130 let mut hasher = core::hash::SipHasher::new();
131 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
132 core::hash::Hasher::finish(&hasher)
134 /// Get inner Public Key
137 pub extern "C" fn DelayedPaymentBasepoint_to_public_key(this_arg: &crate::lightning::ln::channel_keys::DelayedPaymentBasepoint) -> crate::c_types::PublicKey {
138 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_public_key();
139 crate::c_types::PublicKey::from_rust(&ret)
143 /// Serialize the DelayedPaymentBasepoint object into a byte array which can be read by DelayedPaymentBasepoint_read
144 pub extern "C" fn DelayedPaymentBasepoint_write(obj: &crate::lightning::ln::channel_keys::DelayedPaymentBasepoint) -> crate::c_types::derived::CVec_u8Z {
145 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
148 pub(crate) extern "C" fn DelayedPaymentBasepoint_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
149 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeDelayedPaymentBasepoint) })
152 /// Read a DelayedPaymentBasepoint from a byte array, created by DelayedPaymentBasepoint_write
153 pub extern "C" fn DelayedPaymentBasepoint_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_DelayedPaymentBasepointDecodeErrorZ {
154 let res: Result<lightning::ln::channel_keys::DelayedPaymentBasepoint, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
155 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::channel_keys::DelayedPaymentBasepoint { 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() };
159 use lightning::ln::channel_keys::DelayedPaymentKey as nativeDelayedPaymentKeyImport;
160 pub(crate) type nativeDelayedPaymentKey = nativeDelayedPaymentKeyImport;
162 /// A derived key built from a [`DelayedPaymentBasepoint`] and `per_commitment_point`.
164 /// The delayed payment key is used to pay the commitment state broadcaster their
165 /// non-HTLC-encumbered funds after a delay. This delay gives their counterparty a chance to
166 /// punish and claim all the channel funds if the state broadcasted was previously revoked.
168 /// [See the BOLT specs]
169 /// (https://github.com/lightning/bolts/blob/master/03-transactions.md#localpubkey-local_htlcpubkey-remote_htlcpubkey-local_delayedpubkey-and-remote_delayedpubkey-derivation)
170 /// for more information on key derivation details.
173 pub struct DelayedPaymentKey {
174 /// A pointer to the opaque Rust object.
176 /// Nearly everywhere, inner must be non-null, however in places where
177 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
178 pub inner: *mut nativeDelayedPaymentKey,
179 /// Indicates that this is the only struct which contains the same pointer.
181 /// Rust functions which take ownership of an object provided via an argument require
182 /// this to be true and invalidate the object pointed to by inner.
186 impl Drop for DelayedPaymentKey {
188 if self.is_owned && !<*mut nativeDelayedPaymentKey>::is_null(self.inner) {
189 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
193 /// Frees any resources used by the DelayedPaymentKey, if is_owned is set and inner is non-NULL.
195 pub extern "C" fn DelayedPaymentKey_free(this_obj: DelayedPaymentKey) { }
197 /// Used only if an object of this type is returned as a trait impl by a method
198 pub(crate) extern "C" fn DelayedPaymentKey_free_void(this_ptr: *mut c_void) {
199 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeDelayedPaymentKey) };
202 impl DelayedPaymentKey {
203 pub(crate) fn get_native_ref(&self) -> &'static nativeDelayedPaymentKey {
204 unsafe { &*ObjOps::untweak_ptr(self.inner) }
206 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeDelayedPaymentKey {
207 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
209 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
210 pub(crate) fn take_inner(mut self) -> *mut nativeDelayedPaymentKey {
211 assert!(self.is_owned);
212 let ret = ObjOps::untweak_ptr(self.inner);
213 self.inner = core::ptr::null_mut();
218 pub extern "C" fn DelayedPaymentKey_get_a(this_ptr: &DelayedPaymentKey) -> crate::c_types::PublicKey {
219 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
220 crate::c_types::PublicKey::from_rust(&inner_val)
223 pub extern "C" fn DelayedPaymentKey_set_a(this_ptr: &mut DelayedPaymentKey, mut val: crate::c_types::PublicKey) {
224 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_rust();
226 /// Constructs a new DelayedPaymentKey given each field
229 pub extern "C" fn DelayedPaymentKey_new(mut a_arg: crate::c_types::PublicKey) -> DelayedPaymentKey {
230 DelayedPaymentKey { inner: ObjOps::heap_alloc(lightning::ln::channel_keys::DelayedPaymentKey (
234 /// Checks if two DelayedPaymentKeys contain equal inner contents.
235 /// This ignores pointers and is_owned flags and looks at the values in fields.
236 /// Two objects with NULL inner values will be considered "equal" here.
238 pub extern "C" fn DelayedPaymentKey_eq(a: &DelayedPaymentKey, b: &DelayedPaymentKey) -> bool {
239 if a.inner == b.inner { return true; }
240 if a.inner.is_null() || b.inner.is_null() { return false; }
241 if a.get_native_ref() == b.get_native_ref() { true } else { false }
243 impl Clone for DelayedPaymentKey {
244 fn clone(&self) -> Self {
246 inner: if <*mut nativeDelayedPaymentKey>::is_null(self.inner) { core::ptr::null_mut() } else {
247 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
253 /// Used only if an object of this type is returned as a trait impl by a method
254 pub(crate) extern "C" fn DelayedPaymentKey_clone_void(this_ptr: *const c_void) -> *mut c_void {
255 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeDelayedPaymentKey)).clone() })) as *mut c_void
258 /// Creates a copy of the DelayedPaymentKey
259 pub extern "C" fn DelayedPaymentKey_clone(orig: &DelayedPaymentKey) -> DelayedPaymentKey {
262 /// Get a string which allows debug introspection of a DelayedPaymentKey object
263 pub extern "C" fn DelayedPaymentKey_debug_str_void(o: *const c_void) -> Str {
264 alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::channel_keys::DelayedPaymentKey }).into()}
265 ///Derive a public delayedpubkey using one node\'s `per_commitment_point` and its countersignatory\'s `basepoint`
268 pub extern "C" fn DelayedPaymentKey_from_basepoint(countersignatory_basepoint: &crate::lightning::ln::channel_keys::DelayedPaymentBasepoint, mut per_commitment_point: crate::c_types::PublicKey) -> crate::lightning::ln::channel_keys::DelayedPaymentKey {
269 let mut ret = lightning::ln::channel_keys::DelayedPaymentKey::from_basepoint(secp256k1::global::SECP256K1, countersignatory_basepoint.get_native_ref(), &per_commitment_point.into_rust());
270 crate::lightning::ln::channel_keys::DelayedPaymentKey { inner: ObjOps::heap_alloc(ret), is_owned: true }
273 ///Build a delayedpubkey directly from an already-derived private key
276 pub extern "C" fn DelayedPaymentKey_from_secret_key(sk: *const [u8; 32]) -> crate::lightning::ln::channel_keys::DelayedPaymentKey {
277 let mut ret = lightning::ln::channel_keys::DelayedPaymentKey::from_secret_key(secp256k1::global::SECP256K1, &::bitcoin::secp256k1::SecretKey::from_slice(&unsafe { *sk}[..]).unwrap());
278 crate::lightning::ln::channel_keys::DelayedPaymentKey { inner: ObjOps::heap_alloc(ret), is_owned: true }
281 /// Get inner Public Key
284 pub extern "C" fn DelayedPaymentKey_to_public_key(this_arg: &crate::lightning::ln::channel_keys::DelayedPaymentKey) -> crate::c_types::PublicKey {
285 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_public_key();
286 crate::c_types::PublicKey::from_rust(&ret)
290 /// Serialize the DelayedPaymentKey object into a byte array which can be read by DelayedPaymentKey_read
291 pub extern "C" fn DelayedPaymentKey_write(obj: &crate::lightning::ln::channel_keys::DelayedPaymentKey) -> crate::c_types::derived::CVec_u8Z {
292 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
295 pub(crate) extern "C" fn DelayedPaymentKey_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
296 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeDelayedPaymentKey) })
299 /// Read a DelayedPaymentKey from a byte array, created by DelayedPaymentKey_write
300 pub extern "C" fn DelayedPaymentKey_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_DelayedPaymentKeyDecodeErrorZ {
301 let res: Result<lightning::ln::channel_keys::DelayedPaymentKey, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
302 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::channel_keys::DelayedPaymentKey { 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() };
306 use lightning::ln::channel_keys::HtlcBasepoint as nativeHtlcBasepointImport;
307 pub(crate) type nativeHtlcBasepoint = nativeHtlcBasepointImport;
309 /// Base key used in conjunction with a `per_commitment_point` to generate an [`HtlcKey`].
311 /// HTLC keys are used to ensure only the recipient of an HTLC can claim it on-chain with the HTLC
312 /// preimage and that only the sender of an HTLC can claim it on-chain after it has timed out.
313 /// Thus, both channel counterparties' HTLC keys will appears in each HTLC output's script.
316 pub struct HtlcBasepoint {
317 /// A pointer to the opaque Rust object.
319 /// Nearly everywhere, inner must be non-null, however in places where
320 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
321 pub inner: *mut nativeHtlcBasepoint,
322 /// Indicates that this is the only struct which contains the same pointer.
324 /// Rust functions which take ownership of an object provided via an argument require
325 /// this to be true and invalidate the object pointed to by inner.
329 impl Drop for HtlcBasepoint {
331 if self.is_owned && !<*mut nativeHtlcBasepoint>::is_null(self.inner) {
332 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
336 /// Frees any resources used by the HtlcBasepoint, if is_owned is set and inner is non-NULL.
338 pub extern "C" fn HtlcBasepoint_free(this_obj: HtlcBasepoint) { }
340 /// Used only if an object of this type is returned as a trait impl by a method
341 pub(crate) extern "C" fn HtlcBasepoint_free_void(this_ptr: *mut c_void) {
342 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeHtlcBasepoint) };
346 pub(crate) fn get_native_ref(&self) -> &'static nativeHtlcBasepoint {
347 unsafe { &*ObjOps::untweak_ptr(self.inner) }
349 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeHtlcBasepoint {
350 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
352 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
353 pub(crate) fn take_inner(mut self) -> *mut nativeHtlcBasepoint {
354 assert!(self.is_owned);
355 let ret = ObjOps::untweak_ptr(self.inner);
356 self.inner = core::ptr::null_mut();
361 pub extern "C" fn HtlcBasepoint_get_a(this_ptr: &HtlcBasepoint) -> crate::c_types::PublicKey {
362 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
363 crate::c_types::PublicKey::from_rust(&inner_val)
366 pub extern "C" fn HtlcBasepoint_set_a(this_ptr: &mut HtlcBasepoint, mut val: crate::c_types::PublicKey) {
367 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_rust();
369 /// Constructs a new HtlcBasepoint given each field
372 pub extern "C" fn HtlcBasepoint_new(mut a_arg: crate::c_types::PublicKey) -> HtlcBasepoint {
373 HtlcBasepoint { inner: ObjOps::heap_alloc(lightning::ln::channel_keys::HtlcBasepoint (
377 /// Checks if two HtlcBasepoints contain equal inner contents.
378 /// This ignores pointers and is_owned flags and looks at the values in fields.
379 /// Two objects with NULL inner values will be considered "equal" here.
381 pub extern "C" fn HtlcBasepoint_eq(a: &HtlcBasepoint, b: &HtlcBasepoint) -> bool {
382 if a.inner == b.inner { return true; }
383 if a.inner.is_null() || b.inner.is_null() { return false; }
384 if a.get_native_ref() == b.get_native_ref() { true } else { false }
386 impl Clone for HtlcBasepoint {
387 fn clone(&self) -> Self {
389 inner: if <*mut nativeHtlcBasepoint>::is_null(self.inner) { core::ptr::null_mut() } else {
390 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
396 /// Used only if an object of this type is returned as a trait impl by a method
397 pub(crate) extern "C" fn HtlcBasepoint_clone_void(this_ptr: *const c_void) -> *mut c_void {
398 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeHtlcBasepoint)).clone() })) as *mut c_void
401 /// Creates a copy of the HtlcBasepoint
402 pub extern "C" fn HtlcBasepoint_clone(orig: &HtlcBasepoint) -> HtlcBasepoint {
405 /// Get a string which allows debug introspection of a HtlcBasepoint object
406 pub extern "C" fn HtlcBasepoint_debug_str_void(o: *const c_void) -> Str {
407 alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::channel_keys::HtlcBasepoint }).into()}
408 /// Generates a non-cryptographic 64-bit hash of the HtlcBasepoint.
410 pub extern "C" fn HtlcBasepoint_hash(o: &HtlcBasepoint) -> u64 {
411 if o.inner.is_null() { return 0; }
412 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
414 let mut hasher = core::hash::SipHasher::new();
415 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
416 core::hash::Hasher::finish(&hasher)
418 /// Get inner Public Key
421 pub extern "C" fn HtlcBasepoint_to_public_key(this_arg: &crate::lightning::ln::channel_keys::HtlcBasepoint) -> crate::c_types::PublicKey {
422 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_public_key();
423 crate::c_types::PublicKey::from_rust(&ret)
427 /// Serialize the HtlcBasepoint object into a byte array which can be read by HtlcBasepoint_read
428 pub extern "C" fn HtlcBasepoint_write(obj: &crate::lightning::ln::channel_keys::HtlcBasepoint) -> crate::c_types::derived::CVec_u8Z {
429 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
432 pub(crate) extern "C" fn HtlcBasepoint_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
433 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHtlcBasepoint) })
436 /// Read a HtlcBasepoint from a byte array, created by HtlcBasepoint_write
437 pub extern "C" fn HtlcBasepoint_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HtlcBasepointDecodeErrorZ {
438 let res: Result<lightning::ln::channel_keys::HtlcBasepoint, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
439 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::channel_keys::HtlcBasepoint { 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() };
443 use lightning::ln::channel_keys::HtlcKey as nativeHtlcKeyImport;
444 pub(crate) type nativeHtlcKey = nativeHtlcKeyImport;
446 /// A derived key built from a [`HtlcBasepoint`] and `per_commitment_point`.
448 /// HTLC keys are used to ensure only the recipient of an HTLC can claim it on-chain with the HTLC
449 /// preimage and that only the sender of an HTLC can claim it on-chain after it has timed out.
450 /// Thus, both channel counterparties' HTLC keys will appears in each HTLC output's script.
452 /// [See the BOLT specs]
453 /// (https://github.com/lightning/bolts/blob/master/03-transactions.md#localpubkey-local_htlcpubkey-remote_htlcpubkey-local_delayedpubkey-and-remote_delayedpubkey-derivation)
454 /// for more information on key derivation details.
458 /// A pointer to the opaque Rust object.
460 /// Nearly everywhere, inner must be non-null, however in places where
461 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
462 pub inner: *mut nativeHtlcKey,
463 /// Indicates that this is the only struct which contains the same pointer.
465 /// Rust functions which take ownership of an object provided via an argument require
466 /// this to be true and invalidate the object pointed to by inner.
470 impl Drop for HtlcKey {
472 if self.is_owned && !<*mut nativeHtlcKey>::is_null(self.inner) {
473 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
477 /// Frees any resources used by the HtlcKey, if is_owned is set and inner is non-NULL.
479 pub extern "C" fn HtlcKey_free(this_obj: HtlcKey) { }
481 /// Used only if an object of this type is returned as a trait impl by a method
482 pub(crate) extern "C" fn HtlcKey_free_void(this_ptr: *mut c_void) {
483 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeHtlcKey) };
487 pub(crate) fn get_native_ref(&self) -> &'static nativeHtlcKey {
488 unsafe { &*ObjOps::untweak_ptr(self.inner) }
490 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeHtlcKey {
491 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
493 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
494 pub(crate) fn take_inner(mut self) -> *mut nativeHtlcKey {
495 assert!(self.is_owned);
496 let ret = ObjOps::untweak_ptr(self.inner);
497 self.inner = core::ptr::null_mut();
502 pub extern "C" fn HtlcKey_get_a(this_ptr: &HtlcKey) -> crate::c_types::PublicKey {
503 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
504 crate::c_types::PublicKey::from_rust(&inner_val)
507 pub extern "C" fn HtlcKey_set_a(this_ptr: &mut HtlcKey, mut val: crate::c_types::PublicKey) {
508 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_rust();
510 /// Constructs a new HtlcKey given each field
513 pub extern "C" fn HtlcKey_new(mut a_arg: crate::c_types::PublicKey) -> HtlcKey {
514 HtlcKey { inner: ObjOps::heap_alloc(lightning::ln::channel_keys::HtlcKey (
518 /// Checks if two HtlcKeys contain equal inner contents.
519 /// This ignores pointers and is_owned flags and looks at the values in fields.
520 /// Two objects with NULL inner values will be considered "equal" here.
522 pub extern "C" fn HtlcKey_eq(a: &HtlcKey, b: &HtlcKey) -> bool {
523 if a.inner == b.inner { return true; }
524 if a.inner.is_null() || b.inner.is_null() { return false; }
525 if a.get_native_ref() == b.get_native_ref() { true } else { false }
527 impl Clone for HtlcKey {
528 fn clone(&self) -> Self {
530 inner: if <*mut nativeHtlcKey>::is_null(self.inner) { core::ptr::null_mut() } else {
531 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
537 /// Used only if an object of this type is returned as a trait impl by a method
538 pub(crate) extern "C" fn HtlcKey_clone_void(this_ptr: *const c_void) -> *mut c_void {
539 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeHtlcKey)).clone() })) as *mut c_void
542 /// Creates a copy of the HtlcKey
543 pub extern "C" fn HtlcKey_clone(orig: &HtlcKey) -> HtlcKey {
546 /// Get a string which allows debug introspection of a HtlcKey object
547 pub extern "C" fn HtlcKey_debug_str_void(o: *const c_void) -> Str {
548 alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::channel_keys::HtlcKey }).into()}
549 ///Derive a public htlcpubkey using one node\'s `per_commitment_point` and its countersignatory\'s `basepoint`
552 pub extern "C" fn HtlcKey_from_basepoint(countersignatory_basepoint: &crate::lightning::ln::channel_keys::HtlcBasepoint, mut per_commitment_point: crate::c_types::PublicKey) -> crate::lightning::ln::channel_keys::HtlcKey {
553 let mut ret = lightning::ln::channel_keys::HtlcKey::from_basepoint(secp256k1::global::SECP256K1, countersignatory_basepoint.get_native_ref(), &per_commitment_point.into_rust());
554 crate::lightning::ln::channel_keys::HtlcKey { inner: ObjOps::heap_alloc(ret), is_owned: true }
557 ///Build a htlcpubkey directly from an already-derived private key
560 pub extern "C" fn HtlcKey_from_secret_key(sk: *const [u8; 32]) -> crate::lightning::ln::channel_keys::HtlcKey {
561 let mut ret = lightning::ln::channel_keys::HtlcKey::from_secret_key(secp256k1::global::SECP256K1, &::bitcoin::secp256k1::SecretKey::from_slice(&unsafe { *sk}[..]).unwrap());
562 crate::lightning::ln::channel_keys::HtlcKey { inner: ObjOps::heap_alloc(ret), is_owned: true }
565 /// Get inner Public Key
568 pub extern "C" fn HtlcKey_to_public_key(this_arg: &crate::lightning::ln::channel_keys::HtlcKey) -> crate::c_types::PublicKey {
569 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_public_key();
570 crate::c_types::PublicKey::from_rust(&ret)
574 /// Serialize the HtlcKey object into a byte array which can be read by HtlcKey_read
575 pub extern "C" fn HtlcKey_write(obj: &crate::lightning::ln::channel_keys::HtlcKey) -> crate::c_types::derived::CVec_u8Z {
576 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
579 pub(crate) extern "C" fn HtlcKey_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
580 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeHtlcKey) })
583 /// Read a HtlcKey from a byte array, created by HtlcKey_write
584 pub extern "C" fn HtlcKey_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_HtlcKeyDecodeErrorZ {
585 let res: Result<lightning::ln::channel_keys::HtlcKey, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
586 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::channel_keys::HtlcKey { 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() };
590 use lightning::ln::channel_keys::RevocationBasepoint as nativeRevocationBasepointImport;
591 pub(crate) type nativeRevocationBasepoint = nativeRevocationBasepointImport;
593 /// Master key used in conjunction with per_commitment_point to generate [htlcpubkey](https://github.com/lightning/bolts/blob/master/03-transactions.md#key-derivation) for the latest state of a channel.
594 /// A watcher can be given a [RevocationBasepoint] to generate per commitment [RevocationKey] to create justice transactions.
597 pub struct RevocationBasepoint {
598 /// A pointer to the opaque Rust object.
600 /// Nearly everywhere, inner must be non-null, however in places where
601 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
602 pub inner: *mut nativeRevocationBasepoint,
603 /// Indicates that this is the only struct which contains the same pointer.
605 /// Rust functions which take ownership of an object provided via an argument require
606 /// this to be true and invalidate the object pointed to by inner.
610 impl Drop for RevocationBasepoint {
612 if self.is_owned && !<*mut nativeRevocationBasepoint>::is_null(self.inner) {
613 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
617 /// Frees any resources used by the RevocationBasepoint, if is_owned is set and inner is non-NULL.
619 pub extern "C" fn RevocationBasepoint_free(this_obj: RevocationBasepoint) { }
621 /// Used only if an object of this type is returned as a trait impl by a method
622 pub(crate) extern "C" fn RevocationBasepoint_free_void(this_ptr: *mut c_void) {
623 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRevocationBasepoint) };
626 impl RevocationBasepoint {
627 pub(crate) fn get_native_ref(&self) -> &'static nativeRevocationBasepoint {
628 unsafe { &*ObjOps::untweak_ptr(self.inner) }
630 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRevocationBasepoint {
631 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
633 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
634 pub(crate) fn take_inner(mut self) -> *mut nativeRevocationBasepoint {
635 assert!(self.is_owned);
636 let ret = ObjOps::untweak_ptr(self.inner);
637 self.inner = core::ptr::null_mut();
642 pub extern "C" fn RevocationBasepoint_get_a(this_ptr: &RevocationBasepoint) -> crate::c_types::PublicKey {
643 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
644 crate::c_types::PublicKey::from_rust(&inner_val)
647 pub extern "C" fn RevocationBasepoint_set_a(this_ptr: &mut RevocationBasepoint, mut val: crate::c_types::PublicKey) {
648 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_rust();
650 /// Constructs a new RevocationBasepoint given each field
653 pub extern "C" fn RevocationBasepoint_new(mut a_arg: crate::c_types::PublicKey) -> RevocationBasepoint {
654 RevocationBasepoint { inner: ObjOps::heap_alloc(lightning::ln::channel_keys::RevocationBasepoint (
658 /// Checks if two RevocationBasepoints contain equal inner contents.
659 /// This ignores pointers and is_owned flags and looks at the values in fields.
660 /// Two objects with NULL inner values will be considered "equal" here.
662 pub extern "C" fn RevocationBasepoint_eq(a: &RevocationBasepoint, b: &RevocationBasepoint) -> bool {
663 if a.inner == b.inner { return true; }
664 if a.inner.is_null() || b.inner.is_null() { return false; }
665 if a.get_native_ref() == b.get_native_ref() { true } else { false }
667 impl Clone for RevocationBasepoint {
668 fn clone(&self) -> Self {
670 inner: if <*mut nativeRevocationBasepoint>::is_null(self.inner) { core::ptr::null_mut() } else {
671 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
677 /// Used only if an object of this type is returned as a trait impl by a method
678 pub(crate) extern "C" fn RevocationBasepoint_clone_void(this_ptr: *const c_void) -> *mut c_void {
679 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeRevocationBasepoint)).clone() })) as *mut c_void
682 /// Creates a copy of the RevocationBasepoint
683 pub extern "C" fn RevocationBasepoint_clone(orig: &RevocationBasepoint) -> RevocationBasepoint {
686 /// Get a string which allows debug introspection of a RevocationBasepoint object
687 pub extern "C" fn RevocationBasepoint_debug_str_void(o: *const c_void) -> Str {
688 alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::channel_keys::RevocationBasepoint }).into()}
689 /// Generates a non-cryptographic 64-bit hash of the RevocationBasepoint.
691 pub extern "C" fn RevocationBasepoint_hash(o: &RevocationBasepoint) -> u64 {
692 if o.inner.is_null() { return 0; }
693 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
695 let mut hasher = core::hash::SipHasher::new();
696 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
697 core::hash::Hasher::finish(&hasher)
699 /// Get inner Public Key
702 pub extern "C" fn RevocationBasepoint_to_public_key(this_arg: &crate::lightning::ln::channel_keys::RevocationBasepoint) -> crate::c_types::PublicKey {
703 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_public_key();
704 crate::c_types::PublicKey::from_rust(&ret)
708 /// Serialize the RevocationBasepoint object into a byte array which can be read by RevocationBasepoint_read
709 pub extern "C" fn RevocationBasepoint_write(obj: &crate::lightning::ln::channel_keys::RevocationBasepoint) -> crate::c_types::derived::CVec_u8Z {
710 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
713 pub(crate) extern "C" fn RevocationBasepoint_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
714 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRevocationBasepoint) })
717 /// Read a RevocationBasepoint from a byte array, created by RevocationBasepoint_write
718 pub extern "C" fn RevocationBasepoint_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RevocationBasepointDecodeErrorZ {
719 let res: Result<lightning::ln::channel_keys::RevocationBasepoint, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
720 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::channel_keys::RevocationBasepoint { 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() };
724 use lightning::ln::channel_keys::RevocationKey as nativeRevocationKeyImport;
725 pub(crate) type nativeRevocationKey = nativeRevocationKeyImport;
727 /// The revocation key is used to allow a channel party to revoke their state - giving their
728 /// counterparty the required material to claim all of their funds if they broadcast that state.
730 /// Each commitment transaction has a revocation key based on the basepoint and
731 /// per_commitment_point which is used in both commitment and HTLC transactions.
733 /// See [the BOLT spec for derivation details]
734 /// (https://github.com/lightning/bolts/blob/master/03-transactions.md#revocationpubkey-derivation)
737 pub struct RevocationKey {
738 /// A pointer to the opaque Rust object.
740 /// Nearly everywhere, inner must be non-null, however in places where
741 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
742 pub inner: *mut nativeRevocationKey,
743 /// Indicates that this is the only struct which contains the same pointer.
745 /// Rust functions which take ownership of an object provided via an argument require
746 /// this to be true and invalidate the object pointed to by inner.
750 impl Drop for RevocationKey {
752 if self.is_owned && !<*mut nativeRevocationKey>::is_null(self.inner) {
753 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
757 /// Frees any resources used by the RevocationKey, if is_owned is set and inner is non-NULL.
759 pub extern "C" fn RevocationKey_free(this_obj: RevocationKey) { }
761 /// Used only if an object of this type is returned as a trait impl by a method
762 pub(crate) extern "C" fn RevocationKey_free_void(this_ptr: *mut c_void) {
763 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRevocationKey) };
767 pub(crate) fn get_native_ref(&self) -> &'static nativeRevocationKey {
768 unsafe { &*ObjOps::untweak_ptr(self.inner) }
770 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRevocationKey {
771 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
773 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
774 pub(crate) fn take_inner(mut self) -> *mut nativeRevocationKey {
775 assert!(self.is_owned);
776 let ret = ObjOps::untweak_ptr(self.inner);
777 self.inner = core::ptr::null_mut();
782 pub extern "C" fn RevocationKey_get_a(this_ptr: &RevocationKey) -> crate::c_types::PublicKey {
783 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
784 crate::c_types::PublicKey::from_rust(&inner_val)
787 pub extern "C" fn RevocationKey_set_a(this_ptr: &mut RevocationKey, mut val: crate::c_types::PublicKey) {
788 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = val.into_rust();
790 /// Constructs a new RevocationKey given each field
793 pub extern "C" fn RevocationKey_new(mut a_arg: crate::c_types::PublicKey) -> RevocationKey {
794 RevocationKey { inner: ObjOps::heap_alloc(lightning::ln::channel_keys::RevocationKey (
798 /// Checks if two RevocationKeys contain equal inner contents.
799 /// This ignores pointers and is_owned flags and looks at the values in fields.
800 /// Two objects with NULL inner values will be considered "equal" here.
802 pub extern "C" fn RevocationKey_eq(a: &RevocationKey, b: &RevocationKey) -> bool {
803 if a.inner == b.inner { return true; }
804 if a.inner.is_null() || b.inner.is_null() { return false; }
805 if a.get_native_ref() == b.get_native_ref() { true } else { false }
807 impl Clone for RevocationKey {
808 fn clone(&self) -> Self {
810 inner: if <*mut nativeRevocationKey>::is_null(self.inner) { core::ptr::null_mut() } else {
811 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
817 /// Used only if an object of this type is returned as a trait impl by a method
818 pub(crate) extern "C" fn RevocationKey_clone_void(this_ptr: *const c_void) -> *mut c_void {
819 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeRevocationKey)).clone() })) as *mut c_void
822 /// Creates a copy of the RevocationKey
823 pub extern "C" fn RevocationKey_clone(orig: &RevocationKey) -> RevocationKey {
826 /// Get a string which allows debug introspection of a RevocationKey object
827 pub extern "C" fn RevocationKey_debug_str_void(o: *const c_void) -> Str {
828 alloc::format!("{:?}", unsafe { o as *const crate::lightning::ln::channel_keys::RevocationKey }).into()}
829 /// Generates a non-cryptographic 64-bit hash of the RevocationKey.
831 pub extern "C" fn RevocationKey_hash(o: &RevocationKey) -> u64 {
832 if o.inner.is_null() { return 0; }
833 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
835 let mut hasher = core::hash::SipHasher::new();
836 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
837 core::hash::Hasher::finish(&hasher)
839 /// Derives a per-commitment-transaction revocation public key from one party's per-commitment
840 /// point and the other party's [`RevocationBasepoint`]. This is the public equivalent of
841 /// [`chan_utils::derive_private_revocation_key`] - using only public keys to derive a public
842 /// key instead of private keys.
844 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
845 /// generated (ie our own).
847 /// [`chan_utils::derive_private_revocation_key`]: crate::ln::chan_utils::derive_private_revocation_key
850 pub extern "C" fn RevocationKey_from_basepoint(countersignatory_basepoint: &crate::lightning::ln::channel_keys::RevocationBasepoint, mut per_commitment_point: crate::c_types::PublicKey) -> crate::lightning::ln::channel_keys::RevocationKey {
851 let mut ret = lightning::ln::channel_keys::RevocationKey::from_basepoint(secp256k1::global::SECP256K1, countersignatory_basepoint.get_native_ref(), &per_commitment_point.into_rust());
852 crate::lightning::ln::channel_keys::RevocationKey { inner: ObjOps::heap_alloc(ret), is_owned: true }
855 /// Get inner Public Key
858 pub extern "C" fn RevocationKey_to_public_key(this_arg: &crate::lightning::ln::channel_keys::RevocationKey) -> crate::c_types::PublicKey {
859 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_public_key();
860 crate::c_types::PublicKey::from_rust(&ret)
864 /// Serialize the RevocationKey object into a byte array which can be read by RevocationKey_read
865 pub extern "C" fn RevocationKey_write(obj: &crate::lightning::ln::channel_keys::RevocationKey) -> crate::c_types::derived::CVec_u8Z {
866 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
869 pub(crate) extern "C" fn RevocationKey_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
870 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRevocationKey) })
873 /// Read a RevocationKey from a byte array, created by RevocationKey_write
874 pub extern "C" fn RevocationKey_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RevocationKeyDecodeErrorZ {
875 let res: Result<lightning::ln::channel_keys::RevocationKey, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
876 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::channel_keys::RevocationKey { 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() };