From f8078e127bd05d8056f13245656bcc0af8f3e08d Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 19 May 2022 10:22:56 -0500 Subject: [PATCH] Add C type for bitcoin::util::address::WitnessVersion --- c-bindings-gen/src/types.rs | 5 +++++ lightning-c-bindings/src/c_types/mod.rs | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index b10ce3d..1e1bffa 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -901,6 +901,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some("crate::c_types::Transaction"), "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"), "bitcoin::blockdata::block::BlockHeader" if is_ref => Some("*const [u8; 80]"), "bitcoin::blockdata::block::Block" if is_ref => Some("crate::c_types::u8slice"), @@ -986,6 +987,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "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(""), + "bitcoin::util::address::WitnessVersion" => Some(""), "bitcoin::blockdata::block::BlockHeader" => Some("&::bitcoin::consensus::encode::deserialize(unsafe { &*"), "bitcoin::blockdata::block::Block" if is_ref => Some("&::bitcoin::consensus::encode::deserialize("), @@ -1067,6 +1069,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "bitcoin::blockdata::transaction::OutPoint" => Some(")"), "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(".into_rust()"), "bitcoin::network::constants::Network" => Some(".into_bitcoin()"), + "bitcoin::util::address::WitnessVersion" => Some(".into()"), "bitcoin::blockdata::block::BlockHeader" => Some(" }).unwrap()"), "bitcoin::blockdata::block::Block" => Some(".to_slice()).unwrap()"), @@ -1159,6 +1162,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "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("), + "bitcoin::util::address::WitnessVersion" => Some(""), "bitcoin::blockdata::block::BlockHeader" if is_ref => Some("&local_"), "bitcoin::blockdata::block::Block" if is_ref => Some("crate::c_types::u8slice::from_slice(&local_"), @@ -1234,6 +1238,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "bitcoin::blockdata::transaction::OutPoint" => Some(")"), "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(")"), "bitcoin::network::constants::Network" => Some(")"), + "bitcoin::util::address::WitnessVersion" => Some(".into()"), "bitcoin::blockdata::block::BlockHeader" if is_ref => Some(""), "bitcoin::blockdata::block::Block" if is_ref => Some(")"), diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 71aa679..471b067 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -12,6 +12,7 @@ use bitcoin::secp256k1::Error as SecpError; use bitcoin::secp256k1::ecdsa::RecoveryId; use bitcoin::secp256k1::ecdsa::RecoverableSignature as SecpRecoverableSignature; use bitcoin::bech32; +use bitcoin::util::address; use core::convert::TryInto; // Bindings need at least rustc 1.34 use core::ffi::c_void; @@ -46,6 +47,18 @@ impl Into for u5 { fn into(self) -> bech32::u5 { bech32::u5::try_from_u8(self.0).expect("u5 objects must be in the range 0..32") } } +/// Integer in the range `0..=16` +#[derive(PartialEq, Eq, Copy, Clone)] +#[repr(C)] +pub struct WitnessVersion(u8); + +impl From for WitnessVersion { + fn from(o: address::WitnessVersion) -> Self { Self(o.into_num()) } +} +impl Into for WitnessVersion { + fn into(self) -> address::WitnessVersion { address::WitnessVersion::from_num(self.0).expect("WitnessVersion objects must be in the range 0..=16") } +} + #[derive(Clone)] #[repr(C)] /// Represents a valid secp256k1 public key serialized in "compressed form" as a 33 byte array. -- 2.39.5