webassembly/CLI_README.md

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 space
  • 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
  • select_keypair(name) - Select a keypair for operations
  • list_keypairs() - List available keypairs

Cryptographic Operations

  • sign(message) - Sign a message with the selected keypair
  • verify(message, signature) - Verify a signature
  • generate_key() - Generate a symmetric encryption key
  • encrypt(key, message) - Encrypt a message with a symmetric key
  • decrypt(key, ciphertext) - Decrypt a message with a symmetric key

Ethereum Operations

  • create_ethereum_wallet() - Create an Ethereum wallet
  • get_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>