Overview
Thexmtp_cryptography crate provides low-level cryptographic primitives and utilities for the XMTP protocol. It includes signature verification, key generation, hashing utilities, and OpenMLS integration.
Installation
Key Exports
tls_codec::SecretVLBytes
Type alias for secret byte arrays with automatic zeroization on drop. Used for securely handling private keys and sensitive cryptographic material.
module
Re-export of the OpenMLS crate for MLS protocol operations.
Core Modules
signature
Signature creation and verification:- ECDSA signature recovery
- Ethereum address validation
- EIP-191 message signing
- Signature format conversion
basic_credential
OpenMLS basic credential implementation:- Identity credential management
- Credential serialization/deserialization
- Integration with MLS key packages
hash
Cryptographic hashing utilities:- SHA-256 hashing
- Message digest computation
- Hash-based operations
rand
Secure random number generation:- Cryptographically secure RNG
- Random key generation
- Nonce generation
ethereum
Ethereum-specific cryptography:- Wallet signature verification
- Address derivation
- Chain-specific operations
configuration
Cryptographic configuration constants:- Key lengths
- Algorithm parameters
- Protocol constants
utils
Utility functions for cryptographic operations.Main Types and Traits
enum
Signature types that support public key recovery.Variants:
Eip191Signature(Vec<u8>)- Ethereum EIP-191 signature with recovery ID
recover_address(message: &str) -> Result<String>- Recover signer address
enum
Errors that can occur during signature operations.Variants:
BadAddressFormat- Invalid address formatBadSignatureFormat- Invalid signature formatBadSignature- Signature verification failedSigner- Signer errorUnknown- Unknown error
enum
Errors related to identifier validation.Common cases:
- Invalid Ethereum address format
- Invalid address length
- Unsupported identifier type
Signature Functions
fn(Address) -> String
Convert an Ethereum H160 address to a hex string with 0x prefix.
fn(&str) -> bool
Check if a string is a valid Ethereum address (valid hex and length 20).
fn(&Secret) -> Result<[u8; 32]>
Derive a public key from a private key for Ed25519.
Usage Examples
Signature Verification
Address Conversion
Working with Secret Keys
Random Key Generation
Hashing Operations
OpenMLS Integration
Configuration Constants
From theconfiguration module:
usize
Length of Ed25519 keys in bytes (32).
usize
Standard signature length for various algorithms.
Features
feature
Expose private keys in addition to public keys (use with caution).
feature
Testing utilities and mock implementations.
Platform Support
- Native (all platforms)
- WebAssembly (with getrandom support)
- Requires
wasm_jsfeature for browser environments
- Requires
Security Considerations
Secret Zeroization
TheSecret type automatically zeroizes memory on drop:
Random Number Generation
Always use the provided RNG functions for cryptographic operations:Signature Validation
Always validate addresses after signature recovery:Error Handling
Dependencies
Key cryptographic dependencies:libcrux-ed25519- Ed25519 operationsed25519-dalek- EdDSA signaturesopenmls- MLS protocolalloy- Ethereum cryptographysha2- SHA-256 hashingrand- Secure random number generationzeroize- Memory zeroization
