From 7dac91d973b4fc97e2ca2d0d894b46ec3e686ff3 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Tue, 31 Oct 2023 14:12:39 +0100 Subject: [PATCH 1/3] Deprecate `capacity` and `is_empty` These functions always return the same value so are not useful and actually calling them is a red flag. --- src/ecdsa/serialized_signature.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ecdsa/serialized_signature.rs b/src/ecdsa/serialized_signature.rs index 1b2a65a..bf71ecc 100644 --- a/src/ecdsa/serialized_signature.rs +++ b/src/ecdsa/serialized_signature.rs @@ -84,6 +84,7 @@ impl SerializedSignature { } /// Get the capacity of the underlying data buffer. + #[deprecated = "This always returns 72"] #[inline] pub fn capacity(&self) -> usize { self.data.len() } @@ -106,6 +107,7 @@ impl SerializedSignature { pub fn from_signature(sig: &Signature) -> SerializedSignature { sig.serialize_der() } /// Check if the space is zero. + #[deprecated = "This always returns false"] #[inline] pub fn is_empty(&self) -> bool { self.len() == 0 } } From dc3eab7aaa7082be4d598edc5a1e511f5f976a38 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Tue, 31 Oct 2023 14:17:18 +0100 Subject: [PATCH 2/3] Implement `Borrow<[u8]>`, `PartialEq<[u8]>`, `Hash` These traits were missing and could be useful if e.g. one wants to store serialized signatures in a set/map and access them using `[u8]`. --- src/ecdsa/serialized_signature.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/ecdsa/serialized_signature.rs b/src/ecdsa/serialized_signature.rs index bf71ecc..1aba65f 100644 --- a/src/ecdsa/serialized_signature.rs +++ b/src/ecdsa/serialized_signature.rs @@ -7,6 +7,7 @@ //! unable to run on platforms without allocator. We implement a special type to encapsulate //! serialized signatures and since it's a bit more complicated it has its own module. +use core::borrow::Borrow; use core::{fmt, ops}; pub use into_iter::IntoIter; @@ -41,11 +42,30 @@ impl PartialEq for SerializedSignature { fn eq(&self, other: &SerializedSignature) -> bool { **self == **other } } +impl PartialEq<[u8]> for SerializedSignature { + #[inline] + fn eq(&self, other: &[u8]) -> bool { **self == *other } +} + +impl PartialEq for [u8] { + #[inline] + fn eq(&self, other: &SerializedSignature) -> bool { *self == **other } +} + +impl core::hash::Hash for SerializedSignature { + fn hash(&self, state: &mut H) { (**self).hash(state) } +} + impl AsRef<[u8]> for SerializedSignature { #[inline] fn as_ref(&self) -> &[u8] { self } } +impl Borrow<[u8]> for SerializedSignature { + #[inline] + fn borrow(&self) -> &[u8] { self } +} + impl ops::Deref for SerializedSignature { type Target = [u8]; From 62c839c9e03b5cacb7da34aecff6eefc29f8f293 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Tue, 31 Oct 2023 14:32:09 +0100 Subject: [PATCH 3/3] Implement conversion traits Converting signature to serialized signature and back is natural, so the conversion traits should be implemented. --- src/ecdsa/serialized_signature.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/ecdsa/serialized_signature.rs b/src/ecdsa/serialized_signature.rs index 1aba65f..bfd035f 100644 --- a/src/ecdsa/serialized_signature.rs +++ b/src/ecdsa/serialized_signature.rs @@ -8,6 +8,7 @@ //! serialized signatures and since it's a bit more complicated it has its own module. use core::borrow::Borrow; +use core::convert::TryFrom; use core::{fmt, ops}; pub use into_iter::IntoIter; @@ -91,6 +92,28 @@ impl<'a> IntoIterator for &'a SerializedSignature { fn into_iter(self) -> Self::IntoIter { self.iter() } } +impl From for SerializedSignature { + fn from(value: Signature) -> Self { Self::from_signature(&value) } +} + +impl<'a> From<&'a Signature> for SerializedSignature { + fn from(value: &'a Signature) -> Self { Self::from_signature(value) } +} + +impl TryFrom for Signature { + type Error = Error; + + fn try_from(value: SerializedSignature) -> Result { value.to_signature() } +} + +impl<'a> TryFrom<&'a SerializedSignature> for Signature { + type Error = Error; + + fn try_from(value: &'a SerializedSignature) -> Result { + value.to_signature() + } +} + impl SerializedSignature { /// Creates `SerializedSignature` from data and length. ///