stratum_node_core/directory.rs
1//! The gate directory's `app_keys` entry.
2//!
3//! The controller's `gate.sock` directory answer carries
4//! `"app_keys":[{"pk":hex,"owner":hex}]`: the enabled application keys and
5//! their `active` owners. Absent means empty, and every change to the list
6//! is a new directory revision. Keys are written as 64 lower-case hex
7//! digits, not as `did:nostr:` text. The gate keeps its own tolerant parser;
8//! this is the published shape.
9
10use serde::{Deserialize, Serialize};
11
12use crate::did::DidNostr;
13
14/// One listed application key: an enabled key and its active owner.
15///
16/// # Examples
17///
18/// ```
19/// use stratum_node_core::{DidNostr, DirectoryAppKey};
20///
21/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
22/// let pk = DidNostr::from_hex(&"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5")?;
23/// let owner = DidNostr::from_hex(&"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")?;
24/// let entry = DirectoryAppKey { pk, owner };
25/// let text = serde_json::to_string(&entry)?;
26/// assert_eq!(text, format!(r#"{{"pk":"{}","owner":"{}"}}"#, pk.to_hex(), owner.to_hex()));
27/// assert_eq!(serde_json::from_str::<DirectoryAppKey>(&text)?, entry);
28/// # Ok(())
29/// # }
30/// ```
31#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
32pub struct DirectoryAppKey {
33 /// The application key.
34 #[serde(with = "hex_pk")]
35 pub pk: DidNostr,
36 /// The member who owns the installation the key belongs to.
37 #[serde(with = "hex_pk")]
38 pub owner: DidNostr,
39}
40
41/// Serde for a key as 64 lower-case hex digits, for the wire members the
42/// contract types `hex`.
43pub(crate) mod hex_pk {
44 use serde::{Deserialize, Deserializer, Serializer};
45
46 use crate::did::DidNostr;
47
48 pub(crate) fn serialize<S: Serializer>(pk: &DidNostr, s: S) -> Result<S::Ok, S::Error> {
49 s.serialize_str(&pk.to_hex())
50 }
51
52 pub(crate) fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<DidNostr, D::Error> {
53 let text = String::deserialize(d)?;
54 DidNostr::from_hex(&text).map_err(serde::de::Error::custom)
55 }
56}