Skip to content

Commit 362b02a

Browse files
committed
Add Keypair::try_from_base58_string
Add a conversion method that doesn't panic on errors.
1 parent 41c663a commit 362b02a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

keypair/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ impl Keypair {
6868
}
6969

7070
/// Recovers a `Keypair` from a base58-encoded string
71-
pub fn from_base58_string(s: &str) -> Self {
71+
pub fn try_from_base58_string(s: &str) -> Result<Self, Box<dyn error::Error>> {
7272
let mut buf = [0u8; ed25519_dalek::KEYPAIR_LENGTH];
73-
bs58::decode(s).onto(&mut buf).unwrap();
74-
Self::from_bytes(&buf).unwrap()
73+
let written = bs58::decode(s).onto(&mut buf)?;
74+
Ok(Self::from_bytes(&buf[..written])?)
75+
}
76+
77+
/// Recovers a `Keypair` from a base58-encoded string. Panic on errors.
78+
pub fn from_base58_string(s: &str) -> Self {
79+
Self::try_from_base58_string(s).unwrap()
7580
}
7681

7782
/// Returns this `Keypair` as a base58-encoded string

0 commit comments

Comments
 (0)