1 //! This module contains standard C-mapped types for types not in the original crate.
3 /// Auto-generated C-mapped types for templated containers
6 use bitcoin::Transaction as BitcoinTransaction;
7 use bitcoin::hashes::Hash;
8 use bitcoin::secp256k1::key::PublicKey as SecpPublicKey;
9 use bitcoin::secp256k1::key::SecretKey as SecpSecretKey;
10 use bitcoin::secp256k1::Signature as SecpSignature;
11 use bitcoin::secp256k1::Error as SecpError;
12 use bitcoin::secp256k1::recovery::RecoveryId;
13 use bitcoin::secp256k1::recovery::RecoverableSignature as SecpRecoverableSignature;
16 use std::convert::TryInto; // Bindings need at least rustc 1.34
18 use std::io::{Cursor, Read}; // TODO: We should use core2 here when we support no_std
21 /// A dummy struct of which an instance must never exist.
22 /// This corresponds to the Rust type `Infallible`, or, in unstable rust, `!`
23 pub struct NotConstructable {
24 _priv_thing: core::convert::Infallible,
26 impl From<core::convert::Infallible> for NotConstructable {
27 fn from(_: core::convert::Infallible) -> Self { unreachable!(); }
30 /// Integer in the range `0..32`
31 #[derive(PartialEq, Eq, Copy, Clone)]
32 #[allow(non_camel_case_types)]
36 impl From<bech32::u5> for u5 {
37 fn from(o: bech32::u5) -> Self { Self(o.to_u8()) }
39 impl Into<bech32::u5> for u5 {
40 fn into(self) -> bech32::u5 { bech32::u5::try_from_u8(self.0).expect("u5 objects must be in the range 0..32") }
45 /// Represents a valid secp256k1 public key serialized in "compressed form" as a 33 byte array.
46 pub struct PublicKey {
47 /// The bytes of the public key
48 pub compressed_form: [u8; 33],
51 pub(crate) fn from_rust(pk: &SecpPublicKey) -> Self {
53 compressed_form: pk.serialize(),
56 pub(crate) fn into_rust(&self) -> SecpPublicKey {
57 SecpPublicKey::from_slice(&self.compressed_form).unwrap()
59 pub(crate) fn is_null(&self) -> bool { self.compressed_form[..] == [0; 33][..] }
60 pub(crate) fn null() -> Self { Self { compressed_form: [0; 33] } }
64 /// Represents a valid secp256k1 secret key serialized as a 32 byte array.
65 pub struct SecretKey {
66 /// The bytes of the secret key
70 // from_rust isn't implemented for a ref since we just return byte array refs directly
71 pub(crate) fn from_rust(sk: SecpSecretKey) -> Self {
72 let mut bytes = [0; 32];
73 bytes.copy_from_slice(&sk[..]);
76 pub(crate) fn into_rust(&self) -> SecpSecretKey {
77 SecpSecretKey::from_slice(&self.bytes).unwrap()
83 /// Represents a secp256k1 signature serialized as two 32-byte numbers
84 pub struct Signature {
85 /// The bytes of the signature in "compact" form
86 pub compact_form: [u8; 64],
89 pub(crate) fn from_rust(pk: &SecpSignature) -> Self {
91 compact_form: pk.serialize_compact(),
94 pub(crate) fn into_rust(&self) -> SecpSignature {
95 SecpSignature::from_compact(&self.compact_form).unwrap()
97 // The following are used for Option<Signature> which we support, but don't use anymore
98 #[allow(unused)] pub(crate) fn is_null(&self) -> bool { self.compact_form[..] == [0; 64][..] }
99 #[allow(unused)] pub(crate) fn null() -> Self { Self { compact_form: [0; 64] } }
104 /// Represents a secp256k1 signature serialized as two 32-byte numbers as well as a tag which
105 /// allows recovering the exact public key which created the signature given the message.
106 pub struct RecoverableSignature {
107 /// The bytes of the signature in "compact" form plus a "Recovery ID" which allows for
109 pub serialized_form: [u8; 68],
111 impl RecoverableSignature {
112 pub(crate) fn from_rust(pk: &SecpRecoverableSignature) -> Self {
113 let (id, compact_form) = pk.serialize_compact();
114 let mut serialized_form = [0; 68];
115 serialized_form[0..64].copy_from_slice(&compact_form[..]);
116 serialized_form[64..].copy_from_slice(&id.to_i32().to_le_bytes());
117 Self { serialized_form }
119 pub(crate) fn into_rust(&self) -> SecpRecoverableSignature {
121 id.copy_from_slice(&self.serialized_form[64..]);
122 SecpRecoverableSignature::from_compact(&self.serialized_form[0..64],
123 RecoveryId::from_i32(i32::from_le_bytes(id)).expect("Invalid Recovery ID"))
129 #[derive(Copy, Clone)]
130 /// Represents an error returned from libsecp256k1 during validation of some secp256k1 data
131 pub enum Secp256k1Error {
132 /// Signature failed verification
134 /// Badly sized message ("messages" are actually fixed-sized digests; see the MESSAGE_SIZE constant)
144 /// Invalid tweak for add_assign or mul_assign
146 /// tweak_add_check failed on an xonly public key
148 /// Didn't pass enough memory to context creation with preallocated memory
151 impl Secp256k1Error {
152 pub(crate) fn from_rust(err: SecpError) -> Self {
154 SecpError::IncorrectSignature => Secp256k1Error::IncorrectSignature,
155 SecpError::InvalidMessage => Secp256k1Error::InvalidMessage,
156 SecpError::InvalidPublicKey => Secp256k1Error::InvalidPublicKey,
157 SecpError::InvalidSignature => Secp256k1Error::InvalidSignature,
158 SecpError::InvalidSecretKey => Secp256k1Error::InvalidSecretKey,
159 SecpError::InvalidRecoveryId => Secp256k1Error::InvalidRecoveryId,
160 SecpError::InvalidTweak => Secp256k1Error::InvalidTweak,
161 SecpError::TweakCheckFailed => Secp256k1Error::TweakCheckFailed,
162 SecpError::NotEnoughMemory => Secp256k1Error::NotEnoughMemory,
168 #[allow(missing_docs)] // If there's no docs upstream, that's good enough for us
169 #[derive(Clone, Copy, PartialEq)]
170 /// Represents an IO Error. Note that some information is lost in the conversion from Rust.
192 pub(crate) fn from_rust(err: std::io::Error) -> Self {
194 std::io::ErrorKind::NotFound => IOError::NotFound,
195 std::io::ErrorKind::PermissionDenied => IOError::PermissionDenied,
196 std::io::ErrorKind::ConnectionRefused => IOError::ConnectionRefused,
197 std::io::ErrorKind::ConnectionReset => IOError::ConnectionReset,
198 std::io::ErrorKind::ConnectionAborted => IOError::ConnectionAborted,
199 std::io::ErrorKind::NotConnected => IOError::NotConnected,
200 std::io::ErrorKind::AddrInUse => IOError::AddrInUse,
201 std::io::ErrorKind::AddrNotAvailable => IOError::AddrNotAvailable,
202 std::io::ErrorKind::BrokenPipe => IOError::BrokenPipe,
203 std::io::ErrorKind::AlreadyExists => IOError::AlreadyExists,
204 std::io::ErrorKind::WouldBlock => IOError::WouldBlock,
205 std::io::ErrorKind::InvalidInput => IOError::InvalidInput,
206 std::io::ErrorKind::InvalidData => IOError::InvalidData,
207 std::io::ErrorKind::TimedOut => IOError::TimedOut,
208 std::io::ErrorKind::WriteZero => IOError::WriteZero,
209 std::io::ErrorKind::Interrupted => IOError::Interrupted,
210 std::io::ErrorKind::Other => IOError::Other,
211 std::io::ErrorKind::UnexpectedEof => IOError::UnexpectedEof,
215 pub(crate) fn to_rust(&self) -> std::io::Error {
216 std::io::Error::new(match self {
217 IOError::NotFound => std::io::ErrorKind::NotFound,
218 IOError::PermissionDenied => std::io::ErrorKind::PermissionDenied,
219 IOError::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
220 IOError::ConnectionReset => std::io::ErrorKind::ConnectionReset,
221 IOError::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
222 IOError::NotConnected => std::io::ErrorKind::NotConnected,
223 IOError::AddrInUse => std::io::ErrorKind::AddrInUse,
224 IOError::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
225 IOError::BrokenPipe => std::io::ErrorKind::BrokenPipe,
226 IOError::AlreadyExists => std::io::ErrorKind::AlreadyExists,
227 IOError::WouldBlock => std::io::ErrorKind::WouldBlock,
228 IOError::InvalidInput => std::io::ErrorKind::InvalidInput,
229 IOError::InvalidData => std::io::ErrorKind::InvalidData,
230 IOError::TimedOut => std::io::ErrorKind::TimedOut,
231 IOError::WriteZero => std::io::ErrorKind::WriteZero,
232 IOError::Interrupted => std::io::ErrorKind::Interrupted,
233 IOError::Other => std::io::ErrorKind::Other,
234 IOError::UnexpectedEof => std::io::ErrorKind::UnexpectedEof,
240 /// A serialized transaction, in (pointer, length) form.
242 /// This type optionally owns its own memory, and thus the semantics around access change based on
243 /// the `data_is_owned` flag. If `data_is_owned` is set, you must call `Transaction_free` to free
244 /// the underlying buffer before the object goes out of scope. If `data_is_owned` is not set, any
245 /// access to the buffer after the scope in which the object was provided to you is invalid. eg,
246 /// access after you return from the call in which a `!data_is_owned` `Transaction` is provided to
247 /// you would be invalid.
249 /// Note that, while it may change in the future, because transactions on the Rust side are stored
250 /// in a deserialized form, all `Transaction`s generated on the Rust side will have `data_is_owned`
251 /// set. Similarly, while it may change in the future, all `Transaction`s you pass to Rust may have
252 /// `data_is_owned` either set or unset at your discretion.
253 pub struct Transaction {
254 /// The serialized transaction data.
256 /// This is non-const for your convenience, an object passed to Rust is never written to.
258 /// The length of the serialized transaction
260 /// Whether the data pointed to by `data` should be freed or not.
261 pub data_is_owned: bool,
264 fn from_vec(vec: Vec<u8>) -> Self {
265 let datalen = vec.len();
266 let data = Box::into_raw(vec.into_boxed_slice());
268 data: unsafe { (*data).as_mut_ptr() },
273 pub(crate) fn into_bitcoin(&self) -> BitcoinTransaction {
274 if self.datalen == 0 { panic!("0-length buffer can never represent a valid Transaction"); }
275 ::bitcoin::consensus::encode::deserialize(unsafe { std::slice::from_raw_parts(self.data, self.datalen) }).unwrap()
277 pub(crate) fn from_bitcoin(btc: &BitcoinTransaction) -> Self {
278 let vec = ::bitcoin::consensus::encode::serialize(btc);
282 impl Drop for Transaction {
284 if self.data_is_owned && self.datalen != 0 {
285 let _ = derived::CVec_u8Z { data: self.data as *mut u8, datalen: self.datalen };
289 impl Clone for Transaction {
290 fn clone(&self) -> Self {
291 let sl = unsafe { std::slice::from_raw_parts(self.data, self.datalen) };
292 let mut v = Vec::new();
293 v.extend_from_slice(&sl);
298 /// Frees the data buffer, if data_is_owned is set and datalen > 0.
299 pub extern "C" fn Transaction_free(_res: Transaction) { }
301 pub(crate) fn bitcoin_to_C_outpoint(outpoint: ::bitcoin::blockdata::transaction::OutPoint) -> crate::lightning::chain::transaction::OutPoint {
302 crate::lightning::chain::transaction::OutPoint_new(ThirtyTwoBytes { data: outpoint.txid.into_inner() }, outpoint.vout.try_into().unwrap())
304 pub(crate) fn C_to_bitcoin_outpoint(outpoint: crate::lightning::chain::transaction::OutPoint) -> ::bitcoin::blockdata::transaction::OutPoint {
306 ::bitcoin::blockdata::transaction::OutPoint {
307 txid: (*outpoint.inner).txid, vout: (*outpoint.inner).index as u32
314 /// A transaction output including a scriptPubKey and value.
315 /// This type *does* own its own memory, so must be free'd appropriately.
317 /// The script_pubkey in this output
318 pub script_pubkey: derived::CVec_u8Z,
319 /// The value, in satoshis, of this output
324 pub(crate) fn into_rust(mut self) -> ::bitcoin::blockdata::transaction::TxOut {
325 ::bitcoin::blockdata::transaction::TxOut {
326 script_pubkey: self.script_pubkey.into_rust().into(),
330 pub(crate) fn from_rust(txout: ::bitcoin::blockdata::transaction::TxOut) -> Self {
332 script_pubkey: derived::CVec_u8Z::from(txout.script_pubkey.into_bytes()),
339 /// Convenience function for constructing a new TxOut
340 pub extern "C" fn TxOut_new(script_pubkey: derived::CVec_u8Z, value: u64) -> TxOut {
341 TxOut { script_pubkey, value }
344 /// Frees the data pointed to by script_pubkey.
345 pub extern "C" fn TxOut_free(_res: TxOut) { }
347 /// Creates a new TxOut which has the same data as `orig` but with a new script buffer.
348 pub extern "C" fn TxOut_clone(orig: &TxOut) -> TxOut { orig.clone() }
351 /// A "slice" referencing some byte array. This is simply a length-tagged pointer which does not
352 /// own the memory pointed to by data.
354 /// A pointer to the byte buffer
356 /// The number of bytes pointed to by `data`.
360 pub(crate) fn from_slice(s: &[u8]) -> Self {
366 pub(crate) fn to_slice(&self) -> &[u8] {
367 if self.datalen == 0 { return &[]; }
368 unsafe { std::slice::from_raw_parts(self.data, self.datalen) }
370 pub(crate) fn to_reader<'a>(&'a self) -> Cursor<&'a [u8]> {
371 let sl = self.to_slice();
374 pub(crate) fn from_vec(v: &derived::CVec_u8Z) -> u8slice {
375 Self::from_slice(v.as_slice())
378 pub(crate) fn reader_to_vec<R: Read>(r: &mut R) -> derived::CVec_u8Z {
379 let mut res = Vec::new();
380 r.read_to_end(&mut res).unwrap();
381 derived::CVec_u8Z::from(res)
385 #[derive(Copy, Clone)]
386 /// Arbitrary 32 bytes, which could represent one of a few different things. You probably want to
387 /// look up the corresponding function in rust-lightning's docs.
388 pub struct ThirtyTwoBytes {
389 /// The thirty-two bytes
392 impl ThirtyTwoBytes {
393 pub(crate) fn null() -> Self {
394 Self { data: [0; 32] }
399 /// A 3-byte byte array.
400 pub struct ThreeBytes { /** The three bytes */ pub data: [u8; 3], }
403 /// A 4-byte byte array.
404 pub struct FourBytes { /** The four bytes */ pub data: [u8; 4], }
407 /// A 10-byte byte array.
408 pub struct TenBytes { /** The ten bytes */ pub data: [u8; 10], }
411 /// A 16-byte byte array.
412 pub struct SixteenBytes { /** The sixteen bytes */ pub data: [u8; 16], }
415 /// A 20-byte byte array.
416 pub struct TwentyBytes { /** The twenty bytes */ pub data: [u8; 20], }
418 pub(crate) struct VecWriter(pub Vec<u8>);
419 impl lightning::util::ser::Writer for VecWriter {
420 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
421 self.0.extend_from_slice(buf);
425 pub(crate) fn serialize_obj<I: lightning::util::ser::Writeable>(i: &I) -> derived::CVec_u8Z {
426 let mut out = VecWriter(Vec::new());
427 i.write(&mut out).unwrap();
428 derived::CVec_u8Z::from(out.0)
430 pub(crate) fn deserialize_obj<I: lightning::util::ser::Readable>(s: u8slice) -> Result<I, lightning::ln::msgs::DecodeError> {
431 I::read(&mut s.to_slice())
433 pub(crate) fn deserialize_obj_arg<A, I: lightning::util::ser::ReadableArgs<A>>(s: u8slice, args: A) -> Result<I, lightning::ln::msgs::DecodeError> {
434 I::read(&mut s.to_slice(), args)
439 /// A Rust str object, ie a reference to a UTF8-valid string.
440 /// This is *not* null-terminated so cannot be used directly as a C string!
442 /// A pointer to the string's bytes, in UTF8 encoding
443 pub chars: *const u8,
444 /// The number of bytes (not characters!) pointed to by `chars`
446 /// Whether the data pointed to by `chars` should be freed or not.
447 pub chars_is_owned: bool,
449 impl Into<Str> for &'static str {
450 fn into(self) -> Str {
451 Str { chars: self.as_ptr(), len: self.len(), chars_is_owned: false }
455 pub(crate) fn into_str(&self) -> &'static str {
456 if self.len == 0 { return ""; }
457 std::str::from_utf8(unsafe { std::slice::from_raw_parts(self.chars, self.len) }).unwrap()
459 pub(crate) fn into_string(mut self) -> String {
460 let bytes = if self.len == 0 {
462 } else if self.chars_is_owned {
464 Box::from_raw(std::slice::from_raw_parts_mut(unsafe { self.chars as *mut u8 }, self.len))
466 self.chars_is_owned = false;
469 let mut ret = Vec::with_capacity(self.len);
470 ret.extend_from_slice(unsafe { std::slice::from_raw_parts(self.chars, self.len) });
473 String::from_utf8(bytes).unwrap()
476 impl Into<Str> for String {
477 fn into(self) -> Str {
478 let s = Box::leak(self.into_boxed_str());
479 Str { chars: s.as_ptr(), len: s.len(), chars_is_owned: true }
485 if self.chars_is_owned && self.len != 0 {
486 let _ = derived::CVec_u8Z { data: self.chars as *mut u8, datalen: self.len };
491 /// Frees the data buffer, if chars_is_owned is set and len > 0.
492 pub extern "C" fn Str_free(_res: Str) { }
494 // Note that the C++ headers memset(0) all the Templ types to avoid deallocation!
495 // Thus, they must gracefully handle being completely null in _free.
497 // TODO: Integer/bool primitives should avoid the pointer indirection for underlying types
498 // everywhere in the containers.
501 pub(crate) union CResultPtr<O, E> {
502 pub(crate) result: *mut O,
503 pub(crate) err: *mut E,
506 pub(crate) struct CResultTempl<O, E> {
507 pub(crate) contents: CResultPtr<O, E>,
508 pub(crate) result_ok: bool,
510 impl<O, E> CResultTempl<O, E> {
511 pub(crate) extern "C" fn ok(o: O) -> Self {
513 contents: CResultPtr {
514 result: Box::into_raw(Box::new(o)),
519 pub(crate) extern "C" fn err(e: E) -> Self {
521 contents: CResultPtr {
522 err: Box::into_raw(Box::new(e)),
528 impl<O, E> Drop for CResultTempl<O, E> {
531 if unsafe { !self.contents.result.is_null() } {
532 unsafe { Box::from_raw(self.contents.result) };
534 } else if unsafe { !self.contents.err.is_null() } {
535 unsafe { Box::from_raw(self.contents.err) };
540 /// Utility to make it easy to set a pointer to null and get its original value in line.
541 pub(crate) trait TakePointer<T> {
542 fn take_ptr(&mut self) -> T;
544 impl<T> TakePointer<*const T> for *const T {
545 fn take_ptr(&mut self) -> *const T {
547 *self = std::ptr::null();
551 impl<T> TakePointer<*mut T> for *mut T {
552 fn take_ptr(&mut self) -> *mut T {
554 *self = std::ptr::null_mut();
560 pub(crate) mod ObjOps {
562 #[must_use = "returns new dangling pointer"]
563 pub(crate) fn heap_alloc<T>(obj: T) -> *mut T {
564 let ptr = Box::into_raw(Box::new(obj));
565 nonnull_ptr_to_inner(ptr)
568 pub(crate) fn nonnull_ptr_to_inner<T>(ptr: *const T) -> *mut T {
569 if core::mem::size_of::<T>() == 0 {
570 // We map `None::<T>` as `T { inner: null, .. }` which works great for all
571 // non-Zero-Sized-Types `T`.
572 // For ZSTs, we need to differentiate between null implying `None` and null implying
573 // `Some` with no allocation.
574 // Thus, for ZSTs, we add one (usually) page here, which should always be aligned.
575 // Note that this relies on undefined behavior! A pointer to NULL may be valid, but a
576 // pointer to NULL + 4096 is almost certainly not. That said, Rust's existing use of
577 // `(*mut T)1` for the pointer we're adding to is also not defined, so we should be
579 // Note that we add 4095 here as at least the Java client assumes that the low bit on
580 // any heap pointer is 0, which is generally provided by malloc, but which is not true
581 // for ZSTs "allocated" by `Box::new`.
582 debug_assert_eq!(ptr as usize, 1);
583 unsafe { (ptr as *mut T).cast::<u8>().add(4096 - 1).cast::<T>() }
585 // In order to get better test coverage, also increment non-ZST pointers with
586 // --cfg=test_mod_pointers, which is set in genbindings.sh for debug builds.
587 #[cfg(test_mod_pointers)]
588 unsafe { (ptr as *mut T).cast::<u8>().add(4096).cast::<T>() }
589 #[cfg(not(test_mod_pointers))]
590 unsafe { ptr as *mut T }
594 /// Invert nonnull_ptr_to_inner
595 pub(crate) fn untweak_ptr<T>(ptr: *mut T) -> *mut T {
596 if core::mem::size_of::<T>() == 0 {
597 unsafe { ptr.cast::<u8>().sub(4096 - 1).cast::<T>() }
599 #[cfg(test_mod_pointers)]
600 unsafe { ptr.cast::<u8>().sub(4096).cast::<T>() }
601 #[cfg(not(test_mod_pointers))]