Skip to main content

stratum_node_cli/
lib.rs

1//! The library behind `stratum`, the operator's client for a Stratum node.
2//!
3//! **Pre-release:** this is `0.1.0-alpha`. The commands, output and API may
4//! change before 0.1.0, and nothing built on it has been deployed yet.
5//!
6//! A Stratum node serves an administrative API on loopback only. `stratum`
7//! talks to that API over plain HTTP and holds the operator's own Nostr key;
8//! it never sees the node key or the node's state volume, except that
9//! `stratum setup` reads the bootstrap token file the operator points it at.
10//!
11//! - **URLs.** [`LoopbackUrl`] accepts `http://` URLs whose host is an
12//!   IPv4 address in `127.0.0.0/8`, `[::1]` or `localhost`, and nothing
13//!   else: Step 1 has no TLS. `localhost` is connected to `127.0.0.1`
14//!   without a name lookup.
15//! - **Operator keys.** [`generate_key_file`] writes a fresh key as one
16//!   `nsec1…` line to a file created exclusively with mode `0600`;
17//!   [`load_key_file`] refuses symbolic links and modes wider than `0600`.
18//!   The secret is never printed, logged or returned as text.
19//! - **Changes.** [`parse_changes`] reads `--set path=value` arguments typed
20//!   by the schema in `stratum-node-config`.
21//! - **Commands.** [`Session`] runs each command against one node: `status`,
22//!   `config` (`schema`, `show`, `preview`, `apply`), `setup`,
23//!   `sign-challenge`, `audit checkpoint` and `backup`. Every authenticated
24//!   request is signed with
25//!   [`NostrSecretKey::sign_request`](stratum_node_core::NostrSecretKey::sign_request)
26//!   over the exact URL, method and body bytes sent.
27//! - **Exit codes.** Failures are [`CliError`]s, each mapped to a stable
28//!   code in [`exit`].
29//!
30//! The crate targets Unix: key files rely on Unix permission bits.
31//!
32//! # Example
33//!
34//! ```no_run
35//! use stratum_node_cli::{Output, OutputFormat, Session};
36//!
37//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
38//! let output = Output::new(OutputFormat::Human);
39//! let session = Session::new("http://127.0.0.1:7460".parse()?, Some("operator.nsec".into()), output);
40//! let code = session.status().unwrap_or_else(|err| {
41//!     output.error(&err);
42//!     err.exit_code()
43//! });
44//! std::process::exit(i32::from(code));
45//! # }
46//! ```
47
48#[cfg(not(unix))]
49compile_error!("stratum-node-cli supports Unix only: key files rely on Unix permission bits");
50
51mod changes;
52mod client;
53mod error;
54pub mod exit;
55mod key;
56mod output;
57mod private_file;
58mod redact;
59mod resume;
60mod session;
61mod setup;
62mod token;
63mod url;
64mod wire;
65
66pub use changes::{parse_changes, ParseChangeError};
67pub use error::CliError;
68pub use key::{generate_key_file, load_key_file, KeyFileError};
69pub use output::{Output, OutputFormat};
70pub use private_file::PrivateFileError;
71pub use session::{ApplyOptions, Confirmation, Session, SetupOptions};
72pub use token::{BootstrapToken, TokenFileError};
73pub use url::{LoopbackUrl, ParseUrlError};
74
75/// The default node URL: the loopback address the controller listens on by
76/// default.
77pub const DEFAULT_URL: &str = "http://127.0.0.1:7460";
78
79/// Compiles the README's examples as doctests.
80#[cfg(doctest)]
81#[doc = include_str!("../README.md")]
82struct ReadmeDoctests;