Skip to main content

zaun_community_protocol/signed/
export_manifest.rs

1//! `ExportManifest` (ctx `zaun/v1/export-manifest`, ADR-0013): the signed table of contents of
2//! a `zaun-export/v1` bundle. Every chunk of the bundle is named here with its size and
3//! `SHA3-256`, so the manifest's signature covers the whole bundle.
4
5use 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
13/// The bundle format this manifest describes.
14pub const EXPORT_FORMAT: &str = "zaun-export/v1";
15/// At most this many chunks.
16pub const MANIFEST_ENTRIES_MAX: usize = 1 << 20;
17/// Bytes per chunk name.
18pub const ENTRY_NAME_MAX: usize = 128;
19
20/// One chunk: what it is, its length and its `SHA3-256`.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct ManifestEntry {
23    /// `table/<table>/<n>` or `file/<attachment id>/<n>`.
24    pub name: String,
25    /// Bytes.
26    pub size: u64,
27    /// `SHA3-256` of the chunk.
28    pub sha3_256: [u8; 32],
29}
30
31/// Signed by the exporting host's key (and, in a later version, the root key when the owner
32/// exports). The key is inside the bytes: an importer trusts it only once it matches a host
33/// delegation signed by the community's root (ADR-0013).
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct ExportManifest {
36    /// 2: the community.
37    pub community_id: CommunityId,
38    /// 3: the exporting host's key; the signer.
39    pub host_public_key: PublicKey,
40    /// 4: the exporter's `host.db` schema version.
41    pub schema_version: u32,
42    /// 5: the descriptor revision the exporter held.
43    pub revision: u64,
44    /// 6: the delegation epoch the exporter held.
45    pub delegation_epoch: u64,
46    /// 7: ms since the epoch.
47    pub exported_at: u64,
48    /// 8: every chunk, in bundle order.
49    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    /// Verifies a manifest under the host key inside its own bytes. The key is read before
108    /// verification and decides nothing else; the importer must then match it to a delegation
109    /// the community's root signed.
110    ///
111    /// # Errors
112    /// [`SignedError`] when the bytes do not decode or the signature does not verify.
113    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}