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.
This commit is contained in:
Martin Habovstiak 2025-03-04 20:25:45 +01:00
parent 6483244280
commit b87ddc0043
1 changed files with 1 additions and 1 deletions

View File

@ -466,7 +466,7 @@ impl PrivateKey {
) -> Result<PrivateKey, secp256k1::Error> {
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,
))