stratum-node-cli 0.1.0-alpha.1

The `stratum` operator client for Stratum pod nodes: operator key files, bootstrap claim, signed config apply, audit checkpoint and backup over the loopback API
Documentation
//! Request and response bodies of the node's `/api/v1` loopback API.
//!
//! Request structs serialise their fields in a fixed order, and every
//! request body is serialised once and signed and sent as those bytes.
//! Response structs ignore members they do not name, so a node that adds
//! a member stays readable.

use serde::{Deserialize, Serialize};
use stratum_node_config::Change;
use stratum_node_core::{ChallengeId, DidNostr, OperationId, Sha256Digest, UnixTime};

/// `POST /api/v1/config/preview`.
#[derive(Debug, Serialize)]
pub(crate) struct PreviewRequest<'a> {
    pub(crate) base_revision: u64,
    pub(crate) changes: &'a [Change],
}

/// `POST /api/v1/bootstrap/challenge`.
#[derive(Debug, Serialize)]
pub(crate) struct ChallengeRequest<'a> {
    pub(crate) did: DidNostr,
    pub(crate) base_revision: u64,
    pub(crate) changes: &'a [Change],
}

/// `POST /api/v1/config/apply`.
#[derive(Debug, Serialize)]
pub(crate) struct ApplyRequest<'a> {
    pub(crate) node: DidNostr,
    pub(crate) operation_id: &'a OperationId,
    pub(crate) base_revision: u64,
    pub(crate) changes: &'a [Change],
}

/// `POST /api/v1/backup`.
#[derive(Debug, Serialize)]
pub(crate) struct BackupRequest<'a> {
    pub(crate) node: DidNostr,
    pub(crate) operation_id: &'a OperationId,
}

/// `GET /readyz`.
#[derive(Debug, Deserialize)]
pub(crate) struct Readyz {
    pub(crate) status: String,
    pub(crate) version: String,
    pub(crate) head_revision: u64,
    pub(crate) active_revision: u64,
    pub(crate) public_config_hash: Option<String>,
    pub(crate) node_did: DidNostr,
    pub(crate) api_version: u64,
}

/// One entry of a preview's structured change list.
#[derive(Debug, Deserialize)]
pub(crate) struct ChangeEntry {
    pub(crate) path: String,
    pub(crate) apply_class: String,
    #[serde(default)]
    pub(crate) old: Option<serde_json::Value>,
    pub(crate) new: serde_json::Value,
}

/// One validation error, in a preview or an `invalid` error body.
#[derive(Debug, Deserialize)]
pub(crate) struct ValidationEntry {
    #[serde(default)]
    pub(crate) path: Option<String>,
    pub(crate) message: String,
}

/// `POST /api/v1/config/preview` response.
#[derive(Debug, Deserialize)]
pub(crate) struct PreviewResponse {
    pub(crate) base_revision: u64,
    pub(crate) text: String,
    pub(crate) diff: String,
    pub(crate) changes: Vec<ChangeEntry>,
    pub(crate) strictest_apply_class: Option<String>,
    pub(crate) errors: Vec<ValidationEntry>,
}

/// `POST /api/v1/bootstrap/challenge` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ChallengeResponse {
    pub(crate) challenge_id: ChallengeId,
    pub(crate) claim_body: String,
    pub(crate) url: String,
    pub(crate) method: String,
    pub(crate) expires_at: UnixTime,
}

/// `POST /api/v1/bootstrap/claim` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ClaimResponse {
    pub(crate) administrator: DidNostr,
    pub(crate) node_did: DidNostr,
    pub(crate) revision: u64,
    pub(crate) public_config_hash: Sha256Digest,
}

/// `GET /api/v1/config` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ConfigResponse {
    pub(crate) head_revision: u64,
    pub(crate) active_revision: u64,
    pub(crate) public_config_hash: Sha256Digest,
    pub(crate) text: String,
}

/// `POST /api/v1/config/apply` response.
#[derive(Debug, Deserialize)]
pub(crate) struct ApplyResponse {
    pub(crate) revision: u64,
    pub(crate) public_config_hash: Sha256Digest,
    pub(crate) strictest_apply_class: Option<String>,
    pub(crate) restart_required: bool,
}

/// `GET /api/v1/audit/checkpoint` response.
#[derive(Debug, Deserialize)]
pub(crate) struct CheckpointResponse {
    pub(crate) sequence: u64,
    pub(crate) tail_hash: Sha256Digest,
    pub(crate) checkpoint: String,
    pub(crate) node_did: DidNostr,
}

/// `POST /api/v1/backup` response.
#[derive(Debug, Deserialize)]
pub(crate) struct BackupResponse {
    pub(crate) directory: String,
    pub(crate) manifest: serde_json::Value,
    pub(crate) checkpoint: String,
}

/// The name in a backup manifest's `format` member.
pub(crate) const BACKUP_FORMAT: &str = "stratum-backup";

/// The only backup manifest `format_version` this client reads.
pub(crate) const BACKUP_FORMAT_VERSION: u64 = 1;

/// The files a backup manifest lists, in its fixed order.
pub(crate) const BACKUP_FILES: [&str; 3] = ["stratum.db", "stratum.toml", "node-key.age"];

/// The content of the signed backup manifest event: the controller's
/// format (`stratum-backup`, version 1).
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct BackupManifest {
    pub(crate) format: String,
    pub(crate) format_version: u64,
    pub(crate) node_did: DidNostr,
    pub(crate) created_at: UnixTime,
    pub(crate) config_revision: u64,
    pub(crate) audit_checkpoint: String,
    pub(crate) files: Vec<ManifestFile>,
}

/// One file listed in a [`BackupManifest`].
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ManifestFile {
    pub(crate) name: String,
    #[expect(
        dead_code,
        reason = "parsed to check the format; checked by stratumd restore"
    )]
    pub(crate) sha256: Sha256Digest,
    #[expect(
        dead_code,
        reason = "parsed to check the format; checked by stratumd restore"
    )]
    pub(crate) size: u64,
}

/// The error envelope every failing route returns.
///
/// The contract says an `invalid` error "adds `errors`" without saying at
/// which level, so the list is read from inside `error` and beside it.
#[derive(Debug, Deserialize)]
pub(crate) struct ErrorEnvelope {
    pub(crate) error: ErrorDetail,
    #[serde(default)]
    pub(crate) errors: Vec<ValidationEntry>,
}

/// The body of [`ErrorEnvelope`].
#[derive(Debug, Deserialize)]
pub(crate) struct ErrorDetail {
    pub(crate) code: String,
    pub(crate) message: String,
    #[serde(default)]
    pub(crate) errors: Vec<ValidationEntry>,
}