cardano_serialization_lib/serialization/crypto/
vrf_cert.rs

1use crate::*;
2
3impl cbor_event::se::Serialize for VRFCert {
4    fn serialize<'se, W: Write>(
5        &self,
6        serializer: &'se mut Serializer<W>,
7    ) -> cbor_event::Result<&'se mut Serializer<W>> {
8        serializer.write_array(cbor_event::Len::Len(2))?;
9        serializer.write_bytes(&self.output)?;
10        serializer.write_bytes(&self.proof)?;
11        Ok(serializer)
12    }
13}
14
15impl Deserialize for VRFCert {
16    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
17        (|| -> Result<_, DeserializeError> {
18            let len = raw.array()?;
19            let output = (|| -> Result<_, DeserializeError> { Ok(raw.bytes()?) })()
20                .map_err(|e| e.annotate("output"))?;
21            let proof = (|| -> Result<_, DeserializeError> { Ok(raw.bytes()?) })()
22                .map_err(|e| e.annotate("proof"))?;
23            if proof.len() != Self::PROOF_LEN {
24                return Err(DeserializeFailure::CBOR(cbor_event::Error::WrongLen(
25                    Self::PROOF_LEN as u64,
26                    cbor_event::Len::Len(proof.len() as u64),
27                    "proof length",
28                ))
29                .into());
30            }
31            match len {
32                cbor_event::Len::Len(_) =>
33                /* TODO: check finite len somewhere */
34                {
35                    ()
36                }
37                cbor_event::Len::Indefinite => match raw.special()? {
38                    CBORSpecial::Break =>
39                    /* it's ok */
40                    {
41                        ()
42                    }
43                    _ => return Err(DeserializeFailure::EndingBreakMissing.into()),
44                },
45            }
46            Ok(VRFCert { output, proof })
47        })()
48        .map_err(|e| e.annotate("VRFCert"))
49    }
50}