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 12, 2023
1 parent 9ba02ac commit 74db359
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 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 @@ -72,6 +89,35 @@ impl Config {
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 +266,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 74db359

Please sign in to comment.