From a4438d63e02b8241d07b0b64592f3b988a8bdcb5 Mon Sep 17 00:00:00 2001 From: despiegk Date: Tue, 13 May 2025 08:02:23 +0300 Subject: [PATCH] ... --- src/vault/ethereum/storage.rs | 4 +-- .../keypair/tests/implementation_tests.rs | 7 ---- src/vault/keypair/tests/mod.rs | 3 -- src/vault/{keypair => keyspace}/README.md | 0 .../{keypair => keyspace}/keypair_types.rs | 0 src/vault/{keypair => keyspace}/mod.rs | 0 .../{keypair => keyspace}/session_manager.rs | 2 +- src/vault/keyspace/spec.md | 36 +++++++++++++++++++ .../tests/keypair_types_tests.rs | 2 +- src/vault/keyspace/tests/mod.rs | 3 ++ .../tests/session_manager_tests.rs | 4 +-- src/vault/mod.rs | 4 +-- src/vault/symmetric/implementation.rs | 2 +- 13 files changed, 48 insertions(+), 19 deletions(-) delete mode 100644 src/vault/keypair/tests/implementation_tests.rs delete mode 100644 src/vault/keypair/tests/mod.rs rename src/vault/{keypair => keyspace}/README.md (100%) rename src/vault/{keypair => keyspace}/keypair_types.rs (100%) rename src/vault/{keypair => keyspace}/mod.rs (100%) rename src/vault/{keypair => keyspace}/session_manager.rs (97%) create mode 100644 src/vault/keyspace/spec.md rename src/vault/{keypair => keyspace}/tests/keypair_types_tests.rs (97%) create mode 100644 src/vault/keyspace/tests/mod.rs rename src/vault/{keypair => keyspace}/tests/session_manager_tests.rs (97%) diff --git a/src/vault/ethereum/storage.rs b/src/vault/ethereum/storage.rs index 127d7b7..e74fb26 100644 --- a/src/vault/ethereum/storage.rs +++ b/src/vault/ethereum/storage.rs @@ -16,7 +16,7 @@ static ETH_WALLETS: Lazy>>> = Lazy::ne /// Creates an Ethereum wallet from the currently selected keypair for a specific network. pub fn create_ethereum_wallet_for_network(network: NetworkConfig) -> Result { // Get the currently selected keypair - let keypair = crate::vault::keypair::get_selected_keypair()?; + let keypair = crate::vault::keyspace::get_selected_keypair()?; // Create an Ethereum wallet from the keypair let wallet = EthereumWallet::from_keypair(&keypair, network)?; @@ -77,7 +77,7 @@ pub fn clear_ethereum_wallets_for_network(network_name: &str) { /// Creates an Ethereum wallet from a name and the currently selected keypair for a specific network. pub fn create_ethereum_wallet_from_name_for_network(name: &str, network: NetworkConfig) -> Result { // Get the currently selected keypair - let keypair = crate::vault::keypair::get_selected_keypair()?; + let keypair = crate::vault::keyspace::get_selected_keypair()?; // Create an Ethereum wallet from the name and keypair let wallet = EthereumWallet::from_name_and_keypair(name, &keypair, network)?; diff --git a/src/vault/keypair/tests/implementation_tests.rs b/src/vault/keypair/tests/implementation_tests.rs deleted file mode 100644 index b62bb10..0000000 --- a/src/vault/keypair/tests/implementation_tests.rs +++ /dev/null @@ -1,7 +0,0 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} \ No newline at end of file diff --git a/src/vault/keypair/tests/mod.rs b/src/vault/keypair/tests/mod.rs deleted file mode 100644 index d24426c..0000000 --- a/src/vault/keypair/tests/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod implementation_tests; -mod keypair_types_tests; -mod session_manager_tests; \ No newline at end of file diff --git a/src/vault/keypair/README.md b/src/vault/keyspace/README.md similarity index 100% rename from src/vault/keypair/README.md rename to src/vault/keyspace/README.md diff --git a/src/vault/keypair/keypair_types.rs b/src/vault/keyspace/keypair_types.rs similarity index 100% rename from src/vault/keypair/keypair_types.rs rename to src/vault/keyspace/keypair_types.rs diff --git a/src/vault/keypair/mod.rs b/src/vault/keyspace/mod.rs similarity index 100% rename from src/vault/keypair/mod.rs rename to src/vault/keyspace/mod.rs diff --git a/src/vault/keypair/session_manager.rs b/src/vault/keyspace/session_manager.rs similarity index 97% rename from src/vault/keypair/session_manager.rs rename to src/vault/keyspace/session_manager.rs index 74094b0..3fa7a11 100644 --- a/src/vault/keypair/session_manager.rs +++ b/src/vault/keyspace/session_manager.rs @@ -2,7 +2,7 @@ use once_cell::sync::Lazy; use std::sync::Mutex; use crate::vault::error::CryptoError; -use crate::vault::keypair::keypair_types::{KeyPair, KeySpace}; // Assuming KeyPair and KeySpace will be in keypair_types.rs +use crate::vault::keyspace::keypair_types::{KeyPair, KeySpace}; // Assuming KeyPair and KeySpace will be in keypair_types.rs /// Session state for the current key space and selected keypair. pub struct Session { diff --git a/src/vault/keyspace/spec.md b/src/vault/keyspace/spec.md new file mode 100644 index 0000000..add2d42 --- /dev/null +++ b/src/vault/keyspace/spec.md @@ -0,0 +1,36 @@ +# Keyspace Module Specification + +This document explains the purpose and functionality of the `keyspace` module within the Hero Vault. + +## Purpose of the Module + +The `keyspace` module provides a secure and organized way to manage cryptographic keypairs. It allows for the creation, storage, loading, and utilization of keypairs within designated containers called keyspaces. This module is essential for handling sensitive cryptographic material securely. + +## What is a Keyspace? + +A keyspace is a logical container designed to hold multiple cryptographic keypairs. It is represented by the `KeySpace` struct in the code. Keyspaces can be encrypted and persisted to disk, providing a secure method for storing collections of keypairs. Each keyspace is identified by a unique name. + +## What is a Keypair? + +A keypair, represented by the `KeyPair` struct, is a fundamental cryptographic element consisting of a mathematically linked pair of keys: a public key and a private key. In this module, ECDSA (Elliptic Curve Digital Signature Algorithm) keypairs are used. + +* **Private Key:** This key is kept secret and is used for operations like signing data or decrypting messages intended for the keypair's owner. +* **Public Key:** This key can be shared openly and is used to verify signatures created by the corresponding private key or to encrypt messages that can only be decrypted by the private key. + +## How Many Keypairs Per Space? + +A keyspace can hold multiple keypairs. The `KeySpace` struct uses a `HashMap` to store keypairs, where each keypair is associated with a unique string name. There is no inherent, fixed limit on the number of keypairs a keyspace can contain, beyond the practical limitations of system memory. + +## How Do We Load Them? + +Keyspaces are loaded from persistent storage (disk) using the `KeySpace::load` function, which requires the keyspace name and a password for decryption. Once a `KeySpace` object is loaded into memory, it can be set as the currently active keyspace for the session using the `session_manager::set_current_space` function. Individual keypairs within the loaded keyspace are then accessed by their names using functions like `session_manager::select_keypair` and `session_manager::get_selected_keypair`. + +## What Do They Do? + +Keypairs within a keyspace are used to perform various cryptographic operations. The `KeyPair` struct provides methods for: + +* **Digital Signatures:** Signing messages with the private key (`KeyPair::sign`) and verifying those signatures with the public key (`KeyPair::verify`). +* **Ethereum Address Derivation:** Generating an Ethereum address from the public key (`KeyPair::to_ethereum_address`). +* **Asymmetric Encryption/Decryption:** Encrypting data using a recipient's public key (`KeyPair::encrypt_asymmetric`) and decrypting data encrypted with the keypair's public key using the private key (`KeyPair::decrypt_asymmetric`). + +The `session_manager` module provides functions that utilize the currently selected keypair to perform these operations within the context of the active session. \ No newline at end of file diff --git a/src/vault/keypair/tests/keypair_types_tests.rs b/src/vault/keyspace/tests/keypair_types_tests.rs similarity index 97% rename from src/vault/keypair/tests/keypair_types_tests.rs rename to src/vault/keyspace/tests/keypair_types_tests.rs index fe45775..01752b6 100644 --- a/src/vault/keypair/tests/keypair_types_tests.rs +++ b/src/vault/keyspace/tests/keypair_types_tests.rs @@ -1,5 +1,5 @@ -use crate::vault::keypair::keypair_types::{KeyPair, KeySpace}; +use crate::vault::keyspace::keypair_types::{KeyPair, KeySpace}; #[cfg(test)] mod tests { diff --git a/src/vault/keyspace/tests/mod.rs b/src/vault/keyspace/tests/mod.rs new file mode 100644 index 0000000..770d0e5 --- /dev/null +++ b/src/vault/keyspace/tests/mod.rs @@ -0,0 +1,3 @@ + +mod keypair_types_tests; +mod session_manager_tests; \ No newline at end of file diff --git a/src/vault/keypair/tests/session_manager_tests.rs b/src/vault/keyspace/tests/session_manager_tests.rs similarity index 97% rename from src/vault/keypair/tests/session_manager_tests.rs rename to src/vault/keyspace/tests/session_manager_tests.rs index 416c671..66ba44f 100644 --- a/src/vault/keypair/tests/session_manager_tests.rs +++ b/src/vault/keyspace/tests/session_manager_tests.rs @@ -1,8 +1,8 @@ -use crate::vault::keypair::session_manager::{ +use crate::vault::keyspace::session_manager::{ clear_session, create_keypair, create_space, get_current_space, get_selected_keypair, list_keypairs, select_keypair, set_current_space, SESSION, }; -use crate::vault::keypair::keypair_types::KeySpace; +use crate::vault::keyspace::keypair_types::KeySpace; // Helper function to clear the session before each test fn setup_test() { diff --git a/src/vault/mod.rs b/src/vault/mod.rs index 130333c..b97a574 100644 --- a/src/vault/mod.rs +++ b/src/vault/mod.rs @@ -9,7 +9,7 @@ //! - Key-value store with encryption pub mod error; -pub mod keypair; +pub mod keyspace; pub mod symmetric; pub mod ethereum; pub mod kvs; @@ -17,4 +17,4 @@ pub mod kvs; // Re-export modules // Re-export common types for convenience pub use error::CryptoError; -pub use keypair::{KeyPair, KeySpace}; +pub use keyspace::{KeyPair, KeySpace}; diff --git a/src/vault/symmetric/implementation.rs b/src/vault/symmetric/implementation.rs index 3b201d7..2fa9520 100644 --- a/src/vault/symmetric/implementation.rs +++ b/src/vault/symmetric/implementation.rs @@ -7,7 +7,7 @@ use serde::{Serialize, Deserialize}; use sha2::{Sha256, Digest}; use crate::vault::error::CryptoError; -use crate::vault::keypair::KeySpace; +use crate::vault::keyspace::KeySpace; /// The size of the nonce in bytes. const NONCE_SIZE: usize = 12;