Merge pull request #40 from TheBlueMatt/main
[ldk-c-bindings] / lightning-c-bindings / src / lightning / chain / transaction.rs
index b4ea08cdd07292a2e4c89a02a12523bb68867deb..2006596dd5ede442ea152b736b50aac544e541f5 100644 (file)
@@ -8,6 +8,7 @@
 
 //! Types describing on-chain transactions.
 
+use std::str::FromStr;
 use std::ffi::c_void;
 use bitcoin::hashes::Hash;
 use crate::c_types::*;
@@ -38,7 +39,7 @@ pub struct OutPoint {
 impl Drop for OutPoint {
        fn drop(&mut self) {
                if self.is_owned && !<*mut nativeOutPoint>::is_null(self.inner) {
-                       let _ = unsafe { Box::from_raw(self.inner) };
+                       let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
                }
        }
 }
@@ -51,11 +52,17 @@ extern "C" fn OutPoint_free_void(this_ptr: *mut c_void) {
        unsafe { let _ = Box::from_raw(this_ptr as *mut nativeOutPoint); }
 }
 #[allow(unused)]
-/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
 impl OutPoint {
+       pub(crate) fn get_native_ref(&self) -> &'static nativeOutPoint {
+               unsafe { &*ObjOps::untweak_ptr(self.inner) }
+       }
+       pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeOutPoint {
+               unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
+       }
+       /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
        pub(crate) fn take_inner(mut self) -> *mut nativeOutPoint {
                assert!(self.is_owned);
-               let ret = self.inner;
+               let ret = ObjOps::untweak_ptr(self.inner);
                self.inner = std::ptr::null_mut();
                ret
        }
@@ -63,39 +70,39 @@ impl OutPoint {
 /// The referenced transaction's txid.
 #[no_mangle]
 pub extern "C" fn OutPoint_get_txid(this_ptr: &OutPoint) -> *const [u8; 32] {
-       let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.txid;
+       let mut inner_val = &mut this_ptr.get_native_mut_ref().txid;
        inner_val.as_inner()
 }
 /// The referenced transaction's txid.
 #[no_mangle]
 pub extern "C" fn OutPoint_set_txid(this_ptr: &mut OutPoint, mut val: crate::c_types::ThirtyTwoBytes) {
-       unsafe { &mut *this_ptr.inner }.txid = ::bitcoin::hash_types::Txid::from_slice(&val.data[..]).unwrap();
+       unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.txid = ::bitcoin::hash_types::Txid::from_slice(&val.data[..]).unwrap();
 }
 /// The index of the referenced output in its transaction's vout.
 #[no_mangle]
 pub extern "C" fn OutPoint_get_index(this_ptr: &OutPoint) -> u16 {
-       let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.index;
+       let mut inner_val = &mut this_ptr.get_native_mut_ref().index;
        *inner_val
 }
 /// The index of the referenced output in its transaction's vout.
 #[no_mangle]
 pub extern "C" fn OutPoint_set_index(this_ptr: &mut OutPoint, mut val: u16) {
-       unsafe { &mut *this_ptr.inner }.index = val;
+       unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.index = val;
 }
 /// Constructs a new OutPoint given each field
 #[must_use]
 #[no_mangle]
 pub extern "C" fn OutPoint_new(mut txid_arg: crate::c_types::ThirtyTwoBytes, mut index_arg: u16) -> OutPoint {
-       OutPoint { inner: Box::into_raw(Box::new(nativeOutPoint {
+       OutPoint { inner: ObjOps::heap_alloc(nativeOutPoint {
                txid: ::bitcoin::hash_types::Txid::from_slice(&txid_arg.data[..]).unwrap(),
                index: index_arg,
-       })), is_owned: true }
+       }), is_owned: true }
 }
 impl Clone for OutPoint {
        fn clone(&self) -> Self {
                Self {
                        inner: if <*mut nativeOutPoint>::is_null(self.inner) { std::ptr::null_mut() } else {
-                               Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
+                               ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
                        is_owned: true,
                }
        }
@@ -110,18 +117,37 @@ pub(crate) extern "C" fn OutPoint_clone_void(this_ptr: *const c_void) -> *mut c_
 pub extern "C" fn OutPoint_clone(orig: &OutPoint) -> OutPoint {
        orig.clone()
 }
+/// Checks if two OutPoints contain equal inner contents.
+/// This ignores pointers and is_owned flags and looks at the values in fields.
+/// Two objects with NULL inner values will be considered "equal" here.
+#[no_mangle]
+pub extern "C" fn OutPoint_eq(a: &OutPoint, b: &OutPoint) -> bool {
+       if a.inner == b.inner { return true; }
+       if a.inner.is_null() || b.inner.is_null() { return false; }
+       if a.get_native_ref() == b.get_native_ref() { true } else { false }
+}
+/// Checks if two OutPoints contain equal inner contents.
+#[no_mangle]
+pub extern "C" fn OutPoint_hash(o: &OutPoint) -> u64 {
+       if o.inner.is_null() { return 0; }
+       // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
+       #[allow(deprecated)]
+       let mut hasher = core::hash::SipHasher::new();
+       std::hash::Hash::hash(o.get_native_ref(), &mut hasher);
+       std::hash::Hasher::finish(&hasher)
+}
 /// Convert an `OutPoint` to a lightning channel id.
 #[must_use]
 #[no_mangle]
 pub extern "C" fn OutPoint_to_channel_id(this_arg: &OutPoint) -> crate::c_types::ThirtyTwoBytes {
-       let mut ret = unsafe { &*this_arg.inner }.to_channel_id();
+       let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.to_channel_id();
        crate::c_types::ThirtyTwoBytes { data: ret }
 }
 
 #[no_mangle]
 /// Serialize the OutPoint object into a byte array which can be read by OutPoint_read
 pub extern "C" fn OutPoint_write(obj: &OutPoint) -> crate::c_types::derived::CVec_u8Z {
-       crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
+       crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
 }
 #[no_mangle]
 pub(crate) extern "C" fn OutPoint_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
@@ -131,6 +157,6 @@ pub(crate) extern "C" fn OutPoint_write_void(obj: *const c_void) -> crate::c_typ
 /// Read a OutPoint from a byte array, created by OutPoint_write
 pub extern "C" fn OutPoint_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_OutPointDecodeErrorZ {
        let res = crate::c_types::deserialize_obj(ser);
-       let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::chain::transaction::OutPoint { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
+       let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::chain::transaction::OutPoint { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
        local_res
 }