From fafd36137e938ea8b9110634dfabe5e20a17f473 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Thu, 12 Nov 2026 19:16:15 -0600 Subject: [PATCH] pkgs/id crate split step 6a: WorldScopes -> id-caps, hex_encode -> id-core (#4409) Prep for step 6 (id-directory): the directory modules name exactly two things from the worlds crate, WorldScopes (the ceiling surface) and world::hex_encode. Design seam 10 says id-directory must not depend on id-worlds, so both move down first, following the step 1-2 precedent of shared vocabulary moving into the caps/kernel crates. - WorldScopes moves to a new id_caps::world_scopes module. Its only outward reference, world_caps::PROVIDER_REGISTRY in allows_effect, resolves inside id-caps unchanged. id-worlds re-exports it at world::WorldScopes, so every existing path compiles as before. all_recognized becomes pub (it was pub(crate)); a named EMPTY constant replaces direct tuple construction at the two core.rs sites that used it, and test code that built sets from raw fields now uses bits()/union()/from_bits(). - hex_encode moves to a new id_core::hex module; world::util re-exports it, keeping crate::world::hex_encode valid for directory, directory_auth and directory_mail. Behaviour-preserving throughout. Verification on the laptop at 6741fc50a4 baseline: id-caps 87 passed (baseline 87), id-core 386 passed + the pre-existing #8354 frames failure (identical at baseline), id-worlds 721 passed (baseline 721), facade 1704 passed / 0 failed / 19 ignored (identical to baseline). FUEL_BUDGETS in crates/id-worlds/src/sandbox.rs is byte-identical to the baseline snapshot (diff empty); sandbox.rs is untouched by this change. --- pkgs/id/crates/id-caps/src/lib.rs | 1 + pkgs/id/crates/id-caps/src/world_scopes.rs | 204 ++++++++++++++++++++ pkgs/id/crates/id-core/src/hex.rs | 18 ++ pkgs/id/crates/id-core/src/lib.rs | 3 +- pkgs/id/crates/id-worlds/src/world/core.rs | 6 +- pkgs/id/crates/id-worlds/src/world/mod.rs | 196 +------------------ pkgs/id/crates/id-worlds/src/world/tests.rs | 65 +++---- pkgs/id/crates/id-worlds/src/world/util.rs | 14 +- 8 files changed, 264 insertions(+), 243 deletions(-) create mode 100644 pkgs/id/crates/id-caps/src/world_scopes.rs create mode 100644 pkgs/id/crates/id-core/src/hex.rs diff --git a/pkgs/id/crates/id-caps/src/lib.rs b/pkgs/id/crates/id-caps/src/lib.rs index 52777806d1..bcc7289691 100644 --- a/pkgs/id/crates/id-caps/src/lib.rs +++ b/pkgs/id/crates/id-caps/src/lib.rs @@ -13,3 +13,4 @@ pub mod world_caps; pub mod world_limits; pub mod world_manifest; +pub mod world_scopes; diff --git a/pkgs/id/crates/id-caps/src/world_scopes.rs b/pkgs/id/crates/id-caps/src/world_scopes.rs new file mode 100644 index 0000000000..7f103d48ac --- /dev/null +++ b/pkgs/id/crates/id-caps/src/world_scopes.rs @@ -0,0 +1,204 @@ +//! Permission scopes granted by a world capability: the scope +//! vocabulary shared by the world runtime and the directory. +//! +//! Moved here from `id-worlds` (`world::WorldScopes`) as step 6a of +//! the pkgs/id crate split (#3447 umbrella, #4409): `id-directory` +//! names this type in its ceiling surface, and the directory must +//! not depend on the worlds crate. `id-worlds` re-exports it at its +//! historical `world::WorldScopes` path, so no call site changed. +//! `allows_effect` reads the provider registry in +//! [`crate::world_caps`], this crate's own catalog module. + +/// Permission scopes granted by a world capability. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct WorldScopes(u8); + +impl WorldScopes { + /// Join the world and receive snapshots/events. + pub const JOIN: Self = Self(0b001); + /// Send chat messages. + pub const CHAT: Self = Self(0b010); + /// Submit game input events. + pub const INPUT: Self = Self(0b100); + /// Mint capabilities with a subset of this set. + pub const DELEGATE: Self = Self(0b1000); + /// Read chat lines without being able to send them. `CHAT` also reads. + pub const HISTORY: Self = Self(0b1_0000); + /// Install and upgrade the world's program. Carries no capability, + /// network, file or process authority: only the install path reads it. + pub const BUILDER: Self = Self(0b10_0000); + /// Revoke participants. Carries no capability, network, file or process + /// authority: only the revoke path reads it. + pub const MODERATOR: Self = Self(0b100_0000); + /// No scopes at all: the empty set. + pub const EMPTY: Self = Self(0); + + /// Default guest permissions. + pub const GUEST: Self = Self(Self::JOIN.0 | Self::CHAT.0 | Self::INPUT.0); + /// Every participant scope. The admin authority bits `BUILDER` and + /// `MODERATOR` are deliberately excluded: they are issued only by the + /// admin, never implied by a ceiling or handed out by a delegator. + pub const ALL: Self = Self(Self::GUEST.0 | Self::DELEGATE.0 | Self::HISTORY.0); + /// Every bit the type recognizes, including the admin authority bits. + /// [`Self::from_bits`] accepts these so a journaled builder or moderator + /// capability round-trips, without widening [`Self::ALL`]. + const ALL_BITS: u8 = Self::ALL.0 | Self::BUILDER.0 | Self::MODERATOR.0; + + /// Every recognized bit, including the admin authority bits. Used as + /// the seed for authority-aware effective-scope computation: unlike + /// [`Self::ALL`], this lets an admin-issued `BUILDER` / `MODERATOR` + /// bit survive the ancestor-chain intersection in + /// `WorldCore::authorize` (design #835 phase 2). It grants nothing on + /// its own — the chain intersection only ever removes bits. + pub const fn all_recognized() -> Self { + Self(Self::ALL_BITS) + } + + /// Whether this set includes the builder authority (install modules). + #[must_use] + pub const fn is_builder(self) -> bool { + self.contains(Self::BUILDER) + } + + /// Whether this set includes the moderator authority (revoke participants). + #[must_use] + pub const fn is_moderator(self) -> bool { + self.contains(Self::MODERATOR) + } + + /// Role presets used by `id world invite --role`. `spectator` joins and + /// sees the world but not the chat; `observer` also reads chat without + /// sending it; `player` submits input; `guest` (the default) also chats; + /// `delegator` additionally mints narrower capabilities for others; + /// `builder` additionally installs modules; `moderator` additionally + /// revokes participants. + /// + /// The names are stable; the mapping to scopes is part of the protocol. + #[must_use] + pub fn from_role(name: &str) -> Option { + match name { + "spectator" => Some(Self::JOIN), + "observer" => Some(Self(Self::JOIN.0 | Self::HISTORY.0)), + "player" => Some(Self(Self::JOIN.0 | Self::INPUT.0)), + "guest" => Some(Self::GUEST), + "delegator" => Some(Self(Self::GUEST.0 | Self::DELEGATE.0)), + "builder" => Some(Self(Self::GUEST.0 | Self::BUILDER.0)), + "moderator" => Some(Self(Self::GUEST.0 | Self::MODERATOR.0)), + _ => None, + } + } + + /// The names accepted by [`Self::from_role`]. + pub const ROLES: &'static [&'static str] = &[ + "spectator", + "observer", + "player", + "guest", + "delegator", + "builder", + "moderator", + ]; + + /// Raw scope bits, as journaled. + #[must_use] + pub const fn bits(self) -> u8 { + self.0 + } + + /// Scopes from raw bits; `None` if any bit names no scope. The admin + /// authority bits round-trip here even though they are outside [`Self::ALL`]. + #[must_use] + pub const fn from_bits(bits: u8) -> Option { + if bits & !Self::ALL_BITS == 0 { + Some(Self(bits)) + } else { + None + } + } + + /// Every scope either set includes. + #[must_use] + pub const fn union(self, other: Self) -> Self { + Self(self.0 | other.0) + } + + /// Only the scopes both sets include. + #[must_use] + pub const fn intersect(self, other: Self) -> Self { + Self(self.0 & other.0) + } + + /// Whether this scope set includes `required`. + #[must_use] + pub const fn contains(self, required: Self) -> bool { + self.0 & required.0 == required.0 + } + + /// This set with the scopes it includes by implication: `CHAT` reads, so + /// it carries `HISTORY`. Apply it wherever sets are compared as sets. + #[must_use] + pub const fn implied(self) -> Self { + if self.contains(Self::CHAT) { + Self(self.0 | Self::HISTORY.0) + } else { + self + } + } + + /// Whether a participant with this set may see chat lines. + #[must_use] + pub const fn reads_chat(self) -> bool { + self.0 & (Self::CHAT.0 | Self::HISTORY.0) != 0 + } + + /// The scopes a delegate of this set may hold: a subset that keeps `JOIN`. + /// `None` unless this set includes `DELEGATE` and `requested` is a subset. + /// A set with `CHAT` may delegate `HISTORY`, which `CHAT` includes. + #[must_use] + pub const fn attenuate(self, requested: Self) -> Option { + if self.contains(Self::DELEGATE) + && requested.contains(Self::JOIN) + && self.implied().contains(requested) + { + Some(requested) + } else { + None + } + } + + /// Whether a participant with this set may cause the effect `cap`. Every + /// connected participant may read; `chat.say` also needs `CHAT`. + #[must_use] + pub fn allows_effect(self, cap: &str) -> bool { + match crate::world_caps::PROVIDER_REGISTRY + .get(cap) + .map_or(crate::world_caps::RequiredScope::Join, |provider| { + provider.required_scope + }) { + crate::world_caps::RequiredScope::Chat => self.contains(Self::CHAT), + // Reading chat needs only the right to read it (`CHAT` or + // `HISTORY`), the same rule the event stream applies. + crate::world_caps::RequiredScope::ChatRead => self.reads_chat(), + crate::world_caps::RequiredScope::Join => self.contains(Self::JOIN), + } + } + + /// Scopes named on the wire (`join`, `chat`, `history`, `input`, + /// `delegate`, `builder`, `moderator`), or `None` if a name is unknown. + #[must_use] + pub fn from_names<'a>(names: impl IntoIterator) -> Option { + names.into_iter().try_fold(Self(0), |scopes, name| { + let bit = match name { + "join" => Self::JOIN, + "chat" => Self::CHAT, + "history" => Self::HISTORY, + "input" => Self::INPUT, + "delegate" => Self::DELEGATE, + "builder" => Self::BUILDER, + "moderator" => Self::MODERATOR, + _ => return None, + }; + Some(Self(scopes.0 | bit.0)) + }) + } +} diff --git a/pkgs/id/crates/id-core/src/hex.rs b/pkgs/id/crates/id-core/src/hex.rs new file mode 100644 index 0000000000..3116c289cf --- /dev/null +++ b/pkgs/id/crates/id-core/src/hex.rs @@ -0,0 +1,18 @@ +//! Hex encoding helpers (leaf utilities with no dependencies). +//! +//! `hex_encode` moved here from `id-worlds` (`world::hex_encode`) +//! as step 6a of the pkgs/id crate split (#3447 umbrella, #4409): +//! the directory modules use it, and `id-directory` must not depend +//! on the worlds crate. `id-worlds` re-exports it at its historical +//! path. + +/// Lower-case hex encoding of a byte slice, two characters per byte. +pub fn hex_encode(bytes: &[u8]) -> String { + const HEX: &[u8; 16] = b"0123456789abcdef"; + let mut encoded = String::with_capacity(bytes.len() * 2); + for byte in bytes { + encoded.push(char::from(HEX[usize::from(byte >> 4)])); + encoded.push(char::from(HEX[usize::from(byte & 0x0f)])); + } + encoded +} diff --git a/pkgs/id/crates/id-core/src/lib.rs b/pkgs/id/crates/id-core/src/lib.rs index 57391d7bac..028657286c 100644 --- a/pkgs/id/crates/id-core/src/lib.rs +++ b/pkgs/id/crates/id-core/src/lib.rs @@ -28,6 +28,7 @@ pub mod boundary_surfaces; pub mod connectivity; pub mod discovery; pub mod frames; +pub mod hex; pub mod ip_evidence; pub mod json_unique; pub mod membership; @@ -35,8 +36,8 @@ pub mod rejection; pub mod residency; pub mod sanctions; pub mod sync_method; -pub mod time; pub mod text; +pub mod time; pub mod tuple; mod unicode; pub use unicode::is_format_char; diff --git a/pkgs/id/crates/id-worlds/src/world/core.rs b/pkgs/id/crates/id-worlds/src/world/core.rs index 6ef334cc3d..e68fcff065 100644 --- a/pkgs/id/crates/id-worlds/src/world/core.rs +++ b/pkgs/id/crates/id-worlds/src/world/core.rs @@ -1734,7 +1734,7 @@ impl WorldCore { .map(|record| JournalEntry::Issued { participant: record.participant.clone(), digest: hex_encode(&record.digest), - scopes: record.scopes.0, + scopes: record.scopes.bits(), parent: record.parent, expires_at: record.bounds.expires_at, uses: record.bounds.uses, @@ -1924,7 +1924,7 @@ impl WorldCore { && subject != parent_subject && !self.directory.are_friends(parent_subject, subject) { - return Some(WorldScopes(0)); + return Some(WorldScopes::EMPTY); } current = record.parent; } @@ -2605,7 +2605,7 @@ impl WorldCore { pub(crate) fn viewer_scopes(&self, participant_id: u64) -> WorldScopes { self.effective_scopes(participant_id) - .unwrap_or(WorldScopes(0)) + .unwrap_or(WorldScopes::EMPTY) } pub(crate) fn viewer(&self, token: &JoinCapability) -> Result { diff --git a/pkgs/id/crates/id-worlds/src/world/mod.rs b/pkgs/id/crates/id-worlds/src/world/mod.rs index 54de7c709b..9f0450b88a 100644 --- a/pkgs/id/crates/id-worlds/src/world/mod.rs +++ b/pkgs/id/crates/id-worlds/src/world/mod.rs @@ -162,196 +162,12 @@ impl Default for WorldLimits { } } -/// Permission scopes granted by a world capability. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct WorldScopes(u8); - -impl WorldScopes { - /// Join the world and receive snapshots/events. - pub const JOIN: Self = Self(0b001); - /// Send chat messages. - pub const CHAT: Self = Self(0b010); - /// Submit game input events. - pub const INPUT: Self = Self(0b100); - /// Mint capabilities with a subset of this set. - pub const DELEGATE: Self = Self(0b1000); - /// Read chat lines without being able to send them. `CHAT` also reads. - pub const HISTORY: Self = Self(0b1_0000); - /// Install and upgrade the world's program. Carries no capability, - /// network, file or process authority: only the install path reads it. - pub const BUILDER: Self = Self(0b10_0000); - /// Revoke participants. Carries no capability, network, file or process - /// authority: only the revoke path reads it. - pub const MODERATOR: Self = Self(0b100_0000); - /// Default guest permissions. - pub const GUEST: Self = Self(Self::JOIN.0 | Self::CHAT.0 | Self::INPUT.0); - /// Every participant scope. The admin authority bits `BUILDER` and - /// `MODERATOR` are deliberately excluded: they are issued only by the - /// admin, never implied by a ceiling or handed out by a delegator. - pub const ALL: Self = Self(Self::GUEST.0 | Self::DELEGATE.0 | Self::HISTORY.0); - /// Every bit the type recognizes, including the admin authority bits. - /// [`Self::from_bits`] accepts these so a journaled builder or moderator - /// capability round-trips, without widening [`Self::ALL`]. - const ALL_BITS: u8 = Self::ALL.0 | Self::BUILDER.0 | Self::MODERATOR.0; - - /// Every recognized bit, including the admin authority bits. Used as - /// the seed for authority-aware effective-scope computation: unlike - /// [`Self::ALL`], this lets an admin-issued `BUILDER` / `MODERATOR` - /// bit survive the ancestor-chain intersection in - /// `WorldCore::authorize` (design #835 phase 2). It grants nothing on - /// its own — the chain intersection only ever removes bits. - pub(crate) const fn all_recognized() -> Self { - Self(Self::ALL_BITS) - } - - /// Whether this set includes the builder authority (install modules). - #[must_use] - pub const fn is_builder(self) -> bool { - self.contains(Self::BUILDER) - } - - /// Whether this set includes the moderator authority (revoke participants). - #[must_use] - pub const fn is_moderator(self) -> bool { - self.contains(Self::MODERATOR) - } - - /// Role presets used by `id world invite --role`. `spectator` joins and - /// sees the world but not the chat; `observer` also reads chat without - /// sending it; `player` submits input; `guest` (the default) also chats; - /// `delegator` additionally mints narrower capabilities for others; - /// `builder` additionally installs modules; `moderator` additionally - /// revokes participants. - /// - /// The names are stable; the mapping to scopes is part of the protocol. - #[must_use] - pub fn from_role(name: &str) -> Option { - match name { - "spectator" => Some(Self::JOIN), - "observer" => Some(Self(Self::JOIN.0 | Self::HISTORY.0)), - "player" => Some(Self(Self::JOIN.0 | Self::INPUT.0)), - "guest" => Some(Self::GUEST), - "delegator" => Some(Self(Self::GUEST.0 | Self::DELEGATE.0)), - "builder" => Some(Self(Self::GUEST.0 | Self::BUILDER.0)), - "moderator" => Some(Self(Self::GUEST.0 | Self::MODERATOR.0)), - _ => None, - } - } - - /// The names accepted by [`Self::from_role`]. - pub const ROLES: &'static [&'static str] = &[ - "spectator", - "observer", - "player", - "guest", - "delegator", - "builder", - "moderator", - ]; - - /// Raw scope bits, as journaled. - #[must_use] - pub const fn bits(self) -> u8 { - self.0 - } - - /// Scopes from raw bits; `None` if any bit names no scope. The admin - /// authority bits round-trip here even though they are outside [`Self::ALL`]. - #[must_use] - pub const fn from_bits(bits: u8) -> Option { - if bits & !Self::ALL_BITS == 0 { - Some(Self(bits)) - } else { - None - } - } - - /// Every scope either set includes. - #[must_use] - pub const fn union(self, other: Self) -> Self { - Self(self.0 | other.0) - } - - /// Only the scopes both sets include. - #[must_use] - pub const fn intersect(self, other: Self) -> Self { - Self(self.0 & other.0) - } - - /// Whether this scope set includes `required`. - #[must_use] - pub const fn contains(self, required: Self) -> bool { - self.0 & required.0 == required.0 - } - - /// This set with the scopes it includes by implication: `CHAT` reads, so - /// it carries `HISTORY`. Apply it wherever sets are compared as sets. - #[must_use] - pub const fn implied(self) -> Self { - if self.contains(Self::CHAT) { - Self(self.0 | Self::HISTORY.0) - } else { - self - } - } - - /// Whether a participant with this set may see chat lines. - #[must_use] - pub const fn reads_chat(self) -> bool { - self.0 & (Self::CHAT.0 | Self::HISTORY.0) != 0 - } - - /// The scopes a delegate of this set may hold: a subset that keeps `JOIN`. - /// `None` unless this set includes `DELEGATE` and `requested` is a subset. - /// A set with `CHAT` may delegate `HISTORY`, which `CHAT` includes. - #[must_use] - pub const fn attenuate(self, requested: Self) -> Option { - if self.contains(Self::DELEGATE) - && requested.contains(Self::JOIN) - && self.implied().contains(requested) - { - Some(requested) - } else { - None - } - } - - /// Whether a participant with this set may cause the effect `cap`. Every - /// connected participant may read; `chat.say` also needs `CHAT`. - #[must_use] - pub fn allows_effect(self, cap: &str) -> bool { - match crate::world_caps::PROVIDER_REGISTRY - .get(cap) - .map_or(crate::world_caps::RequiredScope::Join, |provider| { - provider.required_scope - }) { - crate::world_caps::RequiredScope::Chat => self.contains(Self::CHAT), - // Reading chat needs only the right to read it (`CHAT` or - // `HISTORY`), the same rule the event stream applies. - crate::world_caps::RequiredScope::ChatRead => self.reads_chat(), - crate::world_caps::RequiredScope::Join => self.contains(Self::JOIN), - } - } - - /// Scopes named on the wire (`join`, `chat`, `history`, `input`, - /// `delegate`, `builder`, `moderator`), or `None` if a name is unknown. - #[must_use] - pub fn from_names<'a>(names: impl IntoIterator) -> Option { - names.into_iter().try_fold(Self(0), |scopes, name| { - let bit = match name { - "join" => Self::JOIN, - "chat" => Self::CHAT, - "history" => Self::HISTORY, - "input" => Self::INPUT, - "delegate" => Self::DELEGATE, - "builder" => Self::BUILDER, - "moderator" => Self::MODERATOR, - _ => return None, - }; - Some(Self(scopes.0 | bit.0)) - }) - } -} +/// Permission scopes granted by a world capability. The definition +/// lives in `id-caps` ([`id_caps::world_scopes::WorldScopes`]) so +/// `id-directory` can name the vocabulary without depending on this +/// crate (crate split step 6a, #4409); re-exported here at its +/// historical path. +pub use id_caps::world_scopes::WorldScopes; /// Bearer secret that grants access to one world. Never serialize it into a snapshot. #[derive(Clone, PartialEq, Eq)] diff --git a/pkgs/id/crates/id-worlds/src/world/tests.rs b/pkgs/id/crates/id-worlds/src/world/tests.rs index 947a1ad958..4c5e42d7d3 100644 --- a/pkgs/id/crates/id-worlds/src/world/tests.rs +++ b/pkgs/id/crates/id-worlds/src/world/tests.rs @@ -3220,10 +3220,7 @@ mod tests { let core = WorldCore::new("caps", WorldLimits::default()).unwrap(); let world = WorldHandle::spawn_with_program(core, Box::new(Mock::new())); let (_, token) = world - .issue( - "ann", - WorldScopes(WorldScopes::JOIN.bits() | WorldScopes::INPUT.bits()), - ) + .issue("ann", WorldScopes::JOIN.union(WorldScopes::INPUT)) .await .unwrap(); world @@ -3251,15 +3248,7 @@ mod tests { async fn a_participant_with_chat_lets_the_program_say_what_it_asks() { let core = WorldCore::new("caps", WorldLimits::default()).unwrap(); let world = WorldHandle::spawn_with_program(core, Box::new(Mock::new())); - let (_, token) = world - .issue( - "ann", - WorldScopes( - WorldScopes::JOIN.bits() | WorldScopes::CHAT.bits() | WorldScopes::INPUT.bits(), - ), - ) - .await - .unwrap(); + let (_, token) = world.issue("ann", WorldScopes::GUEST).await.unwrap(); world .update_caps(vec!["chat.say".to_owned()], Vec::new()) .await @@ -3321,7 +3310,7 @@ mod tests { display_name: "ann".to_owned(), }, digest, - scopes: WorldScopes::GUEST.0, + scopes: WorldScopes::GUEST.bits(), parent: None, expires_at: None, uses: None, @@ -3584,7 +3573,8 @@ mod tests { used: 0, subject: Some(subject.to_owned()), }; - let delegated_scopes = WorldScopes::JOIN.0 | WorldScopes::INPUT.0 | WorldScopes::CHAT.0; + let delegated_scopes = + WorldScopes::JOIN.bits() | WorldScopes::INPUT.bits() | WorldScopes::CHAT.bits(); let entries = vec![ issued_with(1, WorldScopes::ALL.bits(), None, "subject-a"), issued_with(2, delegated_scopes, Some(1), "subject-b"), @@ -3642,7 +3632,7 @@ mod tests { #[test] fn test_restore_refuses_issued_scopes_without_join() { - for scopes in [0, WorldScopes::CHAT.0, WorldScopes::INPUT.0] { + for scopes in [0, WorldScopes::CHAT.bits(), WorldScopes::INPUT.bits()] { let error = restore_with_scopes(scopes).unwrap_err(); assert!( error.to_string().contains("join scope"), @@ -3654,9 +3644,9 @@ mod tests { #[test] fn test_restore_accepts_guest_and_admin_bit_scopes() { for scopes in [ - WorldScopes::GUEST.0, - WorldScopes::JOIN.0 | WorldScopes::BUILDER.0, - WorldScopes::JOIN.0 | WorldScopes::MODERATOR.0, + WorldScopes::GUEST.bits(), + WorldScopes::JOIN.bits() | WorldScopes::BUILDER.bits(), + WorldScopes::JOIN.bits() | WorldScopes::MODERATOR.bits(), ] { let restored = restore_with_scopes(scopes).unwrap(); let record = restored.core.capabilities.get(&1).unwrap(); @@ -3765,7 +3755,7 @@ mod tests { display_name: "ann".to_owned(), }, digest, - scopes: WorldScopes::GUEST.0, + scopes: WorldScopes::GUEST.bits(), parent: None, expires_at: None, uses: None, @@ -4604,9 +4594,7 @@ mod tests { } fn records(&mut self) -> Result> { - Ok(Some( - serde_json::json!({"events": self.events}).to_string(), - )) + Ok(Some(serde_json::json!({"events": self.events}).to_string())) } } @@ -4939,7 +4927,7 @@ mod tests { .unwrap(); core.ledger .set_granted(BTreeSet::from(["time.tick".to_owned()])); - let owner_scopes = WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()); + let owner_scopes = WorldScopes::GUEST.union(WorldScopes::DELEGATE); let (_, owner) = core .issue_capability("ann", owner_scopes, CapabilityBounds::default()) .unwrap(); @@ -5022,7 +5010,7 @@ mod tests { let (spectator, _) = core .issue_capability( "spectator", - WorldScopes(WorldScopes::JOIN.bits() | WorldScopes::INPUT.bits()), + WorldScopes::JOIN.union(WorldScopes::INPUT), CapabilityBounds::default(), ) .unwrap(); @@ -5568,7 +5556,7 @@ mod tests { let (_, owner) = core .issue_capability( "Ada", - WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()), + WorldScopes::GUEST.union(WorldScopes::DELEGATE), CapabilityBounds { expires_at: Some(in_an_hour()), uses: None, @@ -5612,7 +5600,7 @@ mod tests { let (_, owner) = core .issue_capability( "Ada", - WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()), + WorldScopes::GUEST.union(WorldScopes::DELEGATE), CapabilityBounds { expires_at: None, uses: Some(3), @@ -5641,7 +5629,7 @@ mod tests { #[test] fn a_delegated_grant_carries_no_use_count() { let mut core = world(8); - let delegator = WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()); + let delegator = WorldScopes::GUEST.union(WorldScopes::DELEGATE); let (_, owner) = core .issue_capability("Ada", delegator, CapabilityBounds::default()) .unwrap(); @@ -5792,7 +5780,7 @@ mod tests { let (owner, owner_token) = source .issue_capability( "Ada", - WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()), + WorldScopes::GUEST.union(WorldScopes::DELEGATE), CapabilityBounds { expires_at: Some(in_an_hour()), uses: None, @@ -5816,8 +5804,7 @@ mod tests { JournalEntry::Issued { participant: owner, digest: hex_encode(&token_digest(owner_token.expose())), - scopes: WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()) - .bits(), + scopes: WorldScopes::GUEST.union(WorldScopes::DELEGATE).bits(), parent: None, expires_at: Some(in_an_hour()), uses: None, @@ -5844,7 +5831,7 @@ mod tests { let (owner, owner_token) = source .issue_capability( "Ada", - WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()), + WorldScopes::GUEST.union(WorldScopes::DELEGATE), CapabilityBounds::default(), ) .unwrap(); @@ -6380,7 +6367,7 @@ mod tests { #[test] fn attenuation_only_narrows_and_needs_delegate() { let join = WorldScopes::JOIN; - let delegator = WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()); + let delegator = WorldScopes::GUEST.union(WorldScopes::DELEGATE); assert_eq!(delegator.attenuate(join), Some(join)); assert!(delegator.attenuate(WorldScopes::GUEST).is_some()); assert!( @@ -6391,7 +6378,7 @@ mod tests { delegator.attenuate(WorldScopes::CHAT).is_none(), "must keep join" ); - let join_only = WorldScopes(WorldScopes::JOIN.bits() | WorldScopes::DELEGATE.bits()); + let join_only = WorldScopes::JOIN.union(WorldScopes::DELEGATE); assert!( join_only.attenuate(WorldScopes::GUEST).is_none(), "widening" @@ -6402,12 +6389,12 @@ mod tests { fn scope_names_and_effects_follow_the_scopes() { assert_eq!( WorldScopes::from_names(["join", "delegate"]), - Some(WorldScopes(0b1001)) + WorldScopes::from_bits(0b1001) ); assert_eq!(WorldScopes::from_names(["join", "telepathy"]), None); assert!(WorldScopes::GUEST.allows_effect("chat.say")); assert!(WorldScopes::GUEST.allows_effect("chat.history")); - let spectator = WorldScopes(WorldScopes::JOIN.bits() | WorldScopes::INPUT.bits()); + let spectator = WorldScopes::JOIN.union(WorldScopes::INPUT); assert!(!spectator.allows_effect("chat.say")); assert!(!spectator.allows_effect("chat.history")); assert!(spectator.allows_effect("time.now")); @@ -6416,14 +6403,14 @@ mod tests { #[test] fn delegated_capabilities_are_narrow_and_die_with_their_parent() { let mut core = world(8); - let root = WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()); + let root = WorldScopes::GUEST.union(WorldScopes::DELEGATE); let (owner, owner_token) = core .issue_capability("Ada", root, CapabilityBounds::default()) .unwrap(); let (child, child_token) = core .attenuate( &owner_token, - WorldScopes(WorldScopes::JOIN.bits() | WorldScopes::DELEGATE.bits()), + WorldScopes::JOIN.union(WorldScopes::DELEGATE), CapabilityBounds::default(), "Bo", ) @@ -6470,7 +6457,7 @@ mod tests { ..WorldLimits::default() }; let mut core = WorldCore::new("limit", limits).unwrap(); - let root = WorldScopes(WorldScopes::GUEST.bits() | WorldScopes::DELEGATE.bits()); + let root = WorldScopes::GUEST.union(WorldScopes::DELEGATE); let (_, owner_token) = core .issue_capability("Ada", root, CapabilityBounds::default()) .unwrap(); diff --git a/pkgs/id/crates/id-worlds/src/world/util.rs b/pkgs/id/crates/id-worlds/src/world/util.rs index 36c85950b6..4bd1e476b3 100644 --- a/pkgs/id/crates/id-worlds/src/world/util.rs +++ b/pkgs/id/crates/id-worlds/src/world/util.rs @@ -28,16 +28,10 @@ pub(crate) fn constant_time_eq(left: &[u8; 32], right: &[u8; 32]) -> bool { bool::from(left.ct_eq(right)) } -/// Lower-case hex encoding of a byte slice, two characters per byte. -pub fn hex_encode(bytes: &[u8]) -> String { - const HEX: &[u8; 16] = b"0123456789abcdef"; - let mut encoded = String::with_capacity(bytes.len() * 2); - for byte in bytes { - encoded.push(char::from(HEX[usize::from(byte >> 4)])); - encoded.push(char::from(HEX[usize::from(byte & 0x0f)])); - } - encoded -} +/// Lower-case hex encoding of a byte slice; the definition lives +/// in `id-core` ([`id_core::hex::hex_encode`], crate split step 6a, +/// #4409) and is re-exported at its historical path. +pub use id_core::hex::hex_encode; /// Publish a new records snapshot through the watch channel when it differs. pub fn publish_records(