Introduce serialisation functions for PublicKey
What changed, and why it matters
This commit adds two new helper methods, serialize_compressed and serialize_uncompressed, to the bitcoin::PublicKey type. Previously, users had to reach into the underlying secp256k1 library to serialize a public key. The change simply wraps that existing behavior in a more convenient API and updates internal callers to use the new methods. There is no indication of a security bug being fixed.
No security action required. Treat as a normal API enhancement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces PublicKey::serialize_compressed() -> [u8; 33] and PublicKey::serialize_uncompressed() -> [u8; 65], each delegating to the existing secp256k1 PublicKey serialization routines. It refactors Builder::push_key and PublicKey::with_serialized to call these new wrappers instead of calling to_inner().serialize() / to_inner().serialize_uncompressed() directly. The behavior is unchanged; it is a pure API ergonomics improvement.
Changed components
bitcoin/src/crypto/key.rsbitcoin/src/blockdata/script/builder.rsInspect captured patch +26 / −4
diff --git a/bitcoin/src/blockdata/script/builder.rs b/bitcoin/src/blockdata/script/builder.rs
index 8dac2bf2..ae9cce64 100644
--- a/bitcoin/src/blockdata/script/builder.rs
+++ b/bitcoin/src/blockdata/script/builder.rs
@@ -138,9 +138,9 @@ impl<T> Builder<T> {
/// Adds instructions to push a public key onto the stack.
pub fn push_key(self, key: PublicKey) -> Self {
if key.compressed() {
- self.push_slice(key.to_inner().serialize())
+ self.push_slice(key.serialize_compressed())
} else {
- self.push_slice(key.to_inner().serialize_uncompressed())
+ self.push_slice(key.serialize_uncompressed())
}
}
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index 3ded3559..7d1726e2 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -489,6 +489,9 @@ impl PublicKey {
/// This will call the provided function with the key as a byte slice in either
/// compressed or uncompressed form.
///
+ /// See [`PublicKey::serialize_compressed`] and [`PublicKey::serialize_uncompressed`]
+ /// for more information on the byte formats for the key.
+ ///
/// # Examples
///
/// ```
@@ -507,12 +510,31 @@ impl PublicKey {
/// ```
pub fn with_serialized<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
if self.compressed() {
- f(&self.to_inner().serialize())
+ f(&self.serialize_compressed())
} else {
- f(&self.to_inner().serialize_uncompressed())
+ f(&self.serialize_uncompressed())
}
}
+ /// Serializes the key as a byte-encoded pair of values.
+ ///
+ /// This function serializes the key in compressed form, where the y-coordinate is
+ /// represented by only a single bit, as x determines it up to one bit.
+ ///
+ /// If you want to serialize while considering the compressedness of this key,
+ /// use [`with_serialized`] instead.
+ ///
+ /// [`with_serialized`]: PublicKey::with_serialized
+ pub fn serialize_compressed(&self) -> [u8; 33] { self.to_inner().serialize() }
+
+ /// Serializes the key as a byte-encoded pair of values, in uncompressed form.
+ ///
+ /// If you want to serialize while considering the compressedness of this key,
+ /// use [`with_serialized`] instead.
+ ///
+ /// [`with_serialized`]: PublicKey::with_serialized
+ pub fn serialize_uncompressed(&self) -> [u8; 65] { self.to_inner().serialize_uncompressed() }
+
/// Returns bitcoin 160-bit hash of the public key.
pub fn pubkey_hash(&self) -> PubkeyHash {
PubkeyHash(self.with_serialized(hash160::Hash::hash))
Why this scored 19/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.