From b87ddc0043697b51dea7a7ed41020ea7f6862cd0 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Tue, 4 Mar 2025 20:25:45 +0100 Subject: [PATCH] Don't panic in `PrivateKey::from_slice` During upgrade of `secp256k1` a number of function calls needed to be rewritten to not use `from_slice` but `from_byte_array`. Unfortunately, the conversions wasn't correct and caused panics on invalid inputs rather than errors. This fixes it simply by calling `try_into` on the entire slice and converting the error. --- bitcoin/src/crypto/key.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 7a045cfb5..d73f84180 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -466,7 +466,7 @@ impl PrivateKey { ) -> Result { Ok(PrivateKey::new( secp256k1::SecretKey::from_byte_array( - data[..32].try_into().expect("Slice should be exactly 32 bytes"), + data.try_into().map_err(|_| secp256k1::Error::InvalidSecretKey)?, )?, network, ))