weirdnet-collab 0.3.0

Collaboration control for Weirdnet apps: project members and roles, signed allow and deny rules, owner consent grants, a decision with a reason chain, invites and a signed, mergeable audit log. App-agnostic: the app defines what a project, role and permission are.
Documentation
//! The canonical byte layout every signature in this crate covers: a domain tag, then fields in
//! a fixed order, strings and byte fields with a 32-bit big-endian length, integers big-endian.
pub(crate) struct Enc(Vec<u8>);

impl Enc {
    pub fn new(domain: &str) -> Self {
        let mut v = format!("weirdnet-collab/{domain}/1").into_bytes();
        v.push(0);
        Enc(v)
    }

    pub fn s(mut self, s: &str) -> Self {
        self.0.extend_from_slice(&(s.len() as u32).to_be_bytes());
        self.0.extend_from_slice(s.as_bytes());
        self
    }

    pub fn b(mut self, b: &[u8]) -> Self {
        self.0.extend_from_slice(&(b.len() as u32).to_be_bytes());
        self.0.extend_from_slice(b);
        self
    }

    pub fn u8(mut self, n: u8) -> Self {
        self.0.push(n);
        self
    }

    pub fn u32(mut self, n: u32) -> Self {
        self.0.extend_from_slice(&n.to_be_bytes());
        self
    }

    pub fn i64(mut self, n: i64) -> Self {
        self.0.extend_from_slice(&n.to_be_bytes());
        self
    }

    pub fn opt_i64(self, n: Option<i64>) -> Self {
        match n {
            Some(n) => self.u8(1).i64(n),
            None => self.u8(0),
        }
    }

    pub fn opt_s(self, s: Option<&str>) -> Self {
        match s {
            Some(s) => self.u8(1).s(s),
            None => self.u8(0),
        }
    }

    pub fn done(self) -> Vec<u8> {
        self.0
    }
}

/// Whether `key` (a protobuf-encoded public key) is `user`'s and signed `bytes` as `sig`.
pub(crate) fn signed_by(user: &weirdnet_contacts::UserId, key: &[u8], bytes: &[u8], sig: &[u8]) -> bool {
    weirdnet_contacts::UserId::of_key_bytes(key).as_ref() == Some(user)
        && weirdnet_net::PublicKey::try_decode_protobuf(key).map(|pk| pk.verify(bytes, sig)).unwrap_or(false)
}