Skip to content

Commit 988d606

Browse files
authored
CBST2-10: Improve error handling (#298)
1 parent ea48da6 commit 988d606

File tree

2 files changed

+27
-39
lines changed

2 files changed

+27
-39
lines changed

crates/common/src/signer/loader.rs

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ impl SignerLoader {
5757
pub fn load_from_env(self) -> eyre::Result<Vec<ConsensusSigner>> {
5858
Ok(match self {
5959
SignerLoader::File { key_path } => {
60-
let path = load_env_var(SIGNER_KEYS_ENV).unwrap_or(
61-
key_path.to_str().ok_or_eyre("Missing signer key path")?.to_string(),
62-
);
60+
let path = load_env_var(SIGNER_KEYS_ENV).map(PathBuf::from).unwrap_or(key_path);
6361
let file = std::fs::read_to_string(path)
6462
.unwrap_or_else(|_| panic!("Unable to find keys file"));
6563

@@ -73,12 +71,10 @@ impl SignerLoader {
7371
SignerLoader::ValidatorsDir { keys_path, secrets_path, format } => {
7472
// TODO: hacky way to load for now, we should support reading the
7573
// definitions.yml file
76-
let keys_path = load_env_var(SIGNER_DIR_KEYS_ENV).unwrap_or(
77-
keys_path.to_str().ok_or_eyre("Missing signer keys path")?.to_string(),
78-
);
79-
let secrets_path = load_env_var(SIGNER_DIR_SECRETS_ENV).unwrap_or(
80-
secrets_path.to_str().ok_or_eyre("Missing signer secrets path")?.to_string(),
81-
);
74+
let keys_path =
75+
load_env_var(SIGNER_DIR_KEYS_ENV).map(PathBuf::from).unwrap_or(keys_path);
76+
let secrets_path =
77+
load_env_var(SIGNER_DIR_SECRETS_ENV).map(PathBuf::from).unwrap_or(secrets_path);
8278

8379
return match format {
8480
ValidatorKeysFormat::Lighthouse => {
@@ -114,8 +110,8 @@ impl<'de> Deserialize<'de> for FileKey {
114110
}
115111

116112
fn load_from_lighthouse_format(
117-
keys_path: String,
118-
secrets_path: String,
113+
keys_path: PathBuf,
114+
secrets_path: PathBuf,
119115
) -> eyre::Result<Vec<ConsensusSigner>> {
120116
let entries = fs::read_dir(keys_path.clone())?;
121117

@@ -129,8 +125,8 @@ fn load_from_lighthouse_format(
129125
if path.is_dir() {
130126
if let Some(maybe_pubkey) = path.file_name().and_then(|d| d.to_str()) {
131127
if let Ok(pubkey) = BlsPublicKey::from_hex(maybe_pubkey) {
132-
let ks_path = format!("{}/{}/voting-keystore.json", keys_path, maybe_pubkey);
133-
let pw_path = format!("{}/{}", secrets_path, pubkey);
128+
let ks_path = keys_path.join(maybe_pubkey).join("voting-keystore.json");
129+
let pw_path = secrets_path.join(pubkey.to_string());
134130

135131
match load_one(ks_path, pw_path) {
136132
Ok(signer) => signers.push(signer),
@@ -147,8 +143,8 @@ fn load_from_lighthouse_format(
147143
}
148144

149145
fn load_from_teku_format(
150-
keys_path: String,
151-
secrets_path: String,
146+
keys_path: PathBuf,
147+
secrets_path: PathBuf,
152148
) -> eyre::Result<Vec<ConsensusSigner>> {
153149
let entries = fs::read_dir(keys_path.clone())?;
154150
let mut signers = Vec::new();
@@ -171,8 +167,8 @@ fn load_from_teku_format(
171167
.0;
172168

173169
match load_one(
174-
format!("{keys_path}/{file_name}.json"),
175-
format!("{secrets_path}/{file_name}.txt"),
170+
keys_path.join(format!("{file_name}.json")),
171+
secrets_path.join(format!("{file_name}.txt")),
176172
) {
177173
Ok(signer) => signers.push(signer),
178174
Err(e) => warn!("Sign load error: {e}"),
@@ -183,8 +179,8 @@ fn load_from_teku_format(
183179
}
184180

185181
fn load_from_lodestar_format(
186-
keys_path: String,
187-
password_path: String,
182+
keys_path: PathBuf,
183+
password_path: PathBuf,
188184
) -> eyre::Result<Vec<ConsensusSigner>> {
189185
let entries = fs::read_dir(keys_path)?;
190186
let mut signers = Vec::new();
@@ -198,15 +194,7 @@ fn load_from_lodestar_format(
198194
continue;
199195
}
200196

201-
let key_path = match path.as_os_str().to_str() {
202-
Some(key_path) => key_path,
203-
None => {
204-
warn!("Path {path:?} cannot be converted to string");
205-
continue;
206-
}
207-
};
208-
209-
match load_one(key_path.to_string(), password_path.clone()) {
197+
match load_one(path, password_path.clone()) {
210198
Ok(signer) => signers.push(signer),
211199
Err(e) => warn!("Sign load error: {e}"),
212200
}
@@ -233,8 +221,8 @@ fn load_from_lodestar_format(
233221
/// }
234222
/// ```
235223
fn load_from_prysm_format(
236-
accounts_path: String,
237-
password_path: String,
224+
accounts_path: PathBuf,
225+
password_path: PathBuf,
238226
) -> eyre::Result<Vec<ConsensusSigner>> {
239227
let accounts_file = File::open(accounts_path)?;
240228
let accounts_reader = BufReader::new(accounts_file);
@@ -281,25 +269,26 @@ fn load_from_prysm_format(
281269
Ok(signers)
282270
}
283271

284-
fn load_one(ks_path: String, pw_path: String) -> eyre::Result<ConsensusSigner> {
272+
fn load_one(ks_path: PathBuf, pw_path: PathBuf) -> eyre::Result<ConsensusSigner> {
285273
let keystore = Keystore::from_json_file(ks_path).map_err(|_| eyre!("failed reading json"))?;
286-
let password =
287-
fs::read(pw_path.clone()).map_err(|e| eyre!("Failed to read password ({pw_path}): {e}"))?;
274+
let password = fs::read(pw_path.clone())
275+
.map_err(|e| eyre!("Failed to read password ({}): {e}", pw_path.display()))?;
288276
let key =
289277
keystore.decrypt_keypair(&password).map_err(|_| eyre!("failed decrypting keypair"))?;
290278
ConsensusSigner::new_from_bytes(key.sk.serialize().as_bytes())
291279
}
292280

293281
pub fn load_bls_signer(keys_path: PathBuf, secrets_path: PathBuf) -> eyre::Result<BlsSigner> {
294-
load_one(keys_path.to_string_lossy().to_string(), secrets_path.to_string_lossy().to_string())
282+
load_one(keys_path, secrets_path)
295283
}
296284

297285
pub fn load_ecdsa_signer(keys_path: PathBuf, secrets_path: PathBuf) -> eyre::Result<EcdsaSigner> {
298-
let key_file = std::fs::File::open(keys_path.to_string_lossy().to_string())?;
286+
let key_file = std::fs::File::open(keys_path)?;
299287
let key_reader = std::io::BufReader::new(key_file);
300288
let keystore: JsonKeystore = serde_json::from_reader(key_reader)?;
301289
let password = std::fs::read(secrets_path)?;
302-
let decrypted_password = eth2_keystore::decrypt(&password, &keystore.crypto).unwrap();
290+
let decrypted_password = eth2_keystore::decrypt(&password, &keystore.crypto)
291+
.map_err(|_| eyre::eyre!("Error decrypting ECDSA keystore"))?;
303292

304293
EcdsaSigner::new_from_bytes(decrypted_password.as_bytes())
305294
}

crates/common/src/signer/schemes/ecdsa.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{ops::Deref, str::FromStr};
22

33
use alloy::{
4-
primitives::{Address, PrimitiveSignature, B256},
4+
primitives::{Address, PrimitiveSignature},
55
signers::{local::PrivateKeySigner, SignerSync},
66
};
77
use eyre::ensure;
@@ -63,8 +63,7 @@ pub enum EcdsaSigner {
6363

6464
impl EcdsaSigner {
6565
pub fn new_random() -> Self {
66-
let secret = B256::random();
67-
Self::new_from_bytes(secret.as_slice()).unwrap()
66+
Self::Local(PrivateKeySigner::random())
6867
}
6968

7069
pub fn new_from_bytes(bytes: &[u8]) -> eyre::Result<Self> {

0 commit comments

Comments
 (0)