diff --git a/kairos-cli/src/client.rs b/kairos-cli/src/client.rs index 6d723279..cdb65955 100644 --- a/kairos-cli/src/client.rs +++ b/kairos-cli/src/client.rs @@ -3,6 +3,7 @@ use casper_client::types::{DeployBuilder, DeployHash, ExecutableDeployItem, Time use casper_client_types::{crypto::SecretKey, runtime_args, ContractHash, RuntimeArgs, U512}; use kairos_server::routes::contract_hash::ContractHashPath; use kairos_server::routes::deposit::DepositPath; +use kairos_server::routes::get_chain_name::GetChainNamePath; use kairos_server::routes::get_nonce::GetNoncePath; use kairos_server::PublicKey; use reqwest::Url; @@ -49,6 +50,7 @@ pub fn deposit( base_url: &Url, deposit_session_wasm_bytes: &[u8], depositor_secret_key: &SecretKey, + chain_name: &str, contract_hash: &ContractHash, amount: impl Into, recipient: casper_client_types::PublicKey, @@ -61,16 +63,12 @@ pub fn deposit( "recipient" => recipient }, ); - let deploy = DeployBuilder::new( - env!("CASPER_CHAIN_NAME"), - deposit_session, - depositor_secret_key, - ) - .with_standard_payment(MAX_GAS_FEE_PAYMENT_AMOUNT) // max amount allowed to be used on gas fees - .with_timestamp(Timestamp::now()) - .with_ttl(TimeDiff::from_millis(60_000)) // 1 min - .build() - .map_err(|err| KairosClientError::CasperClientError(err.to_string()))?; + let deploy = DeployBuilder::new(chain_name, deposit_session, depositor_secret_key) + .with_standard_payment(MAX_GAS_FEE_PAYMENT_AMOUNT) // max amount allowed to be used on gas fees + .with_timestamp(Timestamp::now()) + .with_ttl(TimeDiff::from_millis(60_000)) // 1 min + .build() + .map_err(|err| KairosClientError::CasperClientError(err.to_string()))?; let response = reqwest::blocking::Client::new() .post(base_url.join(DepositPath::PATH).unwrap()) @@ -122,3 +120,16 @@ pub fn contract_hash(base_url: &Url) -> Result .map_err(KairosClientError::from) } } + +pub fn get_chain_name(base_url: &Url) -> Result { + let response = reqwest::blocking::Client::new() + .get(base_url.join(GetChainNamePath::PATH).unwrap()) + .send() + .map_err(KairosClientError::from)? + .error_for_status(); + + match response { + Err(err) => Err(KairosClientError::from(err)), + Ok(response) => response.json::().map_err(KairosClientError::from), + } +} diff --git a/kairos-cli/src/commands/deposit.rs b/kairos-cli/src/commands/deposit.rs index c7bbd894..8fd01cd8 100644 --- a/kairos-cli/src/commands/deposit.rs +++ b/kairos-cli/src/commands/deposit.rs @@ -2,7 +2,9 @@ use std::fs; use std::path::PathBuf; use crate::client; -use crate::common::args::{AmountArg, ContractHashArg, PrivateKeyPathArg, RecipientArg}; +use crate::common::args::{ + AmountArg, ChainNameArg, ContractHashArg, PrivateKeyPathArg, RecipientArg, +}; use crate::error::CliError; use casper_client_types::{crypto::SecretKey, ContractHash}; @@ -27,6 +29,8 @@ pub struct Args { recipient: RecipientArg, #[clap(flatten)] session_path: SessionPathArg, + #[clap(flatten)] + chain_name: ChainNameArg, } pub fn run(args: Args, kairos_server_address: Url) -> Result { @@ -45,6 +49,10 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result { } None => client::contract_hash(&kairos_server_address)?, }; + let chain_name = match args.chain_name.field { + None => client::get_chain_name(&kairos_server_address)?, + Some(name) => name, + }; let deposit_session_wasm: Vec = match args.session_path.field { Some(deposit_session_wasm_path) => { @@ -62,6 +70,7 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result { &kairos_server_address, &deposit_session_wasm, &depositor_secret_key, + &chain_name, &contract_hash, amount, args.recipient.try_into()?, diff --git a/kairos-cli/src/common/args.rs b/kairos-cli/src/common/args.rs index 50cc5bf5..1ec0487f 100644 --- a/kairos-cli/src/common/args.rs +++ b/kairos-cli/src/common/args.rs @@ -49,3 +49,14 @@ impl TryFrom for casper_client_types::PublicKey { Ok(pk) } } + +#[derive(Args, Debug)] +pub struct ChainNameArg { + #[arg( + id = "chain-name", + long, + value_name = "NAME", + help = "Name of the chain, to avoid the deploy from being accidentally included in a different chain" + )] + pub field: Option, +} diff --git a/kairos-server/src/lib.rs b/kairos-server/src/lib.rs index a441bf51..506ad1b4 100644 --- a/kairos-server/src/lib.rs +++ b/kairos-server/src/lib.rs @@ -31,6 +31,7 @@ pub fn app_router(state: ServerState) -> Router { .typed_post(routes::deposit_handler) .typed_post(routes::withdraw_handler) .typed_post(routes::transfer_handler) + .typed_get(routes::get_chain_name_handler) .typed_post(routes::get_nonce_handler) .typed_get(routes::contract_hash_handler); #[cfg(feature = "deposit-mock")] diff --git a/kairos-server/src/routes/get_chain_name.rs b/kairos-server/src/routes/get_chain_name.rs new file mode 100644 index 00000000..e8c9e07a --- /dev/null +++ b/kairos-server/src/routes/get_chain_name.rs @@ -0,0 +1,18 @@ +use axum::{extract::State, Json}; +use axum_extra::routing::TypedPath; +use tracing::*; + +use crate::{state::ServerState, AppErr}; + +#[derive(TypedPath, Debug, Clone, Copy)] +#[typed_path("/api/v1/chain_name")] +pub struct GetChainNamePath; + +#[instrument(level = "trace", skip(_state), ret)] +pub async fn get_chain_name_handler( + _: GetChainNamePath, + _state: State, +) -> Result, AppErr> { + let chain_name = env!("CASPER_CHAIN_NAME"); // NOTE: This should be obtained from RPC, rather than from hardcoded value. + Ok(Json(String::from(chain_name))) +} diff --git a/kairos-server/src/routes/mod.rs b/kairos-server/src/routes/mod.rs index c26becf6..eeccbd6b 100644 --- a/kairos-server/src/routes/mod.rs +++ b/kairos-server/src/routes/mod.rs @@ -2,6 +2,7 @@ pub mod contract_hash; pub mod deposit; #[cfg(feature = "deposit-mock")] pub mod deposit_mock; +pub mod get_chain_name; pub mod get_nonce; pub mod transfer; pub mod withdraw; @@ -14,6 +15,7 @@ pub use deposit::deposit_handler; pub use deposit_mock::deposit_mock_handler; #[cfg(feature = "database")] pub use fetch::query_transactions_handler; +pub use get_chain_name::get_chain_name_handler; pub use get_nonce::get_nonce_handler; pub use transfer::transfer_handler; pub use withdraw::withdraw_handler;