Update rust-bitcoin
[rust-lightning] / lightning-c-bindings / src / c_types / mod.rs
index 813e401b8e7e2b5f567b8791117c815c02aa0d61..0a96f3e7d6789d0ad0be381022db26bb78b727ed 100644 (file)
@@ -2,11 +2,14 @@ pub mod derived;
 
 use bitcoin::Script as BitcoinScript;
 use bitcoin::Transaction as BitcoinTransaction;
+use bitcoin::hashes::Hash;
 use bitcoin::secp256k1::key::PublicKey as SecpPublicKey;
 use bitcoin::secp256k1::key::SecretKey as SecpSecretKey;
 use bitcoin::secp256k1::Signature as SecpSignature;
 use bitcoin::secp256k1::Error as SecpError;
 
+use std::convert::TryInto; // Bindings need at least rustc 1.34
+
 #[derive(Clone)]
 #[repr(C)]
 pub struct PublicKey {
@@ -42,6 +45,7 @@ impl SecretKey {
 }
 
 #[repr(C)]
+#[derive(Clone)]
 pub struct Signature {
        pub compact_form: [u8; 64],
 }
@@ -54,8 +58,9 @@ impl Signature {
        pub(crate) fn into_rust(&self) -> SecpSignature {
                SecpSignature::from_compact(&self.compact_form).unwrap()
        }
-       pub(crate) fn is_null(&self) -> bool { self.compact_form[..] == [0; 64][..] }
-       pub(crate) fn null() -> Self { Self { compact_form: [0; 64] } }
+       // The following are used for Option<Signature> which we support, but don't use anymore
+       #[allow(unused)] pub(crate) fn is_null(&self) -> bool { self.compact_form[..] == [0; 64][..] }
+       #[allow(unused)] pub(crate) fn null() -> Self { Self { compact_form: [0; 64] } }
 }
 
 #[repr(C)]
@@ -67,8 +72,8 @@ pub enum Secp256k1Error {
        InvalidSecretKey,
        InvalidRecoveryId,
        InvalidTweak,
+       TweakCheckFailed,
        NotEnoughMemory,
-       CallbackPanicked,
 }
 impl Secp256k1Error {
        pub(crate) fn from_rust(err: SecpError) -> Self {
@@ -80,6 +85,7 @@ impl Secp256k1Error {
                        SecpError::InvalidSecretKey => Secp256k1Error::InvalidSecretKey,
                        SecpError::InvalidRecoveryId => Secp256k1Error::InvalidRecoveryId,
                        SecpError::InvalidTweak => Secp256k1Error::InvalidTweak,
+                       SecpError::TweakCheckFailed => Secp256k1Error::TweakCheckFailed,
                        SecpError::NotEnoughMemory => Secp256k1Error::NotEnoughMemory,
                }
        }
@@ -100,7 +106,8 @@ impl Secp256k1Error {
 /// set. Similarly, while it may change in the future, all `Transaction`s you pass to Rust may have
 /// `data_is_owned` either set or unset at your discretion.
 pub struct Transaction {
-       pub data: *const u8,
+       /// This is non-const for your convenience, an object passed to Rust is never written to.
+       pub data: *mut u8,
        pub datalen: usize,
        pub data_is_owned: bool,
 }
@@ -122,13 +129,17 @@ impl Transaction {
 impl Drop for Transaction {
        fn drop(&mut self) {
                if self.data_is_owned && self.datalen != 0 {
-                       let _ = CVecTempl { data: self.data as *mut u8, datalen: self.datalen };
+                       let _ = derived::CVec_u8Z { data: self.data as *mut u8, datalen: self.datalen };
                }
        }
 }
 #[no_mangle]
 pub extern "C" fn Transaction_free(_res: Transaction) { }
 
+pub(crate) fn bitcoin_to_C_outpoint(outpoint: ::bitcoin::blockdata::transaction::OutPoint) -> crate::chain::transaction::OutPoint {
+       crate::chain::transaction::OutPoint_new(ThirtyTwoBytes { data: outpoint.txid.into_inner() }, outpoint.vout.try_into().unwrap())
+}
+
 #[repr(C)]
 #[derive(Clone)]
 /// A transaction output including a scriptPubKey and value.
@@ -147,13 +158,15 @@ impl TxOut {
        }
        pub(crate) fn from_rust(txout: ::bitcoin::blockdata::transaction::TxOut) -> Self {
                Self {
-                       script_pubkey: CVecTempl::from(txout.script_pubkey.into_bytes()),
+                       script_pubkey: derived::CVec_u8Z::from(txout.script_pubkey.into_bytes()),
                        value: txout.value
                }
        }
 }
 #[no_mangle]
 pub extern "C" fn TxOut_free(_res: TxOut) { }
+#[no_mangle]
+pub extern "C" fn TxOut_clone(orig: &TxOut) -> TxOut { orig.clone() }
 
 #[repr(C)]
 pub struct u8slice {
@@ -211,11 +224,14 @@ impl lightning::util::ser::Writer for VecWriter {
 pub(crate) fn serialize_obj<I: lightning::util::ser::Writeable>(i: &I) -> derived::CVec_u8Z {
        let mut out = VecWriter(Vec::new());
        i.write(&mut out).unwrap();
-       CVecTempl::from(out.0)
+       derived::CVec_u8Z::from(out.0)
 }
 pub(crate) fn deserialize_obj<I: lightning::util::ser::Readable>(s: u8slice) -> Result<I, lightning::ln::msgs::DecodeError> {
        I::read(&mut s.to_slice())
 }
+pub(crate) fn deserialize_obj_arg<A, I: lightning::util::ser::ReadableArgs<A>>(s: u8slice, args: A) -> Result<I, lightning::ln::msgs::DecodeError> {
+       I::read(&mut s.to_slice(), args)
+}
 
 #[repr(C)]
 #[derive(Copy, Clone)]
@@ -244,14 +260,14 @@ impl Into<&'static str> for Str {
 // everywhere in the containers.
 
 #[repr(C)]
-pub union CResultPtr<O, E> {
-       pub result: *mut O,
-       pub err: *mut E,
+pub(crate) union CResultPtr<O, E> {
+       pub(crate) result: *mut O,
+       pub(crate) err: *mut E,
 }
 #[repr(C)]
-pub struct CResultTempl<O, E> {
-       pub contents: CResultPtr<O, E>,
-       pub result_ok: bool,
+pub(crate) struct CResultTempl<O, E> {
+       pub(crate) contents: CResultPtr<O, E>,
+       pub(crate) result_ok: bool,
 }
 impl<O, E> CResultTempl<O, E> {
        pub(crate) extern "C" fn ok(o: O) -> Self {
@@ -271,7 +287,6 @@ impl<O, E> CResultTempl<O, E> {
                }
        }
 }
-pub extern "C" fn CResultTempl_free<O, E>(_res: CResultTempl<O, E>) { }
 impl<O, E> Drop for CResultTempl<O, E> {
        fn drop(&mut self) {
                if self.result_ok {
@@ -284,126 +299,6 @@ impl<O, E> Drop for CResultTempl<O, E> {
        }
 }
 
-#[repr(C)]
-pub struct CVecTempl<T> {
-       pub data: *mut T,
-       pub datalen: usize
-}
-impl<T> CVecTempl<T> {
-       pub(crate) fn into_rust(&mut self) -> Vec<T> {
-               if self.datalen == 0 { return Vec::new(); }
-               let ret = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }.into();
-               self.data = std::ptr::null_mut();
-               self.datalen = 0;
-               ret
-       }
-       pub(crate) fn as_slice(&self) -> &[T] {
-               unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) }
-       }
-}
-impl<T> From<Vec<T>> for CVecTempl<T> {
-       fn from(v: Vec<T>) -> Self {
-               let datalen = v.len();
-               let data = Box::into_raw(v.into_boxed_slice());
-               CVecTempl { datalen, data: unsafe { (*data).as_mut_ptr() } }
-       }
-}
-pub extern "C" fn CVecTempl_free<T>(_res: CVecTempl<T>) { }
-impl<T> Drop for CVecTempl<T> {
-       fn drop(&mut self) {
-               if self.datalen == 0 { return; }
-               unsafe { Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) };
-       }
-}
-impl<T: Clone> Clone for CVecTempl<T> {
-       fn clone(&self) -> Self {
-               let mut res = Vec::new();
-               if self.datalen == 0 { return Self::from(res); }
-               res.clone_from_slice(unsafe { std::slice::from_raw_parts_mut(self.data, self.datalen) });
-               Self::from(res)
-       }
-}
-
-#[repr(C)]
-pub struct C2TupleTempl<A, B> {
-       pub a: *mut A,
-       pub b: *mut B,
-}
-impl<A, B> From<(A, B)> for C2TupleTempl<A, B> {
-       fn from(tup: (A, B)) -> Self {
-               Self {
-                       a: Box::into_raw(Box::new(tup.0)),
-                       b: Box::into_raw(Box::new(tup.1)),
-               }
-       }
-}
-impl<A, B> C2TupleTempl<A, B> {
-       pub(crate) fn to_rust(mut self) -> (A, B) {
-               let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) });
-               self.a = std::ptr::null_mut();
-               self.b = std::ptr::null_mut();
-               res
-       }
-}
-pub extern "C" fn C2TupleTempl_free<A, B>(_res: C2TupleTempl<A, B>) { }
-impl<A, B> Drop for C2TupleTempl<A, B> {
-       fn drop(&mut self) {
-               if !self.a.is_null() {
-                       unsafe { Box::from_raw(self.a) };
-               }
-               if !self.b.is_null() {
-                       unsafe { Box::from_raw(self.b) };
-               }
-       }
-}
-impl <A: Clone, B: Clone> Clone for C2TupleTempl<A, B> {
-       fn clone(&self) -> Self {
-               Self {
-                       a: Box::into_raw(Box::new(unsafe { &*self.a }.clone())),
-                       b: Box::into_raw(Box::new(unsafe { &*self.b }.clone()))
-               }
-       }
-}
-
-#[repr(C)]
-pub struct C3TupleTempl<A, B, C> {
-       pub a: *mut A,
-       pub b: *mut B,
-       pub c: *mut C,
-}
-impl<A, B, C> From<(A, B, C)> for C3TupleTempl<A, B, C> {
-       fn from(tup: (A, B, C)) -> Self {
-               Self {
-                       a: Box::into_raw(Box::new(tup.0)),
-                       b: Box::into_raw(Box::new(tup.1)),
-                       c: Box::into_raw(Box::new(tup.2)),
-               }
-       }
-}
-impl<A, B, C> C3TupleTempl<A, B, C> {
-       pub(crate) fn to_rust(mut self) -> (A, B, C) {
-               let res = (unsafe { *Box::from_raw(self.a) }, unsafe { *Box::from_raw(self.b) }, unsafe { *Box::from_raw(self.c) });
-               self.a = std::ptr::null_mut();
-               self.b = std::ptr::null_mut();
-               self.c = std::ptr::null_mut();
-               res
-       }
-}
-pub extern "C" fn C3TupleTempl_free<A, B, C>(_res: C3TupleTempl<A, B, C>) { }
-impl<A, B, C> Drop for C3TupleTempl<A, B, C> {
-       fn drop(&mut self) {
-               if !self.a.is_null() {
-                       unsafe { Box::from_raw(self.a) };
-               }
-               if !self.b.is_null() {
-                       unsafe { Box::from_raw(self.b) };
-               }
-               if !self.c.is_null() {
-                       unsafe { Box::from_raw(self.c) };
-               }
-       }
-}
-
 /// Utility to make it easy to set a pointer to null and get its original value in line.
 pub(crate) trait TakePointer<T> {
        fn take_ptr(&mut self) -> T;