1.0.1 • Published 3 months ago
@solagent/wallet v1.0.1
@solagent/wallet
A secure, type-safe Solana wallet management library for the SolAgent ecosystem. Easily generate, import, and manage Solana keypairs with robust error handling.
Features
- 🔐 Generate new Solana wallets with one line of code
- 📁 Load wallets from environment variables, files, or raw private keys
- 🛡️ Type-safe implementation with comprehensive error handling
- 🔄 Convert between base58 and native key formats
- 📦 Lightweight with zero unnecessary dependencies
Installation
npm install @solagent/wallet
# or
yarn add @solagent/wallet
Usage
Basic Wallet Operations
import { Wallet } from '@solagent/wallet';
// Create a new wallet
const wallet = new Wallet();
console.log('Public key:', wallet.pubkey);
console.log('Private key:', wallet.toBase58());
// Import from private key
const importedWallet = Wallet.fromBase58(wallet.toBase58());
console.log('Imported public key:', importedWallet.pubkey);
Environment Variables
// .env file:
// PRIVATE_KEY=your_base58_encoded_private_key_here
const envWallet = Wallet.fromEnv('PRIVATE_KEY');
File Operations
// Save wallet to file
const wallet = new Wallet();
wallet.saveToFile('my-wallet.key');
// Load from file later
const loadedWallet = Wallet.fromFile('my-wallet.key');
console.log('Loaded public key:', loadedWallet.pubkey);
Error Handling
try {
const wallet = Wallet.fromBase58('invalid_key');
} catch (error) {
if (error instanceof WalletError) {
console.error('Wallet error:', error.message);
}
}
API Reference
Wallet
new Wallet()
- Creates a new random walletWallet.fromBase58(privateKey: string)
- Creates wallet from base58 private keyWallet.fromEnv(variableName: string)
- Creates wallet from environment variableWallet.fromFile(filePath: string)
- Creates wallet from key file
Instance Properties
keypair: Keypair
- The underlying Solana keypairpubkey: string
- Base58 encoded public key
Instance Methods
toBase58(): string
- Returns private key as base58 stringsaveToFile(filePath: string): void
- Saves private key to file
Security Considerations
- Always store private keys securely
- Never commit key files or .env files to version control
- Consider using hardware wallets for production deployments
Contributing
Pull requests are welcome! Please ensure:
- All tests pass (
npm test
) - Code is properly typed
- New features include tests
License
Apache 2.0