Skip to content

Commit a846824

Browse files
committed
Add a TweakedPubKey to mirror bitcoin::key::TweakedPublicKey
1 parent 8b185c0 commit a846824

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

c-bindings-gen/src/types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ fn initial_clonable_types() -> HashSet<String> {
869869
res.insert("crate::c_types::EightU16s".to_owned());
870870
res.insert("crate::c_types::SecretKey".to_owned());
871871
res.insert("crate::c_types::PublicKey".to_owned());
872+
res.insert("crate::c_types::TweakedPublicKey".to_owned());
872873
res.insert("crate::c_types::Transaction".to_owned());
873874
res.insert("crate::c_types::Witness".to_owned());
874875
res.insert("crate::c_types::WitnessVersion".to_owned());
@@ -1071,6 +1072,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
10711072
"core::num::NonZeroU8" => Some("u8"),
10721073

10731074
"secp256k1::PublicKey"|"bitcoin::secp256k1::PublicKey" => Some("crate::c_types::PublicKey"),
1075+
"bitcoin::key::TweakedPublicKey" => Some("crate::c_types::TweakedPublicKey"),
10741076
"bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::ECDSASignature"),
10751077
"bitcoin::secp256k1::schnorr::Signature" => Some("crate::c_types::SchnorrSignature"),
10761078
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature"),
@@ -1182,6 +1184,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
11821184

11831185
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" if is_ref => Some("&"),
11841186
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(""),
1187+
"bitcoin::key::TweakedPublicKey" if is_ref => Some("&"),
1188+
"bitcoin::key::TweakedPublicKey" => Some(""),
11851189
"bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" if is_ref => Some("&"),
11861190
"bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(""),
11871191
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(""),
@@ -1296,6 +1300,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
12961300
"core::num::NonZeroU8" => Some(").expect(\"Value must be non-zero\")"),
12971301

12981302
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(".into_rust()"),
1303+
"bitcoin::key::TweakedPublicKey" => Some(".into_rust()"),
12991304
"bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(".into_rust()"),
13001305
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(".into_rust()"),
13011306
"bitcoin::secp256k1::SecretKey" if !is_ref => Some(".into_rust()"),
@@ -1414,6 +1419,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
14141419
"u128" => Some(""),
14151420

14161421
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some("crate::c_types::PublicKey::from_rust(&"),
1422+
"bitcoin::key::TweakedPublicKey" => Some("crate::c_types::TweakedPublicKey::from_rust(&"),
14171423
"bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::ECDSASignature::from_rust(&"),
14181424
"bitcoin::secp256k1::schnorr::Signature" => Some("crate::c_types::SchnorrSignature::from_rust(&"),
14191425
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature::from_rust(&"),
@@ -1519,6 +1525,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
15191525
"u128" => Some(".into()"),
15201526

15211527
"bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(")"),
1528+
"bitcoin::key::TweakedPublicKey" => Some(")"),
15221529
"bitcoin::secp256k1::ecdsa::Signature"|"bitcoin::secp256k1::schnorr::Signature" => Some(")"),
15231530
"bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(")"),
15241531
"bitcoin::secp256k1::SecretKey" if !is_ref => Some(")"),

lightning-c-bindings/src/c_types/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use bitcoin::Transaction as BitcoinTransaction;
77
use bitcoin::Witness as BitcoinWitness;
88
use bitcoin::address;
99
use bitcoin::address::WitnessProgram as BitcoinWitnessProgram;
10+
use bitcoin::key::TweakedPublicKey as BitcoinTweakedPublicKey;
11+
use bitcoin::key::XOnlyPublicKey;
1012
use bitcoin::hashes::Hash;
1113
use bitcoin::secp256k1::PublicKey as SecpPublicKey;
1214
use bitcoin::secp256k1::SecretKey as SecpSecretKey;
@@ -169,6 +171,25 @@ impl PublicKey {
169171
pub(crate) fn null() -> Self { Self { compressed_form: [0; 33] } }
170172
}
171173

174+
#[derive(Clone)]
175+
#[repr(C)]
176+
/// Represents a tweaked X-only public key as required for BIP 340 (Taproot).
177+
pub struct TweakedPublicKey {
178+
/// The bytes of the public key X coordinate
179+
pub x_coordinate: [u8; 32],
180+
}
181+
impl TweakedPublicKey {
182+
pub(crate) fn from_rust(pk: &BitcoinTweakedPublicKey) -> Self {
183+
Self {
184+
x_coordinate: pk.serialize(),
185+
}
186+
}
187+
pub(crate) fn into_rust(&self) -> BitcoinTweakedPublicKey {
188+
let xonly_key = XOnlyPublicKey::from_slice(&self.x_coordinate).unwrap();
189+
BitcoinTweakedPublicKey::dangerous_assume_tweaked(xonly_key)
190+
}
191+
}
192+
172193
#[repr(C)]
173194
#[derive(Clone)]
174195
/// Represents a valid secp256k1 secret key serialized as a 32 byte array.

0 commit comments

Comments
 (0)