Make with_serialized a public function on PublicKey
What changed, and why it matters
This commit simply changes an existing internal helper function to be publicly usable and adds documentation and examples. It does not fix a bug, change behavior, or introduce any new security-sensitive logic. The function already existed and behaved the same way; only its visibility changed from private to public.
No security action required. This is a routine API-visibility/documentation change. Reviewers may optionally consider whether exposing the helper is desirable from an API-design standpoint, but it is not a security issue.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch makes PublicKey::with_serialized a pub fn and adds rustdoc with doctests. The implementation is unchanged: it serializes the public key as compressed or uncompressed bytes depending on the key’s compressed flag and passes that byte slice to a callback. There is no change to serialization logic, no new dependencies, and no security boundary crossed beyond expanding the public API surface.
Changed components
bitcoin/src/crypto/key.rsPublicKey::with_serializedInspect captured patch +22 / −1
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index 49056f01..3ded3559 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -484,7 +484,28 @@ impl PublicKey {
Self::from_secp_uncompressed(key)
}
- fn with_serialized<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
+ /// Serializes the key as a byte-encoded pair of values.
+ ///
+ /// This will call the provided function with the key as a byte slice in either
+ /// compressed or uncompressed form.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bitcoin::PublicKey;
+ /// use bitcoin::hashes::hash160;
+ ///
+ /// let key = "02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8"
+ /// .parse::<PublicKey>()
+ /// .unwrap();
+ /// assert!(key.compressed());
+ /// let vec_out = key.with_serialized(<[_]>::to_vec);
+ /// assert_eq!(vec_out.len(), 33);
+ ///
+ /// let hash = key.with_serialized(hash160::Hash::hash).to_string();
+ /// assert_eq!(hash, "dabedb4de2bd2bfec5d38475b9c64af13999a043");
+ /// ```
+ pub fn with_serialized<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
if self.compressed() {
f(&self.to_inner().serialize())
} else {
Why this scored 16/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.