diff --git a/bin/reth/src/args/rpc_server_args.rs b/bin/reth/src/args/rpc_server_args.rs index 0a1ec745cb4a..32005b1638f2 100644 --- a/bin/reth/src/args/rpc_server_args.rs +++ b/bin/reth/src/args/rpc_server_args.rs @@ -10,6 +10,7 @@ use crate::{ config::RethRpcConfig, ext::RethNodeCommandConfig, }, + utils::get_or_create_jwt_secret_from_path, }; use clap::{ builder::{PossibleValue, RangedU64ValueParser, TypedValueParser}, @@ -449,15 +450,7 @@ impl RethRpcConfig for RpcServerArgs { debug!(target: "reth::cli", user_path=?fpath, "Reading JWT auth secret file"); JwtSecret::from_file(fpath) } - None => { - if default_jwt_path.exists() { - debug!(target: "reth::cli", ?default_jwt_path, "Reading JWT auth secret file"); - JwtSecret::from_file(&default_jwt_path) - } else { - info!(target: "reth::cli", ?default_jwt_path, "Creating JWT auth secret file"); - JwtSecret::try_create(&default_jwt_path) - } - } + None => get_or_create_jwt_secret_from_path(&default_jwt_path), } } diff --git a/bin/reth/src/utils.rs b/bin/reth/src/utils.rs index e2c74384e4ed..6994fd81e8c1 100644 --- a/bin/reth/src/utils.rs +++ b/bin/reth/src/utils.rs @@ -18,13 +18,14 @@ use reth_interfaces::p2p::{ use reth_primitives::{ fs, BlockHashOrNumber, ChainSpec, HeadersDirection, SealedBlock, SealedHeader, }; +use reth_rpc::{JwtError, JwtSecret}; use std::{ env::VarError, path::{Path, PathBuf}, rc::Rc, sync::Arc, }; -use tracing::info; +use tracing::{debug, info}; /// Exposing `open_db_read_only` function pub mod db { @@ -247,3 +248,14 @@ impl ListFilter { self.len = len; } } +/// Attempts to retrieve or create a JWT secret from the specified path. + +pub fn get_or_create_jwt_secret_from_path(path: &Path) -> Result { + if path.exists() { + debug!(target: "reth::cli", ?path, "Reading JWT auth secret file"); + JwtSecret::from_file(path) + } else { + info!(target: "reth::cli", ?path, "Creating JWT auth secret file"); + JwtSecret::try_create(path) + } +}