stratum-node-core 0.1.0-alpha.4

Zero-I/O core types for Stratum pod nodes: did:nostr principals, typed identifiers, a strict NIP-98 signed-request verifier and bootstrap claim bodies
Documentation
//! The gate directory's `app_keys` entry.
//!
//! The controller's `gate.sock` directory answer carries
//! `"app_keys":[{"pk":hex,"owner":hex}]`: the enabled application keys and
//! their `active` owners. Absent means empty, and every change to the list
//! is a new directory revision. Keys are written as 64 lower-case hex
//! digits, not as `did:nostr:` text. The gate keeps its own tolerant parser;
//! this is the published shape.

use serde::{Deserialize, Serialize};

use crate::did::DidNostr;

/// One listed application key: an enabled key and its active owner.
///
/// # Examples
///
/// ```
/// use stratum_node_core::{DidNostr, DirectoryAppKey};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pk = DidNostr::from_hex(&"c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5")?;
/// let owner = DidNostr::from_hex(&"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")?;
/// let entry = DirectoryAppKey { pk, owner };
/// let text = serde_json::to_string(&entry)?;
/// assert_eq!(text, format!(r#"{{"pk":"{}","owner":"{}"}}"#, pk.to_hex(), owner.to_hex()));
/// assert_eq!(serde_json::from_str::<DirectoryAppKey>(&text)?, entry);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct DirectoryAppKey {
    /// The application key.
    #[serde(with = "hex_pk")]
    pub pk: DidNostr,
    /// The member who owns the installation the key belongs to.
    #[serde(with = "hex_pk")]
    pub owner: DidNostr,
}

/// Serde for a key as 64 lower-case hex digits, for the wire members the
/// contract types `hex`.
pub(crate) mod hex_pk {
    use serde::{Deserialize, Deserializer, Serializer};

    use crate::did::DidNostr;

    pub(crate) fn serialize<S: Serializer>(pk: &DidNostr, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&pk.to_hex())
    }

    pub(crate) fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<DidNostr, D::Error> {
        let text = String::deserialize(d)?;
        DidNostr::from_hex(&text).map_err(serde::de::Error::custom)
    }
}