Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSPR.cloud auth token support. #474

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Changelog

Changelog for `odra`.
## [1.0.0] - 2024-05-xx
## [1.1.0] - 2024-06-XX
### Added
- Support for CSPR.cloud's auth token.

## [1.0.0] - 2024-05-23
### Added
- Add `try_deploy` function to the `Deployer` trait, which allows deploying a contract and returning an error if the deployment fails.
- Add `Cep18` to `odra-modules` - a standard for fungible tokens.
Expand Down
6 changes: 6 additions & 0 deletions examples/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# ODRA_CASPER_LIVENET_SECRET_KEY_PATH=<path to secret_key.pem>

# RPC address of the node that will be used to deploy the contracts.
# For CSPR.cloud, you can use the following address:
# - https://node.cspr.cloud
# - https://node.testnet.cspr.cloud
# ODRA_CASPER_LIVENET_NODE_ADDRESS=http://localhost:7777

# Chain name of the network. Sample values:
Expand All @@ -18,3 +21,6 @@
# Paths to the secret keys of the additional acccounts. Main secret key will be 0th account.
# ODRA_CASPER_LIVENET_KEY_1=./keys/secret_key_1.pem
# ODRA_CASPER_LIVENET_KEY_2=./keys/secret_key_2.pem

# If using CSPR.cloud, you can set the auth token here.
# CSPR_CLOUD_AUTH_TOKEN=
16 changes: 15 additions & 1 deletion odra-casper/rpc-client/src/casper_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub const ENV_NODE_ADDRESS: &str = "ODRA_CASPER_LIVENET_NODE_ADDRESS";
pub const ENV_CHAIN_NAME: &str = "ODRA_CASPER_LIVENET_CHAIN_NAME";
/// Environment variable holding a filename prefix for additional accounts.
pub const ENV_ACCOUNT_PREFIX: &str = "ODRA_CASPER_LIVENET_KEY_";
/// Environment variable holding cspr.cloud auth token.
pub const ENV_CSPR_CLOUD_AUTH_TOKEN: &str = "CSPR_CLOUD_AUTH_TOKEN";
Comment on lines +47 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add documentation for ENV_CSPR_CLOUD_AUTH_TOKEN.

It's good practice to document the purpose and usage of each environment variable, especially for new additions like ENV_CSPR_CLOUD_AUTH_TOKEN. This helps maintain clarity and usability of the code.


fn get_env_variable(name: &str) -> String {
std::env::var(name).unwrap_or_else(|err| {
Expand All @@ -55,10 +57,15 @@ fn get_env_variable(name: &str) -> String {
})
}

fn get_optional_env_variable(name: &str) -> Option<String> {
std::env::var(name).ok()
}

/// Client for interacting with Casper node.
pub struct CasperClient {
node_address: String,
chain_name: String,
cspr_cloud_auth_token: Option<String>,
active_account: usize,
secret_keys: Vec<SecretKey>,
gas: U512
Expand All @@ -83,6 +90,7 @@ impl CasperClient {
CasperClient {
node_address: get_env_variable(ENV_NODE_ADDRESS),
chain_name: get_env_variable(ENV_CHAIN_NAME),
cspr_cloud_auth_token: get_optional_env_variable(ENV_CSPR_CLOUD_AUTH_TOKEN),
active_account: 0,
secret_keys,
gas: U512::zero()
Expand Down Expand Up @@ -735,7 +743,13 @@ impl CasperClient {

fn post_request<T: DeserializeOwned>(&self, request: Value) -> T {
let client = reqwest::blocking::Client::new();
let response = client.post(self.node_address_rpc()).json(&request).send();

let mut client = client.post(self.node_address_rpc());
if let Some(token) = &self.cspr_cloud_auth_token {
client = client.header("Authorization", token);
}
let response = client.json(&request).send();

let response = match response {
Ok(r) => r,
Err(e) => {
Expand Down
Loading