Add a `TweakedPubKey` to mirror `bitcoin::key::TweakedPublicKey`
[ldk-c-bindings] / lightning-c-bindings / src / c_types / mod.rs
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.