Skip to content

Commit

Permalink
secp256r1: add a from_bytes constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Feb 24, 2024
1 parent afc80e5 commit 3612056
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/types/crypto/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl Secp256r1PublicKey {
pub const fn as_bytes(&self) -> &[u8] {
&self.0
}

pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self, std::array::TryFromSliceError> {
<[u8; Self::LENGTH]>::try_from(bytes.as_ref()).map(Self)
}
}

impl std::str::FromStr for Secp256r1PublicKey {
Expand Down Expand Up @@ -158,6 +162,10 @@ impl Secp256r1Signature {
pub const fn as_bytes(&self) -> &[u8] {
&self.0
}

pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self, std::array::TryFromSliceError> {
<[u8; Self::LENGTH]>::try_from(bytes.as_ref()).map(Self)
}
}

impl std::str::FromStr for Secp256r1Signature {
Expand Down

0 comments on commit 3612056

Please sign in to comment.