Deserialize an attestation from bytes, returning the attestation and its hash. The hash is computed over the input bytes (the entire serialized form).
(data: &[u8])
| 205 | /// |
| 206 | /// The hash is computed over the input bytes (the entire serialized form). |
| 207 | pub fn deserialize(data: &[u8]) -> Result<(Self, Hash), AttestationError> { |
| 208 | if data.len() < MAGIC.len() + 1 { |
| 209 | return Err(AttestationError::Codec { |
| 210 | reason: format!( |
| 211 | "data too short: {} bytes (minimum {})", |
| 212 | data.len(), |
| 213 | MAGIC.len() + 1 |
| 214 | ), |
| 215 | }); |
| 216 | } |
| 217 | |
| 218 | if &data[..4] != MAGIC { |
| 219 | return Err(AttestationError::Codec { |
| 220 | reason: format!("invalid magic: expected {:?}, got {:?}", MAGIC, &data[..4]), |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | let attestation: Self = |
| 225 | postcard::from_bytes(&data[4..]).map_err(|e| AttestationError::Codec { |
| 226 | reason: format!("postcard deserialize failed: {}", e), |
| 227 | })?; |
| 228 | |
| 229 | if attestation.version > SCHEMA_VERSION { |
| 230 | return Err(AttestationError::UnsupportedVersion { |
| 231 | version: attestation.version, |
| 232 | max_supported: SCHEMA_VERSION, |
| 233 | }); |
| 234 | } |
| 235 | |
| 236 | let hash = Hash::of(data); |
| 237 | Ok((attestation, hash)) |
| 238 | } |
| 239 | |
| 240 | /// Read an attestation from a reader (e.g., a file). |
| 241 | pub fn read_from<R: Read>(reader: &mut R) -> Result<(Self, Hash), AttestationError> { |