stratum-node-core 0.1.0-alpha.1

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
//! Zero-I/O core types for Stratum pod nodes.
//!
//! **Pre-release:** this is `0.1.0-alpha`. The API and formats may change
//! before 0.1.0.
//!
//! A Stratum node is administered by a Nostr key. This crate holds the parts
//! of that model that need no I/O, so the controller, the command-line client
//! and any other implementation agree byte for byte:
//!
//! - **Principals.** [`DidNostr`] is a `did:nostr` identifier over a BIP-340
//!   x-only public key, convertible to and from hex and NIP-19 `npub`.
//! - **Identifiers.** [`EventId`], [`Sha256Digest`], [`ChallengeId`],
//!   [`OperationId`], [`Revision`] and [`UnixTime`] each have one canonical
//!   text form and validate on construction and deserialisation.
//! - **Events.** [`SignedEvent`] is a NIP-01 event whose ID and signature
//!   have been verified; [`UnsignedEvent`] is one waiting to be signed by a
//!   [`NostrSecretKey`].
//! - **Signed requests.** [`verify_request`] checks an `Authorization: Nostr`
//!   header against the request the server received, under a profile
//!   stricter than NIP-98, and returns a [`VerifiedRequest`].
//!   [`NostrSecretKey::sign_request`] produces such headers.
//! - **Bootstrap claims.** [`ClaimBody`] is the immutable body an
//!   administrator signs to claim a node, kept as the exact bytes issued.
//!
//! The crate never reads a clock, a random source, the environment, the
//! filesystem or the network. The current time, auxiliary signing
//! randomness, challenge bytes and the expected request are all parameters,
//! which keeps every check deterministic and testable.
//!
//! Cryptography comes from maintained crates: BIP-340 Schnorr from `k256`,
//! SHA-256 from `sha2`, bech32 from `bech32`. The signing and verification
//! paths are tested against the published BIP-340 vectors.
//!
//! # Replay protection
//!
//! [`verify_request`] proves who signed a request, for which URL, method and
//! body, and roughly when. It does not make a request single use. A server
//! must record [`VerifiedRequest::event_id`] durably and refuse a repeat.
//!
//! # Example
//!
//! ```
//! use stratum_node_core::{verify_request, ExpectedRequest, NostrSecretKey, RequestPolicy, UnixTime};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Client side: sign a request. Use real randomness for the 32 bytes.
//! let key = NostrSecretKey::from_nsec(
//!     "nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5",
//! )?;
//! let body = br#"{"node":"did:nostr:...","operation_id":"op-1"}"#;
//! let request = ExpectedRequest::new("POST", "http://127.0.0.1:7070/api/v1/config/apply", Some(body));
//! let now = UnixTime::from_secs(1_764_720_000);
//! let header = key.sign_request(&request, now, &[0x42; 32])?;
//!
//! // Server side: verify it against what actually arrived.
//! let verified = verify_request(&header, &request, now, &RequestPolicy::new())?;
//! assert_eq!(verified.signer(), key.public());
//! # Ok(())
//! # }
//! ```

mod claim;
mod did;
mod encoding;
mod event;
mod id;
mod request;
mod schnorr;
mod secret;

pub use claim::{CheckClaimError, ClaimBody, ClaimFields, ParseClaimError, CLAIM_BODY_VERSION};
pub use did::{DidNostr, ParseDidError};
pub use event::{ParseEventError, SignedEvent, UnsignedEvent};
pub use id::{
    ChallengeId, EventId, OperationId, ParseIdError, Revision, Sha256Digest, UnixTime,
    OPERATION_ID_MAX_LEN,
};
pub use request::{
    verify_request, ExpectedRequest, RequestPolicy, RequestPolicyError, VerifiedRequest,
    VerifyRequestError, HTTP_AUTH_KIND,
};
pub use secret::{NostrSecretKey, ParseKeyError, SignError};

/// The version of the signed-request profile [`verify_request`] enforces and
/// [`NostrSecretKey::sign_request`] produces.
pub const REQUEST_PROFILE_VERSION: u32 = 1;

/// Compiles and runs the README example as a doctest.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;