Skip to content

Commit 8f7b572

Browse files
committed
Update hardcoded and manually-defined types for LDK 0.0.117
1 parent 737b617 commit 8f7b572

File tree

2 files changed

+75
-34
lines changed

2 files changed

+75
-34
lines changed

c-bindings-gen/src/types.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,8 @@ fn initial_clonable_types() -> HashSet<String> {
863863
res.insert("crate::c_types::WitnessVersion".to_owned());
864864
res.insert("crate::c_types::TxIn".to_owned());
865865
res.insert("crate::c_types::TxOut".to_owned());
866-
res.insert("crate::c_types::Signature".to_owned());
866+
res.insert("crate::c_types::ECDSASignature".to_owned());
867+
res.insert("crate::c_types::SchnorrSignature".to_owned());
867868
res.insert("crate::c_types::RecoverableSignature".to_owned());
868869
res.insert("crate::c_types::BigEndianScalar".to_owned());
869870
res.insert("crate::c_types::Bech32Error".to_owned());
@@ -1030,10 +1031,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
10301031
"[u8; 12]" if !is_ref => Some("crate::c_types::TwelveBytes"),
10311032
"[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes"),
10321033
"[u8; 3]" if !is_ref => Some("crate::c_types::ThreeBytes"), // Used for RGB values
1033-
"[u16; 8]" if !is_ref => Some("crate::c_types::EightU16s"),
1034+
"[u16; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoU16s"),
10341035

10351036
"str" if is_ref => Some("crate::c_types::Str"),
1036-
"alloc::string::String"|"String" => Some("crate::c_types::Str"),
1037+
"alloc::string::String"|"String"|"std::path::PathBuf" => Some("crate::c_types::Str"),
10371038

10381039
"bitcoin::Address" => Some("crate::c_types::Str"),
10391040

@@ -1057,7 +1058,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
10571058
"core::num::NonZeroU8" => Some("u8"),
10581059

10591060
"secp256k1::PublicKey"|"bitcoin::secp256k1::PublicKey" => Some("crate::c_types::PublicKey"),
1060-
"bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::Signature"),
1061+
"bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::ECDSASignature"),
1062+
"bitcoin::secp256k1::schnorr::Signature" => Some("crate::c_types::SchnorrSignature"),
10611063
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature"),
10621064
"bitcoin::secp256k1::SecretKey" if is_ref => Some("*const [u8; 32]"),
10631065
"bitcoin::secp256k1::SecretKey" if !is_ref => Some("crate::c_types::SecretKey"),
@@ -1102,10 +1104,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
11021104
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
11031105
|"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
11041106
|"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId"
1107+
|"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId"
11051108
if is_ref => Some("*const [u8; 32]"),
11061109
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
11071110
|"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
11081111
|"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId"
1112+
|"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId"
11091113
if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
11101114

11111115
"lightning::io::Read" => Some("crate::c_types::u8slice"),
@@ -1134,13 +1138,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
11341138
"[u8; 12]" if !is_ref => Some(""),
11351139
"[u8; 4]" if !is_ref => Some(""),
11361140
"[u8; 3]" if !is_ref => Some(""),
1137-
"[u16; 8]" if !is_ref => Some(""),
1141+
"[u16; 32]" if !is_ref => Some(""),
11381142

11391143
"[u8]" if is_ref => Some(""),
11401144
"[usize]" if is_ref => Some(""),
11411145

11421146
"str" if is_ref => Some(""),
1143-
"alloc::string::String"|"String" => Some(""),
1147+
"alloc::string::String"|"String"|"std::path::PathBuf" => Some(""),
11441148
"std::io::Error"|"lightning::io::Error"|"lightning::io::ErrorKind" => Some(""),
11451149
// Note that we'll panic for String if is_ref, as we only have non-owned memory, we
11461150
// cannot create a &String.
@@ -1162,12 +1166,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
11621166

11631167
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" if is_ref => Some("&"),
11641168
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(""),
1165-
"bitcoin::secp256k1::ecdsa::Signature" if is_ref => Some("&"),
1166-
"bitcoin::secp256k1::ecdsa::Signature" => Some(""),
1169+
"bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" if is_ref => Some("&"),
1170+
"bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(""),
11671171
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(""),
11681172
"bitcoin::secp256k1::SecretKey" if is_ref => Some("&::bitcoin::secp256k1::SecretKey::from_slice(&unsafe { *"),
11691173
"bitcoin::secp256k1::SecretKey" if !is_ref => Some(""),
1170-
"bitcoin::secp256k1::KeyPair" if !is_ref => Some("::bitcoin::secp256k1::KeyPair::new("),
1174+
"bitcoin::secp256k1::KeyPair" if !is_ref => Some("::bitcoin::secp256k1::KeyPair::from_secret_key(&secp256k1::global::SECP256K1, &"),
11711175
"bitcoin::secp256k1::Scalar" if is_ref => Some("&"),
11721176
"bitcoin::secp256k1::Scalar" if !is_ref => Some(""),
11731177
"bitcoin::secp256k1::ecdh::SharedSecret" if !is_ref => Some("::bitcoin::secp256k1::ecdh::SharedSecret::from_bytes("),
@@ -1217,6 +1221,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
12171221
"lightning::ln::channelmanager::PaymentId" if is_ref=> Some("&::lightning::ln::channelmanager::PaymentId( unsafe { *"),
12181222
"lightning::ln::channelmanager::InterceptId" if !is_ref => Some("::lightning::ln::channelmanager::InterceptId("),
12191223
"lightning::ln::channelmanager::InterceptId" if is_ref=> Some("&::lightning::ln::channelmanager::InterceptId( unsafe { *"),
1224+
"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if !is_ref => Some("::lightning::ln::ChannelId("),
1225+
"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId" if is_ref => Some("&::lightning::ln::ChannelId(unsafe { *"),
12201226
"lightning::sign::KeyMaterial" if !is_ref => Some("::lightning::sign::KeyMaterial("),
12211227
"lightning::sign::KeyMaterial" if is_ref=> Some("&::lightning::sign::KeyMaterial( unsafe { *"),
12221228
"lightning::chain::ClaimId" if !is_ref => Some("::lightning::chain::ClaimId("),
@@ -1244,13 +1250,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
12441250
"[u8; 12]" if !is_ref => Some(".data"),
12451251
"[u8; 4]" if !is_ref => Some(".data"),
12461252
"[u8; 3]" if !is_ref => Some(".data"),
1247-
"[u16; 8]" if !is_ref => Some(".data"),
1253+
"[u16; 32]" if !is_ref => Some(".data"),
12481254

12491255
"[u8]" if is_ref => Some(".to_slice()"),
12501256
"[usize]" if is_ref => Some(".to_slice()"),
12511257

12521258
"str" if is_ref => Some(".into_str()"),
12531259
"alloc::string::String"|"String" => Some(".into_string()"),
1260+
"std::path::PathBuf" => Some(".into_pathbuf()"),
12541261
"std::io::Error"|"lightning::io::Error" => Some(".to_rust()"),
12551262
"lightning::io::ErrorKind" => Some(".to_rust_kind()"),
12561263

@@ -1270,7 +1277,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
12701277
"core::num::NonZeroU8" => Some(").expect(\"Value must be non-zero\")"),
12711278

12721279
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(".into_rust()"),
1273-
"bitcoin::secp256k1::ecdsa::Signature" => Some(".into_rust()"),
1280+
"bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(".into_rust()"),
12741281
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(".into_rust()"),
12751282
"bitcoin::secp256k1::SecretKey" if !is_ref => Some(".into_rust()"),
12761283
"bitcoin::secp256k1::SecretKey" if is_ref => Some("}[..]).unwrap()"),
@@ -1311,10 +1318,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
13111318
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
13121319
|"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
13131320
|"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId"
1321+
|"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId"
13141322
if !is_ref => Some(".data)"),
13151323
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
13161324
|"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
13171325
|"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId"
1326+
|"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId"
13181327
if is_ref => Some(" })"),
13191328

13201329
// List of traits we map (possibly during processing of other files):
@@ -1355,13 +1364,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
13551364
"[u8; 12]" if !is_ref => Some("crate::c_types::TwelveBytes { data: "),
13561365
"[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes { data: "),
13571366
"[u8; 3]" if is_ref => Some(""),
1358-
"[u16; 8]" if !is_ref => Some("crate::c_types::EightU16s { data: "),
1367+
"[u16; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoU16s { data: "),
13591368

13601369
"[u8]" if is_ref => Some("local_"),
13611370
"[usize]" if is_ref => Some("local_"),
13621371

13631372
"str" if is_ref => Some(""),
1364-
"alloc::string::String"|"String" => Some(""),
1373+
"alloc::string::String"|"String"|"std::path::PathBuf" => Some(""),
13651374

13661375
"bitcoin::Address" => Some("alloc::string::ToString::to_string(&"),
13671376

@@ -1385,7 +1394,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
13851394
"u128" => Some(""),
13861395

13871396
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some("crate::c_types::PublicKey::from_rust(&"),
1388-
"bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::Signature::from_rust(&"),
1397+
"bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::ECDSASignature::from_rust(&"),
1398+
"bitcoin::secp256k1::schnorr::Signature" => Some("crate::c_types::SchnorrSignature::from_rust(&"),
13891399
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature::from_rust(&"),
13901400
"bitcoin::secp256k1::SecretKey" if is_ref => Some(""),
13911401
"bitcoin::secp256k1::SecretKey" if !is_ref => Some("crate::c_types::SecretKey::from_rust("),
@@ -1429,10 +1439,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
14291439
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
14301440
|"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
14311441
|"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId"
1442+
|"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId"
14321443
if is_ref => Some("&"),
14331444
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
14341445
|"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
14351446
|"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId"
1447+
|"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId"
14361448
if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
14371449

14381450
"lightning::io::Read" => Some("crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec("),
@@ -1456,14 +1468,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
14561468
"[u8; 12]" if !is_ref => Some(" }"),
14571469
"[u8; 4]" if !is_ref => Some(" }"),
14581470
"[u8; 3]" if is_ref => Some(""),
1459-
"[u16; 8]" if !is_ref => Some(" }"),
1471+
"[u16; 32]" if !is_ref => Some(" }"),
14601472

14611473
"[u8]" if is_ref => Some(""),
14621474
"[usize]" if is_ref => Some(""),
14631475

14641476
"str" if is_ref => Some(".into()"),
1465-
"alloc::string::String"|"String" if is_ref => Some(".as_str().into()"),
1466-
"alloc::string::String"|"String" => Some(".into()"),
1477+
"alloc::string::String"|"String"|"std::path::PathBuf" if is_ref => Some(".as_str().into()"),
1478+
"alloc::string::String"|"String"|"std::path::PathBuf" => Some(".into()"),
14671479

14681480
"bitcoin::Address" => Some(").into()"),
14691481

@@ -1486,7 +1498,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
14861498
"u128" => Some(".into()"),
14871499

14881500
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(")"),
1489-
"bitcoin::secp256k1::ecdsa::Signature" => Some(")"),
1501+
"bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(")"),
14901502
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(")"),
14911503
"bitcoin::secp256k1::SecretKey" if !is_ref => Some(")"),
14921504
"bitcoin::secp256k1::SecretKey" if is_ref => Some(".as_ref()"),
@@ -1528,10 +1540,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
15281540
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
15291541
|"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
15301542
|"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId"
1543+
|"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId"
15311544
if is_ref => Some(".0"),
15321545
"lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
15331546
|"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
15341547
|"lightning::sign::KeyMaterial"|"lightning::chain::ClaimId"
1548+
|"lightning::ln::ChannelId"|"lightning::ln::channel_id::ChannelId"
15351549
if !is_ref => Some(".0 }"),
15361550

15371551
"lightning::io::Read" => Some("))"),

lightning-c-bindings/src/c_types/mod.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use bitcoin::Witness as BitcoinWitness;
88
use bitcoin::hashes::Hash;
99
use bitcoin::secp256k1::PublicKey as SecpPublicKey;
1010
use bitcoin::secp256k1::SecretKey as SecpSecretKey;
11-
use bitcoin::secp256k1::ecdsa::Signature as SecpSignature;
11+
use bitcoin::secp256k1::ecdsa::Signature as ECDSASecpSignature;
12+
use bitcoin::secp256k1::schnorr::Signature as SchnorrSecpSignature;
1213
use bitcoin::secp256k1::Error as SecpError;
1314
use bitcoin::secp256k1::ecdsa::RecoveryId;
1415
use bitcoin::secp256k1::ecdsa::RecoverableSignature as SecpRecoverableSignature;
@@ -17,6 +18,7 @@ use bitcoin::bech32;
1718
use bitcoin::util::address;
1819

1920
use core::convert::TryInto; // Bindings need at least rustc 1.34
21+
use alloc::borrow::ToOwned;
2022
use core::ffi::c_void;
2123

2224
#[cfg(feature = "std")]
@@ -135,23 +137,38 @@ impl SecretKey {
135137

136138
#[repr(C)]
137139
#[derive(Clone)]
138-
/// Represents a secp256k1 signature serialized as two 32-byte numbers
139-
pub struct Signature {
140+
/// Represents a secp256k1 ECDSA signature serialized as two 32-byte numbers
141+
pub struct ECDSASignature {
140142
/// The bytes of the signature in "compact" form
141143
pub compact_form: [u8; 64],
142144
}
143-
impl Signature {
144-
pub(crate) fn from_rust(pk: &SecpSignature) -> Self {
145+
impl ECDSASignature {
146+
pub(crate) fn from_rust(pk: &ECDSASecpSignature) -> Self {
145147
Self {
146148
compact_form: pk.serialize_compact(),
147149
}
148150
}
149-
pub(crate) fn into_rust(&self) -> SecpSignature {
150-
SecpSignature::from_compact(&self.compact_form).unwrap()
151+
pub(crate) fn into_rust(&self) -> ECDSASecpSignature {
152+
ECDSASecpSignature::from_compact(&self.compact_form).unwrap()
153+
}
154+
}
155+
156+
#[repr(C)]
157+
#[derive(Clone)]
158+
/// Represents a secp256k1 Schnorr signature serialized as two 32-byte numbers
159+
pub struct SchnorrSignature {
160+
/// The bytes of the signature as two 32-byte numbers
161+
pub compact_form: [u8; 64],
162+
}
163+
impl SchnorrSignature {
164+
pub(crate) fn from_rust(pk: &SchnorrSecpSignature) -> Self {
165+
Self {
166+
compact_form: pk.as_ref().clone(),
167+
}
168+
}
169+
pub(crate) fn into_rust(&self) -> SchnorrSecpSignature {
170+
SchnorrSecpSignature::from_slice(&self.compact_form).unwrap()
151171
}
152-
// The following are used for Option<Signature> which we support, but don't use anymore
153-
#[allow(unused)] pub(crate) fn is_null(&self) -> bool { self.compact_form[..] == [0; 64][..] }
154-
#[allow(unused)] pub(crate) fn null() -> Self { Self { compact_form: [0; 64] } }
155172
}
156173

157174
#[repr(C)]
@@ -673,8 +690,8 @@ pub struct TwentyBytes { /** The twenty bytes */ pub data: [u8; 20], }
673690

674691
#[derive(Clone)]
675692
#[repr(C)]
676-
/// 8 u16s
677-
pub struct EightU16s { /** The eight 16-bit integers */ pub data: [u16; 8], }
693+
/// 32 u16s
694+
pub struct ThirtyTwoU16s { /** The thirty-two 16-bit integers */ pub data: [u16; 32], }
678695

679696
pub(crate) struct VecWriter(pub Vec<u8>);
680697
impl lightning::util::ser::Writer for VecWriter {
@@ -709,14 +726,14 @@ pub struct Str {
709726
/// Whether the data pointed to by `chars` should be freed or not.
710727
pub chars_is_owned: bool,
711728
}
712-
impl Into<Str> for &'static str {
729+
impl Into<Str> for &str {
713730
fn into(self) -> Str {
714-
Str { chars: self.as_ptr(), len: self.len(), chars_is_owned: false }
731+
self.to_owned().into()
715732
}
716733
}
717-
impl Into<Str> for &mut &'static str {
734+
impl Into<Str> for &mut &str {
718735
fn into(self) -> Str {
719-
let us: &'static str = *self;
736+
let us: &str = *self;
720737
us.into()
721738
}
722739
}
@@ -742,13 +759,23 @@ impl Str {
742759
};
743760
String::from_utf8(bytes).unwrap()
744761
}
762+
#[cfg(feature = "std")]
763+
pub(crate) fn into_pathbuf(mut self) -> std::path::PathBuf {
764+
std::path::PathBuf::from(self.into_string())
765+
}
745766
}
746767
impl Into<Str> for String {
747768
fn into(self) -> Str {
748769
let s = Box::leak(self.into_boxed_str());
749770
Str { chars: s.as_ptr(), len: s.len(), chars_is_owned: true }
750771
}
751772
}
773+
#[cfg(feature = "std")]
774+
impl Into<Str> for std::path::PathBuf {
775+
fn into(self) -> Str {
776+
self.into_os_string().into_string().expect("We expect paths to be UTF-8 valid").into()
777+
}
778+
}
752779
impl Clone for Str {
753780
fn clone(&self) -> Self {
754781
String::from(self.into_str()).into()

0 commit comments

Comments
 (0)