From 360ca24581290f757cffba5e378e52b33e6ce256 Mon Sep 17 00:00:00 2001 From: parazyd Date: Wed, 20 Apr 2022 10:48:43 +0200 Subject: [PATCH] crypto/keypair: Implement from_str() for PublicKey. --- src/crypto/keypair.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/crypto/keypair.rs b/src/crypto/keypair.rs index 1e5454b30..ef4761a8b 100644 --- a/src/crypto/keypair.rs +++ b/src/crypto/keypair.rs @@ -76,6 +76,16 @@ impl PublicKey { self.0.to_bytes() } + /// Tries to create a `PublicKey` instance from a base58 encoded string. + pub fn from_str(encoded: &str) -> Result { + let decoded = bs58::decode(encoded).into_vec()?; + if decoded.len() != 32 { + return Err(Error::PublicKeyFromStr) + } + + Self::from_bytes(&decoded.try_into().unwrap()) + } + pub fn from_bytes(bytes: &[u8; 32]) -> Result { match pallas::Point::from_bytes(bytes).into() { Some(k) => Ok(Self(k)),