Skip to content

Commit

Permalink
feat: validate indexer address config variable
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-eiger committed Dec 8, 2023
1 parent 91758b2 commit 6ba258b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions subgraph-radio/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::value_parser;
use clap::{command, Args, Parser};
use derive_getters::Getters;
use ethers::signers::WalletError;
use graphcast_sdk::Account;
use graphcast_sdk::{
build_wallet,
callbook::CallBook,
Expand All @@ -28,6 +29,22 @@ pub enum CoverageLevel {
Comprehensive,
}

fn is_valid_ethereum_address(address: &str) -> bool {
address.len() == 42
&& address.starts_with("0x")
&& address[2..].chars().all(|c| c.is_ascii_hexdigit())
}

fn parse_indexer_address(value: &str) -> Result<String, ConfigError> {
if !is_valid_ethereum_address(value) {
return Err(ConfigError::ValidateInput(
"Invalid Ethereum address".to_string(),
));
}

Ok(value.to_string())
}

#[derive(Clone, Debug, Parser, Serialize, Deserialize, Getters, Default)]
#[clap(
name = "subgraph-radio",
Expand Down Expand Up @@ -67,11 +84,41 @@ impl Config {
};

std::env::set_var("RUST_LOG", config.radio_setup().log_level.clone());

// Enables tracing under RUST_LOG variable
init_tracing(config.radio_setup().log_format.to_string()).expect("Could not set up global default subscriber for logger, check environmental variable `RUST_LOG` or the CLI input `log-level`");
config
}

pub async fn validate_indexer_address(&self) {
let input = self.wallet_input().unwrap();
let wallet = build_wallet(input).unwrap();
let agent = wallet_address(&wallet);

let account = Account::new(
agent.to_ascii_lowercase(),
self.graph_stack()
.indexer_address()
.to_string()
.to_ascii_lowercase(),
);

let verified = account
.verify(
self.graph_stack().network_subgraph(),
self.graph_stack().registry_subgraph(),
&IdentityValidation::Indexer,
)
.await;

if verified.is_err() {
panic!(
"Indexer address validation failed: {:?}",
verified.unwrap_err()
);
}
}

/// Validate that private key as an Eth wallet
fn parse_key(value: &str) -> Result<String, WalletError> {
// The wallet can be stored instead of the original private key
Expand Down Expand Up @@ -220,6 +267,7 @@ pub struct GraphStack {
pub graph_node_status_endpoint: String,
#[clap(
long,
value_parser = parse_indexer_address,
value_name = "INDEXER_ADDRESS",
env = "INDEXER_ADDRESS",
help = "Graph account corresponding to Graphcast operator"
Expand Down
2 changes: 2 additions & 0 deletions subgraph-radio/src/operator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ impl RadioOperator {
debug!("Set global static instance of graphcast_agent");
_ = GRAPHCAST_AGENT.set(graphcast_agent.clone());

config.validate_indexer_address().await;

//TODO: Refactor indexer management server validation to SDK, similar to graph node status endpoint
if let Some(url) = &config.graph_stack.indexer_management_server_endpoint {
_ = health_query(url)
Expand Down

0 comments on commit 6ba258b

Please sign in to comment.