Skip to main content

qualia_core_db/domain_licence/
registry.rs

1//! Declared-domain registry (WIP §4-A): which domains this node/build declares
2//! licences for, and where each licence lives.
3//!
4//! The effective declared set is the union of:
5//!  - [`COMPILED_IN_LICENSED_DOMAINS`] (baked into the build),
6//!  - signed `DomainLicence` subject lists (checked at verification, not here),
7//!  - this on-disk registry (`licence_registry.json`).
8
9use serde::{Deserialize, Serialize};
10
11use super::verify::normalize_domain;
12
13/// Domains compiled into this build as licensed (WIP §4-A "compiled-in
14/// declaration"). Client-specific builds may patch this list; the default is
15/// empty — declarations should normally travel in the signed instrument.
16pub const COMPILED_IN_LICENSED_DOMAINS: &[&str] = &[];
17
18/// Where a declared domain's licence record comes from.
19#[repr(u8)]
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
21pub enum RegistrySource {
22    /// Signed `.qlic.json` installed on this node.
23    InstalledFile = 1,
24    /// `/.well-known/qualia-licence.json` on the licensed origin.
25    WellKnown = 2,
26    /// `_qualia-licence` DNS TXT carrying the digest.
27    DnsTxt = 3,
28    /// Anchored on a nominated chain (digest discovered by lookup).
29    ChainAnchor = 4,
30}
31
32/// One declared domain → licence mapping.
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34pub struct RegistryEntry {
35    /// Normalised domain name.
36    pub domain: String,
37    /// `did:q42:licence:…` this declaration resolves to.
38    pub licence_id: String,
39    /// Canonical digest (`sha256:<hex>`) when known — used to reject stale files.
40    #[serde(default)]
41    pub digest: String,
42    /// How the licence was declared.
43    pub source: RegistrySource,
44}
45
46/// On-disk registry of declared domains → licences.
47#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
48pub struct LicenceRegistry {
49    #[serde(default)]
50    pub entries: Vec<RegistryEntry>,
51}
52
53impl LicenceRegistry {
54    /// Look up the declaration for a domain (normalised before compare).
55    pub fn lookup(&self, domain: &str) -> Option<&RegistryEntry> {
56        let d = normalize_domain(domain);
57        self.entries.iter().find(|e| e.domain == d)
58    }
59
60    /// Insert or replace the declaration for `entry.domain`.
61    pub fn upsert(&mut self, entry: RegistryEntry) {
62        let d = normalize_domain(&entry.domain);
63        self.entries.retain(|e| e.domain != d);
64        self.entries.push(RegistryEntry { domain: d, ..entry });
65    }
66
67    /// Remove the declaration for a domain. Returns true if one existed.
68    pub fn remove(&mut self, domain: &str) -> bool {
69        let d = normalize_domain(domain);
70        let before = self.entries.len();
71        self.entries.retain(|e| e.domain != d);
72        self.entries.len() != before
73    }
74
75    /// Is `domain` declared — in the registry or compiled into the build?
76    pub fn is_declared(&self, domain: &str) -> bool {
77        let d = normalize_domain(domain);
78        COMPILED_IN_LICENSED_DOMAINS
79            .iter()
80            .any(|c| normalize_domain(c) == d)
81            || self.lookup(&d).is_some()
82    }
83
84    /// All declared domains (registry ∪ compiled-in), normalised, deduped.
85    pub fn declared_domains(&self) -> Vec<String> {
86        let mut out: Vec<String> = self.entries.iter().map(|e| e.domain.clone()).collect();
87        for c in COMPILED_IN_LICENSED_DOMAINS {
88            let n = normalize_domain(c);
89            if !out.contains(&n) {
90                out.push(n);
91            }
92        }
93        out
94    }
95
96    /// Serialise the registry to pretty JSON.
97    pub fn to_json(&self) -> Result<String, serde_json::Error> {
98        serde_json::to_string_pretty(self)
99    }
100
101    /// Deserialise a registry from JSON.
102    pub fn from_json(text: &str) -> Result<Self, serde_json::Error> {
103        serde_json::from_str(text)
104    }
105}