zaun_community_protocol/signed/
export_manifest.rs1use super::error::SignedError;
6use super::object::{FORMAT_V1, Signed, SignedObject, read_format};
7use crate::crypto::Context;
8use crate::encoding::{Decode, DecodeError, Encode, EncodeError, Reader, Writer};
9use crate::ids::CommunityId;
10use crate::json::SignedJson;
11use crate::keys::PublicKey;
12
13pub const EXPORT_FORMAT: &str = "zaun-export/v1";
15pub const MANIFEST_ENTRIES_MAX: usize = 1 << 20;
17pub const ENTRY_NAME_MAX: usize = 128;
19
20#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct ManifestEntry {
23 pub name: String,
25 pub size: u64,
27 pub sha3_256: [u8; 32],
29}
30
31#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct ExportManifest {
36 pub community_id: CommunityId,
38 pub host_public_key: PublicKey,
40 pub schema_version: u32,
42 pub revision: u64,
44 pub delegation_epoch: u64,
46 pub exported_at: u64,
48 pub entries: Vec<ManifestEntry>,
50}
51
52impl Encode for ExportManifest {
53 fn encode(&self, w: &mut Writer) -> Result<(), EncodeError> {
54 w.u8(FORMAT_V1);
55 w.string(EXPORT_FORMAT, EXPORT_FORMAT.len())?;
56 w.put(&self.community_id)?;
57 w.put(&self.host_public_key)?;
58 w.u32(self.schema_version);
59 w.u64(self.revision);
60 w.u64(self.delegation_epoch);
61 w.u64(self.exported_at);
62 w.list(&self.entries, MANIFEST_ENTRIES_MAX, |w, e| {
63 if e.name.is_empty() {
64 return Err(EncodeError::Invalid("entry name"));
65 }
66 w.string(&e.name, ENTRY_NAME_MAX)?;
67 w.u64(e.size);
68 w.fixed(&e.sha3_256);
69 Ok(())
70 })
71 }
72}
73
74impl Decode for ExportManifest {
75 fn decode(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
76 read_format(r)?;
77 if r.string(EXPORT_FORMAT.len())? != EXPORT_FORMAT {
78 return Err(DecodeError::Invalid("export format"));
79 }
80 Ok(Self {
81 community_id: r.get()?,
82 host_public_key: r.get()?,
83 schema_version: r.u32()?,
84 revision: r.u64()?,
85 delegation_epoch: r.u64()?,
86 exported_at: r.u64()?,
87 entries: r.list(MANIFEST_ENTRIES_MAX, |r| {
88 let name = r.string(ENTRY_NAME_MAX)?;
89 if name.is_empty() {
90 return Err(DecodeError::Invalid("entry name"));
91 }
92 Ok(ManifestEntry {
93 name: name.to_owned(),
94 size: r.u64()?,
95 sha3_256: r.array()?,
96 })
97 })?,
98 })
99 }
100}
101
102impl SignedObject for ExportManifest {
103 const CONTEXT: Context = Context::ExportManifest;
104}
105
106impl ExportManifest {
107 pub fn verify_self_signed(
114 json: &SignedJson,
115 min_suite: u8,
116 ) -> Result<Signed<Self>, SignedError> {
117 let claimed = Self::decode_unverified(&json.bytes)?;
118 Signed::<Self>::from_json(json, &claimed.host_public_key, min_suite)
119 }
120}