From 635d4754e073e070a3b705a3f538c333fb212aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Tue, 23 Jul 2024 15:29:01 +0200 Subject: [PATCH 1/4] Allow to provide custom deposit WASM path to CLI. --- kairos-cli/src/client.rs | 11 +--------- kairos-cli/src/commands/deposit.rs | 32 +++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/kairos-cli/src/client.rs b/kairos-cli/src/client.rs index 3c0d5f3c..6d723279 100644 --- a/kairos-cli/src/client.rs +++ b/kairos-cli/src/client.rs @@ -8,8 +8,6 @@ use kairos_server::PublicKey; use reqwest::Url; use serde::{Deserialize, Serialize}; use std::fmt; -use std::fs; -use std::path::Path; // max amount allowed to be used on gas fees pub const MAX_GAS_FEE_PAYMENT_AMOUNT: u64 = 1_000_000_000_000; @@ -49,19 +47,12 @@ impl From for KairosClientError { pub fn deposit( base_url: &Url, + deposit_session_wasm_bytes: &[u8], depositor_secret_key: &SecretKey, contract_hash: &ContractHash, amount: impl Into, recipient: casper_client_types::PublicKey, ) -> Result { - let deposit_session_wasm_path = - Path::new(env!("PATH_TO_SESSION_BINARIES")).join("deposit-session-optimized.wasm"); - let deposit_session_wasm_bytes = fs::read(&deposit_session_wasm_path).unwrap_or_else(|err| { - panic!( - "Failed to read the deposit session wasm as bytes from file: {:?}.\n{}", - deposit_session_wasm_path, err - ) - }); let deposit_session = ExecutableDeployItem::new_module_bytes( deposit_session_wasm_bytes.into(), runtime_args! { diff --git a/kairos-cli/src/commands/deposit.rs b/kairos-cli/src/commands/deposit.rs index cd72f23b..02a07029 100644 --- a/kairos-cli/src/commands/deposit.rs +++ b/kairos-cli/src/commands/deposit.rs @@ -1,9 +1,12 @@ +use std::fs; +use std::path::PathBuf; + use crate::client; use crate::common::args::{AmountArg, ContractHashArg, PrivateKeyPathArg, RecipientArg}; use crate::error::CliError; use casper_client_types::{crypto::SecretKey, ContractHash}; -use clap::Parser; +use clap::{Args as ClapArgs, Parser}; use hex::FromHex; use reqwest::Url; @@ -19,6 +22,8 @@ pub struct Args { contract_hash: ContractHashArg, #[clap(flatten)] recipient: RecipientArg, + #[clap(flatten)] + session_path: SessionPathArg, } pub fn run(args: Args, kairos_server_address: Url) -> Result { @@ -38,8 +43,21 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result { None => client::contract_hash(&kairos_server_address)?, }; + let deposit_session_wasm: Vec = match args.session_path.field { + Some(deposit_session_wasm_path) => { + fs::read(&deposit_session_wasm_path).unwrap_or_else(|err| { + panic!( + "Failed to read the deposit session wasm as bytes from file: {:?}.\n{}", + deposit_session_wasm_path, err + ) + }) + } + None => panic!("No deposit session available!"), + }; + client::deposit( &kairos_server_address, + &deposit_session_wasm, &depositor_secret_key, &contract_hash, amount, @@ -59,3 +77,15 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result { output }) } + +#[derive(ClapArgs, Debug)] +pub struct SessionPathArg { + #[arg( + id = "session-path", + long, + short = 's', + value_name = "PATH", + help = "Path to the custom WASM session code for deposit" + )] + pub field: Option, +} From 7098d87b0713ebbc75acfaa07feda8daf35251a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Tue, 23 Jul 2024 15:30:15 +0200 Subject: [PATCH 2/4] Use hardcoded env to provide built-in session code. --- kairos-cli/build.rs | 20 ++++++++++++++++++++ kairos-cli/src/commands/deposit.rs | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 kairos-cli/build.rs diff --git a/kairos-cli/build.rs b/kairos-cli/build.rs new file mode 100644 index 00000000..64c6a5fb --- /dev/null +++ b/kairos-cli/build.rs @@ -0,0 +1,20 @@ +use std::env; +use std::fs; +use std::path::Path; + +fn main() { + // Path + let session_binaries_dir = env::var("PATH_TO_SESSION_BINARIES") + .expect("PATH_TO_SESSION_BINARIES environment variable is not set"); + + // Get the output directory set by Cargo. + let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set"); + let source_path = Path::new(&session_binaries_dir).join("deposit-session-optimized.wasm"); + let dest_path = Path::new(&out_dir).join("deposit-session-optimized.wasm"); + + // Copy the file from the source to the destination + fs::copy(&source_path, &dest_path).expect("Failed to copy WASM file"); + + // Print out a message to re-run this script if the source file changes. + println!("cargo:rerun-if-changed={}", source_path.display()); +} diff --git a/kairos-cli/src/commands/deposit.rs b/kairos-cli/src/commands/deposit.rs index 02a07029..c7bbd894 100644 --- a/kairos-cli/src/commands/deposit.rs +++ b/kairos-cli/src/commands/deposit.rs @@ -12,6 +12,9 @@ use reqwest::Url; use kairos_crypto::error::CryptoError; +const DEFAULT_DEPOSIT_SESSION_WASM: &[u8] = + include_bytes!(concat!(env!("OUT_DIR"), "/deposit-session-optimized.wasm")); + #[derive(Parser, Debug)] pub struct Args { #[clap(flatten)] @@ -52,7 +55,7 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result { ) }) } - None => panic!("No deposit session available!"), + None => DEFAULT_DEPOSIT_SESSION_WASM.to_vec(), }; client::deposit( From 652999245477cf8be21121e2b91d8354a94f1419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Tue, 23 Jul 2024 15:44:20 +0200 Subject: [PATCH 3/4] Fix clippy error. --- kairos-cli/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kairos-cli/build.rs b/kairos-cli/build.rs index 64c6a5fb..3bce4324 100644 --- a/kairos-cli/build.rs +++ b/kairos-cli/build.rs @@ -13,7 +13,7 @@ fn main() { let dest_path = Path::new(&out_dir).join("deposit-session-optimized.wasm"); // Copy the file from the source to the destination - fs::copy(&source_path, &dest_path).expect("Failed to copy WASM file"); + fs::copy(&source_path, dest_path).expect("Failed to copy WASM file"); // Print out a message to re-run this script if the source file changes. println!("cargo:rerun-if-changed={}", source_path.display()); From 8c68f90280831032dc81a25fe3f60c6145ba1d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Thu, 25 Jul 2024 12:05:47 +0200 Subject: [PATCH 4/4] kairos-cli: make short args distinct, fixes #166 --- kairos-cli/src/commands/fetch.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kairos-cli/src/commands/fetch.rs b/kairos-cli/src/commands/fetch.rs index 9d146c48..25cd6108 100644 --- a/kairos-cli/src/commands/fetch.rs +++ b/kairos-cli/src/commands/fetch.rs @@ -11,17 +11,17 @@ use chrono::NaiveDateTime; pub struct Args { #[arg(long, short, value_name = "PUBLIC_KEY_HEX")] sender: Option, - #[arg(long, short, value_name = "ISO8601_TIMESTAMP")] + #[arg(long, short = 't', value_name = "ISO8601_TIMESTAMP")] min_timestamp: Option, - #[arg(long, short, value_name = "ISO8601_TIMESTAMP")] + #[arg(long, short = 'T', value_name = "ISO8601_TIMESTAMP")] max_timestamp: Option, - #[arg(long, short, value_name = "NUM_MOTES")] + #[arg(long, short = 'm', value_name = "NUM_MOTES")] min_amount: Option, - #[arg(long, short, value_name = "NUM_MOTES")] + #[arg(long, short = 'M', value_name = "NUM_MOTES")] max_amount: Option, #[arg(long, short, value_name = "PUBLIC_KEY_HEX")] recipient: Option, - #[arg(long, short, value_name = "TRANSACTION_TYPE", value_enum)] + #[arg(long, short = 'x', value_name = "TRANSACTION_TYPE", value_enum)] transaction_type: Option, }