1use crate::artifact::{catalog::Artifact, codec::Reader};
3use crate::context::LocalSharedStatePackageContext;
4pub use crate::wire::images::{DESCRIPTOR_LIMIT, IMAGE_BYTES};
5use anyhow::{Result, ensure};
6fn valid(ok: bool) -> Result<()> {
7 ensure!(ok, "error encrypted-tail-invalid");
8 Ok(())
9}
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct Descriptor {
12 pub vault: [u8; 32],
13 pub stream: [u8; 32],
14 pub generation: [u8; 32],
15 pub object: [u8; 32],
16 pub artifact: Artifact,
17}
18impl Descriptor {
19 pub fn decode(bytes: &[u8]) -> Result<Self> {
20 valid(bytes.len() <= DESCRIPTOR_LIMIT)?;
21 let mut r = Reader(bytes);
22 valid(r.take(8)? == b"AVEN\x03\x00\x01\x01")?;
23 let value = Self {
24 vault: r.array()?,
25 stream: r.array()?,
26 generation: r.array()?,
27 object: r.array()?,
28 artifact: Artifact::read(&mut r, IMAGE_BYTES as u64, true)?,
29 };
30 r.end()?;
31 Ok(value)
32 }
33 pub fn encode(&self) -> Result<Vec<u8>> {
34 self.artifact.shape(IMAGE_BYTES as u64, true)?;
35 let mut bytes = b"AVEN\x03\x00\x01\x01".to_vec();
36 for id in [&self.vault, &self.stream, &self.generation, &self.object] {
37 bytes.extend(id);
38 }
39 self.artifact.write(&mut bytes)?;
40 Ok(bytes)
41 }
42 pub fn context(&self) -> LocalSharedStatePackageContext {
43 LocalSharedStatePackageContext {
44 vault_id: self.vault,
45 generation_id: self.generation,
46 }
47 }
48 pub fn byte_size(&self) -> i64 {
49 self.artifact.chunks.iter().map(|c| c.length as i64).sum()
50 }
51 pub fn verify_chunk(&self, index: usize, bytes: &[u8]) -> Result<()> {
52 self.artifact
53 .verify_chunk(bytes, index, self.context(), self.stream, self.object, 1, 0)?;
54 Ok(())
55 }
56 pub fn verify(&self, records: &[Vec<u8>]) -> Result<()> {
57 self.artifact
58 .verify(records, self.context(), self.stream, self.object, 1, 0)?;
59 Ok(())
60 }
61}