Skip to main content

gent_crypto/authority/
agents.rs

1use super::*;
2use uuid::Uuid;
3
4const LEGACY_DOMAIN: &[u8] = b"gent/legacy-agent-binding/v1\0";
5const MAX_LEGACY_BINDING_BYTES: usize = MAX_OWNER_CHAIN_BYTES + 4096;
6
7/// Public, node-bound IDs prevent an authenticated node from claiming another
8/// node's chat UUID before the receiver has seen its directory announcement.
9pub fn agent_id(node: &str, nonce: Uuid) -> Result<String> {
10    id(node)?;
11    ensure!(!nonce.is_nil(), "nil chat nonce");
12    Ok(scoped_id(
13        b"gent/agent-id/v1\0",
14        &[Uuid::parse_str(node)?.as_bytes(), nonce.as_bytes()],
15    )
16    .to_string())
17}
18
19/// Domain-separated UUIDv8 identifier from length-prefixed public inputs.
20pub fn scoped_id(domain: &[u8], parts: &[&[u8]]) -> Uuid {
21    let mut hash = Sha256::new();
22    hash.update((domain.len() as u64).to_be_bytes());
23    hash.update(domain);
24    for part in parts {
25        hash.update((part.len() as u64).to_be_bytes());
26        hash.update(part);
27    }
28    let mut bytes: [u8; 16] = hash.finalize()[..16].try_into().expect("SHA256 prefix");
29    bytes[6] = (bytes[6] & 0x0f) | 0x80;
30    bytes[8] = (bytes[8] & 0x3f) | 0x80;
31    Uuid::from_bytes(bytes)
32}
33
34#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
35#[serde(deny_unknown_fields)]
36pub struct LegacyAgentBinding {
37    pub anchor: TrustAnchor,
38    #[serde(default, skip_serializing_if = "Vec::is_empty")]
39    pub owner_transitions: Vec<SignedOwnerTransition>,
40    pub agent: String,
41    pub node: String,
42}
43#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
44#[serde(deny_unknown_fields)]
45pub struct SignedLegacyAgentBinding {
46    pub binding: LegacyAgentBinding,
47    pub signature: Vec<u8>,
48}
49impl SignedLegacyAgentBinding {
50    /// Verify historical provenance only. Use verify_current for a live registration.
51    pub fn verify(&self, anchor: &TrustAnchor, agent: &str, node: &str) -> Result<()> {
52        id(agent)?;
53        id(node)?;
54        ensure!(
55            &self.binding.anchor == anchor
56                && self.binding.agent == agent
57                && self.binding.node == node,
58            "legacy chat binding mismatch"
59        );
60        let owner = verify_lineage(anchor, &self.binding.owner_transitions)?;
61        verify(
62            &owner,
63            LEGACY_DOMAIN,
64            &self.binding,
65            &self.signature,
66            MAX_LEGACY_BINDING_BYTES,
67        )
68    }
69
70    /// Historical signatures alone cannot authorize a live registration.
71    pub fn verify_current(&self, state: &VerifiedState, agent: &str, node: &str) -> Result<()> {
72        self.verify(state.anchor(), agent, node)?;
73        ensure!(
74            self.binding.owner_transitions == state.state().owner_transitions,
75            "legacy binding is from another owner epoch"
76        );
77        ensure!(
78            state.state().nodes.contains_key(node),
79            "legacy binding node is unavailable"
80        );
81        Ok(())
82    }
83}
84impl OwnerKey {
85    /// Trusted owner migration approval. The runtime must retain unique bindings
86    /// and reject a chat UUID already assigned to a different node.
87    pub fn bind_legacy_agent(
88        &self,
89        state: &VerifiedState,
90        agent: &str,
91        node: &str,
92        now: u64,
93    ) -> Result<SignedLegacyAgentBinding> {
94        id(agent)?;
95        id(node)?;
96        state.require_fresh(now)?;
97        ensure!(
98            self.public_key() == state.state().owner_key && state.state().nodes.contains_key(node),
99            "legacy chat binding requires owner authority and enrolled node"
100        );
101        let binding = LegacyAgentBinding {
102            anchor: state.anchor().clone(),
103            owner_transitions: state.state().owner_transitions.clone(),
104            agent: agent.into(),
105            node: node.into(),
106        };
107        let signature = self
108            .0
109            .sign(&signing_input(
110                LEGACY_DOMAIN,
111                &binding,
112                MAX_LEGACY_BINDING_BYTES,
113            )?)
114            .to_bytes()
115            .to_vec();
116        Ok(SignedLegacyAgentBinding { binding, signature })
117    }
118}