Skip to main content

zaun_community_protocol/signed/
bot_package.rs

1//! `BotPackage` (ctx `zaun/v1/bot-package`, specification §4.9, ADR-0019): the signed header
2//! of a bot package. It names the bot, its version and publisher, the WIT world it targets,
3//! and the `SHA3-256` of the manifest and the component, so the publisher's signature covers
4//! the whole `.zbot` file.
5
6mod container;
7mod rules;
8
9pub use container::{
10    PACKAGE_MAGIC, PACKAGE_VERSION, PackageError, ParsedPackage, VerifiedPackage, parse_package,
11    verify_package, write_package, write_unsigned_package,
12};
13pub use rules::{SUPPORTED_WORLDS, compare_versions, valid_app_id, valid_version, valid_world};
14
15use super::object::{FORMAT_V1, SignedObject, read_format};
16use crate::crypto::Context;
17use crate::encoding::{Decode, DecodeError, Encode, EncodeError, Reader, Writer};
18use crate::keys::{PUBLIC_KEY_LEN, PublicKey};
19
20/// The package format this header describes.
21pub const PACKAGE_FORMAT: &str = "zaun-bot/v1";
22/// Bytes of `app_id` (3–64).
23pub const APP_ID_MAX: usize = 64;
24/// Bytes of `version`.
25pub const VERSION_MAX: usize = 64;
26/// Bytes of `world`.
27pub const WORLD_MAX: usize = 64;
28
29/// The signed header of a bot package (§4.9), signed by the publisher key inside it.
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct BotPackage {
32    /// 3: lowercase reverse-DNS, 3–64 bytes.
33    pub app_id: String,
34    /// 4: Semantic Versioning 2.0.0 without build metadata, ≤ 64 bytes.
35    pub version: String,
36    /// 5: the publisher key; the signer. `None` (written as `PUBLIC_KEY_LEN` zero bytes) only
37    /// in an unsigned development package, whose signature is all zero too.
38    pub publisher_key: Option<PublicKey>,
39    /// 6: `zaun:bot/bot@<semver>`, ≤ 64 bytes.
40    pub world: String,
41    /// 7: `SHA3-256` of the manifest bytes.
42    pub manifest_sha3: [u8; 32],
43    /// 8: `SHA3-256` of the component bytes.
44    pub component_sha3: [u8; 32],
45    /// 9: ms since the epoch.
46    pub created_at: u64,
47}
48
49impl BotPackage {
50    /// The field rules of §4.9; the failing field's name.
51    fn check(&self) -> Result<(), &'static str> {
52        if !valid_app_id(&self.app_id) {
53            return Err("app_id");
54        }
55        if !valid_version(&self.version) {
56            return Err("version");
57        }
58        if !valid_world(&self.world) {
59            return Err("world");
60        }
61        Ok(())
62    }
63}
64
65impl Encode for BotPackage {
66    fn encode(&self, w: &mut Writer) -> Result<(), EncodeError> {
67        self.check().map_err(EncodeError::Invalid)?;
68        w.u8(FORMAT_V1);
69        w.string(PACKAGE_FORMAT, PACKAGE_FORMAT.len())?;
70        w.string(&self.app_id, APP_ID_MAX)?;
71        w.string(&self.version, VERSION_MAX)?;
72        match &self.publisher_key {
73            Some(key) => w.put(key)?,
74            None => w.fixed(&[0; PUBLIC_KEY_LEN]),
75        }
76        w.string(&self.world, WORLD_MAX)?;
77        w.fixed(&self.manifest_sha3);
78        w.fixed(&self.component_sha3);
79        w.u64(self.created_at);
80        Ok(())
81    }
82}
83
84impl Decode for BotPackage {
85    fn decode(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
86        read_format(r)?;
87        if r.string(PACKAGE_FORMAT.len())? != PACKAGE_FORMAT {
88            return Err(DecodeError::Invalid("package_format"));
89        }
90        let package = Self {
91            app_id: r.string(APP_ID_MAX)?.to_owned(),
92            version: r.string(VERSION_MAX)?.to_owned(),
93            publisher_key: read_publisher_key(r)?,
94            world: r.string(WORLD_MAX)?.to_owned(),
95            manifest_sha3: r.array()?,
96            component_sha3: r.array()?,
97            created_at: r.u64()?,
98        };
99        package.check().map_err(DecodeError::Invalid)?;
100        Ok(package)
101    }
102}
103
104/// A publisher key, or the all-zero placeholder of an unsigned package (suite byte 0, which
105/// no key has).
106fn read_publisher_key(r: &mut Reader<'_>) -> Result<Option<PublicKey>, DecodeError> {
107    if r.peek_u8()? != 0 {
108        return r.get().map(Some);
109    }
110    if r.fixed(PUBLIC_KEY_LEN)?.iter().any(|b| *b != 0) {
111        return Err(DecodeError::UnknownSuite(0));
112    }
113    Ok(None)
114}
115
116impl SignedObject for BotPackage {
117    const CONTEXT: Context = Context::BotPackage;
118}