Skip to main content

qualia_core_db/did_qi/
mailbox_bind.rs

1//! Publish Qualia Identifier `CscpMailbox` services into a CSCP private mailbox.
2//!
3//! Invitation-scoped only. Deactivated documents publish nothing. Direct
4//! locators are not stored when the service disclosure forbids probing.
5//! HostnameAlias is not a CSCP locator and is ignored here.
6
7use crate::net::peer::connectivity::policy::Disclosure as FabricDisclosure;
8use crate::net::peer::fabric::contact::{ContactDescriptor, LocatorKind};
9use crate::net::peer::fabric::kernel::FabricError;
10use crate::net::peer::fabric::mailbox::PrivateMailbox;
11
12use super::document::QiDocument;
13use super::service::{CscpMailbox, Disclosure as QiDisclosure, LocatorClass};
14
15fn fabric_disclosure(d: QiDisclosure) -> FabricDisclosure {
16    match d {
17        QiDisclosure::DirectPermitted => FabricDisclosure::DirectPermitted,
18        QiDisclosure::ApprovedRelaysOnly => FabricDisclosure::ApprovedRelaysOnly,
19        QiDisclosure::QualifiedMultiHop => FabricDisclosure::QualifiedMultiHop,
20        QiDisclosure::Isolated => FabricDisclosure::Isolated,
21    }
22}
23
24fn locator_kind(kind: LocatorClass) -> LocatorKind {
25    match kind {
26        LocatorClass::Mailbox => LocatorKind::Mailbox,
27        LocatorClass::RelayHint => LocatorKind::RelayHint,
28        LocatorClass::Direct => LocatorKind::Direct,
29    }
30}
31
32fn descriptor_from_service(s: &CscpMailbox, expiry_unix: u32) -> ContactDescriptor {
33    let mut locator = [0u8; 16];
34    if s.kind == LocatorClass::RelayHint && s.hint_count > 0 {
35        locator.copy_from_slice(&s.hints[0].operator_hash[..16]);
36    }
37    ContactDescriptor {
38        contact_key: s.contact_key,
39        generation: s.generation,
40        expiry_unix,
41        network_generation: 1,
42        kind: locator_kind(s.kind),
43        locator,
44    }
45}
46
47/// Publish each `CscpMailbox` service from `doc` into `mailbox`.
48///
49/// Returns the number of descriptors stored. `qi.deactivated` yields `0`.
50pub fn publish_qi_document(
51    mailbox: &mut PrivateMailbox,
52    doc: &QiDocument,
53    now_unix: u32,
54    expiry_unix: u32,
55) -> Result<usize, FabricError> {
56    if doc.deactivated {
57        return Ok(0);
58    }
59    if expiry_unix <= now_unix {
60        return Err(FabricError::Expired);
61    }
62    let mut n = 0usize;
63    let mut i = 0usize;
64    while i < doc.service_count as usize {
65        let s = &doc.services[i];
66        let disclosure = fabric_disclosure(s.disclosure);
67        if s.public_dht && disclosure != FabricDisclosure::DirectPermitted {
68            return Err(FabricError::PolicyDenied);
69        }
70        let d = descriptor_from_service(s, expiry_unix);
71        mailbox.publish_under(d, disclosure)?;
72        n += 1;
73        i += 1;
74    }
75    Ok(n)
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use crate::did_qi::git_object::GitObjectStore;
82    use crate::did_qi::method::{create, deactivate};
83    use crate::did_qi::service::{CscpMailbox, Disclosure, RelayHint};
84    use crate::did_qi::sha256_32;
85    use crate::did_qi::QiDocument;
86
87    fn sk() -> [u8; 32] {
88        [3u8; 32]
89    }
90
91    #[test]
92    fn live_mailbox_publishes_contact_key() {
93        let mut store = GitObjectStore::new();
94        let mut doc = QiDocument::empty();
95        let key = sha256_32(b"did:qi:bind:contact");
96        doc.services[0] = CscpMailbox::mailbox(key, Disclosure::ApprovedRelaysOnly);
97        doc.services[0].hints[0] =
98            RelayHint::new(b"invite-1", sha256_32(b"did:qi:bind:operator")).unwrap();
99        doc.services[0].hint_count = 1;
100        doc.service_count = 1;
101        create(&mut store, &sk(), &doc).unwrap();
102        let mut mailbox = PrivateMailbox::new();
103        let n = publish_qi_document(&mut mailbox, &doc, 10, 2_000_000_000).unwrap();
104        assert_eq!(n, 1);
105        let got = mailbox
106            .resolve(&key, 10, FabricDisclosure::ApprovedRelaysOnly)
107            .unwrap();
108        assert_eq!(got.contact_key, key);
109        assert_eq!(got.kind, LocatorKind::Mailbox);
110        assert_eq!(got.locator, [0u8; 16]);
111    }
112
113    #[test]
114    fn deactivated_document_publishes_nothing() {
115        let mut store = GitObjectStore::new();
116        let mut doc = QiDocument::empty();
117        let key = [9u8; 32];
118        doc.services[0] = CscpMailbox::mailbox(key, Disclosure::ApprovedRelaysOnly);
119        doc.service_count = 1;
120        let id = create(&mut store, &sk(), &doc).unwrap();
121        deactivate(&mut store, &sk(), &id).unwrap();
122        let mut tomb = QiDocument::empty();
123        crate::did_qi::read(&store, &id, &mut tomb).unwrap();
124        let mut mailbox = PrivateMailbox::new();
125        assert_eq!(
126            publish_qi_document(&mut mailbox, &tomb, 10, 2_000_000_000).unwrap(),
127            0
128        );
129        assert_eq!(
130            mailbox.resolve(&key, 10, FabricDisclosure::ApprovedRelaysOnly),
131            Err(FabricError::Illegal)
132        );
133    }
134
135    #[test]
136    fn relay_only_direct_service_is_not_stored() {
137        let mut doc = QiDocument::empty();
138        let key = [4u8; 32];
139        doc.services[0] = CscpMailbox::direct(key, Disclosure::ApprovedRelaysOnly);
140        doc.service_count = 1;
141        let mut mailbox = PrivateMailbox::new();
142        assert_eq!(
143            publish_qi_document(&mut mailbox, &doc, 10, 2_000_000_000),
144            Err(FabricError::PolicyDenied)
145        );
146        assert_eq!(
147            mailbox.resolve(&key, 10, FabricDisclosure::DirectPermitted),
148            Err(FabricError::Illegal)
149        );
150    }
151
152    #[test]
153    fn hostname_alias_is_not_a_cscp_locator() {
154        let mut doc = QiDocument::empty();
155        doc.has_hostname_alias = true;
156        doc.hostname_did_web =
157            crate::did_qi::AkaEntry::from_slice(b"did:web:example.invalid").unwrap();
158        let mut mailbox = PrivateMailbox::new();
159        assert_eq!(
160            publish_qi_document(&mut mailbox, &doc, 10, 2_000_000_000).unwrap(),
161            0
162        );
163    }
164}