X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Fc_types%2Fmod.rs;h=d4f4fafe5abd7232b1f550cf6fdf8f8c32160672;hb=HEAD;hp=3fe5eb62eece32b501e415fa22ae8bca1846d088;hpb=00ed41fad818748a795634e82040b04c1ea4455d;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 3fe5eb6..858e227 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -7,6 +7,8 @@ use bitcoin::Transaction as BitcoinTransaction; use bitcoin::Witness as BitcoinWitness; use bitcoin::address; use bitcoin::address::WitnessProgram as BitcoinWitnessProgram; +use bitcoin::key::TweakedPublicKey as BitcoinTweakedPublicKey; +use bitcoin::key::XOnlyPublicKey; use bitcoin::hashes::Hash; use bitcoin::secp256k1::PublicKey as SecpPublicKey; use bitcoin::secp256k1::SecretKey as SecpSecretKey; @@ -22,11 +24,10 @@ use core::convert::TryInto; // Bindings need at least rustc 1.34 use alloc::borrow::ToOwned; use core::ffi::c_void; -#[cfg(feature = "std")] +#[cfg(not(feature = "no-std"))] pub(crate) use std::io::{self, Cursor, Read}; #[cfg(feature = "no-std")] pub(crate) use core2::io::{self, Cursor, Read}; -#[cfg(feature = "no-std")] use alloc::{boxed::Box, vec::Vec, string::String}; use core::convert::TryFrom; @@ -169,6 +170,25 @@ impl PublicKey { pub(crate) fn null() -> Self { Self { compressed_form: [0; 33] } } } +#[derive(Clone)] +#[repr(C)] +/// Represents a tweaked X-only public key as required for BIP 340 (Taproot). +pub struct TweakedPublicKey { + /// The bytes of the public key X coordinate + pub x_coordinate: [u8; 32], +} +impl TweakedPublicKey { + pub(crate) fn from_rust(pk: &BitcoinTweakedPublicKey) -> Self { + Self { + x_coordinate: pk.serialize(), + } + } + pub(crate) fn into_rust(&self) -> BitcoinTweakedPublicKey { + let xonly_key = XOnlyPublicKey::from_slice(&self.x_coordinate).unwrap(); + BitcoinTweakedPublicKey::dangerous_assume_tweaked(xonly_key) + } +} + #[repr(C)] #[derive(Clone)] /// Represents a valid secp256k1 secret key serialized as a 32 byte array. @@ -271,6 +291,9 @@ impl BigEndianScalar { pub extern "C" fn BigEndianScalar_new(big_endian_bytes: ThirtyTwoBytes) -> BigEndianScalar { BigEndianScalar { big_endian_bytes: big_endian_bytes.data } } +#[no_mangle] +/// Creates a new BigEndianScalar which has the same data as `orig` +pub extern "C" fn BigEndianScalar_clone(orig: &BigEndianScalar) -> BigEndianScalar { orig.clone() } #[repr(C)] #[derive(Copy, Clone)] @@ -753,6 +776,7 @@ pub struct ThirtyTwoBytes { pub data: [u8; 32], } +#[derive(Clone)] #[repr(C)] /// A 3-byte byte array. pub struct ThreeBytes { /** The three bytes */ pub data: [u8; 3], } @@ -844,7 +868,7 @@ impl Str { }; String::from_utf8(bytes).unwrap() } - #[cfg(feature = "std")] + #[cfg(not(feature = "no-std"))] pub(crate) fn into_pathbuf(mut self) -> std::path::PathBuf { std::path::PathBuf::from(self.into_string()) } @@ -855,7 +879,7 @@ impl Into for String { Str { chars: s.as_ptr(), len: s.len(), chars_is_owned: true } } } -#[cfg(feature = "std")] +#[cfg(not(feature = "no-std"))] impl Into for std::path::PathBuf { fn into(self) -> Str { self.into_os_string().into_string().expect("We expect paths to be UTF-8 valid").into()