]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Add support for witnesses via a manual mapping
authorMatt Corallo <git@bluematt.me>
Sat, 24 Dec 2022 01:14:33 +0000 (01:14 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 3 Jan 2023 22:23:42 +0000 (22:23 +0000)
c-bindings-gen/src/types.rs
lightning-c-bindings/src/c_types/mod.rs

index a678e7ad20cec973ce165dcf981ae0a2a1de5037..7132918f19bb44c293bf6cc22c8c81b232fd0a49 100644 (file)
@@ -808,6 +808,7 @@ fn initial_clonable_types() -> HashSet<String> {
        res.insert("crate::c_types::SecretKey".to_owned());
        res.insert("crate::c_types::PublicKey".to_owned());
        res.insert("crate::c_types::Transaction".to_owned());
+       res.insert("crate::c_types::Witness".to_owned());
        res.insert("crate::c_types::TxOut".to_owned());
        res.insert("crate::c_types::Signature".to_owned());
        res.insert("crate::c_types::RecoverableSignature".to_owned());
@@ -1006,6 +1007,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some("crate::lightning::chain::transaction::OutPoint"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some("crate::c_types::Transaction"),
+                       "bitcoin::Witness" => Some("crate::c_types::Witness"),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut"),
                        "bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network"),
                        "bitcoin::util::address::WitnessVersion" => Some("crate::c_types::WitnessVersion"),
@@ -1095,6 +1097,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("::bitcoin::blockdata::script::Script::from("),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("&"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(""),
+                       "bitcoin::Witness" if is_ref => Some("&"),
+                       "bitcoin::Witness" => Some(""),
                        "bitcoin::blockdata::transaction::OutPoint" => Some("crate::c_types::C_to_bitcoin_outpoint("),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(""),
                        "bitcoin::network::constants::Network" => Some(""),
@@ -1182,6 +1186,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::blockdata::script::Script" if is_ref => Some(".to_slice()))"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_rust())"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(".into_bitcoin()"),
+                       "bitcoin::Witness" => Some(".into_bitcoin()"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some(")"),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(".into_rust()"),
                        "bitcoin::network::constants::Network" => Some(".into_bitcoin()"),
@@ -1280,6 +1285,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(""),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("crate::c_types::Transaction::from_bitcoin("),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some("crate::c_types::Transaction::from_bitcoin(&"),
+                       "bitcoin::Witness" if is_ref => Some("crate::c_types::Witness::from_bitcoin("),
+                       "bitcoin::Witness" if !is_ref => Some("crate::c_types::Witness::from_bitcoin(&"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some("crate::c_types::bitcoin_to_C_outpoint("),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut::from_rust("),
                        "bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network::from_bitcoin("),
@@ -1360,6 +1367,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::blockdata::script::Script" if is_ref => Some("[..])"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_bytes().into()"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(")"),
+                       "bitcoin::Witness" => Some(")"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some(")"),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(")"),
                        "bitcoin::network::constants::Network" => Some(")"),
index 6720039a912d65285ad40eab0a6093a7267198f3..f23f002d05c7ccfe8607ffd5b30eae0c79d0017b 100644 (file)
@@ -4,6 +4,7 @@
 pub mod derived;
 
 use bitcoin::Transaction as BitcoinTransaction;
+use bitcoin::Witness as BitcoinWitness;
 use bitcoin::hashes::Hash;
 use bitcoin::secp256k1::PublicKey as SecpPublicKey;
 use bitcoin::secp256k1::SecretKey as SecpSecretKey;
@@ -460,6 +461,57 @@ impl Clone for Transaction {
 /// Frees the data buffer, if data_is_owned is set and datalen > 0.
 pub extern "C" fn Transaction_free(_res: Transaction) { }
 
+#[repr(C)]
+/// A serialized witness.
+pub struct Witness {
+       /// The serialized transaction data.
+       ///
+       /// This is non-const for your convenience, an object passed to Rust is never written to.
+       pub data: *mut u8,
+       /// The length of the serialized transaction
+       pub datalen: usize,
+       /// Whether the data pointed to by `data` should be freed or not.
+       pub data_is_owned: bool,
+}
+impl Witness {
+       fn from_vec(vec: Vec<u8>) -> Self {
+               let datalen = vec.len();
+               let data = Box::into_raw(vec.into_boxed_slice());
+               Self {
+                       data: unsafe { (*data).as_mut_ptr() },
+                       datalen,
+                       data_is_owned: true,
+               }
+       }
+       pub(crate) fn into_bitcoin(&self) -> BitcoinWitness {
+               ::bitcoin::consensus::encode::deserialize(unsafe { core::slice::from_raw_parts(self.data, self.datalen) }).unwrap()
+       }
+       pub(crate) fn from_bitcoin(btc: &BitcoinWitness) -> Self {
+               let vec = ::bitcoin::consensus::encode::serialize(btc);
+               Self::from_vec(vec)
+       }
+}
+
+impl Drop for Witness {
+       fn drop(&mut self) {
+               if self.data_is_owned && self.datalen != 0 {
+                       let _ = derived::CVec_u8Z { data: self.data as *mut u8, datalen: self.datalen };
+               }
+       }
+}
+impl Clone for Witness {
+       fn clone(&self) -> Self {
+               let sl = unsafe { core::slice::from_raw_parts(self.data, self.datalen) };
+               let mut v = Vec::new();
+               v.extend_from_slice(&sl);
+               Self::from_vec(v)
+       }
+}
+
+#[no_mangle]
+/// Frees the data pointed to by data
+pub extern "C" fn Witness_free(_res: Witness) { }
+
 pub(crate) fn bitcoin_to_C_outpoint(outpoint: ::bitcoin::blockdata::transaction::OutPoint) -> crate::lightning::chain::transaction::OutPoint {
        crate::lightning::chain::transaction::OutPoint_new(ThirtyTwoBytes { data: outpoint.txid.into_inner() }, outpoint.vout.try_into().unwrap())
 }