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
//! Host handover: the host signs a record naming the member who becomes host. A `Leave`
//! handover is given when the host leaves on purpose; a `Standby` one is given ahead of time to
//! the next host, who uses it only when the host can no longer be reached. Either way the
//! successor applies it to its own copy of the project ([`crate::Project::accept_handover`]).
use crate::enc::{Enc, signed_by};
use crate::ids::ProjectId;
use crate::records::SignError;
use serde::{Deserialize, Serialize};
use weirdnet_contacts::UserId;
use weirdnet_net::Keypair;

/// Why the host role moves.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum HandoverKind {
    /// The host is leaving now.
    Leave,
    /// Signed in advance, for when the host can no longer be reached.
    Standby,
}

/// A signed handover of the host role.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Handover {
    /// The project.
    pub project: ProjectId,
    /// The host who signed it.
    pub from: UserId,
    /// The member who becomes host.
    pub to: UserId,
    /// Leave or standby.
    pub kind: HandoverKind,
    /// When it was signed, by the host's clock.
    pub at_ms: i64,
    /// The host's public key, protobuf-encoded.
    pub key: Vec<u8>,
    /// The host's signature.
    pub sig: Vec<u8>,
}

impl Handover {
    fn bytes(&self) -> Vec<u8> {
        Enc::new("handover")
            .s(self.project.as_str())
            .s(self.from.as_str())
            .s(self.to.as_str())
            .u8(match self.kind {
                HandoverKind::Leave => 0,
                HandoverKind::Standby => 1,
            })
            .i64(self.at_ms)
            .b(&self.key)
            .done()
    }

    /// Sign a handover of `project` from `host` to `to`. Use [`crate::Project::hand_over`],
    /// which also checks that `to` is a member.
    pub(crate) fn sign(host: &Keypair, project: &ProjectId, to: &UserId, kind: HandoverKind, now: i64) -> Result<Handover, SignError> {
        let mut h = Handover {
            project: project.clone(),
            from: UserId::of_keypair(host),
            to: to.clone(),
            kind,
            at_ms: now,
            key: host.public().encode_protobuf(),
            sig: Vec::new(),
        };
        h.sig = host.sign(&h.bytes()).map_err(|e| SignError(e.to_string()))?;
        Ok(h)
    }

    /// Whether `from` signed it.
    pub fn verify(&self) -> bool {
        signed_by(&self.from, &self.key, &self.bytes(), &self.sig)
    }
}