From 74276d5f5b2bbc8aab1f12df7664f0fc02942676 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sun, 6 Oct 2024 21:42:47 +0400 Subject: [PATCH] avoid changing behaviour of Keypair::from_bytes --- sdk/keypair/src/lib.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sdk/keypair/src/lib.rs b/sdk/keypair/src/lib.rs index ccf1c58d8f174a..f54c680c5bec3d 100644 --- a/sdk/keypair/src/lib.rs +++ b/sdk/keypair/src/lib.rs @@ -44,12 +44,16 @@ impl Keypair { /// Recovers a `Keypair` from a byte array pub fn from_bytes(slice: &[u8]) -> Result { - let Ok(bytes): Result<[u8; 64], _> = slice.try_into() else { - return Err(ed25519_dalek::SignatureError::from_source(String::from( - "candidate keypair byte array is too short", - ))); - }; - Ok(Self(ed25519_dalek::SigningKey::from_keypair_bytes(&bytes)?)) + let arr: [u8; ed25519_dalek::KEYPAIR_LENGTH] = slice + .get(0..ed25519_dalek::KEYPAIR_LENGTH) + .ok_or_else(|| { + ed25519_dalek::SignatureError::from_source(String::from( + "candidate keypair byte array is too short", + )) + })? + .try_into() + .unwrap(); + Ok(Self(ed25519_dalek::SigningKey::from_keypair_bytes(&arr)?)) } /// Returns this `Keypair` as a byte array