pub struct Session { /* private fields */ }Expand description
One node, one optional operator key file and one output format: the
context every stratum command runs in.
Each command method writes its results through Output and returns
the process exit code on success (see exit); failures
are returned as CliError, whose
exit_code gives theirs. Every authenticated
request is signed with NostrSecretKey::sign_request over the exact
URL, method and body bytes sent, with fresh auxiliary randomness.
§Examples
use stratum_node_cli::{CliError, Output, OutputFormat, Session};
let session = Session::new("http://127.0.0.1:7460".parse()?, None, Output::new(OutputFormat::Json));
// Commands that need the operator key say so before touching the network.
assert!(matches!(session.audit_checkpoint(), Err(CliError::MissingKeyFile)));Implementations§
Source§impl Session
impl Session
Sourcepub fn new(url: LoopbackUrl, key_file: Option<PathBuf>, output: Output) -> Self
pub fn new(url: LoopbackUrl, key_file: Option<PathBuf>, output: Output) -> Self
Creates a session for the node at url, signing with the key in
key_file when a command needs it.
Sourcepub fn key_generate(&self, out: &Path) -> Result<u8, CliError>
pub fn key_generate(&self, out: &Path) -> Result<u8, CliError>
stratum key generate --out <file>: creates a new operator key file
and prints the operator’s DID and npub.
§Errors
CliError::KeyFile when the file exists or cannot be created.
Sourcepub fn key_show(&self) -> Result<u8, CliError>
pub fn key_show(&self) -> Result<u8, CliError>
stratum key show: prints the DID and npub of the operator key.
§Errors
Sourcepub fn status(&self) -> Result<u8, CliError>
pub fn status(&self) -> Result<u8, CliError>
stratum status: reads /readyz and returns exit::OK when the
node is ready, exit::UNCONFIGURED or exit::DEGRADED.
§Errors
CliError::Unreachable (exit 2) or CliError::Protocol.
Sourcepub fn config_schema(&self) -> Result<u8, CliError>
pub fn config_schema(&self) -> Result<u8, CliError>
stratum config schema: prints the node’s configuration schema.
§Errors
Transport, protocol and server errors.
Sourcepub fn config_show(&self) -> Result<u8, CliError>
pub fn config_show(&self) -> Result<u8, CliError>
stratum config show: prints the node’s effective configuration,
revisions and public-config hash. Signed.
§Errors
Key, transport, protocol and server errors.
Sourcepub fn config_preview(
&self,
sets: &[String],
base_revision: Option<u64>,
) -> Result<u8, CliError>
pub fn config_preview( &self, sets: &[String], base_revision: Option<u64>, ) -> Result<u8, CliError>
stratum config preview --set path=value …: shows what the change
would do, without changing anything. Signed when a key file is
given (the node requires it once an administrator is bound).
§Errors
CliError::Usage for bad --set arguments,
CliError::InvalidPreview when the result would be invalid, and
key, transport, protocol and server errors.
Sourcepub fn config_apply(&self, options: &ApplyOptions) -> Result<u8, CliError>
pub fn config_apply(&self, options: &ApplyOptions) -> Result<u8, CliError>
stratum config apply --set path=value …: previews the change,
prints the diff and the strictest apply class, and after
confirmation applies it on the same base revision. Signed.
The operation ID is printed before the apply request is sent, so a
retry after a lost response can pass it with --operation-id.
§Errors
CliError::NotConfirmed when not confirmed,
CliError::InvalidPreview, CliError::Usage, and key,
transport, protocol and server errors (conflict for a stale base
revision, invalid for a rejected configuration).
Sourcepub fn audit_checkpoint(&self) -> Result<u8, CliError>
pub fn audit_checkpoint(&self) -> Result<u8, CliError>
stratum audit checkpoint: prints the audit checkpoint <seq>:<hex>
and the node DID. Keep the checkpoint somewhere the node cannot
write: it is what truncation of the audit log is detected against.
Signed.
§Errors
Key, transport, protocol and server errors.
Sourcepub fn backup(
&self,
operation_id_arg: Option<&str>,
expect_node: Option<&DidNostr>,
) -> Result<u8, CliError>
pub fn backup( &self, operation_id_arg: Option<&str>, expect_node: Option<&DidNostr>, ) -> Result<u8, CliError>
stratum backup [--expect-node <did>]: asks the node for a backup
and prints its directory, checkpoint and configuration revision.
Signed.
Before anything is printed, the returned manifest event must verify
(ID and signature), be of kind 30078 and be authored by the node DID
reported by /readyz; its content must be a stratum-backup
version 1 manifest naming that node and listing the three backup
files in order; and its signed audit_checkpoint must equal the
response’s checkpoint. Every printed fact except the backup directory therefore
comes from the signed manifest; the directory is the node’s unsigned
statement of where it wrote the files. When expect_node is given, /readyz must report
it before any request is signed.
§Errors
CliError::Refused for a manifest that fails verification or a
node other than expect_node, backup_refused when the node has no
backup recipient, and key, transport, protocol and server errors.
Sourcepub fn sign_challenge(
&self,
claim_body_file: &Path,
expect_node: &DidNostr,
) -> Result<u8, CliError>
pub fn sign_challenge( &self, claim_body_file: &Path, expect_node: &DidNostr, ) -> Result<u8, CliError>
stratum sign-challenge --claim-body-file <file> --expect-node <did>:
signs a claim body offline and prints the Authorization header
value for it. No network.
Before signing, the body must name this key as claimant and
expect_node as the node, and must not have expired. The expected
node comes from the operator (stratumd prints its node DID at
start), never from the file, so a substituted claim file for another
node is refused. The configuration hash and base revision in the body
cannot be checked offline: they are signed as the node issued them.
stratum setup checks them against its own derivation; prefer it
when the node is reachable.
The URL is the session URL: a bare origin gets
/api/v1/bootstrap/claim appended, and a URL with a path must end in
exactly that path, so no other route can be signed. It must equal
the url the challenge response named. The file’s bytes are signed
exactly as they are, so a trailing newline added by an editor makes
the signature fail at the node; a warning is printed if one is
present.
§Errors
CliError::Usage for a URL with another path,
CliError::Refused when the body names another claimant or node or
has expired, CliError::FileContents when it is not a claim body,
and key and file errors.
Source§impl Session
impl Session
Sourcepub fn setup(&self, options: &SetupOptions) -> Result<u8, CliError>
pub fn setup(&self, options: &SetupOptions) -> Result<u8, CliError>
stratum setup --token-file <path> [--set path=value …]: claims an
unconfigured node for the operator key and commits the initial
configuration. With no --set, the node is claimed with its seed
configuration unchanged.
The flow:
- Read the bootstrap token and
/readyz, which must reportunconfiguredat revision 0, and take the node DID from it. WithSetupOptions::expect_node, that DID must be the expected one, or setup stops before anything else is sent. - Derive the initial configuration locally: the seed text of
stratum-node-config(the text of revision 0) with the changes applied. Preview the same changes on the node; itstextanddiffmust equal the local ones byte for byte. Show the local diff and change list (and withSetupOptions::show_textthe whole text); stop if the result is invalid. - Request a challenge for this key’s DID and the same changes.
- Check the returned claim body locally: it must name this
claimant and the node from
/readyz, must not have expired, must answer the challenge ID and base revision, itsconfig_sha256must be the SHA-256 of the locally derived text (so the hash covers exactly the text shown), and the claim URL must be this client’s own/api/v1/bootstrap/claimURL with methodPOST. - Show the claim fields and ask for confirmation (or
--yes). - Sign the body verbatim, save the signed request with the token to
the resume file (
0600, created exclusively), then send it with the token inStratum-Bootstrap-Token. - Check that the response names this administrator and node, print the administrator DID, revision and public-config hash, and delete the resume file.
If a resume file exists, steps 1 to 6 are skipped and the saved
request is resent byte for byte, so that a node which committed the
claim but lost the response returns its stored result. With
SetupOptions::expect_node, the saved body must name that node and
/readyz must report it before the saved request is resent. The resume
file is kept after a transport failure, a protocol error or a 5xx
response, and deleted after success or any 4xx, which the identical
request can never turn into a success.
§Errors
CliError::Refused for any failed local check (including a node
other than the expected one, or a node preview that differs from
the local derivation),
CliError::NotConfirmed, CliError::InvalidPreview, token, key
and resume-file errors, and transport, protocol and server errors.