Merge pull request #39 from TheBlueMatt/main
[ldk-c-bindings] / lightning-c-bindings / src / lightning / chain / transaction.rs
index db30cdfeb9cc031e34e428ab2719bbdadb48292b..45794a14c39029f880df02418fe94b66582930c2 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::*;
@@ -64,7 +65,7 @@ impl OutPoint {
 #[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;
-       (*inner_val).as_inner()
+       inner_val.as_inner()
 }
 /// The referenced transaction's txid.
 #[no_mangle]
@@ -75,7 +76,7 @@ pub extern "C" fn OutPoint_set_txid(this_ptr: &mut OutPoint, mut val: crate::c_t
 #[no_mangle]
 pub extern "C" fn OutPoint_get_index(this_ptr: &OutPoint) -> u16 {
        let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.index;
-       (*inner_val)
+       *inner_val
 }
 /// The index of the referenced output in its transaction's vout.
 #[no_mangle]
@@ -110,6 +111,25 @@ 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 unsafe { &*a.inner } == unsafe { &*b.inner } { 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(unsafe { &*o.inner }, &mut hasher);
+       std::hash::Hasher::finish(&hasher)
+}
 /// Convert an `OutPoint` to a lightning channel id.
 #[must_use]
 #[no_mangle]