stratum-node-core 0.1.0-alpha.3

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`],
//!   [`InvitationId`], [`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, and
//!   [`NostrSecretKey::sign_request_with_nonce`] adds a random `nonce` tag
//!   so identical requests signed in one second are distinct events.
//!   [`verify_request_without_body`] checks everything but the body digest,
//!   for a proxy that never sees the body, and returns a
//!   [`BodyUncheckedRequest`]. [`UnverifiedRequest`] parses a header once so
//!   its claimed author can be looked up before a signature is verified.
//! - **Bootstrap claims.** [`ClaimBody`] is the immutable body an
//!   administrator signs to claim a node, kept as the exact bytes issued.
//! - **Pod paths.** [`PodPath`] is a request path under the pod allow-list
//!   (`/pods/<pk>/…` or `/pods/<pk>.acl`) and its shape rules, with the
//!   owner, container and sibling-ACL helpers a client needs;
//!   [`is_valid_pod_query`] checks a query string.
//! - **Admission.** [`AdmissionClaim`] is the body a tenant signs to redeem
//!   an invitation ([`InvitationId`]) after reading
//!   [`ADMISSION_NOTICE_V1`]; [`MemberState`] names the states a membership
//!   moves through, with one predicate each for capacity, provisioning and
//!   the gate's directory. [`DirectoryMemberState`] is the gate's tolerant
//!   reading of a directory entry, which keeps an unknown state as a
//!   refused member instead of failing the whole document.
//! - **Member administration.** [`MemberActionRequest`], [`RemoveRequest`],
//!   [`SuspendResponse`], [`ResumeResponse`], [`ArchiveResponse`],
//!   [`RemoveResponse`] and [`MemberView`] are the bodies of the
//!   administrator's suspend, resume, archive, remove and member-view
//!   routes; [`Enforcement`] reports whether the gate applies a suspension,
//!   and [`RemovalArchive`] is an archive reference or a waiver.
//!   [`ErrorCode`] defines the codes `suspended`, `archive_required` and
//!   `pod_data_present`.
//! - **Profile document.** [`profile_card_path`] and [`profile_owner`] give
//!   the one public path per pod and the gate's byte-exact matcher for it;
//!   [`profile_card`] and [`profile_acl`] are the document and ACL a client
//!   writes, and [`card_names_owner`] is `profile init`'s conservative
//!   check of an existing card.
//!
//! 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 admission;
mod claim;
mod did;
mod encoding;
mod error_code;
mod event;
mod id;
mod members;
mod pod_path;
mod profile;
mod request;
mod schnorr;
mod secret;

pub use admission::{
    admission_notice_v1_sha256, AdmissionClaim, AdmissionFields, CheckAdmissionError,
    DirectoryMemberState, MemberState, ParseAdmissionError, ParseMemberStateError,
    ADMISSION_CLAIM_PURPOSE, ADMISSION_NOTICE_V1,
};
pub use claim::{CheckClaimError, ClaimBody, ClaimFields, ParseClaimError, CLAIM_BODY_VERSION};
pub use did::{DidNostr, ParseDidError};
pub use error_code::ErrorCode;
pub use event::{ParseEventError, SignedEvent, UnsignedEvent};
pub use id::{
    ChallengeId, EventId, InvitationId, OperationId, ParseIdError, Revision, Sha256Digest,
    UnixTime, OPERATION_ID_MAX_LEN,
};
pub use members::{
    ArchiveResponse, Enforcement, MemberActionRequest, MemberView, ParseRemovalArchiveError,
    RemovalArchive, RemoveRequest, RemoveResponse, ResumeResponse, SuspendResponse,
};
pub use pod_path::{is_acl_path, is_valid_pod_query, ParsePodPathError, PodPath};
pub use profile::{
    card_names_owner, profile_acl, profile_card, profile_card_path, profile_owner,
    PROFILE_CARD_RELATIVE, PROFILE_CONTENT_TYPE, PROFILE_MAX_BYTES,
};
pub use request::{
    verify_request, verify_request_without_body, BodyUncheckedRequest, ExpectedRequest,
    RequestPolicy, RequestPolicyError, RequestTarget, UnverifiedRequest, 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`] and
/// [`NostrSecretKey::sign_request_with_nonce`] produce. The `nonce` tag is
/// outside the profile, which ignores tags it does not name.
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;