crypto/keypair: Use the FromStr trait for PublicKey.

This commit is contained in:
parazyd
2022-04-22 08:42:21 +02:00
parent 7f0a53f612
commit 7363c50f35

View File

@@ -1,4 +1,4 @@
use std::{convert::TryFrom, io};
use std::{convert::TryFrom, io, str::FromStr};
use halo2_gadgets::ecc::chip::FixedPoint;
use pasta_curves::{
@@ -76,8 +76,19 @@ impl PublicKey {
self.0.to_bytes()
}
pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self> {
match pallas::Point::from_bytes(bytes).into() {
Some(k) => Ok(Self(k)),
None => Err(Error::PublicKeyFromBytes),
}
}
}
impl FromStr for PublicKey {
type Err = crate::Error;
/// Tries to create a `PublicKey` instance from a base58 encoded string.
pub fn from_str(encoded: &str) -> Result<Self> {
fn from_str(encoded: &str) -> std::result::Result<Self, crate::Error> {
let decoded = bs58::decode(encoded).into_vec()?;
if decoded.len() != 32 {
return Err(Error::PublicKeyFromStr)
@@ -85,13 +96,6 @@ impl PublicKey {
Self::from_bytes(&decoded.try_into().unwrap())
}
pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self> {
match pallas::Point::from_bytes(bytes).into() {
Some(k) => Ok(Self(k)),
None => Err(Error::PublicKeyFromBytes),
}
}
}
impl TryFrom<Address> for PublicKey {