/** * Wallet management commands */ import { setPrivateKey, clearPrivateKey, getPrivateKey } from "../utils.js"; import { privateKeyToAddress } from "viem/accounts"; /** * Set wallet private key */ export async function setWallet(privateKey: string): Promise { // Validate private key format if (!privateKey.startsWith('0x')) { console.error('āŒ Private key must start with 0x'); process.exit(1); } if (privateKey.length !== 66) { console.error('āŒ Invalid private key length. Expected 66 characters (0x + 64 hex chars)'); process.exit(1); } try { // Validate by deriving address const address = privateKeyToAddress(privateKey as `0x${string}`); // Store in config setPrivateKey(privateKey); console.log('āœ… Wallet configured successfully'); console.log(`šŸ“ Address: ${address}`); console.log('\nšŸ’” Your private key is stored in ~/.nla/config.json'); console.log(' It will be used automatically for all transactions'); } catch (error) { console.error('āŒ Invalid private key format'); process.exit(1); } } /** * Show current wallet address */ export async function showWallet(): Promise { const privateKey = getPrivateKey(); if (!privateKey) { console.log('ā„¹ļø No wallet configured'); console.log('\nšŸ’” Set your wallet with:'); console.log(' nla wallet:set --private-key '); return; } try { const address = privateKeyToAddress(privateKey as `0x${string}`); console.log('āœ… Wallet configured'); console.log(`šŸ“ Address: ${address}`); } catch (error) { console.error('āŒ Invalid private key in config'); console.log('\nšŸ’” Update your wallet with:'); console.log(' nla wallet:set --private-key '); } } /** * Clear wallet from config */ export async function clearWallet(): Promise { clearPrivateKey(); console.log('āœ… Wallet cleared from config'); console.log('\nšŸ’” Set a new wallet with:'); console.log(' nla wallet:set --private-key '); }