Skip to content

Commit

Permalink
chore(delagation-cli): improve err handling
Browse files Browse the repository at this point in the history
  • Loading branch information
namn-grg committed Oct 15, 2024
1 parent 5cf9a3a commit cda0514
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
23 changes: 5 additions & 18 deletions bolt-delegations-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -69,12 +55,13 @@ fn generate_from_keystore(
chain: &Chain,
) -> Result<SignedDelegation> {
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,
);

Expand Down
10 changes: 10 additions & 0 deletions bolt-delegations-cli/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion bolt-delegations-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlsPublicKey> {
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
Expand Down

0 comments on commit cda0514

Please sign in to comment.