Skip to content

Commit

Permalink
Add Keypair::try_from_base58_string
Browse files Browse the repository at this point in the history
Add a conversion method that doesn't panic on errors.
  • Loading branch information
juchiast committed Mar 2, 2025
1 parent 41c663a commit 362b02a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions keypair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ impl Keypair {
}

/// Recovers a `Keypair` from a base58-encoded string
pub fn from_base58_string(s: &str) -> Self {
pub fn try_from_base58_string(s: &str) -> Result<Self, Box<dyn error::Error>> {
let mut buf = [0u8; ed25519_dalek::KEYPAIR_LENGTH];
bs58::decode(s).onto(&mut buf).unwrap();
Self::from_bytes(&buf).unwrap()
let written = bs58::decode(s).onto(&mut buf)?;
Ok(Self::from_bytes(&buf[..written])?)
}

/// Recovers a `Keypair` from a base58-encoded string. Panic on errors.
pub fn from_base58_string(s: &str) -> Self {
Self::try_from_base58_string(s).unwrap()
}

/// Returns this `Keypair` as a base58-encoded string
Expand Down

0 comments on commit 362b02a

Please sign in to comment.