From b1a78b47dfce7f834e6d5f05e824b217beffa53f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Tue, 13 Feb 2024 16:39:42 +0100 Subject: [PATCH] Refactor constructors of `CasperSigner` and `CasperPrivateKey`. 1. Removed duplicated code (from_key_pathbuf()). 2. Handling paths via `AsRef`. 3. Expose Caspers's `ErrorExt` error. --- kairos-cli/bin/commands/deposit.rs | 2 +- kairos-cli/bin/commands/transfer.rs | 2 +- kairos-cli/bin/commands/withdraw.rs | 2 +- kairos-cli/bin/crypto/error.rs | 11 ++++++----- kairos-cli/bin/crypto/private_key.rs | 13 ++++++------- kairos-cli/bin/crypto/signer.rs | 26 +++++++------------------- kairos-cli/tests/cli_tests.rs | 2 +- 7 files changed, 23 insertions(+), 35 deletions(-) diff --git a/kairos-cli/bin/commands/deposit.rs b/kairos-cli/bin/commands/deposit.rs index a3967e6f..d4e87d11 100644 --- a/kairos-cli/bin/commands/deposit.rs +++ b/kairos-cli/bin/commands/deposit.rs @@ -14,7 +14,7 @@ pub struct Args { pub fn run(args: Args) -> Result { let _amount: u64 = args.amount.field; - let _signer = CasperSigner::from_key_pathbuf(args.private_key_path.field)?; + let _signer = CasperSigner::from_file(args.private_key_path.field)?; // TODO: Create transaction and sign it with `signer`. diff --git a/kairos-cli/bin/commands/transfer.rs b/kairos-cli/bin/commands/transfer.rs index b2c44bda..a5519f1a 100644 --- a/kairos-cli/bin/commands/transfer.rs +++ b/kairos-cli/bin/commands/transfer.rs @@ -19,7 +19,7 @@ pub struct Args { pub fn run(args: Args) -> Result { let _recipient = CasperPublicKey::from_bytes(args.recipient.as_ref())?; let _amount: u64 = args.amount.field; - let _signer = CasperSigner::from_key_pathbuf(args.private_key_path.field)?; + let _signer = CasperSigner::from_file(args.private_key_path.field)?; // TODO: Create transaction and sign it with `signer`. diff --git a/kairos-cli/bin/commands/withdraw.rs b/kairos-cli/bin/commands/withdraw.rs index bdb0c932..5c4023f5 100644 --- a/kairos-cli/bin/commands/withdraw.rs +++ b/kairos-cli/bin/commands/withdraw.rs @@ -14,7 +14,7 @@ pub struct Args { pub fn run(args: Args) -> Result { let _amount: u64 = args.amount.field; - let _signer = CasperSigner::from_key_pathbuf(args.private_key_path.field)?; + let _signer = CasperSigner::from_file(args.private_key_path.field)?; // TODO: Create transaction and sign it with `signer`. diff --git a/kairos-cli/bin/crypto/error.rs b/kairos-cli/bin/crypto/error.rs index c713903e..789afa5d 100644 --- a/kairos-cli/bin/crypto/error.rs +++ b/kairos-cli/bin/crypto/error.rs @@ -1,13 +1,14 @@ +use casper_types::ErrorExt; use thiserror::Error; #[derive(Error, Debug)] pub enum CryptoError { - /// Unable to load a file from the given path. - #[error("failed to load key from file")] - KeyLoad, /// Failed to parse a public key from a raw data. - #[error("failed to parse private key")] - FailedToParseKey, + #[error("failed to parse private key: {error}")] + FailedToParseKey { + #[from] + error: ErrorExt, + }, /// Invalid public key (hexdigest) or other encoding related error. #[error("failed to serialize/deserialize '{context}'")] Serialization { context: &'static str }, diff --git a/kairos-cli/bin/crypto/private_key.rs b/kairos-cli/bin/crypto/private_key.rs index fb8b1895..ece70b67 100644 --- a/kairos-cli/bin/crypto/private_key.rs +++ b/kairos-cli/bin/crypto/private_key.rs @@ -1,14 +1,13 @@ -use casper_types::file_utils::read_file; +use std::path::Path; -use crate::crypto::error::CryptoError; +use super::error::CryptoError; pub struct CasperPrivateKey(pub casper_types::SecretKey); impl CasperPrivateKey { - pub fn from_file(file_path: &str) -> Result { - let data = read_file(file_path).map_err(|_e| CryptoError::KeyLoad)?; - let secret_key = - casper_types::SecretKey::from_pem(data).map_err(|_e| CryptoError::FailedToParseKey)?; - Ok(Self(secret_key)) + pub fn from_file>(file_path: P) -> Result { + casper_types::SecretKey::from_file(file_path) + .map(Self) + .map_err(|error| error.into()) } } diff --git a/kairos-cli/bin/crypto/signer.rs b/kairos-cli/bin/crypto/signer.rs index 1d42d058..dbfc0f79 100644 --- a/kairos-cli/bin/crypto/signer.rs +++ b/kairos-cli/bin/crypto/signer.rs @@ -1,10 +1,10 @@ -use std::path::PathBuf; +use std::path::Path; use super::private_key::CasperPrivateKey; use super::public_key::CasperPublicKey; use crate::crypto::error::CryptoError; use casper_types::bytesrepr::ToBytes; -use casper_types::{crypto, PublicKey, SecretKey}; +use casper_types::{crypto, PublicKey}; pub struct CasperSigner { secret_key: CasperPrivateKey, @@ -13,28 +13,16 @@ pub struct CasperSigner { #[allow(unused)] impl CasperSigner { - fn from_key_raw(secret_key: CasperPrivateKey) -> Self { + pub fn from_file>(file_path: P) -> Result { + let secret_key = CasperPrivateKey::from_file(file_path)?; + // Derive the public key. let public_key = CasperPublicKey::from_key(PublicKey::from(&secret_key.0)); - CasperSigner { + Ok(CasperSigner { secret_key, public_key, - } - } - - pub fn from_file(secret_key_path: &str) -> Result { - let secret_key = - SecretKey::from_file(secret_key_path).map_err(|_e| CryptoError::FailedToParseKey)?; - - Ok(Self::from_key_raw(CasperPrivateKey(secret_key))) - } - - pub fn from_key_pathbuf(secret_key_path: PathBuf) -> Result { - let private_key_path_str: &str = secret_key_path.to_str().ok_or(CryptoError::KeyLoad)?; - let private_key = CasperPrivateKey::from_file(private_key_path_str)?; - - Ok(Self::from_key_raw(private_key)) + }) } pub fn get_public_key(&self) -> CasperPublicKey { diff --git a/kairos-cli/tests/cli_tests.rs b/kairos-cli/tests/cli_tests.rs index 284799c7..2893ba7d 100644 --- a/kairos-cli/tests/cli_tests.rs +++ b/kairos-cli/tests/cli_tests.rs @@ -77,7 +77,7 @@ fn deposit_invalid_private_key_path() { .arg(secret_key_path); cmd.assert() .failure() - .stderr(predicates::str::contains("failed to load key from file")); + .stderr(predicates::str::contains("failed to parse private key")); } #[test]