This document describes the Ledger Flex hardware wallet integration for secure transaction signing in the AgroNet backend.
The Ledger Flex integration provides hardware-based transaction signing where private keys never leave the device, ensuring maximum security for production environments.
- Ledger Nano S
- Ledger Nano X
- Ledger Nano S Plus
- Ledger Flex
- Ledger Stax
Add the following environment variables to your .env file:
# Enable or disable Ledger signing
LEDGER_ENABLED=false
# BIP32 derivation path for Ethereum
LEDGER_DERIVATION_PATH="m/44'/60'/0'/0/0"
# Require user confirmation on device
LEDGER_REQUIRE_CONFIRMATION=true- LEDGER_ENABLED: Set to
trueto enable hardware wallet signing,falseto use mock signing (default:false) - LEDGER_DERIVATION_PATH: BIP32 path for key derivation (default:
"m/44'/60'/0'/0/0"- first Ethereum account) - LEDGER_REQUIRE_CONFIRMATION: Always require user confirmation on device (default:
true)
When LEDGER_ENABLED=true, the Ethereum settlement endpoint (/api/onchain/settle/ethereum) will automatically use the Ledger device for signing transactions.
use agronet_backend::ledger_flex::{LedgerTransport, LedgerSigner};
// Connect to Ledger device
let transport = LedgerTransport::connect()?;
let mut signer = LedgerSigner::new(transport);
// Get Ethereum address from device
let address = signer.get_address("m/44'/60'/0'/0/0")?;
println!("Address: {}", address);
// Sign a transaction
let tx_data = vec![/* RLP-encoded transaction */];
let signature = signer.sign_transaction(&tx_data, "m/44'/60'/0'/0/0")?;- Hardware Security: Private keys never leave the Ledger device
- User Confirmation: All transactions require physical confirmation on the device
- Timeout Protection: 60-second timeout for user confirmation to prevent hanging
- Multi-device Support: Automatically detects and connects to available Ledger devices
- Fallback Mechanism: If Ledger signing fails, falls back to mock signing in development
Install the required system dependencies:
sudo apt-get update
sudo apt-get install -y libudev-dev libusb-1.0-0-devNo additional dependencies required - hidapi uses IOKit framework.
Requires Windows SDK for HID API support.
Error: Ledger device not found or not connected
Solutions:
- Ensure the Ledger device is connected via USB
- Check that the device is unlocked
- Verify that the Ethereum app is open on the device
- On Linux, check USB permissions
Error: User rejected the transaction on device
Solutions:
- Check the transaction details on the device screen
- Approve the transaction by pressing the right button
- Ensure you're not accidentally pressing the left button (reject)
Error: Timeout waiting for user confirmation
Solutions:
- Respond faster to the device prompt (60-second limit)
- Check that the device is not frozen
- Restart the device if unresponsive
Error: Ethereum app not open on Ledger device
Solutions:
- Navigate to the Ethereum app on your Ledger device
- Open the Ethereum app
- Ensure the app is fully loaded before making requests
# Install system dependencies (Linux)
sudo apt-get install -y libudev-dev libusb-1.0-0-dev
# Build the project
SQLX_OFFLINE=true cargo build# Run unit tests
SQLX_OFFLINE=true cargo test --bin AgroNet_backend
# Run only Ledger Flex tests
SQLX_OFFLINE=true cargo test ledger_flexSet LEDGER_ENABLED=false in your .env file to use mock signing for development and testing without requiring a physical Ledger device.
When Ledger signing is enabled, the settlement response will include:
{
"tx_hash": "0x...",
"network": "ethereum",
"status": "signed",
"validation_status": "approved",
"validation_reason": "OK"
}Note the status field:
"signed": Transaction was signed with Ledger hardware wallet"mocked": Transaction used mock signing (development mode)
- Always verify addresses: Double-check the address shown on the Ledger screen
- Use hardware in production: Enable Ledger signing for all production deployments
- Secure the device: Keep your Ledger device in a secure location
- Backup recovery phrase: Store your 24-word recovery phrase in a secure, offline location
- Keep firmware updated: Regularly update your Ledger device firmware
- Test derivation paths: Verify the derivation path produces the expected addresses before use
This integration uses the hidapi crate (MIT/Apache-2.0 licensed) for USB HID communication.