]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Add a `TweakedPubKey` to mirror `bitcoin::key::TweakedPublicKey`
authorMatt Corallo <git@bluematt.me>
Thu, 29 Feb 2024 21:15:47 +0000 (21:15 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 12 May 2024 14:34:31 +0000 (14:34 +0000)
c-bindings-gen/src/types.rs
lightning-c-bindings/src/c_types/mod.rs

index 47ff515ffa85651ec424fc95fe3aa477aa3e55ad..fb884c91fc8e3db93429c945457d340aa9f67c80 100644 (file)
@@ -869,6 +869,7 @@ fn initial_clonable_types() -> HashSet<String> {
        res.insert("crate::c_types::EightU16s".to_owned());
        res.insert("crate::c_types::SecretKey".to_owned());
        res.insert("crate::c_types::PublicKey".to_owned());
+       res.insert("crate::c_types::TweakedPublicKey".to_owned());
        res.insert("crate::c_types::Transaction".to_owned());
        res.insert("crate::c_types::Witness".to_owned());
        res.insert("crate::c_types::WitnessVersion".to_owned());
@@ -1071,6 +1072,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "core::num::NonZeroU8" => Some("u8"),
 
                        "secp256k1::PublicKey"|"bitcoin::secp256k1::PublicKey" => Some("crate::c_types::PublicKey"),
+                       "bitcoin::key::TweakedPublicKey" => Some("crate::c_types::TweakedPublicKey"),
                        "bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::ECDSASignature"),
                        "bitcoin::secp256k1::schnorr::Signature" => Some("crate::c_types::SchnorrSignature"),
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature"),
@@ -1182,6 +1184,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" if is_ref => Some("&"),
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(""),
+                       "bitcoin::key::TweakedPublicKey" if is_ref => Some("&"),
+                       "bitcoin::key::TweakedPublicKey" => Some(""),
                        "bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" if is_ref => Some("&"),
                        "bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(""),
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(""),
@@ -1296,6 +1300,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "core::num::NonZeroU8" => Some(").expect(\"Value must be non-zero\")"),
 
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(".into_rust()"),
+                       "bitcoin::key::TweakedPublicKey" => Some(".into_rust()"),
                        "bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(".into_rust()"),
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(".into_rust()"),
                        "bitcoin::secp256k1::SecretKey" if !is_ref => Some(".into_rust()"),
@@ -1414,6 +1419,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "u128" => Some(""),
 
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some("crate::c_types::PublicKey::from_rust(&"),
+                       "bitcoin::key::TweakedPublicKey" => Some("crate::c_types::TweakedPublicKey::from_rust(&"),
                        "bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::ECDSASignature::from_rust(&"),
                        "bitcoin::secp256k1::schnorr::Signature" => Some("crate::c_types::SchnorrSignature::from_rust(&"),
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature::from_rust(&"),
@@ -1519,6 +1525,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "u128" => Some(".into()"),
 
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(")"),
+                       "bitcoin::key::TweakedPublicKey" => Some(")"),
                        "bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(")"),
                        "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(")"),
                        "bitcoin::secp256k1::SecretKey" if !is_ref => Some(")"),
index 3fe5eb62eece32b501e415fa22ae8bca1846d088..bae1c404e5c6b6d7eeaa0e81627a13b13704f122 100644 (file)
@@ -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;
@@ -169,6 +171,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.