Skip to main content

vela_cli/
lib.rs

1//! `vela` — the command-line binary.
2//!
3//! Hands off to `crate::cli::run_from_args`, after a small read-only
4//! verify intercept (conjecture / proof-packet verification).
5
6use vela_protocol::cli_style as style;
7
8// The CLI / serve / workbench surface, relocated out of the
9// `vela-protocol` library so the substrate crate stays a pure protocol
10// library. These were `vela_protocol::{cli, serve, workbench, cli_*}`
11// before; they now live here and reach into the substrate via
12// `vela_protocol::*`.
13// The standard repository-authority transaction core used by fresh setup,
14// producer registration, verification import, and exact review decisions.
15pub(crate) mod authority_transaction;
16#[allow(dead_code)]
17mod bounded_file;
18mod frontier;
19pub(crate) mod repository_authority_provider;
20pub(crate) use frontier::cli_read;
21mod write;
22pub(crate) use write::{claim_view, cli_object, cli_write};
23mod tools;
24pub(crate) use tools::cli_check;
25mod config;
26pub(crate) mod current_doctor;
27pub(crate) mod current_read;
28pub(crate) mod current_repository_decision;
29pub(crate) mod current_submission;
30pub(crate) mod current_verification;
31pub(crate) mod current_work;
32pub(crate) mod git_hardened;
33pub(crate) use config::{cli_admin, cli_agents, cli_identity};
34// Current repository verification and object projections.
35pub(crate) mod current_repository;
36#[allow(dead_code)]
37pub(crate) mod frontier_txn;
38mod operation_journal;
39mod server;
40mod target_index;
41pub(crate) mod ui;
42pub(crate) mod workflow;
43pub(crate) use server::{cli_commands, cli_engine};
44
45pub mod cli;
46
47pub fn run() {
48    // Color discipline must hold for the read-only intercepts below too:
49    // they print before `run_from_args` initializes styling, so without
50    // this a piped or NO_COLOR invocation would leak ANSI. `init()` is
51    // `Once`-guarded, so the later call in `run_from_args` is a no-op.
52    style::init();
53
54    // Atlas R.2 intercept: read-only verifier subcommands for the
55    // primitives added in R.1 (v0.338). Live ahead of run_from_args()
56    // because the dispatcher in vela-protocol/cli.rs predates these
57    // primitives. When the next vela-protocol release lands them in the
58    // dispatcher proper, this intercept can be removed.
59    if try_handle_atlas_r2_verify_intercept() {
60        return;
61    }
62
63    crate::cli::run_from_args();
64}
65
66fn try_handle_atlas_r2_verify_intercept() -> bool {
67    let argv: Vec<String> = std::env::args().collect();
68    if argv.len() < 3 {
69        return false;
70    }
71    match (argv[1].as_str(), argv[2].as_str()) {
72        ("conjecture", "verify") => {
73            handle_conjecture_verify(&argv[3..]);
74            true
75        }
76        ("proof-packet", "verify") => {
77            handle_proof_packet_verify(&argv[3..]);
78            true
79        }
80        ("proof-packet", "verify-external") => {
81            handle_proof_packet_verify_external(&argv[3..]);
82            true
83        }
84        _ => false,
85    }
86}
87
88fn handle_conjecture_verify(args: &[String]) {
89    let path = match args.first() {
90        Some(p) => p,
91        None => {
92            eprintln!(
93                "{} usage: vela conjecture verify <path>",
94                style::err_prefix()
95            );
96            std::process::exit(2);
97        }
98    };
99    let body = match std::fs::read_to_string(path) {
100        Ok(b) => b,
101        Err(e) => {
102            eprintln!("{} read {path}: {e}", style::err_prefix());
103            std::process::exit(1);
104        }
105    };
106    let conj: vela_protocol_core::conjecture::Conjecture = match serde_json::from_str(&body) {
107        Ok(c) => c,
108        Err(e) => {
109            eprintln!("{} parse {path}: {e}", style::err_prefix());
110            std::process::exit(1);
111        }
112    };
113    if let Err(e) = conj.verify() {
114        eprintln!("{} witness signature/id invalid: {e}", style::err_prefix());
115        std::process::exit(1);
116    }
117    let cosigs = match conj.verify_cosignatures() {
118        Ok(n) => n,
119        Err(e) => {
120            eprintln!("{} co-signature invalid: {e}", style::err_prefix());
121            std::process::exit(1);
122        }
123    };
124    println!(
125        "  {} {} witness:{} cosigners:{} status:{:?}",
126        style::moss("conjecture verified"),
127        conj.id,
128        conj.witness.actor_id,
129        cosigs,
130        conj.status,
131    );
132}
133
134fn handle_proof_packet_verify(args: &[String]) {
135    let path = match args.first() {
136        Some(p) => p,
137        None => {
138            eprintln!(
139                "{} usage: vela proof-packet verify <path>",
140                style::err_prefix()
141            );
142            std::process::exit(2);
143        }
144    };
145    let body = match std::fs::read_to_string(path) {
146        Ok(b) => b,
147        Err(e) => {
148            eprintln!("{} read {path}: {e}", style::err_prefix());
149            std::process::exit(1);
150        }
151    };
152    let packet: vela_edge::proof_packet::ProofPacket = match serde_json::from_str(&body) {
153        Ok(p) => p,
154        Err(e) => {
155            eprintln!("{} parse {path}: {e}", style::err_prefix());
156            std::process::exit(1);
157        }
158    };
159    if let Err(e) = packet.verify() {
160        eprintln!("{} packet invalid: {e}", style::err_prefix());
161        std::process::exit(1);
162    }
163    println!(
164        "  {} {} hash:{} signer:{}",
165        style::moss("proof packet verified"),
166        packet.packet_id,
167        &packet.packet_hash[..24],
168        packet.signer_actor_id,
169    );
170}
171
172fn handle_proof_packet_verify_external(args: &[String]) {
173    let path = match args.first() {
174        Some(p) => p,
175        None => {
176            eprintln!(
177                "{} usage: vela proof-packet verify-external <path>",
178                style::err_prefix()
179            );
180            std::process::exit(2);
181        }
182    };
183    let body = match std::fs::read_to_string(path) {
184        Ok(b) => b,
185        Err(e) => {
186            eprintln!("{} read {path}: {e}", style::err_prefix());
187            std::process::exit(1);
188        }
189    };
190    let packet: vela_edge::proof_packet::ProofPacket = match serde_json::from_str(&body) {
191        Ok(p) => p,
192        Err(e) => {
193            eprintln!("{} parse {path}: {e}", style::err_prefix());
194            std::process::exit(1);
195        }
196    };
197    if let Err(e) = packet.verify() {
198        eprintln!("{} packet invalid: {e}", style::err_prefix());
199        std::process::exit(1);
200    }
201    let n = match packet.verify_external_verifications() {
202        Ok(n) => n,
203        Err(e) => {
204            eprintln!("{} external verification invalid: {e}", style::err_prefix());
205            std::process::exit(1);
206        }
207    };
208    println!(
209        "  {} {} external:{} (signer:{})",
210        style::moss("proof packet + externals verified"),
211        packet.packet_id,
212        n,
213        packet.signer_actor_id,
214    );
215}