Skip to content

Commit

Permalink
Return unwrapped ErrorExt in crypto constructors.
Browse files Browse the repository at this point in the history
More precise errors for caller, with the cost of manual
casting into `CryptoError` in handlers.
  • Loading branch information
koxu1996 committed Feb 16, 2024
1 parent 152858a commit 5556046
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
4 changes: 3 additions & 1 deletion kairos-cli/src/commands/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::common::args::{AmountArg, PrivateKeyPathArg};
use crate::crypto::error::CryptoError;
use crate::crypto::signer::CasperSigner;
use crate::error::CliError;

Expand All @@ -14,7 +15,8 @@ pub struct Args {

pub fn run(args: Args) -> Result<String, CliError> {
let _amount: u64 = args.amount.field;
let _signer = CasperSigner::from_file(args.private_key_path.field)?;
let _signer =
CasperSigner::from_file(args.private_key_path.field).map_err(CryptoError::from)?;

// TODO: Create transaction and sign it with `signer`.

Expand Down
4 changes: 3 additions & 1 deletion kairos-cli/src/commands/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::common::args::{AmountArg, PrivateKeyPathArg};
use crate::crypto::error::CryptoError;
use crate::crypto::public_key::CasperPublicKey;
use crate::crypto::signer::CasperSigner;
use crate::error::CliError;
Expand All @@ -19,7 +20,8 @@ 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_file(args.private_key_path.field)?;
let _signer =
CasperSigner::from_file(args.private_key_path.field).map_err(CryptoError::from)?;

// TODO: Create transaction and sign it with `signer`.

Expand Down
4 changes: 3 additions & 1 deletion kairos-cli/src/commands/withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::common::args::{AmountArg, PrivateKeyPathArg};
use crate::crypto::error::CryptoError;
use crate::crypto::signer::CasperSigner;
use crate::error::CliError;

Expand All @@ -14,7 +15,8 @@ pub struct Args {

pub fn run(args: Args) -> Result<String, CliError> {
let _amount: u64 = args.amount.field;
let _signer = CasperSigner::from_file(args.private_key_path.field)?;
let _signer =
CasperSigner::from_file(args.private_key_path.field).map_err(CryptoError::from)?;

// TODO: Create transaction and sign it with `signer`.

Expand Down
8 changes: 3 additions & 5 deletions kairos-cli/src/crypto/private_key.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::path::Path;

use super::error::CryptoError;
use casper_types::ErrorExt;

pub struct CasperPrivateKey(pub casper_types::SecretKey);

impl CasperPrivateKey {
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())
pub fn from_file<P: AsRef<Path>>(file_path: P) -> Result<Self, ErrorExt> {
casper_types::SecretKey::from_file(file_path).map(Self)
}
}
4 changes: 2 additions & 2 deletions kairos-cli/src/crypto/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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};
use casper_types::{crypto, ErrorExt, PublicKey};

pub struct CasperSigner {
secret_key: CasperPrivateKey,
Expand All @@ -13,7 +13,7 @@ pub struct CasperSigner {

#[allow(unused)]
impl CasperSigner {
pub fn from_file<P: AsRef<Path>>(file_path: P) -> Result<Self, CryptoError> {
pub fn from_file<P: AsRef<Path>>(file_path: P) -> Result<Self, ErrorExt> {
let secret_key = CasperPrivateKey::from_file(file_path)?;

// Derive the public key.
Expand Down

0 comments on commit 5556046

Please sign in to comment.