From cda0514faf04fdb626f7fc897f76efaac4b80005 Mon Sep 17 00:00:00 2001 From: Naman Garg <0708ng@gmail.com> Date: Tue, 15 Oct 2024 12:04:38 +0530 Subject: [PATCH] chore(delagation-cli): improve err handling --- bolt-delegations-cli/src/main.rs | 23 +++++------------------ bolt-delegations-cli/src/types.rs | 10 ++++++++++ bolt-delegations-cli/src/utils.rs | 2 +- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/bolt-delegations-cli/src/main.rs b/bolt-delegations-cli/src/main.rs index e9d3b119a..3eba5b506 100644 --- a/bolt-delegations-cli/src/main.rs +++ b/bolt-delegations-cli/src/main.rs @@ -7,24 +7,10 @@ use lighthouse_eth2_keystore::Keystore; use bolt_delegations_cli::{ config::{Chain, Commands, Opts, SourceType}, - types::{DelegationMessage, SignedDelegation}, + types::{DelegationMessage, KeystoreError, SignedDelegation}, utils::{compute_signing_root_for_delegation, parse_public_key, KEYSTORE_PASSWORD}, }; -#[derive(Debug, thiserror::Error)] -pub enum KeystoreError { - #[error("Failed to read keystore directory: {0}")] - ReadFromDirectory(#[from] std::io::Error), - #[error("Failed to read keystore from JSON file {0}: {1}")] - ReadFromJSON(String, String), - #[error("Failed to decrypt keypair from JSON file {0} with the provided password: {1}")] - KeypairDecryption(String, String), - #[error("Could not find private key associated with public key {0}")] - UnknownPublicKey(String), - #[error("Invalid signature key length. Signature: {0}. Message: {1}")] - SignatureLength(String, String), -} - fn main() -> Result<()> { let _ = dotenvy::dotenv(); @@ -69,12 +55,13 @@ fn generate_from_keystore( chain: &Chain, ) -> Result { let keypair = Keystore::from_json_file(key_path) - .map_err(|e| KeystoreError::ReadFromJSON(key_path.to_owned(), format!("{e:?}")))? + .map_err(|e| KeystoreError::ReadFromJSON(key_path.to_string(), format!("{e:?}")))? .decrypt_keypair(KEYSTORE_PASSWORD.as_bytes()) - .map_err(|e| KeystoreError::KeypairDecryption(key_path.to_owned(), format!("{e:?}")))?; + .map_err(|e| KeystoreError::KeypairDecryption(key_path.to_string(), format!("{e:?}")))?; let delegation = DelegationMessage::new( - BlsPublicKey::try_from(keypair.pk.to_string().as_ref())?, + BlsPublicKey::try_from(keypair.pk.to_string().as_ref()) + .map_err(|e| KeystoreError::UnknownPublicKey(format!("{e:?}")))?, delegatee_pubkey, ); diff --git a/bolt-delegations-cli/src/types.rs b/bolt-delegations-cli/src/types.rs index 42e28a84d..7f5e2b3ef 100644 --- a/bolt-delegations-cli/src/types.rs +++ b/bolt-delegations-cli/src/types.rs @@ -2,6 +2,16 @@ use alloy::signers::k256::sha2::{Digest, Sha256}; use ethereum_consensus::crypto::{PublicKey as BlsPublicKey, Signature as BlsSignature}; use serde::Serialize; +#[derive(Debug, thiserror::Error)] +pub enum KeystoreError { + #[error("Failed to read keystore from JSON file {0}: {1}")] + ReadFromJSON(String, String), + #[error("Failed to decrypt keypair from JSON file {0} with the provided password: {1}")] + KeypairDecryption(String, String), + #[error("Failed to get public key from keypair: {0}")] + UnknownPublicKey(String), +} + /// Event types that can be emitted by the validator pubkey to /// signal some action on the Bolt protocol. #[derive(Debug, Clone, Copy)] diff --git a/bolt-delegations-cli/src/utils.rs b/bolt-delegations-cli/src/utils.rs index 76dcb4f75..7731681d3 100644 --- a/bolt-delegations-cli/src/utils.rs +++ b/bolt-delegations-cli/src/utils.rs @@ -16,7 +16,7 @@ pub const COMMIT_BOOST_DOMAIN_MASK: [u8; 4] = [109, 109, 111, 67]; pub fn parse_public_key(delegatee_pubkey: &str) -> Result { let hex_pk = delegatee_pubkey.strip_prefix("0x").unwrap_or(delegatee_pubkey); BlsPublicKey::try_from(hex::decode(hex_pk).expect("Failed to decode pubkey").as_slice()) - .map_err(|e| eyre::eyre!("Failed to parse public key from string '{}': {}", hex_pk, e)) + .map_err(|e| eyre::eyre!("Failed to parse public key '{}': {}", hex_pk, e)) } /// Helper function to compute the signing root for a delegation message