Skip to content

Commit

Permalink
Merge branch 'rustSDK-1.6' into rustSDK-feat-1.6
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/cli.rs
#	lib/types/deploy.rs
  • Loading branch information
gRoussac committed Nov 17, 2023
2 parents f9d312b + cb505c7 commit 4b27b19
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 43 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ uint = "0.9.4"
tempfile = "3.7.1"

[features]
sdk = ["casper-types/sdk"]
default = ["std-fs-io"]
std-fs-io = ["casper-types/std-fs-io"]

[build-dependencies]
vergen = { version = "7", default-features = false, features = ["git"] }
Expand Down
29 changes: 18 additions & 11 deletions lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ mod dictionary_item_str_params;
mod error;
mod json_args;
mod parse;
pub use parse::account_identifier as parse_account_identifier;
pub use parse::purse_identifier as parse_purse_identifier;
mod payment_str_params;
mod session_str_params;
mod simple_args;
#[cfg(feature = "sdk")]
pub use parse::account_identifier as parse_account_identifier;
#[cfg(feature = "sdk")]
pub use parse::purse_identifier as parse_purse_identifier;
#[cfg(feature = "sdk")]
pub use simple_args::insert_arg;

#[cfg(test)]
Expand Down Expand Up @@ -127,7 +124,7 @@ pub async fn speculative_put_deploy(
/// `force` is true, and a file exists at `maybe_output_path`, it will be overwritten. If `force`
/// is false and a file exists at `maybe_output_path`, [`Error::FileAlreadyExists`] is returned
/// and the file will not be written.
/// With sdk feature file is not written
/// Without the std-fs-io feature the file is not written
/// Returns the Deploy
pub fn make_deploy(
#[allow(unused_variables)] maybe_output_path: &str,
Expand All @@ -138,7 +135,7 @@ pub fn make_deploy(
) -> Result<Deploy, CliError> {
let deploy =
deploy::with_payment_and_session(deploy_params, payment_params, session_params, true)?;
#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
{
let output = parse::output_kind(maybe_output_path, force);
let _ = crate::output_deploy(output, &deploy).map_err(CliError::from);
Expand All @@ -153,8 +150,7 @@ pub fn make_deploy(
/// `force` is true, and a file exists at `maybe_output_path`, it will be overwritten. If `force`
/// is false and a file exists at `maybe_output_path`, [`Error::FileAlreadyExists`] is returned
/// and the file will not be written.
/// Method not available with the sdk feature, use deploy.sign() directly
#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
pub fn sign_deploy_file(
input_path: &str,
secret_key_path: &str,
Expand All @@ -166,6 +162,17 @@ pub fn sign_deploy_file(
crate::sign_deploy_file(input_path, &secret_key, output).map_err(CliError::from)
}

/// Method not available without the std-fs-io feature, use deploy.sign() directly
#[cfg(not(feature = "std-fs-io"))]
pub fn sign_deploy_file(
_input_path: &str,
_secret_key_path: &str,
_maybe_output_path: &str,
_force: bool,
) -> Result<(), CliError> {
Ok(())
}

/// Reads a previously-saved [`Deploy`] from a file and sends it to the network for execution.
///
/// For details of the parameters, see [the module docs](crate::cli#common-parameters).
Expand Down Expand Up @@ -288,7 +295,7 @@ pub async fn speculative_transfer(
/// `force` is true, and a file exists at `maybe_output_path`, it will be overwritten. If `force`
/// is false and a file exists at `maybe_output_path`, [`Error::FileAlreadyExists`] is returned
/// and the file will not be written.
/// With sdk feature file is not written
/// Without the std-fs-io feature, the file is not written
/// Returns the Deploy
pub fn make_transfer(
#[allow(unused_variables)] maybe_output_path: &str,
Expand All @@ -308,7 +315,7 @@ pub fn make_transfer(
payment_params,
true,
)?;
#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
{
let output = parse::output_kind(maybe_output_path, force);
let _ = crate::output_deploy(output, &deploy).map_err(CliError::from);
Expand Down
10 changes: 5 additions & 5 deletions lib/cli/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ fn get_maybe_secret_key(
allow_unsigned_deploy: bool,
) -> Result<Option<SecretKey>, CliError> {
if !secret_key.is_empty() {
#[cfg(feature = "sdk")]
#[cfg(feature = "std-fs-io")]
{
Ok(Some(parse::secret_key_from_file(secret_key)?))
}
#[cfg(not(feature = "std-fs-io"))]
{
let secret_key: SecretKey = match SecretKey::from_pem(secret_key) {
Ok(key) => key,
Expand All @@ -155,10 +159,6 @@ fn get_maybe_secret_key(
};
Ok(Some(secret_key))
}
#[cfg(not(feature = "sdk"))]
{
Ok(Some(parse::secret_key_from_file(secret_key)?))
}
} else if !allow_unsigned_deploy {
Err(CliError::InvalidArgument {
context: "with_payment_and_session (secret_key, allow_unsigned_deploy)",
Expand Down
10 changes: 5 additions & 5 deletions lib/cli/parse.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! This module contains structs and helpers which are used by multiple subcommands related to
//! creating deploys.
#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
use std::path::Path;
use std::{convert::TryInto, fs, io, str::FromStr};

use rand::Rng;
use serde::{self, Deserialize};

use casper_hashing::Digest;
#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
use casper_types::SecretKey;
use casper_types::{
account::AccountHash,
Expand All @@ -19,7 +19,7 @@ use casper_types::{
};

use super::{simple_args, CliError, PaymentStrParams, SessionStrParams};
#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
use crate::OutputKind;
use crate::{
types::{BlockHash, DeployHash, ExecutableDeployItem, TimeDiff, Timestamp},
Expand All @@ -45,7 +45,7 @@ pub(super) fn verbosity(verbosity_level: u64) -> Verbosity {
}
}

#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
pub(super) fn output_kind(maybe_output_path: &str, force: bool) -> OutputKind {
if maybe_output_path.is_empty() {
OutputKind::Stdout
Expand All @@ -54,7 +54,7 @@ pub(super) fn output_kind(maybe_output_path: &str, force: bool) -> OutputKind {
}
}

#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
pub(super) fn secret_key_from_file<P: AsRef<Path>>(
secret_key_path: P,
) -> Result<SecretKey, CliError> {
Expand Down
4 changes: 2 additions & 2 deletions lib/json_rpc/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Call {
None => JsonRpc::request(&self.rpc_id, method),
};

#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
crate::json_pretty_print(&rpc_request, self.verbosity)?;

let client = CLIENT.get_or_init(Client::new);
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Call {
error,
})?;

#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
crate::json_pretty_print(&rpc_response, self.verbosity)?;

let response_kind = match &rpc_response {
Expand Down
10 changes: 6 additions & 4 deletions lib/keygen.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Cryptographic key generation.
use std::{fs, path::Path};

use casper_types::{AsymmetricType, PublicKey, SecretKey};

#[cfg(feature = "std-fs-io")]
use crate::Error;
#[cfg(feature = "std-fs-io")]
use casper_types::{AsymmetricType, PublicKey, SecretKey};
#[cfg(feature = "std-fs-io")]
use std::{fs, path::Path};

/// Default filename for the PEM-encoded secret key file.
pub const SECRET_KEY_PEM: &str = "secret_key.pem";
Expand All @@ -30,6 +31,7 @@ pub const SECP256K1: &str = "secp256k1";
///
/// If `force` is true, existing files will be overwritten. If `force` is false and any of the
/// files exist, [`Error::FileAlreadyExists`] is returned and no files are written.
#[cfg(feature = "std-fs-io")]
pub fn generate_files(output_dir: &str, algorithm: &str, force: bool) -> Result<(), Error> {
if output_dir.is_empty() {
return Err(Error::EmptyKeygenPath);
Expand Down
1 change: 0 additions & 1 deletion lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
pub mod cli;
mod error;
mod json_rpc;
#[cfg(not(any(feature = "sdk")))]
pub mod keygen;
mod output_kind;
pub mod rpcs;
Expand Down
8 changes: 1 addition & 7 deletions lib/rpcs/v1_5_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ pub(crate) mod speculative_exec;

// The following RPCs are all unchanged from v1.4.5, so we just re-export them.

pub(crate) mod get_account {
pub use crate::rpcs::v1_4_5::get_account::GetAccountResult;
}

pub(crate) mod get_auction_info {
pub use crate::rpcs::v1_4_5::get_auction_info::GetAuctionInfoResult;
pub(crate) use crate::rpcs::v1_4_5::get_auction_info::{
Expand Down Expand Up @@ -85,7 +81,5 @@ pub(crate) mod put_deploy {
}

pub(crate) mod query_global_state {
pub use crate::rpcs::v1_4_5::query_global_state::{
GlobalStateIdentifier, QueryGlobalStateResult,
};
pub use crate::rpcs::v1_4_5::query_global_state::GlobalStateIdentifier;
}
4 changes: 1 addition & 3 deletions lib/rpcs/v1_6_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ pub(crate) mod get_block_transfers {
}

pub(crate) mod get_dictionary_item {
pub use crate::rpcs::v1_5_0::get_dictionary_item::{
DictionaryItemIdentifier, GetDictionaryItemResult,
};
pub use crate::rpcs::v1_5_0::get_dictionary_item::GetDictionaryItemResult;
pub(crate) use crate::rpcs::v1_5_0::get_dictionary_item::{
GetDictionaryItemParams, GET_DICTIONARY_ITEM_METHOD,
};
Expand Down
4 changes: 2 additions & 2 deletions lib/types/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ impl<'a> DeployBuilder<'a> {
/// [`with_payment`](Self::with_payment)
pub fn new<C: Into<String>>(chain_name: C, session: ExecutableDeployItem) -> Self {
// Timestamp::now() not available with feature sdk, use default() instead
#[cfg(not(any(feature = "sdk")))]
#[cfg(feature = "std-fs-io")]
let timestamp = Timestamp::now();
#[cfg(feature = "sdk")]
#[cfg(not(feature = "std-fs-io"))]
let timestamp = Timestamp::default();

DeployBuilder {
Expand Down
29 changes: 27 additions & 2 deletions src/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,37 @@ impl ClientCommand for Keygen {
.arg(algorithm::arg())
}

/// Asynchronously runs the keygen command based on the provided command-line arguments.
///
/// # Arguments
///
/// * `matches` - A reference to the `ArgMatches` containing the parsed command-line arguments.
///
/// # Returns
///
/// Returns a `Result` indicating the success or failure of the command execution.
/// If successful, returns `Success::Output` with a message indicating the outcome.
/// If unsuccessful, returns a `CliError` with details about the error.
async fn run(matches: &ArgMatches) -> Result<Success, CliError> {
// Retrieve the output directory, key algorithm, and force flag from command-line arguments
let output_dir = output_dir::get(matches);
let algorithm = algorithm::get(matches);
let force = common::force::get(matches);

keygen::generate_files(&output_dir, algorithm, force)?;
Ok(Success::Output(format!("Wrote files to {}", output_dir)))
// Check if the std-fs-io feature is enabled
#[cfg(feature = "std-fs-io")]
{
// If std-fs-io feature is enabled, generate key files and return success message
keygen::generate_files(&output_dir, algorithm, force)?;
Ok(Success::Output(format!("Wrote files to {}", output_dir)))
}

// If std-fs-io feature is not enabled, return an error message
#[cfg(not(feature = "std-fs-io"))]
{
Ok(Success::Output(format!(
"keygen not available without std-fs-io feature"
)))
}
}
}

0 comments on commit 4b27b19

Please sign in to comment.