stratum_node_core/lib.rs
1//! Zero-I/O core types for Stratum pod nodes.
2//!
3//! **Pre-release:** this is `0.1.0-alpha`. The API and formats may change
4//! before 0.1.0.
5//!
6//! A Stratum node is administered by a Nostr key. This crate holds the parts
7//! of that model that need no I/O, so the controller, the command-line client
8//! and any other implementation agree byte for byte:
9//!
10//! - **Principals.** [`DidNostr`] is a `did:nostr` identifier over a BIP-340
11//! x-only public key, convertible to and from hex and NIP-19 `npub`.
12//! - **Identifiers.** [`EventId`], [`Sha256Digest`], [`ChallengeId`],
13//! [`InvitationId`], [`OperationId`], [`Revision`] and [`UnixTime`] each
14//! have one canonical
15//! text form and validate on construction and deserialisation.
16//! - **Events.** [`SignedEvent`] is a NIP-01 event whose ID and signature
17//! have been verified; [`UnsignedEvent`] is one waiting to be signed by a
18//! [`NostrSecretKey`].
19//! - **Signed requests.** [`verify_request`] checks an `Authorization: Nostr`
20//! header against the request the server received, under a profile
21//! stricter than NIP-98, and returns a [`VerifiedRequest`].
22//! [`NostrSecretKey::sign_request`] produces such headers, and
23//! [`NostrSecretKey::sign_request_with_nonce`] adds a random `nonce` tag
24//! so identical requests signed in one second are distinct events.
25//! [`verify_request_without_body`] checks everything but the body digest,
26//! for a proxy that never sees the body, and returns a
27//! [`BodyUncheckedRequest`]. [`UnverifiedRequest`] parses a header once so
28//! its claimed author can be looked up before a signature is verified.
29//! - **Bootstrap claims.** [`ClaimBody`] is the immutable body an
30//! administrator signs to claim a node, kept as the exact bytes issued.
31//! - **Pod paths.** [`PodPath`] is a request path under the pod allow-list
32//! (`/pods/<pk>/…` or `/pods/<pk>.acl`) and its shape rules, with the
33//! owner, container and sibling-ACL helpers a client needs;
34//! [`is_valid_pod_query`] checks a query string.
35//! - **Admission.** [`AdmissionClaim`] is the body a tenant signs to redeem
36//! an invitation ([`InvitationId`]) after reading
37//! [`ADMISSION_NOTICE_V1`]; [`MemberState`] names the states a membership
38//! moves through.
39//!
40//! The crate never reads a clock, a random source, the environment, the
41//! filesystem or the network. The current time, auxiliary signing
42//! randomness, challenge bytes and the expected request are all parameters,
43//! which keeps every check deterministic and testable.
44//!
45//! Cryptography comes from maintained crates: BIP-340 Schnorr from `k256`,
46//! SHA-256 from `sha2`, bech32 from `bech32`. The signing and verification
47//! paths are tested against the published BIP-340 vectors.
48//!
49//! # Replay protection
50//!
51//! [`verify_request`] proves who signed a request, for which URL, method and
52//! body, and roughly when. It does not make a request single use. A server
53//! must record [`VerifiedRequest::event_id`] durably and refuse a repeat.
54//!
55//! # Example
56//!
57//! ```
58//! use stratum_node_core::{verify_request, ExpectedRequest, NostrSecretKey, RequestPolicy, UnixTime};
59//!
60//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
61//! // Client side: sign a request. Use real randomness for the 32 bytes.
62//! let key = NostrSecretKey::from_nsec(
63//! "nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5",
64//! )?;
65//! let body = br#"{"node":"did:nostr:...","operation_id":"op-1"}"#;
66//! let request = ExpectedRequest::new("POST", "http://127.0.0.1:7070/api/v1/config/apply", Some(body));
67//! let now = UnixTime::from_secs(1_764_720_000);
68//! let header = key.sign_request(&request, now, &[0x42; 32])?;
69//!
70//! // Server side: verify it against what actually arrived.
71//! let verified = verify_request(&header, &request, now, &RequestPolicy::new())?;
72//! assert_eq!(verified.signer(), key.public());
73//! # Ok(())
74//! # }
75//! ```
76
77mod admission;
78mod claim;
79mod did;
80mod encoding;
81mod event;
82mod id;
83mod pod_path;
84mod request;
85mod schnorr;
86mod secret;
87
88pub use admission::{
89 admission_notice_v1_sha256, AdmissionClaim, AdmissionFields, CheckAdmissionError, MemberState,
90 ParseAdmissionError, ParseMemberStateError, ADMISSION_CLAIM_PURPOSE, ADMISSION_NOTICE_V1,
91};
92pub use claim::{CheckClaimError, ClaimBody, ClaimFields, ParseClaimError, CLAIM_BODY_VERSION};
93pub use did::{DidNostr, ParseDidError};
94pub use event::{ParseEventError, SignedEvent, UnsignedEvent};
95pub use id::{
96 ChallengeId, EventId, InvitationId, OperationId, ParseIdError, Revision, Sha256Digest,
97 UnixTime, OPERATION_ID_MAX_LEN,
98};
99pub use pod_path::{is_valid_pod_query, ParsePodPathError, PodPath};
100pub use request::{
101 verify_request, verify_request_without_body, BodyUncheckedRequest, ExpectedRequest,
102 RequestPolicy, RequestPolicyError, RequestTarget, UnverifiedRequest, VerifiedRequest,
103 VerifyRequestError, HTTP_AUTH_KIND,
104};
105pub use secret::{NostrSecretKey, ParseKeyError, SignError};
106
107/// The version of the signed-request profile [`verify_request`] enforces and
108/// [`NostrSecretKey::sign_request`] and
109/// [`NostrSecretKey::sign_request_with_nonce`] produce. The `nonce` tag is
110/// outside the profile, which ignores tags it does not name.
111pub const REQUEST_PROFILE_VERSION: u32 = 1;
112
113/// Compiles and runs the README example as a doctest.
114#[cfg(doctest)]
115#[doc = include_str!("../README.md")]
116struct ReadmeDoctests;