@@ -482,6 +482,231 @@ check_wallet_setup() {
482482fi
483483}
484484
485+ # Generate a secure Ethereum private key
486+ generate_ethereum_private_key () {
487+ # Try multiple methods to generate a secure 32-byte private key
488+
489+ # Method 1: Use openssl if available
490+ if command_exists openssl; then
491+ openssl rand -hex 322> /dev/null&& return
492+ fi
493+
494+ # Method 2: Use /dev/urandom if available (Linux/macOS)
495+ if [[-r " /dev/urandom" ]]; then
496+ head -c 32 /dev/urandom| xxd -p -c 322> /dev/null&& return
497+ fi
498+
499+ # Method 3: Use Python if available
500+ if command_exists python3; then
501+ python3 -c" import secrets; print(secrets.token_hex(32))" 2> /dev/null&& return
502+ fi
503+
504+ # Method 4: Use Node.js if available
505+ if command_exists node; then
506+ node -e" console.log(require('crypto').randomBytes(32).toString('hex'))" 2> /dev/null&& return
507+ fi
508+
509+ # Method 5: Fallback using bash RANDOM (less secure, but better than nothing)
510+ log_warning" Using less secure fallback method for private key generation"
511+ local key=" "
512+ for i in {1..64}; do
513+ key+=$( printf" %x" $(( RANDOM% 16 )) )
514+ done
515+ echo " $key "
516+ }
517+
518+ # Generate a BIP39 mnemonic phrase
519+ generate_mnemonic () {
520+ # BIP39 wordlist (first 128 words for simplicity - enough for basic generation)
521+ local words=(
522+ " abandon" " ability" " able" " about" " above" " absent" " absorb" " abstract"
523+ " absurd" " abuse" " access" " accident" " account" " accuse" " achieve" " acid"
524+ " acoustic" " acquire" " across" " act" " action" " actor" " actress" " actual"
525+ " adapt" " add" " addict" " address" " adjust" " admit" " adult" " advance"
526+ " advice" " aerobic" " affair" " afford" " afraid" " again" " age" " agent"
527+ " agree" " ahead" " aim" " air" " airport" " aisle" " alarm" " album"
528+ " alcohol" " alert" " alien" " all" " alley" " allow" " almost" " alone"
529+ " alpha" " already" " also" " alter" " always" " amateur" " amazing" " among"
530+ " amount" " amused" " analyst" " anchor" " ancient" " anger" " angle" " angry"
531+ " animal" " ankle" " announce" " annual" " another" " answer" " antenna" " antique"
532+ " anxiety" " any" " apart" " apology" " appear" " apple" " approve" " april"
533+ " arch" " arctic" " area" " arena" " argue" " arm" " armed" " armor"
534+ " army" " around" " arrange" " arrest" " arrive" " arrow" " art" " article"
535+ " artist" " artwork" " ask" " aspect" " assault" " asset" " assist" " assume"
536+ " asthma" " athlete" " atom" " attack" " attend" " attitude" " attract" " auction"
537+ " audit" " august" " aunt" " author" " auto" " autumn" " average" " avocado"
538+ " avoid" " awake" " aware" " away" " awesome" " awful" " awkward" " axis"
539+ )
540+
541+ # Generate 12 random words
542+ local mnemonic=" "
543+ local word_count=${# words[@]}
544+
545+ # Try to use secure random number generation
546+ for i in {1..12}; do
547+ local index
548+
549+ # Method 1: Use openssl for random number
550+ if command_exists openssl; then
551+ index=$( openssl rand -hex 1| head -c 2)
552+ index=$(( 0 x$index % word_count))
553+ # Method 2: Use /dev/urandom
554+ elif [[-r " /dev/urandom" ]]; then
555+ index=$( head -c 1 /dev/urandom| od -An -tu1| tr -d' ' )
556+ index=$(( index% word_count))
557+ # Method 3: Use Python
558+ elif command_exists python3; then
559+ index=$( python3 -c" import random; print(random.randint(0,$(( word_count- 1 )) ))" 2> /dev/null)
560+ # Method 4: Fallback to bash RANDOM
561+ else
562+ index=$(( RANDOM% word_count))
563+ fi
564+
565+ if [[$i -eq 1 ]]; then
566+ mnemonic=" ${words[$index]} "
567+ else
568+ mnemonic=" $mnemonic ${words[$index]} "
569+ fi
570+ done
571+
572+ echo " $mnemonic "
573+ }
574+
575+ # Collect user credentials and create .env file
576+ setup_user_credentials () {
577+ log_step" Setting up user credentials..."
578+
579+ cd " $INSTALL_DIR "
580+
581+ # Check if .env already exists
582+ if [[-f " .env" ]]; then
583+ log_info" .env file already exists. Checking contents..."
584+ if grep -q" PRIVATE_KEY=" .env&& grep -q" COLONY_MNEMONIC=" .env; then
585+ log_info" Credentials already configured in .env file"
586+ return 0
587+ fi
588+ fi
589+
590+ echo " "
591+ log_highlight" 🔐 Credential Setup"
592+ echo " MutAnt needs your private key and colony mnemonic to function properly."
593+ echo " These will be stored securely in a .env file in the installation directory."
594+ echo " "
595+ echo " Options:"
596+ echo " - Enter your existing credentials"
597+ echo " - Press Enter to generate new ones automatically"
598+ echo " - Type 'skip' to run in public-only mode (download only)"
599+ echo " - Generated credentials will be cryptographically secure"
600+ echo " "
601+
602+ # Ask for private key
603+ echo -n" Enter your private key (hex format, Enter to generate, or 'skip' for public-only):"
604+ read -r PRIVATE_KEY
605+
606+ if [[" $PRIVATE_KEY " == " skip" ]]; then
607+ log_info" Skipping credential setup. Daemon will run in public-only mode."
608+ PRIVATE_KEY=" "
609+ COLONY_MNEMONIC=" "
610+ # Create minimal .env file
611+ cat> .env<< EOF
612+ # MutAnt Configuration - Public-only mode
613+ # Generated by install script on$( date)
614+
615+ # No credentials configured - running in public-only mode
616+ PRIVATE_KEY=""
617+ COLONY_MNEMONIC=""
618+ EOF
619+ chmod 600 .env
620+ log_success" Created .env file for public-only mode"
621+ return 0
622+ elif [[-z " $PRIVATE_KEY " ]]; then
623+ log_info" No private key provided. Generating a new Ethereum private key..."
624+ PRIVATE_KEY=$( generate_ethereum_private_key)
625+ if [[-n " $PRIVATE_KEY " ]]; then
626+ log_success" Generated new private key:$PRIVATE_KEY "
627+ log_warning" ⚠️ IMPORTANT: Save this private key securely! You'll need it to access your data."
628+ else
629+ log_error" Failed to generate private key. Daemon will run in public-only mode."
630+ PRIVATE_KEY=" "
631+ fi
632+ else
633+ # Basic validation - check if it looks like a hex string
634+ if [[! " $PRIVATE_KEY " =~ ^[0-9a-fA-F]+$ ]]; then
635+ log_warning" Private key doesn't appear to be valid hex format, but continuing..."
636+ fi
637+ fi
638+
639+ echo " "
640+ # Ask for colony mnemonic
641+ echo -n" Enter your colony mnemonic (12-24 words, or press Enter to generate a new one):"
642+ read -r COLONY_MNEMONIC
643+
644+ if [[-z " $COLONY_MNEMONIC " ]]; then
645+ log_info" No colony mnemonic provided. Generating a new 12-word mnemonic..."
646+ COLONY_MNEMONIC=$( generate_mnemonic)
647+ if [[-n " $COLONY_MNEMONIC " ]]; then
648+ log_success" Generated new mnemonic:$COLONY_MNEMONIC "
649+ log_warning" ⚠️ IMPORTANT: Save this mnemonic securely! You'll need it for colony features."
650+ else
651+ log_error" Failed to generate mnemonic. Colony features will be disabled."
652+ COLONY_MNEMONIC=" "
653+ fi
654+ fi
655+
656+ # Create .env file
657+ log_info" Creating .env file..."
658+ cat> .env<< EOF
659+ # MutAnt Configuration
660+ # Generated by install script on$( date)
661+
662+ # Private key for Autonomi network access (hex format)
663+ PRIVATE_KEY="$PRIVATE_KEY "
664+
665+ # Colony mnemonic for decentralized social features (12-24 words)
666+ COLONY_MNEMONIC="$COLONY_MNEMONIC "
667+ EOF
668+
669+ # Set appropriate permissions
670+ chmod 600 .env
671+
672+ log_success" Credentials saved to .env file"
673+ log_info" File permissions set to 600 (owner read/write only)"
674+ echo " "
675+ }
676+
677+ # Load and export environment variables from .env file
678+ load_environment () {
679+ log_step" Loading environment variables..."
680+
681+ cd " $INSTALL_DIR "
682+
683+ if [[-f " .env" ]]; then
684+ log_info" Loading variables from .env file..."
685+
686+ # Export variables from .env file
687+ set -a# Automatically export all variables
688+ source .env
689+ set +a# Stop automatically exporting
690+
691+ # Verify variables are loaded
692+ if [[-n " $PRIVATE_KEY " ]]; then
693+ log_info" Private key loaded (${# PRIVATE_KEY} characters)"
694+ else
695+ log_info" No private key found in .env"
696+ fi
697+
698+ if [[-n " $COLONY_MNEMONIC " ]]; then
699+ log_info" Colony mnemonic loaded"
700+ else
701+ log_info" No colony mnemonic found in .env"
702+ fi
703+
704+ log_success" Environment variables loaded"
705+ else
706+ log_warning" No .env file found, continuing without custom credentials"
707+ fi
708+ }
709+
485710# Start daemon
486711start_daemon () {
487712 log_step" Starting MutAnt daemon..."
@@ -600,10 +825,11 @@ print_final_instructions() {
600825echo " ps aux | grep trunk # Check web server process"
601826echo " "
602827
603- log_highlight" 📁 Important Directories:"
828+ log_highlight" 📁 ImportantFiles & Directories:"
604829echo " Installation:$INSTALL_DIR "
605830echo " Config: ~/.config/mutant/"
606831echo " Logs: ~/.local/share/mutant/"
832+ echo " Credentials:$INSTALL_DIR /.env"
607833echo " "
608834
609835if [[" $WALLET_CONFIGURED " == " false" ]]; then
@@ -640,6 +866,22 @@ print_final_instructions() {
640866echo " cd$INSTALL_DIR && ./install.sh --restart-only"
641867echo " "
642868
869+ log_highlight" 🔐 Credential Management:"
870+ echo " Your credentials are stored in:$INSTALL_DIR /.env"
871+ echo " To view your credentials: cat$INSTALL_DIR /.env"
872+ echo " To update credentials: edit$INSTALL_DIR /.env with your preferred editor"
873+ echo " After editing: cd$INSTALL_DIR && ./install.sh --restart-only"
874+ echo " "
875+
876+ # Show generated credentials warning if .env exists
877+ if [[-f " $INSTALL_DIR /.env" ]]; then
878+ echo -e" ${YELLOW} ⚠️ SECURITY REMINDER:${NC} "
879+ echo " - Keep your private key and mnemonic secure and backed up"
880+ echo " - Never share these credentials with anyone"
881+ echo " - Consider storing a backup in a secure location"
882+ echo " "
883+ fi
884+
643885 log_info" For more information, visit: https://github.com/Champii/Anthill"
644886echo " "
645887}
@@ -704,8 +946,9 @@ main() {
704946 pkill trunk|| true
705947 sleep 2
706948
707- # Start services
949+ # Load environment and start services
708950cd " $INSTALL_DIR " || exit 1
951+ load_environment
709952 start_daemon
710953 start_web_server
711954
@@ -735,6 +978,10 @@ main() {
735978 setup_configuration
736979 check_wallet_setup
737980
981+ # Setup user credentials and environment
982+ setup_user_credentials
983+ load_environment
984+
738985# Start services
739986 start_daemon
740987 start_web_server