Deserialize an attestation from bytes, returning the attestation and its hash. The hash is computed over the input bytes (the entire serialized form). Decoding is version-directed: the first payload byte is the schema version, and only the exact layout written for that version is accepted — every shape must consume its bytes completely. A truncated or misaligned payload is rejected instead of be
(data: &[u8])
| 413 | /// older shape that would silently drop covered fields (review |
| 414 | /// ATOM::aaron::8 R1). |
| 415 | pub fn deserialize(data: &[u8]) -> Result<(Self, Hash), AttestationError> { |
| 416 | if data.len() < MAGIC.len() + 1 { |
| 417 | return Err(AttestationError::Codec { |
| 418 | reason: format!( |
| 419 | "data too short: {} bytes (minimum {})", |
| 420 | data.len(), |
| 421 | MAGIC.len() + 1 |
| 422 | ), |
| 423 | }); |
| 424 | } |
| 425 | |
| 426 | if &data[..4] != MAGIC { |
| 427 | return Err(AttestationError::Codec { |
| 428 | reason: format!("invalid magic: expected {:?}, got {:?}", MAGIC, &data[..4]), |
| 429 | }); |
| 430 | } |
| 431 | |
| 432 | let payload = &data[4..]; |
| 433 | // postcard encodes a u8 field as exactly one leading byte. |
| 434 | let version = payload[0]; |
| 435 | if version > SCHEMA_VERSION { |
| 436 | return Err(AttestationError::UnsupportedVersion { |
| 437 | version, |
| 438 | max_supported: SCHEMA_VERSION, |
| 439 | }); |
| 440 | } |
| 441 | if version == 0 { |
| 442 | return Err(AttestationError::Codec { |
| 443 | reason: format!("invalid attestation schema version {version}"), |
| 444 | }); |
| 445 | } |
| 446 | |
| 447 | // Version-directed exact-shape decode. V1 and V2 carry their own |
| 448 | // historical structs; anything that fails its exact layout match is |
| 449 | // malformed and refused. |
| 450 | let attestation = match version { |
| 451 | 1 => decode_exact_shape::<AttestationV1>(payload) |
| 452 | .map(Attestation::from) |
| 453 | .ok_or_else(|| AttestationError::Codec { |
| 454 | reason: "malformed V1 attestation payload".to_string(), |
| 455 | })?, |
| 456 | 2 => decode_exact_shape::<AttestationV2>(payload) |
| 457 | .map(Attestation::from) |
| 458 | .ok_or_else(|| AttestationError::Codec { |
| 459 | reason: "malformed V2 attestation payload".to_string(), |
| 460 | })?, |
| 461 | 3 => decode_exact_shape::<AttestationV3>(payload) |
| 462 | .map(Attestation::from) |
| 463 | .ok_or_else(|| AttestationError::Codec { |
| 464 | reason: "malformed V3 attestation payload".to_string(), |
| 465 | })?, |
| 466 | _ => decode_exact_shape::<Attestation>(payload).ok_or_else(|| { |
| 467 | AttestationError::Codec { |
| 468 | reason: "malformed V4 attestation payload".to_string(), |
| 469 | } |
| 470 | })?, |
| 471 | }; |
| 472 |