diff --git a/common/types/src/helpers.rs b/common/types/src/helpers.rs index 7c8f69de8d..29bceb8ff7 100644 --- a/common/types/src/helpers.rs +++ b/common/types/src/helpers.rs @@ -17,11 +17,6 @@ impl ConsoleSigningOutput { encoded_signature: encoded_signature.into(), } } - - #[deprecated] - pub fn to_json_string(&self) -> String { - serde_json::to_string(self).expect("json serialization of 'ConsoleSigningOutput' failed") - } } impl Display for ConsoleSigningOutput { diff --git a/mixnode/Cargo.toml b/mixnode/Cargo.toml index 53d3857a55..fce6af86e9 100644 --- a/mixnode/Cargo.toml +++ b/mixnode/Cargo.toml @@ -51,7 +51,7 @@ nym-task = { path = "../common/task" } nym-types = { path = "../common/types" } nym-topology = { path = "../common/topology" } validator-client = { path = "../common/client-libs/validator-client" } -nym-bin-common = { path = "../common/bin-common" } +nym-bin-common = { path = "../common/bin-common", features = ["serde_json"] } cpu-cycles = { path = "../cpu-cycles", optional = true } [dev-dependencies] diff --git a/mixnode/src/commands/init.rs b/mixnode/src/commands/init.rs index 9dc60f2369..db3474c088 100644 --- a/mixnode/src/commands/init.rs +++ b/mixnode/src/commands/init.rs @@ -4,9 +4,9 @@ use super::OverrideConfig; use crate::config::Config; use crate::node::MixNode; -use crate::OutputFormat; use crate::{commands::override_config, config::persistence::pathfinder::MixNodePathfinder}; use clap::Args; +use nym_bin_common::output_format::OutputFormat; use nym_config::NymConfig; use nym_crypto::asymmetric::{encryption, identity}; use std::net::IpAddr; @@ -46,6 +46,9 @@ pub(crate) struct Init { // the alias here is included for backwards compatibility (1.1.4 and before) #[clap(long, alias = "validators", value_delimiter = ',')] nym_apis: Option>, + + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } impl From for OverrideConfig { @@ -63,7 +66,7 @@ impl From for OverrideConfig { } } -pub(crate) fn execute(args: &Init, output: OutputFormat) { +pub(crate) fn execute(args: &Init) { let override_config_fields = OverrideConfig::from(args.clone()); let id = &override_config_fields.id; eprintln!("Initialising mixnode {id}..."); @@ -112,5 +115,5 @@ pub(crate) fn execute(args: &Init, output: OutputFormat) { eprintln!("Saved configuration file to {config_save_location:?}"); eprintln!("Mixnode configuration completed.\n\n\n"); - MixNode::new(config).print_node_details(output) + MixNode::new(config).print_node_details(args.output) } diff --git a/mixnode/src/commands/mod.rs b/mixnode/src/commands/mod.rs index 09b3e6e8bc..26c663bab0 100644 --- a/mixnode/src/commands/mod.rs +++ b/mixnode/src/commands/mod.rs @@ -62,16 +62,14 @@ struct OverrideConfig { pub(crate) async fn execute(args: Cli) { let bin_name = "nym-mixnode"; - - let output = args.output(); - + match args.command { Commands::Describe(m) => describe::execute(m), - Commands::Init(m) => init::execute(&m, output), - Commands::Run(m) => run::execute(&m, output).await, - Commands::Sign(m) => sign::execute(&m, output), + Commands::Init(m) => init::execute(&m), + Commands::Run(m) => run::execute(&m).await, + Commands::Sign(m) => sign::execute(&m), Commands::Upgrade(m) => upgrade::execute(&m), - Commands::NodeDetails(m) => node_details::execute(&m, output), + Commands::NodeDetails(m) => node_details::execute(&m), Commands::Completions(s) => s.generate(&mut crate::Cli::command(), bin_name), Commands::GenerateFigSpec => fig_generate(&mut crate::Cli::command(), bin_name), } diff --git a/mixnode/src/commands/node_details.rs b/mixnode/src/commands/node_details.rs index 9f1d0745a7..a4b185206c 100644 --- a/mixnode/src/commands/node_details.rs +++ b/mixnode/src/commands/node_details.rs @@ -3,8 +3,8 @@ use crate::config::Config; use crate::node::MixNode; -use crate::OutputFormat; use clap::Args; +use nym_bin_common::output_format::OutputFormat; use nym_config::NymConfig; #[derive(Args)] @@ -12,9 +12,12 @@ pub(crate) struct NodeDetails { /// The id of the mixnode you want to show details for #[clap(long)] id: String, + + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } -pub(crate) fn execute(args: &NodeDetails, output: OutputFormat) { +pub(crate) fn execute(args: &NodeDetails) { let config = match Config::load_from_file(&args.id) { Ok(cfg) => cfg, Err(err) => { @@ -27,5 +30,5 @@ pub(crate) fn execute(args: &NodeDetails, output: OutputFormat) { } }; - MixNode::new(config).print_node_details(output) + MixNode::new(config).print_node_details(args.output) } diff --git a/mixnode/src/commands/run.rs b/mixnode/src/commands/run.rs index 6c2509d6a7..b0595d38d8 100644 --- a/mixnode/src/commands/run.rs +++ b/mixnode/src/commands/run.rs @@ -5,10 +5,10 @@ use super::OverrideConfig; use crate::commands::{override_config, version_check}; use crate::config::Config; use crate::node::MixNode; -use crate::OutputFormat; use clap::Args; use nym_config::NymConfig; use std::net::IpAddr; +use nym_bin_common::output_format::OutputFormat; use validator_client::nyxd; #[derive(Args, Clone)] @@ -45,6 +45,9 @@ pub(crate) struct Run { // the alias here is included for backwards compatibility (1.1.4 and before) #[clap(long, alias = "validators", value_delimiter = ',')] nym_apis: Option>, + + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } impl From for OverrideConfig { @@ -63,20 +66,20 @@ impl From for OverrideConfig { } fn show_binding_warning(address: &str) { - println!("\n##### NOTE #####"); - println!( + eprintln!("\n##### NOTE #####"); + eprintln!( "\nYou are trying to bind to {address} - you might not be accessible to other nodes\n\ You can ignore this note if you're running setup on a local network \n\ or have set a custom 'announce-host'" ); - println!("\n\n"); + eprintln!("\n\n"); } fn special_addresses() -> Vec<&'static str> { vec!["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"] } -pub(crate) async fn execute(args: &Run, output: OutputFormat) { +pub(crate) async fn execute(args: &Run) { eprintln!("Starting mixnode {}...", args.id); let mut config = match Config::load_from_file(&args.id) { @@ -107,7 +110,7 @@ pub(crate) async fn execute(args: &Run, output: OutputFormat) { eprintln!( "\nTo bond your mixnode you will need to install the Nym wallet, go to https://nymtech.net/get-involved and select the Download button.\n\ Select the correct version and install it to your machine. You will need to provide the following: \n "); - mixnode.print_node_details(output); + mixnode.print_node_details(args.output); mixnode.run().await } diff --git a/mixnode/src/commands/sign.rs b/mixnode/src/commands/sign.rs index 6abbf8a884..706defb8df 100644 --- a/mixnode/src/commands/sign.rs +++ b/mixnode/src/commands/sign.rs @@ -6,10 +6,10 @@ use std::convert::TryFrom; use crate::commands::validate_bech32_address_or_exit; use crate::config::{persistence::pathfinder::MixNodePathfinder, Config}; use crate::node::MixNode; -use crate::OutputFormat; use anyhow::{bail, Result}; use clap::{ArgGroup, Args}; use log::error; +use nym_bin_common::output_format::OutputFormat; use nym_config::NymConfig; use nym_crypto::asymmetric::identity; use nym_types::helpers::ConsoleSigningOutput; @@ -36,6 +36,9 @@ pub(crate) struct Sign { /// Signs a transaction-specific payload, that is going to be sent to the smart contract, with your identity key #[clap(long)] contract_msg: Option, + + #[clap(short, long, default_value_t = OutputFormat::default())] + output: OutputFormat, } enum SignedTarget { @@ -79,12 +82,7 @@ fn print_signed_text(private_key: &identity::PrivateKey, text: &str, output: Out let signature = private_key.sign_text(text); let sign_output = ConsoleSigningOutput::new(text, signature); - - let msg = match output { - OutputFormat::Json => sign_output.to_json_string(), - OutputFormat::Text => sign_output.to_string(), - }; - println!("{msg}") + println!("{}", output.format(&sign_output)); } fn print_signed_contract_msg( @@ -114,15 +112,10 @@ fn print_signed_contract_msg( let signature = private_key.sign(&decoded).to_base58_string(); let sign_output = ConsoleSigningOutput::new(decoded_string, signature); - - let msg = match output { - OutputFormat::Json => sign_output.to_json_string(), - OutputFormat::Text => sign_output.to_string(), - }; - println!("{msg}") + println!("{}", output.format(&sign_output)); } -pub(crate) fn execute(args: &Sign, output: OutputFormat) { +pub(crate) fn execute(args: &Sign) { let config = match Config::load_from_file(&args.id) { Ok(cfg) => cfg, Err(err) => { @@ -151,13 +144,13 @@ pub(crate) fn execute(args: &Sign, output: OutputFormat) { match signed_target { SignedTarget::Text(text) => { - print_signed_text(identity_keypair.private_key(), &text, output) + print_signed_text(identity_keypair.private_key(), &text, args.output) } SignedTarget::Address(addr) => { - print_signed_address(identity_keypair.private_key(), addr, output) + print_signed_address(identity_keypair.private_key(), addr, args.output) } SignedTarget::ContractMsg(raw_msg) => { - print_signed_contract_msg(identity_keypair.private_key(), &raw_msg, output) + print_signed_contract_msg(identity_keypair.private_key(), &raw_msg, args.output) } } } diff --git a/mixnode/src/main.rs b/mixnode/src/main.rs index 277f4b8f66..e041c9ad08 100644 --- a/mixnode/src/main.rs +++ b/mixnode/src/main.rs @@ -5,7 +5,7 @@ extern crate rocket; use ::nym_config::defaults::setup_env; -use clap::{crate_name, crate_version, Parser, ValueEnum}; +use clap::{crate_name, crate_version, Parser}; use lazy_static::lazy_static; use nym_bin_common::logging::setup_logging; use nym_bin_common::{build_information::BinaryBuildInformation, logging::banner}; @@ -31,23 +31,10 @@ struct Cli { #[clap(short, long)] pub(crate) config_env_file: Option, - #[clap(short, long)] - pub(crate) output: Option, - #[clap(subcommand)] command: commands::Commands, } -impl Cli { - fn output(&self) -> OutputFormat { - if let Some(ref output) = self.output { - output.clone() - } else { - OutputFormat::default() - } - } -} - #[cfg(feature = "cpu-cycles")] pub fn cpu_cycles() { info!("{}", cpu_cycles::cpucycles()) diff --git a/mixnode/src/node/mod.rs b/mixnode/src/node/mod.rs index b38fe40228..28716f120c 100644 --- a/mixnode/src/node/mod.rs +++ b/mixnode/src/node/mod.rs @@ -16,9 +16,9 @@ use crate::node::listener::Listener; use crate::node::node_description::NodeDescription; use crate::node::node_statistics::SharedNodeStats; use crate::node::packet_delayforwarder::{DelayForwarder, PacketDelayForwardSender}; -use crate::OutputFormat; use log::{error, info, warn}; use mixnode_common::verloc::{self, AtomicVerlocResult, VerlocMeasurer}; +use nym_bin_common::output_format::OutputFormat; use nym_bin_common::version_checker::parse_version; use nym_config::NymConfig; use nym_crypto::asymmetric::{encryption, identity}; @@ -96,14 +96,7 @@ impl MixNode { wallet_address: self.config.get_wallet_address().map(|x| x.to_string()), }; - match output { - OutputFormat::Json => println!( - "{}", - serde_json::to_string(&node_details) - .unwrap_or_else(|_| "Could not serialize node details".to_string()) - ), - OutputFormat::Text => println!("{node_details}"), - } + println!("{}", output.format(&node_details)); } fn start_http_api(