Skip to content

Commit d38ee59

Browse files
authored
refactor(l2): add flag for setting sponsor private key (#2281)
**Motivation** <!-- Why does this pull request exist? What are its goals? --> The current implementation requires a `.env` file to exist and the execution panics if this file does not exist. Nevertheless, this has a purpose of being. As this feature should be used in `l2` it is assumed that there's a `.env` file and that is ok because it should. This PR intends to add a second path for setting the sponsor pk without needing a `.env`. **Description** Add a flag `--sponsor-private-key` as a second option for setting this value.
1 parent 652ffd3 commit d38ee59

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

cmd/ethrex/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ hex.workspace = true
2020
tracing.workspace = true
2121
tracing-subscriber.workspace = true
2222
k256.workspace = true
23-
clap = { version = "4.3", features = ["derive"] }
23+
clap = { version = "4.3", features = ["derive", "env"] }
2424
clap_complete = "4.5.17"
2525
eyre = "0.6.12"
2626
directories = "5.0.1"

cmd/ethrex/cli.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use crate::{
1414
DEFAULT_DATADIR,
1515
};
1616

17+
#[cfg(feature = "l2")]
18+
use secp256k1::SecretKey;
19+
1720
pub const VERSION_STRING: &str = env!("CARGO_PKG_VERSION");
1821

1922
#[allow(clippy::upper_case_acronyms)]
@@ -180,6 +183,7 @@ impl Default for Options {
180183
}
181184
}
182185

186+
#[cfg(feature = "l2")]
183187
#[derive(ClapParser)]
184188
pub struct L2Options {
185189
#[arg(
@@ -189,6 +193,8 @@ pub struct L2Options {
189193
help_heading = "L2 options"
190194
)]
191195
pub sponsorable_addresses_file_path: Option<String>,
196+
#[arg(long, value_parser = utils::parse_private_key, env = "SPONSOR_PRIVATE_KEY", help = "The private key of ethrex L2 transactions sponsor.", help_heading = "L2 options")]
197+
pub sponsor_private_key: Option<SecretKey>,
192198
}
193199

194200
#[cfg(feature = "based")]

cmd/ethrex/initializers.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn init_rpc_api(
127127
#[cfg(feature = "l2")]
128128
get_valid_delegation_addresses(l2_opts),
129129
#[cfg(feature = "l2")]
130-
get_sponsor_pk(),
130+
get_sponsor_pk(l2_opts),
131131
)
132132
.into_future();
133133

@@ -360,7 +360,13 @@ pub fn get_valid_delegation_addresses(l2_opts: &L2Options) -> Vec<Address> {
360360
}
361361

362362
#[cfg(feature = "l2")]
363-
pub fn get_sponsor_pk() -> SecretKey {
363+
pub fn get_sponsor_pk(opts: &L2Options) -> SecretKey {
364+
if let Some(pk) = opts.sponsor_private_key {
365+
return pk;
366+
}
367+
368+
warn!("Sponsor private key not provided. Trying to read from the .env file.");
369+
364370
if let Err(e) = read_env_file() {
365371
panic!("Failed to read .env file: {e}");
366372
}

cmd/ethrex/utils.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ use ethrex_common::types::{Block, Genesis};
55
use ethrex_p2p::{kademlia::KademliaTable, sync::SyncMode, types::Node};
66
use ethrex_rlp::decode::RLPDecode;
77
use ethrex_vm::backends::EvmEngine;
8+
use hex::FromHexError;
9+
#[cfg(feature = "l2")]
10+
use secp256k1::SecretKey;
811
use std::{
912
fs::File,
1013
io,
@@ -118,3 +121,15 @@ pub fn read_known_peers(file_path: PathBuf) -> Result<Vec<Node>, serde_json::Err
118121

119122
serde_json::from_reader(file)
120123
}
124+
125+
#[cfg(feature = "l2")]
126+
pub fn parse_private_key(s: &str) -> eyre::Result<SecretKey> {
127+
Ok(SecretKey::from_slice(&parse_hex(s)?)?)
128+
}
129+
130+
pub fn parse_hex(s: &str) -> eyre::Result<Bytes, FromHexError> {
131+
match s.strip_prefix("0x") {
132+
Some(s) => hex::decode(s).map(Into::into),
133+
None => hex::decode(s).map(Into::into),
134+
}
135+
}

0 commit comments

Comments
 (0)