From a8468248975b48e41bdad0939208def727110fd5 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 29 Feb 2024 21:15:47 +0000 Subject: [PATCH] Add a `TweakedPubKey` to mirror `bitcoin::key::TweakedPublicKey` --- c-bindings-gen/src/types.rs | 7 +++++++ lightning-c-bindings/src/c_types/mod.rs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 47ff515..fb884c9 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -869,6 +869,7 @@ fn initial_clonable_types() -> HashSet { 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(")"), diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 3fe5eb6..bae1c40 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; @@ -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. -- 2.39.5