Skip to main content

vector_core/community/v2/
roles.rs

1//! CORD-04 Roles: v2 wire content for the Role (vsk 1), Grant (vsk 3), and Banlist
2//! (vsk 4) control entities.
3//!
4//! The authority ALGEBRA is shared, not copied: these types convert to/from
5//! [`crate::community::roles`]'s `Role`/`MemberGrant`/`Permissions`, and the fold
6//! feeds the shared `authorize_delegation` + `is_authorized`. Only the SERIALIZATION
7//! is v2-native, because CORD-04 §3 rides `permissions` as a decimal STRING (the
8//! shared `Permissions` serializes as a bare `u64` for v1's storage), and a reader
9//! MUST accept either form (a number from an older edition, a string henceforth) and
10//! always write the string.
11
12use serde::{Deserialize, Serialize};
13
14use crate::community::roles::{MemberGrant, Permissions, Role, RoleScope};
15
16/// A member holds at most this many Roles (CORD-04 §2); a Community folds at most
17/// [`MAX_ROLES_PER_COMMUNITY`] Roles (the lowest role_ids win, the same deterministic
18/// cap the member list uses). A Banlist edition holds at most [`MAX_BANLIST`] npubs
19/// (the practical NIP-44-envelope ceiling, CORD-04 §4).
20pub const MAX_ROLES_PER_MEMBER: usize = 64;
21pub const MAX_ROLES_PER_COMMUNITY: usize = 100;
22pub const MAX_BANLIST: usize = 500;
23/// A role `name` shares the protocol-wide 64-byte cap (CORD-04 §2/§3).
24pub const MAX_ROLE_NAME_BYTES: usize = super::control::MAX_NAME_BYTES;
25
26/// CORD-04 §2 Role content (vsk 1), `eid == role_id`. `permissions` rides as a
27/// decimal string (§3); unknown fields round-trip (CORD-02 §6).
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29pub struct RoleContent {
30    pub role_id: String,
31    pub name: String,
32    pub position: u32,
33    #[serde(with = "perm_decimal_string")]
34    pub permissions: u64,
35    pub scope: RoleScope,
36    #[serde(default)]
37    pub color: u32,
38    #[serde(flatten)]
39    pub extra: serde_json::Map<String, serde_json::Value>,
40}
41
42impl RoleContent {
43    pub fn from_role(r: &Role) -> Self {
44        RoleContent {
45            role_id: r.role_id.clone(),
46            name: r.name.clone(),
47            position: r.position,
48            permissions: r.permissions.0,
49            scope: r.scope.clone(),
50            color: r.color,
51            extra: serde_json::Map::new(),
52        }
53    }
54    pub fn into_role(self) -> Role {
55        Role {
56            role_id: self.role_id,
57            name: self.name,
58            position: self.position,
59            permissions: Permissions(self.permissions),
60            scope: self.scope,
61            color: self.color,
62        }
63    }
64}
65
66/// CORD-04 §2 Grant content (vsk 3), `eid == grant_locator(community_id, member)`.
67/// Empty `role_ids` is a revoke.
68#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
69pub struct GrantContent {
70    pub member: String,
71    #[serde(default)]
72    pub role_ids: Vec<String>,
73    /// The staff write key riding a staff-making Grant (CORD-04 §3): the
74    /// current `control_root` NIP-44-encrypted under the granter↔member
75    /// pairwise conversation key, base64 — delivery, never authority. Its
76    /// plaintext is fixed-width `epoch_be[8] ‖ control_root[32]`, and the
77    /// recipient adopts the secret only if it derives to exactly the
78    /// `control_pk` they hold for the named epoch. Opaque pairwise ciphertext
79    /// to every other reader.
80    #[serde(default, skip_serializing_if = "Option::is_none")]
81    pub control_wrap: Option<String>,
82    #[serde(flatten)]
83    pub extra: serde_json::Map<String, serde_json::Value>,
84}
85
86/// Sanity bound on a carried `control_wrap` (a NIP-44 wrap of 40 bytes is
87/// ~130 chars); anything over is dropped on read, never carried.
88pub const MAX_CONTROL_WRAP_CHARS: usize = 1024;
89
90impl GrantContent {
91    pub fn from_grant(g: &MemberGrant) -> Self {
92        GrantContent { member: g.member.clone(), role_ids: g.role_ids.clone(), control_wrap: None, extra: serde_json::Map::new() }
93    }
94    pub fn into_grant(self) -> MemberGrant {
95        MemberGrant { member: self.member, role_ids: self.role_ids }
96    }
97}
98
99/// Serialize a Role's content to its CORD-04 §2 wire JSON (permissions as a string).
100pub fn role_content_json(r: &Role) -> Result<String, String> {
101    serde_json::to_string(&RoleContent::from_role(r)).map_err(|e| e.to_string())
102}
103
104/// Parse a vsk-1 edition's content into a shared `Role`. Accepts permissions as a
105/// string or a legacy number.
106pub fn parse_role_content(content: &str) -> Option<Role> {
107    serde_json::from_str::<RoleContent>(content).ok().map(RoleContent::into_role)
108}
109
110/// Serialize a Grant's content to its CORD-04 §2 wire JSON.
111pub fn grant_content_json(g: &MemberGrant) -> Result<String, String> {
112    serde_json::to_string(&GrantContent::from_grant(g)).map_err(|e| e.to_string())
113}
114
115/// [`grant_content_json`] carrying the staff write key (CORD-04 §3).
116pub fn grant_content_json_with_wrap(g: &MemberGrant, control_wrap: Option<String>) -> Result<String, String> {
117    let mut content = GrantContent::from_grant(g);
118    content.control_wrap = control_wrap;
119    serde_json::to_string(&content).map_err(|e| e.to_string())
120}
121
122/// Parse a vsk-3 edition's content into a shared `MemberGrant`.
123pub fn parse_grant_content(content: &str) -> Option<MemberGrant> {
124    serde_json::from_str::<GrantContent>(content).ok().map(GrantContent::into_grant)
125}
126
127/// Parse a vsk-3 edition's `control_wrap`, if one rides within bounds
128/// (CORD-04 §3). Only the grant's own member can open it — every other
129/// reader treats it as opaque bytes.
130pub fn parse_grant_control_wrap(content: &str) -> Option<String> {
131    serde_json::from_str::<GrantContent>(content)
132        .ok()?
133        .control_wrap
134        .filter(|w| !w.is_empty() && w.len() <= MAX_CONTROL_WRAP_CHARS)
135}
136
137/// Serialize a banlist to its CORD-04 §4 wire JSON (a flat array of lowercase-hex
138/// npubs, replaced entire on every edit).
139pub fn banlist_content_json(banned: &[String]) -> Result<String, String> {
140    serde_json::to_string(banned).map_err(|e| e.to_string())
141}
142
143/// Parse a vsk-4 edition's content into the banned set (lowercase hex). Non-array or
144/// malformed content yields `None` (dropped, never a partial ban).
145pub fn parse_banlist_content(content: &str) -> Option<Vec<String>> {
146    serde_json::from_str::<Vec<String>>(content).ok()
147}
148
149/// A Role's content byte-fits its cap discipline: name ≤ 64 bytes. (The 100-per-
150/// community and 64-per-member caps are fold-side, applied on read.)
151pub fn validate_role(r: &Role) -> Result<(), String> {
152    if r.name.len() > MAX_ROLE_NAME_BYTES {
153        return Err("role name over 64 bytes".to_string());
154    }
155    // Position 0 is the owner's alone; no Role may claim it (CORD-04 §3).
156    if r.position == 0 {
157        return Err("position 0 is reserved to the owner".to_string());
158    }
159    Ok(())
160}
161
162/// A Banlist edition fits its ceiling (CORD-04 §4): refuse an over-cap edit rather
163/// than publish one a strict reader drops.
164pub fn validate_banlist(banned: &[String]) -> Result<(), String> {
165    if banned.len() > MAX_BANLIST {
166        return Err("banlist over the 500-npub ceiling".to_string());
167    }
168    Ok(())
169}
170
171/// CORD-04 §3 permissions: a decimal string on the wire, accepting either a string or
172/// a legacy bare number on read, and always writing the string. Digit-only (a leading
173/// `+`/`-` or whitespace is rejected, matching the strict `ms`/`ev` discipline).
174mod perm_decimal_string {
175    use serde::{Deserialize, Deserializer, Serializer};
176
177    pub fn serialize<S: Serializer>(v: &u64, s: S) -> Result<S::Ok, S::Error> {
178        s.serialize_str(&v.to_string())
179    }
180
181    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<u64, D::Error> {
182        #[derive(Deserialize)]
183        #[serde(untagged)]
184        enum StringOrNumber {
185            S(String),
186            N(u64),
187        }
188        match StringOrNumber::deserialize(d)? {
189            StringOrNumber::N(n) => Ok(n),
190            StringOrNumber::S(s) => {
191                if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) {
192                    return Err(serde::de::Error::custom("permissions must be a decimal string"));
193                }
194                s.parse::<u64>().map_err(serde::de::Error::custom)
195            }
196        }
197    }
198}
199
200#[cfg(test)]
201mod tests {
202    use super::*;
203
204    fn role(perms: u64, pos: u32) -> Role {
205        Role {
206            role_id: "aa".repeat(32),
207            name: "Moderator".into(),
208            position: pos,
209            permissions: Permissions(perms),
210            scope: RoleScope::Server,
211            color: 15158332,
212        }
213    }
214
215    #[test]
216    fn role_permissions_ride_as_a_decimal_string() {
217        // CORD-04 §3 canonical example: 1<<3 KICK | 1<<5 MANAGE_MESSAGES = 40.
218        let json = role_content_json(&role(40, 2)).unwrap();
219        assert!(json.contains("\"permissions\":\"40\""), "permissions is a decimal string, not a number: {json}");
220        assert!(!json.contains("\"permissions\":40"), "must not emit a bare number");
221    }
222
223    #[test]
224    fn role_round_trips_through_the_wire_form() {
225        let r = role(Permissions::ADMIN_ALL, 1);
226        let back = parse_role_content(&role_content_json(&r).unwrap()).unwrap();
227        assert_eq!(back, r);
228    }
229
230    #[test]
231    fn a_legacy_numeric_permissions_is_still_read() {
232        // A reader MUST accept a bare number from an older edition (§3).
233        let legacy = r#"{"role_id":"bb","name":"X","position":3,"permissions":40,"scope":{"kind":"server"},"color":0}"#;
234        assert_eq!(parse_role_content(legacy).unwrap().permissions, Permissions(40));
235    }
236
237    #[test]
238    fn a_non_digit_permissions_string_is_rejected() {
239        for bad in ["\"+40\"", "\"4a\"", "\"\"", "\" 40\""] {
240            let j = format!(r#"{{"role_id":"cc","name":"X","position":2,"permissions":{bad},"scope":{{"kind":"server"}}}}"#);
241            assert!(parse_role_content(&j).is_none(), "rejected non-digit permissions {bad}");
242        }
243    }
244
245    #[test]
246    fn channel_scope_round_trips() {
247        let mut r = role(8, 2);
248        r.scope = RoleScope::Channel("cc".repeat(32));
249        let json = role_content_json(&r).unwrap();
250        assert!(json.contains(r#""scope":{"kind":"channel","channel_id":""#), "{json}");
251        assert_eq!(parse_role_content(&json).unwrap().scope, r.scope);
252    }
253
254    #[test]
255    fn grant_round_trips_and_empty_is_a_revoke() {
256        let g = MemberGrant { member: "dd".repeat(32), role_ids: vec!["aa".repeat(32)] };
257        assert_eq!(parse_grant_content(&grant_content_json(&g).unwrap()).unwrap(), g);
258        let revoke = MemberGrant { member: "ee".repeat(32), role_ids: vec![] };
259        let back = parse_grant_content(&grant_content_json(&revoke).unwrap()).unwrap();
260        assert!(back.role_ids.is_empty());
261    }
262
263    #[test]
264    fn banlist_round_trips() {
265        let banned = vec!["11".repeat(32), "22".repeat(32)];
266        assert_eq!(parse_banlist_content(&banlist_content_json(&banned).unwrap()).unwrap(), banned);
267        assert!(parse_banlist_content("not-an-array").is_none());
268    }
269
270    #[test]
271    fn unknown_role_fields_round_trip() {
272        let j = r#"{"role_id":"ab","name":"X","position":2,"permissions":"8","scope":{"kind":"server"},"color":0,"future":"keep"}"#;
273        let parsed: RoleContent = serde_json::from_str(j).unwrap();
274        let reser = serde_json::to_string(&parsed).unwrap();
275        assert!(reser.contains("\"future\":\"keep\""), "unknown fields survive: {reser}");
276    }
277
278    #[test]
279    fn validate_rejects_pos_zero_and_long_name() {
280        assert!(validate_role(&role(8, 0)).is_err(), "position 0 reserved to owner");
281        let mut long = role(8, 2);
282        long.name = "x".repeat(65);
283        assert!(validate_role(&long).is_err());
284        assert!(validate_banlist(&vec!["z".into(); MAX_BANLIST + 1]).is_err());
285    }
286}