Skip to content

Commit

Permalink
refactor: keychain module (#10)
Browse files Browse the repository at this point in the history
* refactor: keychain module

* fix: run rustfmt

* refactor: better organize errors modules
  • Loading branch information
mikesposito authored Sep 24, 2023
1 parent 4e8196c commit 06b0f99
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 46 deletions.
7 changes: 0 additions & 7 deletions src/account/mod.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/account/signer/errors.rs

This file was deleted.

26 changes: 0 additions & 26 deletions src/account/vault/errors.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Account {
let extended_address = encode(&keccak256(&extended_public_key.to_bytes()));
let address = extended_address[extended_address.len() - 40..].to_string();

assert_is_valid_hex_address(&address).or(Err(AccountError::InvalidPublicKey))?;
assert_is_valid_hex_address(&address)?;

Ok(Account {
address: add0x(&address).to_owned(),
Expand Down
28 changes: 28 additions & 0 deletions src/keychain/account/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::hex::HexError;

#[derive(Debug)]
pub enum AccountError {
InvalidHexAddress,
InvalidKeyLength,
}

impl std::fmt::Display for AccountError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidHexAddress => write!(f, "Invalid hex address"),
Self::InvalidKeyLength => write!(f, "Invalid key length"),
}
}
}

impl From<HexError> for AccountError {
fn from(error: HexError) -> Self {
match error {
HexError::InvalidHex => Self::InvalidHexAddress,
HexError::InvalidHexLength => Self::InvalidHexAddress,
HexError::InvalidHexAddress => Self::InvalidHexAddress,
}
}
}

impl std::error::Error for AccountError {}
5 changes: 5 additions & 0 deletions src/keychain/account/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod account;
pub mod errors;

pub use account::Account;
pub use errors::AccountError;
5 changes: 0 additions & 5 deletions src/account/keychain/errors.rs → src/keychain/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,3 @@ impl From<VaultError> for KeychainError {
Self::VaultError(error)
}
}

#[derive(Debug)]
pub enum AccountError {
InvalidPublicKey,
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{Account, Controller, KeychainError, Observable, Signer, Vault};
use crate::{
keychain::{Account, KeychainError, Signer, Vault},
utils::{Controller, Observable},
};

#[derive(Clone, Debug)]
pub struct KeychainState {
Expand Down
4 changes: 4 additions & 0 deletions src/account/keychain/mod.rs → src/keychain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pub mod account;
pub mod errors;
pub mod keychain;
pub mod signer;
pub mod vault;

pub use account::*;
pub use errors::*;
pub use keychain::*;
pub use signer::*;
pub use vault::*;
14 changes: 14 additions & 0 deletions src/keychain/signer/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[derive(Debug)]
pub enum SignerError {
InvalidPrivateKey,
}

impl std::fmt::Display for SignerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidPrivateKey => write!(f, "Invalid private key"),
}
}
}

impl std::error::Error for SignerError {}
File renamed without changes.
File renamed without changes.
File renamed without changes.
46 changes: 46 additions & 0 deletions src/keychain/vault/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::{error::Error, fmt::Display};

use crate::{AccountError, SignerError};

#[derive(Debug)]
pub enum VaultError {
ForbiddenWhileLocked,
AccountCreation,
InvalidPassword,
InvalidMnemonic,
SignerCreation,
KeyDerivation,
AlreadyUnlocked,
SafeCreation,
SafeDecrypt,
}

impl Display for VaultError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ForbiddenWhileLocked => write!(f, "Forbidden while locked"),
Self::AccountCreation => write!(f, "Account creation error"),
Self::InvalidPassword => write!(f, "Invalid password"),
Self::InvalidMnemonic => write!(f, "Invalid mnemonic"),
Self::SignerCreation => write!(f, "Signer creation error"),
Self::KeyDerivation => write!(f, "Key derivation error"),
Self::AlreadyUnlocked => write!(f, "Already unlocked"),
Self::SafeCreation => write!(f, "Safe creation error"),
Self::SafeDecrypt => write!(f, "Safe decryption error"),
}
}
}

impl From<AccountError> for VaultError {
fn from(_: AccountError) -> Self {
Self::AccountCreation
}
}

impl From<SignerError> for VaultError {
fn from(_: SignerError) -> Self {
Self::SignerCreation
}
}

impl Error for VaultError {}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod account;
pub mod keychain;
pub mod utils;

pub use account::*;
pub use keychain::*;
pub use utils::*;

0 comments on commit 06b0f99

Please sign in to comment.