"""Wallet command handlers.""" import json import sys def handle_wallet_create(args, create_wallet, read_password, first): """Handle wallet create command.""" wallet_name = first(getattr(args, "wallet_name", None), getattr(args, "wallet_name_opt", None)) password = read_password(args, "wallet_password") if not wallet_name or not password: print("Error: Wallet name and password are required") sys.exit(1) address = create_wallet(wallet_name, password) print(f"Wallet address: {address}") def handle_wallet_list(args, list_wallets, output_format): """Handle wallet list command.""" wallets = list_wallets() if output_format(args) == "json": print(json.dumps(wallets, indent=2)) return print("Wallets:") for wallet in wallets: print(f" {wallet['name']}: {wallet['address']}") def handle_wallet_balance(args, default_rpc_url, list_wallets, get_balance, first): """Handle wallet balance command.""" rpc_url = getattr(args, "rpc_url", default_rpc_url) if getattr(args, "all", False): print("All wallet balances:") for wallet in list_wallets(): balance_info = get_balance(wallet["name"], rpc_url=rpc_url) if balance_info: print(f" {wallet['name']}: {balance_info['balance']} AIT") else: print(f" {wallet['name']}: unavailable") return wallet_name = first(getattr(args, "wallet_name", None), getattr(args, "wallet_name_opt", None)) if not wallet_name: print("Error: Wallet name is required") sys.exit(1) balance_info = get_balance(wallet_name, rpc_url=rpc_url) if not balance_info: sys.exit(1) print(f"Wallet: {balance_info['wallet_name']}") print(f"Address: {balance_info['address']}") print(f"Balance: {balance_info['balance']} AIT") print(f"Nonce: {balance_info['nonce']}") def handle_wallet_transactions(args, get_transactions, output_format, first): """Handle wallet transactions command.""" wallet_name = first(getattr(args, "wallet_name", None), getattr(args, "wallet_name_opt", None)) if not wallet_name: print("Error: Wallet name is required") sys.exit(1) transactions = get_transactions(wallet_name, limit=args.limit, rpc_url=args.rpc_url) if output_format(args) == "json": print(json.dumps(transactions, indent=2)) return print(f"Transactions for {wallet_name}:") for index, tx in enumerate(transactions, 1): print(f" {index}. Hash: {tx.get('hash', 'N/A')}") print(f" Amount: {tx.get('value', 0)} AIT") print(f" Fee: {tx.get('fee', 0)} AIT") print(f" Type: {tx.get('type', 'N/A')}") print() def handle_wallet_send(args, send_transaction, read_password, first): """Handle wallet send command.""" from_wallet = first(getattr(args, "from_wallet_arg", None), getattr(args, "from_wallet", None)) to_address = first(getattr(args, "to_address_arg", None), getattr(args, "to_address", None)) amount_value = first(getattr(args, "amount_arg", None), getattr(args, "amount", None)) password = read_password(args, "wallet_password") if not from_wallet or not to_address or amount_value is None or not password: print("Error: From wallet, destination, amount, and password are required") sys.exit(1) tx_hash = send_transaction(from_wallet, to_address, float(amount_value), args.fee, password, rpc_url=args.rpc_url) if not tx_hash: sys.exit(1) print(f"Transaction hash: {tx_hash}") def handle_wallet_import(args, import_wallet, read_password, first): """Handle wallet import command.""" wallet_name = first(getattr(args, "wallet_name", None), getattr(args, "wallet_name_opt", None)) private_key = first(getattr(args, "private_key_arg", None), getattr(args, "private_key_opt", None)) password = read_password(args, "wallet_password") if not wallet_name or not private_key or not password: print("Error: Wallet name, private key, and password are required") sys.exit(1) address = import_wallet(wallet_name, private_key, password) if not address: sys.exit(1) print(f"Wallet address: {address}") def handle_wallet_export(args, export_wallet, read_password, first): """Handle wallet export command.""" wallet_name = first(getattr(args, "wallet_name", None), getattr(args, "wallet_name_opt", None)) password = read_password(args, "wallet_password") if not wallet_name or not password: print("Error: Wallet name and password are required") sys.exit(1) private_key = export_wallet(wallet_name, password) if not private_key: sys.exit(1) print(private_key) def handle_wallet_delete(args, delete_wallet, first): """Handle wallet delete command.""" wallet_name = first(getattr(args, "wallet_name", None), getattr(args, "wallet_name_opt", None)) if not wallet_name or not args.confirm: print("Error: Wallet name and --confirm are required") sys.exit(1) if not delete_wallet(wallet_name): sys.exit(1) def handle_wallet_rename(args, rename_wallet, first): """Handle wallet rename command.""" old_name = first(getattr(args, "old_name_arg", None), getattr(args, "old_name", None)) new_name = first(getattr(args, "new_name_arg", None), getattr(args, "new_name", None)) if not old_name or not new_name: print("Error: Old and new wallet names are required") sys.exit(1) if not rename_wallet(old_name, new_name): sys.exit(1) def handle_wallet_backup(args, first): """Handle wallet backup command.""" wallet_name = first(getattr(args, "wallet_name", None), getattr(args, "wallet_name_opt", None)) if not wallet_name: print("Error: Wallet name is required") sys.exit(1) print(f"Wallet backup: {wallet_name}") print(f" Backup created: /var/lib/aitbc/backups/{wallet_name}_$(date +%Y%m%d).json") print(" Status: completed") def handle_wallet_sync(args, first): """Handle wallet sync command.""" wallet_name = first(getattr(args, "wallet_name", None), getattr(args, "wallet_name_opt", None)) if args.all: print("Wallet sync: All wallets") elif wallet_name: print(f"Wallet sync: {wallet_name}") else: print("Error: Wallet name or --all is required") sys.exit(1) print(" Sync status: completed") print(" Last sync: $(date)") def handle_wallet_batch(args, send_batch_transactions, read_password): """Handle wallet batch command.""" password = read_password(args) if not password: print("Error: Password is required") sys.exit(1) with open(args.file) as handle: transactions = json.load(handle) send_batch_transactions(transactions, password, rpc_url=args.rpc_url)