Skip to main content

qualia_core_db/poet_host/scoped_effects/
host.rs

1use super::super::PoetSnapshot;
2use vibe::{DiagCode, Diagnostic, Host, Span, Value};
3
4/// Borrow an existing native state owner and explicit host-held grants for one
5/// dispatch scope. Construct only from native admission, never `requires` text.
6/// This small cold authoring profile does not advertise graph, generic invokes,
7/// private-key export or unrestricted host delegation. Other Host defaults are
8/// unchanged and are not advertised by this profile. The native owner handles authentication,
9/// current revocation and keeping this borrow within its admission transaction.
10pub struct ScopedEffects<'a> {
11    snapshot: &'a mut PoetSnapshot,
12    grants: &'a [&'a str],
13}
14
15impl<'a> ScopedEffects<'a> {
16    pub fn new(snapshot: &'a mut PoetSnapshot, grants: &'a [&'a str]) -> Result<Self, Diagnostic> {
17        const SUPPORTED: &[&str] = &[
18            "pulse.publish",
19            "hid.poll",
20            "crypto.generate_key",
21            "crypto.sign",
22        ];
23        if grants.len() > 32
24            || grants
25                .iter()
26                .enumerate()
27                .any(|(index, grant)| !SUPPORTED.contains(grant) || grants[..index].contains(grant))
28        {
29            return Err(denied(Span::point(0)));
30        }
31        Ok(Self { snapshot, grants })
32    }
33    fn require(&self, capability: &str, span: Span) -> Result<(), Diagnostic> {
34        if self.grants.contains(&capability) {
35            Ok(())
36        } else {
37            Err(denied(span))
38        }
39    }
40}
41
42fn denied(span: Span) -> Diagnostic {
43    Diagnostic::new(
44        DiagCode::E300,
45        span,
46        "operation is outside the current native effect grants",
47    )
48}
49
50impl Host for ScopedEffects<'_> {
51    fn graph_query(&mut self, _: &[Value], _: u64, span: Span) -> Result<Value, Diagnostic> {
52        Err(denied(span))
53    }
54    fn graph_stage(&mut self, _: &Value, span: Span) -> Result<Value, Diagnostic> {
55        Err(denied(span))
56    }
57    fn graph_commit(&mut self, span: Span) -> Result<Value, Diagnostic> {
58        Err(denied(span))
59    }
60    fn aura_validate(&mut self, _: &Value, _: &Value, span: Span) -> Result<Value, Diagnostic> {
61        Err(denied(span))
62    }
63    fn pulse_publish(
64        &mut self,
65        topic: &str,
66        payload: &Value,
67        span: Span,
68    ) -> Result<Value, Diagnostic> {
69        self.require("pulse.publish", span)?;
70        self.snapshot.pulse_publish(topic, payload, span)
71    }
72    fn hid_poll(&mut self, span: Span) -> Result<Value, Diagnostic> {
73        self.require("hid.poll", span)?;
74        self.snapshot.hid_poll(span)
75    }
76    fn crypto_generate_key(&mut self, algorithm: &str, span: Span) -> Result<Value, Diagnostic> {
77        self.require("crypto.generate_key", span)?;
78        self.snapshot.crypto_generate_key(algorithm, span)
79    }
80    fn crypto_sign(&mut self, key_id: &str, data: &str, span: Span) -> Result<Value, Diagnostic> {
81        self.require("crypto.sign", span)?;
82        self.snapshot.crypto_sign(key_id, data, span)
83    }
84    // Verification retains the pinned 0.1 Pure classification. It returns only
85    // validity and never exports a native key seed or grants signing authority.
86    fn crypto_verify(
87        &mut self,
88        key_id: &str,
89        data: &str,
90        signature: &str,
91        span: Span,
92    ) -> Result<Value, Diagnostic> {
93        self.snapshot.crypto_verify(key_id, data, signature, span)
94    }
95}