txpipe / pallas

Rust-native building blocks for the Cardano blockchain ecosystem
Apache License 2.0
150 stars 60 forks source link

Cannot build a KES private key from a vector of bytes #524

Open ghost opened 1 month ago

ghost commented 1 month ago

I am trying to reconstruct a KesSecretKey from a base64-encoded string coming from a JSON structure that's generated in Haskell, for the purpose of testing header validation logic. Here is the code I am using:

fn deserialize_secret_kes_key<'a, 'de, D>(deserializer: D) -> Result<KesSecretKey<'a>, D::Error>
where
    D: Deserializer<'de>,
{
    let buf = <&str>::deserialize(deserializer)?;
    let mut bytes = general_purpose::STANDARD
        .decode(buf)
        .map_err(serde::de::Error::custom)?;
    KesSecretKey::from_bytes(&mut bytes).map_err(serde::de::Error::custom)
}

which fails to compile because of

error[E0515]: cannot return value referencing local variable `bytes`
  --> ouroboros-praos/tests/validation.rs:35:5
   |
35 |     KesSecretKey::from_bytes(&mut bytes).map_err(serde::de::Error::custom)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |                        |
   |     |                        `bytes` is borrowed here
   |     returns a value referencing data owned by the current function

My knowledge of Rust's borrow-checking is still fledgling so I would appreciate if someone could help me figure out what's the right way to build KesSecretKey