Skip to content

Commit

Permalink
Refactor constructors of CasperSigner and CasperPrivateKey.
Browse files Browse the repository at this point in the history
1. Removed duplicated code (from_key_pathbuf()).
2. Handling paths via `AsRef<Path>`.
3. Expose Caspers's `ErrorExt` error.
  • Loading branch information
koxu1996 committed Feb 13, 2024
1 parent c314c50 commit b1a78b4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 35 deletions.
2 changes: 1 addition & 1 deletion kairos-cli/bin/commands/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Args {

pub fn run(args: Args) -> Result<String, CliError> {
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`.

Expand Down
2 changes: 1 addition & 1 deletion kairos-cli/bin/commands/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Args {
pub fn run(args: Args) -> Result<String, CliError> {
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`.

Expand Down
2 changes: 1 addition & 1 deletion kairos-cli/bin/commands/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Args {

pub fn run(args: Args) -> Result<String, CliError> {
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`.

Expand Down
11 changes: 6 additions & 5 deletions kairos-cli/bin/crypto/error.rs
Original file line number Diff line number Diff line change
@@ -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 },
Expand Down
13 changes: 6 additions & 7 deletions kairos-cli/bin/crypto/private_key.rs
Original file line number Diff line number Diff line change
@@ -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<Self, CryptoError> {
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<P: AsRef<Path>>(file_path: P) -> Result<Self, CryptoError> {
casper_types::SecretKey::from_file(file_path)
.map(Self)
.map_err(|error| error.into())
}
}
26 changes: 7 additions & 19 deletions kairos-cli/bin/crypto/signer.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,28 +13,16 @@ pub struct CasperSigner {

#[allow(unused)]
impl CasperSigner {
fn from_key_raw(secret_key: CasperPrivateKey) -> Self {
pub fn from_file<P: AsRef<Path>>(file_path: P) -> Result<Self, CryptoError> {
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<Self, CryptoError> {
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<Self, CryptoError> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion kairos-cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit b1a78b4

Please sign in to comment.