update docs

This commit is contained in:
Sameh Abouelsaad 2025-05-08 16:28:13 +03:00
parent a86a247180
commit 54f604f16a
5 changed files with 29 additions and 176 deletions

View File

@ -1,11 +1,10 @@
# Crypto CLI and Rhai Scripting
# Hero Vault CLI and Rhai Scripting
This module adds CLI and Rhai scripting capabilities to the WebAssembly Cryptography Module, allowing for command-line operations and scripting of cryptographic functions.
## Features
- Command-line interface for cryptographic operations
- Interactive shell mode
- Simplified command-line interface for script execution
- Rhai scripting engine for automation
- Key management (create, list, import, export)
- Cryptographic operations (sign, verify, encrypt, decrypt)
@ -19,85 +18,23 @@ Build the CLI tool using Cargo:
cargo build --release
```
The binary will be available at `target/release/crypto-cli`.
The binary will be available at `target/release/hero-vault`.
## Usage
### Command Line Interface
The CLI provides several subcommands for different operations:
#### Key Management
```bash
# Create a new key space
crypto-cli key create-space <name> [password]
# List available key spaces
crypto-cli key list-spaces
# Create a new keypair
crypto-cli key create-keypair <name>
# List available keypairs
crypto-cli key list-keypairs
# Export a keypair
crypto-cli key export <name> [output-file]
# Import a keypair
crypto-cli key import <name> [input-file]
```
#### Cryptographic Operations
```bash
# Sign a message
crypto-cli crypto sign <keypair> <message> [output-file]
# Verify a signature
crypto-cli crypto verify <signature> <message> [keypair]
# Encrypt data
crypto-cli crypto encrypt <recipient> <data> [output-file]
# Decrypt data
crypto-cli crypto decrypt <keypair> <data> [output-file]
```
#### Ethereum Operations
```bash
# Create an Ethereum wallet from a keypair
crypto-cli eth create <keypair>
# Get the Ethereum address for a keypair
crypto-cli eth address <keypair>
# Get the balance of an Ethereum address
crypto-cli eth balance <address> <network>
```
### Interactive Shell
Launch the interactive shell with:
```bash
crypto-cli shell
```
In the shell, you can run the same commands as in the CLI but without the `crypto-cli` prefix.
### Rhai Scripting
Execute Rhai scripts with:
The CLI has been simplified to directly process Rhai scripts:
```bash
# Execute a script file
crypto-cli script <path-to-script>
hero-vault path/to/script.rhai
# Execute an inline script
crypto-cli script --inline "create_keypair('test'); sign('Hello, world!');"
# Enable verbose output
hero-vault --verbose path/to/script.rhai
# Specify a custom config file
hero-vault --config custom-config.json path/to/script.rhai
```
## Rhai Scripting API
@ -106,10 +43,10 @@ The Rhai scripting engine provides access to the following functions:
### Key Management
- `create_key_space(name)` - Create a new key space
- `create_key_space(name, password)` - Create a new key space with password
- `encrypt_key_space(password)` - Encrypt the current key space
- `decrypt_key_space(encrypted, password)` - Decrypt a key space
- `create_keypair(name)` - Create a new keypair
- `create_keypair(name, password)` - Create a new keypair
- `select_keypair(name)` - Select a keypair for operations
- `list_keypairs()` - List available keypairs
@ -126,39 +63,22 @@ The Rhai scripting engine provides access to the following functions:
- `create_ethereum_wallet()` - Create an Ethereum wallet
- `get_ethereum_address()` - Get the Ethereum address for the selected keypair
## Example Script
## Example Scripts
```rhai
// Create a key space and keypair
create_key_space("demo");
create_keypair("alice");
select_keypair("alice");
Example scripts are available in the `scripts/rhai` directory:
// Sign and verify a message
let message = "Hello, world!";
let signature = sign(message);
let is_valid = verify(message, signature);
print("Signature valid: " + is_valid);
// Symmetric encryption
let key = generate_key();
let ciphertext = encrypt(key, "Secret message");
let plaintext = decrypt(key, ciphertext);
print("Decrypted: " + plaintext);
// Ethereum operations
create_ethereum_wallet();
let address = get_ethereum_address();
print("Ethereum address: " + address);
```
- `example.rhai` - Basic key management and cryptographic operations
- `advanced_example.rhai` - Advanced cryptographic operations
- `key_persistence_example.rhai` - Persisting keys to disk
- `load_existing_space.rhai` - Loading an existing key space
## Configuration
The CLI uses a configuration file located at `~/.crypto-cli/config.json`. You can specify a different configuration file with the `--config` option.
The CLI uses a configuration file located at `~/.hero-vault/config.json`. You can specify a different configuration file with the `--config` option.
## Verbose Mode
Use the `--verbose` flag to enable verbose output:
```bash
crypto-cli --verbose <command>
hero-vault --verbose path/to/script.rhai

View File

@ -2,14 +2,13 @@
This project provides a WebAssembly module written in Rust that offers cryptographic functionality for web applications.
## Planned Enhancements
## Features
We are planning significant enhancements to this module, including:
- Command Line Interface (CLI)
- Rhai scripting capabilities
- Messaging system integration (Mycelium or NATS)
- **WebAssembly Module** - Core cryptographic functionality for web applications
- **Command Line Interface (CLI)** - Simplified CLI for executing Rhai scripts
- **Rhai Scripting** - Powerful scripting capabilities for automation
For details, see the [Enhancement Specification](ENHANCEMENT_SPEC.md).
For CLI usage details, see the [CLI README](CLI_README.md).
## Features

View File

@ -12,7 +12,7 @@ This directory contains example scripts and documentation for the WebAssembly Cr
The `rhai/` directory contains example Rhai scripts that can be executed using the CLI:
```bash
crypto-cli script --path scripts/rhai/example.rhai
hero-vault scripts/rhai/example.rhai
```
These scripts demonstrate how to use the cryptographic functions exposed to the Rhai scripting engine, including:
@ -43,13 +43,13 @@ You can create your own Rhai scripts to automate cryptographic operations. The f
### Key Space Management
- `create_key_space(name)`: Create a new key space
- `create_key_space(name, password)`: Create a new key space with password
- `encrypt_key_space(password)`: Encrypt the current key space
- `decrypt_key_space(encrypted, password)`: Decrypt and load a key space
### Keypair Operations
- `create_keypair(name)`: Create a new keypair
- `create_keypair(name, password)`: Create a new keypair
- `select_keypair(name)`: Select a keypair for use
- `list_keypairs()`: List all keypairs in the current space

View File

@ -43,7 +43,7 @@ if load_key_space(space_name, password) {
## Key Space Storage
Key spaces are stored in the `~/.crypto-cli/key-spaces/` directory by default. Each key space is stored in a separate JSON file named after the key space (e.g., `my_space.json`).
Key spaces are stored in the `~/.hero-vault/key-spaces/` directory by default. Each key space is stored in a separate JSON file named after the key space (e.g., `my_space.json`).
## Security

View File

@ -1,66 +0,0 @@
use webassembly::core::keypair::{KeySpace, KeyPair};
use webassembly::core::symmetric;
use std::fs;
use serde_json;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a key space
let mut space = KeySpace::new("test-space");
// Add a keypair
let keypair = KeyPair::new("test-keypair");
space.keypairs.insert("test-keypair".to_string(), keypair);
// Print the key space
println!("Key space: {:?}", space);
// Serialize the key space directly to see what it looks like
let direct_serialized = serde_json::to_string_pretty(&space)?;
println!("Direct serialized key space:\n{}", direct_serialized);
// Encrypt the key space
let password = "test123";
let encrypted_space = symmetric::encrypt_key_space(&space, password)?;
// Serialize the encrypted space
let serialized = symmetric::serialize_encrypted_space(&encrypted_space)?;
// Write to file
fs::write("test_keyspace.json", &serialized)?;
println!("Wrote encrypted key space to test_keyspace.json");
// Read from file
let serialized = fs::read_to_string("test_keyspace.json")?;
// Deserialize the encrypted space
let encrypted_space = symmetric::deserialize_encrypted_space(&serialized)?;
println!("Deserialized encrypted space: {:?}", encrypted_space.metadata);
// Decrypt the key space
let decrypted_data = symmetric::decrypt_symmetric(
&symmetric::derive_key_from_password(password),
&encrypted_space.encrypted_data
)?;
println!("Decrypted data length: {}", decrypted_data.len());
println!("Decrypted data preview: {:?}", &decrypted_data[..20]);
// Try to deserialize manually
match serde_json::from_slice::<KeySpace>(&decrypted_data) {
Ok(space) => {
println!("Manual deserialization successful!");
println!("Decrypted key space: {:?}", space);
println!("Keypairs: {:?}", space.list_keypairs());
},
Err(e) => {
println!("Manual deserialization error: {}", e);
// Try to print the decrypted data as a string to see what's wrong
match std::str::from_utf8(&decrypted_data) {
Ok(s) => println!("Decrypted data as string: {}", s),
Err(_) => println!("Decrypted data is not valid UTF-8"),
}
}
}
Ok(())
}