Skip to main content

confium_daemon/methods/
hash.rs

1//! Hash methods: `hash_create`, `hash_update`, `hash_finalize`.
2//!
3//! Hash objects are owned by the daemon and referenced by a client-
4//! supplied handle id (a string). The handle table lives in the
5//! [`HashStore`] passed to each handler.
6//!
7//! For the skeleton, handlers exercise the core `Hash` API under a
8//! per-connection lock. The underlying `confium_core::hash::Hash`
9//! requires a loaded provider, so these methods return an `Engine`
10//! error when no provider offers the requested algorithm.
11
12use serde::Deserialize;
13use serde_json::{Value, json};
14
15use crate::error::RpcError;
16use crate::server::SharedConfium;
17
18/// `hash_create({ "algorithm": "sha-256", "provider": null })`
19/// → `{"handle": "<opaque>"}`
20///
21/// Delegates to [`Hash::new`]. The client picks the algorithm name;
22/// the provider is optional (first provider that supports the
23/// algorithm wins).
24#[derive(Deserialize)]
25struct HashCreateParams {
26    algorithm: String,
27    #[serde(default)]
28    provider: Option<String>,
29}
30
31pub async fn hash_create(
32    cfm: SharedConfium,
33    _params: Value,
34) -> std::result::Result<Value, RpcError> {
35    let p: HashCreateParams =
36        serde_json::from_value(_params).map_err(|e| RpcError::InvalidParams {
37            detail: e.to_string(),
38        })?;
39
40    let cfm = cfm.borrow();
41    // The core Hash::new borrows &Confium, so we stay in the RefCell
42    // borrow for the duration. For a skeleton this is acceptable; a
43    // production daemon would clone the Rc and release the borrow.
44    let _hash = confium_core::hash::Hash::new(&cfm, &p.algorithm, p.provider.as_deref(), None)
45        .map_err(|e| RpcError::Engine {
46            message: e.to_string(),
47        })?;
48    // We have no per-connection handle store in the skeleton; return a
49    // placeholder handle. When handle management is wired (per
50    // connection state), this returns the id under which the hash is
51    // stored.
52    Ok(json!({ "handle": "<pending>" }))
53}
54
55/// `hash_update({ "handle": "...", "data": "<base64>" })` → `{"ok": true}`
56///
57/// The skeleton does not yet track handles; this handler returns an
58/// `Engine` error indicating handle management is pending.
59pub async fn hash_update(
60    _cfm: SharedConfium,
61    _params: Value,
62) -> std::result::Result<Value, RpcError> {
63    Err(RpcError::Engine {
64        message: "hash_update requires per-connection handle management (pending)".to_string(),
65    })
66}
67
68/// `hash_finalize({ "handle": "..." })` → `{"digest": "<base64>"}`
69///
70/// Same caveat as `hash_update`: handle management is pending.
71pub async fn hash_finalize(
72    _cfm: SharedConfium,
73    _params: Value,
74) -> std::result::Result<Value, RpcError> {
75    Err(RpcError::Engine {
76        message: "hash_finalize requires per-connection handle management (pending)".to_string(),
77    })
78}