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//! [`OperationId`], [`Revision`] and [`UnixTime`] each have one canonical
14//! text form and validate on construction and deserialisation.
15//! - **Events.** [`SignedEvent`] is a NIP-01 event whose ID and signature
16//! have been verified; [`UnsignedEvent`] is one waiting to be signed by a
17//! [`NostrSecretKey`].
18//! - **Signed requests.** [`verify_request`] checks an `Authorization: Nostr`
19//! header against the request the server received, under a profile
20//! stricter than NIP-98, and returns a [`VerifiedRequest`].
21//! [`NostrSecretKey::sign_request`] produces such headers.
22//! - **Bootstrap claims.** [`ClaimBody`] is the immutable body an
23//! administrator signs to claim a node, kept as the exact bytes issued.
24//!
25//! The crate never reads a clock, a random source, the environment, the
26//! filesystem or the network. The current time, auxiliary signing
27//! randomness, challenge bytes and the expected request are all parameters,
28//! which keeps every check deterministic and testable.
29//!
30//! Cryptography comes from maintained crates: BIP-340 Schnorr from `k256`,
31//! SHA-256 from `sha2`, bech32 from `bech32`. The signing and verification
32//! paths are tested against the published BIP-340 vectors.
33//!
34//! # Replay protection
35//!
36//! [`verify_request`] proves who signed a request, for which URL, method and
37//! body, and roughly when. It does not make a request single use. A server
38//! must record [`VerifiedRequest::event_id`] durably and refuse a repeat.
39//!
40//! # Example
41//!
42//! ```
43//! use stratum_node_core::{verify_request, ExpectedRequest, NostrSecretKey, RequestPolicy, UnixTime};
44//!
45//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
46//! // Client side: sign a request. Use real randomness for the 32 bytes.
47//! let key = NostrSecretKey::from_nsec(
48//! "nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5",
49//! )?;
50//! let body = br#"{"node":"did:nostr:...","operation_id":"op-1"}"#;
51//! let request = ExpectedRequest::new("POST", "http://127.0.0.1:7070/api/v1/config/apply", Some(body));
52//! let now = UnixTime::from_secs(1_764_720_000);
53//! let header = key.sign_request(&request, now, &[0x42; 32])?;
54//!
55//! // Server side: verify it against what actually arrived.
56//! let verified = verify_request(&header, &request, now, &RequestPolicy::new())?;
57//! assert_eq!(verified.signer(), key.public());
58//! # Ok(())
59//! # }
60//! ```
61
62mod claim;
63mod did;
64mod encoding;
65mod event;
66mod id;
67mod request;
68mod schnorr;
69mod secret;
70
71pub use claim::{CheckClaimError, ClaimBody, ClaimFields, ParseClaimError, CLAIM_BODY_VERSION};
72pub use did::{DidNostr, ParseDidError};
73pub use event::{ParseEventError, SignedEvent, UnsignedEvent};
74pub use id::{
75 ChallengeId, EventId, OperationId, ParseIdError, Revision, Sha256Digest, UnixTime,
76 OPERATION_ID_MAX_LEN,
77};
78pub use request::{
79 verify_request, ExpectedRequest, RequestPolicy, RequestPolicyError, VerifiedRequest,
80 VerifyRequestError, HTTP_AUTH_KIND,
81};
82pub use secret::{NostrSecretKey, ParseKeyError, SignError};
83
84/// The version of the signed-request profile [`verify_request`] enforces and
85/// [`NostrSecretKey::sign_request`] produces.
86pub const REQUEST_PROFILE_VERSION: u32 = 1;
87
88/// Compiles and runs the README example as a doctest.
89#[cfg(doctest)]
90#[doc = include_str!("../README.md")]
91struct ReadmeDoctests;