3.9 KiB
3.9 KiB
Crypto 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
- Rhai scripting engine for automation
- Key management (create, list, import, export)
- Cryptographic operations (sign, verify, encrypt, decrypt)
- Ethereum wallet integration
Installation
Build the CLI tool using Cargo:
cargo build --release
The binary will be available at target/release/crypto-cli
.
Usage
Command Line Interface
The CLI provides several subcommands for different operations:
Key Management
# 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
# 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
# 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:
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:
# Execute a script file
crypto-cli script <path-to-script>
# Execute an inline script
crypto-cli script --inline "create_keypair('test'); sign('Hello, world!');"
Rhai Scripting API
The Rhai scripting engine provides access to the following functions:
Key Management
create_key_space(name)
- Create a new key spaceencrypt_key_space(password)
- Encrypt the current key spacedecrypt_key_space(encrypted, password)
- Decrypt a key spacecreate_keypair(name)
- Create a new keypairselect_keypair(name)
- Select a keypair for operationslist_keypairs()
- List available keypairs
Cryptographic Operations
sign(message)
- Sign a message with the selected keypairverify(message, signature)
- Verify a signaturegenerate_key()
- Generate a symmetric encryption keyencrypt(key, message)
- Encrypt a message with a symmetric keydecrypt(key, ciphertext)
- Decrypt a message with a symmetric key
Ethereum Operations
create_ethereum_wallet()
- Create an Ethereum walletget_ethereum_address()
- Get the Ethereum address for the selected keypair
Example Script
// Create a key space and keypair
create_key_space("demo");
create_keypair("alice");
select_keypair("alice");
// 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);
Configuration
The CLI uses a configuration file located at ~/.crypto-cli/config.json
. You can specify a different configuration file with the --config
option.
Verbose Mode
Use the --verbose
flag to enable verbose output:
crypto-cli --verbose <command>