Replace as_inner accessor on XOnlyPublicKey with to_inner
What changed, and why it matters
This commit is a routine API cleanup in the rust-bitcoin crypto library. It removes a method called as_inner that returned a reference to the underlying key data, and replaces internal uses with to_inner, which returns the key by value. The underlying key type is small and cheap to copy, so this is a consistency improvement, not a security fix. There is no indication this change addresses a vulnerability.
No security action required. Treat as a normal breaking API change; downstream users relying on XOnlyPublicKey::as_inner will need to migrate to to_inner.
Security signals we found
No security-relevant signals detected
API-only refactor with no behavioral change to cryptographic operations
Evidence from the diff
The patch removes XOnlyPublicKey::as_inner() -> &secp256k1::XOnlyPublicKey and switches all internal callers to XOnlyPublicKey::to_inner(self) -> secp256k1::XOnlyPublicKey. Because secp256k1::XOnlyPublicKey is Copy, moving by value is inexpensive and makes the wrapper’s inner-access API uniform. One unrelated call site in LegacyPublicKey::wpubkey_hash is also changed from to_inner().serialize() to serialize_compressed(). No unsafe code, no boundary checks, no cryptographic algorithm changes, and no security-relevant behavior changes are present in the diff.
Changed components
rust-bitcoin crypto/src/key.rsXOnlyPublicKey public APILegacyPublicKey::wpubkey_hash implementationInspect captured patch +8 / −12
diff --git a/crypto/src/key.rs b/crypto/src/key.rs
index 29fdb3ba..ecf1973b 100644
--- a/crypto/src/key.rs
+++ b/crypto/src/key.rs
@@ -83,10 +83,6 @@ mod encapsulate {
#[inline]
pub fn parity(&self) -> Parity { self.parity }
- /// Returns a reference to the inner secp256k1 x-only public key.
- #[inline]
- pub fn as_inner(&self) -> &secp256k1::XOnlyPublicKey { &self.inner }
-
/// Returns the inner secp256k1 x-only public key.
#[inline]
pub fn to_inner(self) -> secp256k1::XOnlyPublicKey { self.inner }
@@ -378,14 +374,14 @@ impl XOnlyPublicKey {
/// Serializes the x-only public key as a byte-encoded x coordinate value (32 bytes).
#[inline]
pub fn serialize(&self) -> ([u8; constants::SCHNORR_PUBLIC_KEY_SIZE], Parity) {
- (self.as_inner().serialize(), self.parity())
+ (self.to_inner().serialize(), self.parity())
}
/// Converts this x-only public key to a full public key.
///
/// The [`FullPublicKey`] is constructed using the parity in this x-only public key.
#[inline]
- pub fn to_public_key(self) -> FullPublicKey { self.as_inner().public_key(self.parity()).into() }
+ pub fn to_public_key(self) -> FullPublicKey { self.to_inner().public_key(self.parity()).into() }
/// Converts this x-only public key to a legacy public key.
///
@@ -399,7 +395,7 @@ impl XOnlyPublicKey {
/// [`XOnlyPublicKey::add_tweak`] as input.
#[inline]
pub fn tweak_add_check(&self, tweaked_key: &Self, tweak: secp256k1::Scalar) -> bool {
- self.as_inner().tweak_add_check(tweaked_key.as_inner(), tweaked_key.parity(), tweak)
+ self.to_inner().tweak_add_check(&tweaked_key.to_inner(), tweaked_key.parity(), tweak)
}
/// Tweaks an [`XOnlyPublicKey`] by adding the generator multiplied with the given tweak to it.
@@ -415,7 +411,7 @@ impl XOnlyPublicKey {
/// If the resulting key would be invalid.
#[inline]
pub fn add_tweak(&self, tweak: &secp256k1::Scalar) -> Result<Self, TweakXOnlyPublicKeyError> {
- match self.as_inner().add_tweak(tweak) {
+ match self.to_inner().add_tweak(tweak) {
Ok((xonly, parity)) => Ok(Self::from_secp(xonly, parity)),
Err(secp256k1::Error::InvalidTweak) => Err(TweakXOnlyPublicKeyError::BadTweak),
Err(secp256k1::Error::InvalidParityValue(_)) =>
@@ -465,7 +461,7 @@ impl From<TweakedPublicKey> for XOnlyPublicKey {
impl fmt::LowerHex for XOnlyPublicKey {
#[inline]
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.as_inner().fmt(f) }
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.to_inner().fmt(f) }
}
// Allocate for serialized size
#[cfg(feature = "alloc")]
@@ -473,7 +469,7 @@ impl_to_hex_from_lower_hex!(XOnlyPublicKey, |_| constants::SCHNORR_PUBLIC_KEY_SI
impl fmt::Display for XOnlyPublicKey {
#[inline]
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.as_inner().fmt(f) }
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.to_inner().fmt(f) }
}
impl Keypair {
@@ -641,7 +637,7 @@ impl LegacyPublicKey {
pub fn wpubkey_hash(&self) -> Result<WPubkeyHash, UncompressedPublicKeyError> {
if self.compressed() {
Ok(WPubkeyHash::from_byte_array(
- hash160::Hash::hash(&self.to_inner().serialize()).to_byte_array(),
+ hash160::Hash::hash(&self.serialize_compressed()).to_byte_array(),
))
} else {
Err(UncompressedPublicKeyError)
@@ -1265,7 +1261,7 @@ impl Serialize for XOnlyPublicKey {
where
S: Serializer,
{
- <secp256k1::XOnlyPublicKey as Serialize>::serialize(self.as_inner(), serializer)
+ <secp256k1::XOnlyPublicKey as Serialize>::serialize(&self.to_inner(), serializer)
}
}
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.