Complete guide for integrating sol-safekey into your Solana trading bot or application.
Sol-SafeKey provides military-grade wallet security with simple integration - just 3 lines of code to add a complete interactive wallet management system to your bot.
- 🔐 Military-Grade Security: AES-256 encryption with PBKDF2 key derivation
- 🚀 Simple Integration: 3 lines of code for complete wallet management
- 🎯 Interactive CLI: Built-in commands for all wallet operations
- 💰 Solana Ready: Native support for SOL, WSOL, SPL tokens, and durable nonce
- 🔒 Secure by Default: Password via stdin pipe (memory only, never environment variables)
Add to your Cargo.toml:
[dependencies]
sol-safekey = { path = "../sol-safekey" }
[features]
default = ["solana-ops"]
solana-ops = ["sol-safekey/solana-ops"]Add this code to your bot's main() function before your bot logic:
use anyhow::Result;
fn main() -> Result<()> {
// Check if running in safekey interactive mode
let args: Vec<String> = std::env::args().skip(1).collect();
if args.first().map(|s| s.as_str()) == Some("safekey") {
// Launch sol-safekey interactive menu
if let Err(e) = sol_safekey::interactive::show_main_menu() {
eprintln!("❌ {}", e);
std::process::exit(1);
}
return Ok(());
}
// Your bot logic starts here...
println!("🤖 Starting bot...");
Ok(())
}That's it! Just 3 lines of actual integration code.
cargo build --features solana-ops --releaseAfter integration, users can run:
./your-bot safekeyThis launches the full interactive menu with all wallet operations:
Wallet Management:
- Create plain text keypair
- Create encrypted keypair (recommended)
- Decrypt encrypted keypair
- Unlock wallet for session
Solana Operations:
- Query SOL balance
- Transfer SOL
- Wrap SOL → WSOL
- Unwrap WSOL → SOL
- Transfer SPL tokens
- Create durable nonce accounts
- PumpSwap Sell - One-click token selling on PumpSwap DEX
- Pump.fun Sell - One-click token selling on Pump.fun bonding curve (internal market)
Sol-SafeKey uses a secure password handling model:
✅ Secure Approach:
- Password passed via stdin pipe
- Exists only in memory
- Never stored in files or environment variables
- Immediately cleared after use
❌ Insecure (Never Do This):
# DON'T: Environment variables can be leaked
export WALLET_PASSWORD="mysecret"
./your-bot✅ Secure (Always Do This):
# Password through stdin pipe - memory only
echo "your-password" | ./your-botCreate a secure startup script for your bot:
#!/bin/bash
# Build the bot
echo "🔧 Building bot..."
cargo build --features solana-ops --release
# Get password securely (no echo)
echo -n "🔐 Enter wallet password: "
read -s WALLET_PASSWORD
echo ""
# Start bot with password piped through stdin
echo "$WALLET_PASSWORD" | ./build-cache/release/your-bot > bot.log 2>&1
EXIT_CODE=$?
# Immediately clear password from memory
WALLET_PASSWORD=""
unset WALLET_PASSWORD
# Check execution result
if [ $EXIT_CODE -eq 0 ]; then
echo "✅ Bot completed successfully"
else
echo "❌ Bot failed with exit code: $EXIT_CODE"
echo "📝 Check bot.log for details"
fiuse sol_safekey::KeyManager;
use std::io::{self, Read};
fn load_wallet() -> Result<solana_sdk::signature::Keypair> {
let wallet_path = "keystore.json";
// Read encrypted keystore
let json = std::fs::read_to_string(wallet_path)?;
// Read password from stdin
let mut password = String::new();
io::stdin().read_to_string(&mut password)?;
let password = password.trim();
// Decrypt and load keypair
let keypair = KeyManager::keypair_from_encrypted_json(&json, password)?;
Ok(keypair)
}use sol_safekey::KeyManager;
fn create_wallet(password: &str) -> Result<()> {
// Generate new keypair
let keypair = KeyManager::generate_keypair();
println!("📍 Wallet Address: {}", keypair.pubkey());
// Encrypt and save
let json = KeyManager::keypair_to_encrypted_json(&keypair, password)?;
std::fs::write("keystore.json", json)?;
println!("✅ Encrypted wallet saved to keystore.json");
Ok(())
}use sol_safekey::solana_ops::SolanaClient;
fn bot_logic(keypair: &solana_sdk::signature::Keypair) -> Result<()> {
// Initialize Solana client
let client = SolanaClient::new("https://api.devnet.solana.com")?;
// Check balance
let balance = client.get_sol_balance(&keypair.pubkey())?;
println!("💰 Balance: {} SOL", balance);
// Transfer SOL
if balance > 0.01 {
let recipient = "RECIPIENT_ADDRESS_HERE".parse()?;
let signature = client.transfer_sol(keypair, &recipient, 0.01)?;
println!("✅ Transfer successful: {}", signature);
}
// Wrap SOL to WSOL
let signature = client.wrap_sol(keypair, 0.1)?;
println!("✅ Wrapped 0.1 SOL: {}", signature);
Ok(())
}See examples/bot_example.rs for a full working example that demonstrates:
- Safekey command integration
- Secure password handling via stdin
- Encrypted wallet loading
- All Solana operations
- Proper error handling
- Production-ready patterns
Build and run:
# Build the example
cargo build --example bot_example --features solana-ops --release
# Launch interactive safekey commands
./build-cache/release/examples/bot_example safekey
# Run bot with password from stdin
echo "your-password" | ./build-cache/release/examples/bot_exampleSol-SafeKey provides comprehensive wallet management:
| Feature | Support |
|---|---|
| Safekey command | ✅ ./your-bot safekey |
| Interactive menu | ✅ Full featured |
| Wallet creation | ✅ AES-256 encryption |
| Password security | ✅ stdin pipe (memory only) |
| SOL operations | ✅ Transfer, balance, wrap/unwrap |
| Token support | ✅ SPL tokens, Token-2022 |
| PumpSwap DEX | ✅ One-click sell with seed-optimized ATA |
| Pump.fun (bonding curve) | ✅ One-click sell on internal market |
| Durable nonce | ✅ Offline transaction support |
| Integration effort | 🎯 3 lines of code |
- Create test wallet:
./your-bot safekey
# Select: Create encrypted keypair → Save to keystore.json- Get devnet SOL:
solana airdrop 2 YOUR_WALLET_ADDRESS --url devnet- Test operations:
./your-bot safekey
# Select: Unlock wallet → Query balance → Transfer SOL- Added sol-safekey dependency to Cargo.toml
- Added 3-line safekey command check to main()
- Created secure startup script with stdin password
- Tested wallet creation with safekey command
- Tested wallet loading in bot logic
- Verified password never in environment variables
- Tested on devnet before production
- Backed up keystore.json securely
-
Never store passwords in:
- Environment variables
- Configuration files
- Source code
- Log files
-
Always use:
- Stdin pipe for password input
- Encrypted keystore files (AES-256)
- Strong passwords (16+ characters)
- Secure backup locations
-
Production Checklist:
- Test thoroughly on devnet
- Backup keystore.json securely
- Use hardware security module (HSM) for high-value accounts
- Implement rate limiting for operations
- Monitor for unusual activity
- Keep dependencies updated
use anyhow::{Context, Result};
fn robust_bot_logic() -> Result<()> {
// Load wallet with context
let keypair = load_wallet()
.context("Failed to load wallet from keystore.json")?;
// Initialize client with retry logic
let client = SolanaClient::new_with_retry("https://api.mainnet-beta.solana.com")
.context("Failed to connect to Solana network")?;
// Perform operations with error handling
match client.get_sol_balance(&keypair.pubkey()) {
Ok(balance) => println!("Balance: {}", balance),
Err(e) => eprintln!("Failed to get balance: {}", e),
}
Ok(())
}- Connection Pooling: Reuse SolanaClient instances
- Batch Operations: Group multiple transactions
- Async Processing: Use tokio for concurrent operations
- Caching: Cache balance checks and account info
- Rate Limiting: Respect RPC node limits
Issue: "Failed to decrypt keystore"
- Cause: Wrong password
- Solution: Verify password or create new wallet
Issue: "Connection refused"
- Cause: RPC node unreachable
- Solution: Check network, try different RPC endpoint
Issue: "Insufficient funds"
- Cause: Not enough SOL for transaction + fees
- Solution: Ensure balance covers amount + ~0.00001 SOL fee
Issue: "Transaction failed"
- Cause: Network congestion or invalid transaction
- Solution: Retry with higher priority fee or check transaction details
- Check the User Guide for detailed operation instructions
- Review
examples/bot_example.rsfor working code - Check logs in
bot.logfor error details - Review the integration steps above to ensure correct implementation
- ✅ Complete integration (3 lines of code)
- ✅ Create test wallet on devnet
- ✅ Test all operations via safekey command
- ✅ Implement your bot logic
- ✅ Test thoroughly on devnet
- 🚀 Deploy to production
Remember: Security is paramount. Never compromise on password handling, always test on devnet first, and keep backups of your keystore files.