//! The bytes a membership fact is signed over, and the check of a signature.//!//! Every signed string is a run of `lp` fields: a 4-byte big-endian length,//! then the bytes. The first field names what is being said, so a signature//! over one kind of statement never verifies as another. A key and a signature//! are written as base64url, the form a device key has on the wire.//!//! admission lp("cotton-admission-v1") ‖ lp(database) ‖ lp(person key) ‖ lp(rights) ‖ lp(invite id) ‖ lp(admitted at)//! device statement lp("cotton-device-statement-v1") ‖ lp(database) ‖ lp(person key) ‖ lp(device key) ‖ lp(vault public key)//! its consent lp("cotton-device-statement-consent-v1") ‖ the same four fields//! removal lp("cotton-removal-v1") ‖ lp(database) ‖ lp("person" | "device_key") ‖ lp(subject) ‖ lp(stamp)//!//! An admission that answers no invite signs an empty invite id.use ed25519_dalek::{Signature, VerifyingKey};usecrate::device::base64url::base64url_decode;pubconst ADMISSION_CONTEXT: &str = "cotton-admission-v1";pubconst DEVICE_STATEMENT_CONTEXT: &str = "cotton-device-statement-v1";pubconst DEVICE_CONSENT_CONTEXT: &str = "cotton-device-statement-consent-v1";pubconst REMOVAL_CONTEXT: &str = "cotton-removal-v1";/// The subject of a removal that names a person key.pubconst PERSON_SUBJECT: &str = "person";/// The subject of a removal that names one device key.pubconst DEVICE_KEY_SUBJECT: &str = "device_key";fn lp(out: &mutVec<u8>, field: &str) { out.extend_from_slice(&(field.len() as u32).to_be_bytes()); out.extend_from_slice(field.as_bytes());}fn fields(parts: &[&str]) -> Vec<u8> {letmut out = Vec::new();for part in parts { lp(&mut out, part); } out}/// What the admitter's person key signs to admit `person_key` with `rights`.pubfn admission_bytes( database_id: &str, person_key: &str, rights: &str, invite_id: Option<&str>, admitted_at: &str,) -> Vec<u8> { fields(&[ ADMISSION_CONTEXT, database_id, person_key, rights, invite_id.unwrap_or(""), admitted_at, ])}/// What a person key signs to say `device_key` is one of its devices.pubfn device_statement_bytes( database_id: &str, person_key: &str, device_key: &str, vault_public_key: &str,) -> Vec<u8> { fields(&[ DEVICE_STATEMENT_CONTEXT, database_id, person_key, device_key, vault_public_key, ])}/// What the named device signs in consent to that statement.pubfn device_consent_bytes( database_id: &str, person_key: &str, device_key: &str, vault_public_key: &str,) -> Vec<u8> { fields(&[ DEVICE_CONSENT_CONTEXT, database_id, person_key, device_key, vault_public_key, ])}/// What the remover's person key signs to remove a person or one device key./// `subject_kind` is [`PERSON_SUBJECT`] or [`DEVICE_KEY_SUBJECT`].pubfn removal_bytes(database_id: &str, subject_kind: &str, subject: &str, stamp: &str) -> Vec<u8> { fields(&[REMOVAL_CONTEXT, database_id, subject_kind, subject, stamp])}/// Whether `signature` is `key`'s Ed25519 signature over `message`. A key or a/// signature that is not base64url of the right length verifies nothing.pubfn verifies(key: &str, message: &[u8], signature: &str) -> bool {letSome(key) = base64url_decode(key).and_then(|bytes| <[u8; 32]>::try_from(bytes).ok()) else {returnfalse; };letSome(signature) = base64url_decode(signature).and_then(|bytes| <[u8; 64]>::try_from(bytes).ok())else {returnfalse; };VerifyingKey::from_bytes(&key) .and_then(|key| key.verify_strict(message, &Signature::from_bytes(&signature))) .is_ok()}#[cfg(test)]#[path = "statements_tests.rs"]mod tests;