1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! 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 Steps 3a and 3b add, from
//! `suspended` to `lease_expired`.
//! - **Applications and installations (Step 3b).** [`ValidManifest`]
//! validates an application [`Manifest`] against the node's
//! [`JobCeilings`], naming the member and [`ManifestRule`] of a refusal;
//! [`ImageId`], [`ContainerPath`], [`MilliCpus`] and [`JobLimits`] are its
//! parts. [`render_offered`] renders the [`OfferedGrant`], the WAC
//! authorisations a member consents to, byte for byte as the controller
//! stores them. [`Schedule`] does the period arithmetic. The tenant and
//! administrator route bodies, the identifiers ([`ApplicationId`],
//! [`InstallationId`], [`JobId`]), the states and the
//! [`EnforcementParts`] of a withdrawal or suspension are in the same
//! group, and [`DirectoryAppKey`] is the gate directory's `app_keys`
//! entry.
//! - **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(())
//! # }
//! ```
pub use ;
pub use ;
pub use ;
pub use DirectoryAppKey;
pub use ErrorCode;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// 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.
;