From 0b6adf59ce7f53c9e381f4f32e20de6eec8a6a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan-=C8=98tefan=20Neac=C5=9Fu?= Date: Tue, 4 Jan 2022 13:49:24 +0100 Subject: [PATCH] Feature/fix gateway sign (#1004) * Include version check in sign command * Ask for wallet address the same way as mixnode The reason for this is that the cosmos mnemonic that is asked at init is for the address that gets rewarded for gateway usage. Since that address is not necessarly set now and it can take a default value, we won't be using that to derive the address in the signing process. --- gateway/src/commands/mod.rs | 19 ++++++++ gateway/src/commands/run.rs | 20 --------- gateway/src/commands/sign.rs | 84 ++++++------------------------------ mixnode/src/commands/mod.rs | 19 ++++++++ mixnode/src/commands/run.rs | 21 --------- mixnode/src/commands/sign.rs | 6 +++ 6 files changed, 56 insertions(+), 113 deletions(-) diff --git a/gateway/src/commands/mod.rs b/gateway/src/commands/mod.rs index aeb072c7eb..b6c30d14a1 100644 --- a/gateway/src/commands/mod.rs +++ b/gateway/src/commands/mod.rs @@ -148,3 +148,22 @@ pub(crate) fn validate_bech32_address_or_exit(address: &str) { process::exit(1); } } + +// this only checks compatibility between config the binary. It does not take into consideration +// network version. It might do so in the future. +pub(crate) fn version_check(cfg: &Config) -> bool { + let binary_version = env!("CARGO_PKG_VERSION"); + let config_version = cfg.get_version(); + if binary_version != config_version { + log::warn!("The gateway binary has different version than what is specified in config file! {} and {}", binary_version, config_version); + if version_checker::is_minor_version_compatible(binary_version, config_version) { + log::info!("but they are still semver compatible. However, consider running the `upgrade` command"); + true + } else { + log::error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); + false + } + } else { + true + } +} diff --git a/gateway/src/commands/run.rs b/gateway/src/commands/run.rs index f89cf213ed..9ba2847477 100644 --- a/gateway/src/commands/run.rs +++ b/gateway/src/commands/run.rs @@ -7,7 +7,6 @@ use crate::node::Gateway; use clap::{App, Arg, ArgMatches}; use config::NymConfig; use log::*; -use version_checker::is_minor_version_compatible; pub fn command_args<'a, 'b>() -> clap::App<'a, 'b> { let app = App::new("run") @@ -95,25 +94,6 @@ fn special_addresses() -> Vec<&'static str> { vec!["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"] } -// this only checks compatibility between config the binary. It does not take into consideration -// network version. It might do so in the future. -fn version_check(cfg: &Config) -> bool { - let binary_version = env!("CARGO_PKG_VERSION"); - let config_version = cfg.get_version(); - if binary_version != config_version { - warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); - if is_minor_version_compatible(binary_version, config_version) { - info!("but they are still semver compatible. However, consider running the `upgrade` command"); - true - } else { - error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); - false - } - } else { - true - } -} - pub async fn execute(matches: ArgMatches<'static>) { let id = matches.value_of(ID_ARG_NAME).unwrap(); diff --git a/gateway/src/commands/sign.rs b/gateway/src/commands/sign.rs index d0e91393bd..404b88744e 100644 --- a/gateway/src/commands/sign.rs +++ b/gateway/src/commands/sign.rs @@ -8,8 +8,6 @@ use colored::Colorize; use config::NymConfig; use crypto::asymmetric::identity; use log::error; -#[cfg(not(feature = "coconut"))] -use validator_client::nymd::AccountId; const SIGN_TEXT_ARG_NAME: &str = "text"; const SIGN_ADDRESS_ARG_NAME: &str = "address"; @@ -55,58 +53,7 @@ pub fn load_identity_keys(pathfinder: &GatewayPathfinder) -> identity::KeyPair { identity_keypair } -#[cfg(not(feature = "coconut"))] -fn derive_address(raw_mnemonic: &str) -> AccountId { - let mnemonic = match raw_mnemonic.parse() { - Ok(mnemonic) => mnemonic, - Err(err) => { - let error_message = format!("failed to parse the provided mnemonic - {}", err).red(); - println!("{}", error_message); - process::exit(1); - } - }; - let wallet = - match validator_client::nymd::wallet::DirectSecp256k1HdWallet::from_mnemonic(mnemonic) { - Ok(wallet) => wallet, - Err(err) => { - let error_message = format!( - "failed to derive your account with the provided mnemonic - {}", - err - ) - .red(); - println!("{}", error_message); - process::exit(1); - } - }; - let account_data = match wallet.try_derive_accounts() { - Ok(data) => data, - Err(err) => { - let error_message = format!( - "failed to derive your account with the provided mnemonic - {}", - err - ) - .red(); - println!("{}", error_message); - process::exit(1); - } - }; - account_data[0].address().clone() -} - -#[cfg(not(feature = "coconut"))] -fn sign_derived_address(private_key: &identity::PrivateKey, address: &AccountId) { - let signature_bytes = private_key.sign(&address.to_bytes()).to_bytes(); - let signature = bs58::encode(signature_bytes).into_string(); - - println!( - "The base58-encoded signature on '{}' is: {}", - address, signature - ) -} - -// we do tiny bit of sanity check validation -#[cfg(feature = "coconut")] -fn sign_provided_address(private_key: &identity::PrivateKey, raw_address: &str) { +fn print_signed_address(private_key: &identity::PrivateKey, raw_address: &str) -> String { let trimmed = raw_address.trim(); validate_bech32_address_or_exit(trimmed); let signature = private_key.sign_text(trimmed); @@ -114,7 +61,8 @@ fn sign_provided_address(private_key: &identity::PrivateKey, raw_address: &str) println!( "The base58-encoded signature on '{}' is: {}", trimmed, signature - ) + ); + signature } fn print_signed_text(private_key: &identity::PrivateKey, text: &str) { @@ -141,28 +89,20 @@ pub fn execute(matches: &ArgMatches) { return; } }; + + if !version_check(&config) { + error!("failed the local version check"); + return; + } + let pathfinder = GatewayPathfinder::new_from_config(&config); let identity_keypair = load_identity_keys(&pathfinder); if let Some(text) = matches.value_of(SIGN_TEXT_ARG_NAME) { print_signed_text(identity_keypair.private_key(), text) - } - - #[cfg(not(feature = "coconut"))] - { - if matches.is_present(SIGN_ADDRESS_ARG_NAME) { - let address = derive_address(&config.get_cosmos_mnemonic()); - sign_derived_address(identity_keypair.private_key(), &address); - } - } - #[cfg(feature = "coconut")] - { - if let Some(address) = matches.value_of(SIGN_ADDRESS_ARG_NAME) { - sign_provided_address(identity_keypair.private_key(), address) - } - } - - if !matches.is_present(SIGN_TEXT_ARG_NAME) && !matches.is_present(SIGN_ADDRESS_ARG_NAME) { + } else if let Some(address) = matches.value_of(SIGN_ADDRESS_ARG_NAME) { + print_signed_address(identity_keypair.private_key(), address); + } else { let error_message = format!( "You must specify either '--{}' or '--{}' argument!", SIGN_TEXT_ARG_NAME, SIGN_ADDRESS_ARG_NAME diff --git a/mixnode/src/commands/mod.rs b/mixnode/src/commands/mod.rs index fdedab8267..3c8a1a628e 100644 --- a/mixnode/src/commands/mod.rs +++ b/mixnode/src/commands/mod.rs @@ -116,3 +116,22 @@ pub(crate) fn validate_bech32_address_or_exit(address: &str) { process::exit(1); } } + +// this only checks compatibility between config the binary. It does not take into consideration +// network version. It might do so in the future. +pub(crate) fn version_check(cfg: &Config) -> bool { + let binary_version = env!("CARGO_PKG_VERSION"); + let config_version = cfg.get_version(); + if binary_version != config_version { + warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); + if version_checker::is_minor_version_compatible(binary_version, config_version) { + info!("but they are still semver compatible. However, consider running the `upgrade` command"); + true + } else { + error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); + false + } + } else { + true + } +} diff --git a/mixnode/src/commands/run.rs b/mixnode/src/commands/run.rs index e609905ed7..70561d53cf 100644 --- a/mixnode/src/commands/run.rs +++ b/mixnode/src/commands/run.rs @@ -6,8 +6,6 @@ use crate::config::Config; use crate::node::MixNode; use clap::{App, Arg, ArgMatches}; use config::NymConfig; -use log::warn; -use version_checker::is_minor_version_compatible; pub fn command_args<'a, 'b>() -> App<'a, 'b> { App::new("run") @@ -73,25 +71,6 @@ fn special_addresses() -> Vec<&'static str> { vec!["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"] } -// this only checks compatibility between config the binary. It does not take into consideration -// network version. It might do so in the future. -fn version_check(cfg: &Config) -> bool { - let binary_version = env!("CARGO_PKG_VERSION"); - let config_version = cfg.get_version(); - if binary_version != config_version { - warn!("The mixnode binary has different version than what is specified in config file! {} and {}", binary_version, config_version); - if is_minor_version_compatible(binary_version, config_version) { - info!("but they are still semver compatible. However, consider running the `upgrade` command"); - true - } else { - error!("and they are semver incompatible! - please run the `upgrade` command before attempting `run` again"); - false - } - } else { - true - } -} - pub async fn execute(matches: ArgMatches<'static>) { let id = matches.value_of(ID_ARG_NAME).unwrap(); diff --git a/mixnode/src/commands/sign.rs b/mixnode/src/commands/sign.rs index 79193c5d77..8bd666dd04 100644 --- a/mixnode/src/commands/sign.rs +++ b/mixnode/src/commands/sign.rs @@ -83,6 +83,12 @@ pub fn execute(matches: &ArgMatches) { return; } }; + + if !version_check(&config) { + error!("failed the local version check"); + return; + } + let pathfinder = MixNodePathfinder::new_from_config(&config); let identity_keypair = load_identity_keys(&pathfinder);