MorphGuard / scripts /setup_ethereum_node.sh
juanquy's picture
Initial clean commit of modular MorphGuard
2978bba
Raw
History Blame Contribute Delete
10.4 kB
#!/bin/bash
# ============================================================================
# MorphGuard - Ethereum Node Setup Script
# ============================================================================
# This script installs and configures a local Ethereum node (Geth) for
# blockchain-based evidence logging. It uses Sepolia Testnet by default.
#
# Usage:
# ./scripts/setup_ethereum_node.sh [--mainnet|--sepolia|--local]
#
# Options:
# --sepolia Connect to Ethereum Sepolia Testnet (default, free)
# --mainnet Connect to Ethereum Mainnet (requires ETH for gas)
# --local Start a local development chain (Ganache)
#
# Prerequisites:
# - Ubuntu/Debian-based system
# - sudo access
# - ~50GB disk space for Sepolia, ~500GB for Mainnet
# ============================================================================
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
NETWORK="${1:-sepolia}"
GETH_DATA_DIR="${MORPHGUARD_GETH_DATA_DIR:-$HOME/.ethereum}"
MORPHGUARD_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
echo -e "${BLUE}"
echo "╔══════════════════════════════════════════════════════════════════╗"
echo "║ MorphGuard - Ethereum Node Setup ║"
echo "║ Immutable Evidence Logging Infrastructure ║"
echo "╚══════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
# ============================================================================
# Helper Functions
# ============================================================================
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
check_disk_space() {
local required_gb=$1
local available_gb=$(df -BG "$HOME" | awk 'NR==2 {print $4}' | tr -d 'G')
if [ "$available_gb" -lt "$required_gb" ]; then
log_error "Insufficient disk space. Need ${required_gb}GB, have ${available_gb}GB."
exit 1
fi
log_info "Disk space check passed (${available_gb}GB available)"
}
# ============================================================================
# Install Geth (Go Ethereum)
# ============================================================================
install_geth() {
if command -v geth &> /dev/null; then
local version=$(geth version | head -n1)
log_info "Geth already installed: $version"
return 0
fi
log_info "Installing Geth (Go Ethereum Client)..."
# Add Ethereum PPA
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
# Install Geth
sudo apt-get install -y ethereum
if command -v geth &> /dev/null; then
log_info "Geth installed successfully: $(geth version | head -n1)"
else
log_error "Geth installation failed."
exit 1
fi
}
# ============================================================================
# Install Ganache (Local Development Chain)
# ============================================================================
install_ganache() {
if command -v ganache &> /dev/null; then
log_info "Ganache already installed."
return 0
fi
log_info "Installing Ganache (Local Development Blockchain)..."
# Check for npm
if ! command -v npm &> /dev/null; then
log_warn "npm not found. Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
fi
# Install Ganache globally
sudo npm install -g ganache
if command -v ganache &> /dev/null; then
log_info "Ganache installed successfully."
else
log_error "Ganache installation failed."
exit 1
fi
}
# ============================================================================
# Generate Wallet (if needed)
# ============================================================================
generate_wallet() {
local keystore_dir="$GETH_DATA_DIR/keystore"
if [ -d "$keystore_dir" ] && [ "$(ls -A $keystore_dir 2>/dev/null)" ]; then
log_info "Existing wallet found in $keystore_dir"
return 0
fi
log_info "Generating new Ethereum wallet..."
# Create a new account with a random password
local password=$(openssl rand -base64 32)
echo "$password" > "$MORPHGUARD_DIR/.eth_wallet_password"
chmod 600 "$MORPHGUARD_DIR/.eth_wallet_password"
mkdir -p "$keystore_dir"
geth account new --datadir "$GETH_DATA_DIR" --password "$MORPHGUARD_DIR/.eth_wallet_password"
# Extract wallet address
local wallet_file=$(ls "$keystore_dir" | head -n1)
local wallet_address="0x$(echo "$wallet_file" | grep -oP '(?<=--)[a-fA-F0-9]{40}')"
log_info "Wallet generated: $wallet_address"
echo "$wallet_address" > "$MORPHGUARD_DIR/.eth_wallet_address"
log_warn "IMPORTANT: Fund this wallet with Sepolia ETH from a faucet:"
echo " https://sepoliafaucet.com/"
echo " https://faucet.sepolia.dev/"
}
# ============================================================================
# Configure Environment
# ============================================================================
configure_env() {
local endpoint=$1
local network=$2
log_info "Configuring MorphGuard environment for $network..."
local env_file="$MORPHGUARD_DIR/.env"
# Read wallet address
local wallet_address=""
if [ -f "$MORPHGUARD_DIR/.eth_wallet_address" ]; then
wallet_address=$(cat "$MORPHGUARD_DIR/.eth_wallet_address")
fi
# Append or update blockchain settings
if [ -f "$env_file" ]; then
# Remove old blockchain settings
sed -i '/^BLOCKCHAIN_/d' "$env_file"
sed -i '/^MORPHGUARD_ETH_/d' "$env_file"
fi
cat >> "$env_file" << EOF
# ============================================================================
# Blockchain Configuration (Auto-generated by setup_ethereum_node.sh)
# ============================================================================
BLOCKCHAIN_ENABLED=true
BLOCKCHAIN_NETWORK=$network
BLOCKCHAIN_ENDPOINT=$endpoint
MORPHGUARD_ETH_WALLET_ADDRESS=$wallet_address
MORPHGUARD_ETH_KEYSTORE_DIR=$GETH_DATA_DIR/keystore
EOF
log_info "Environment configured in $env_file"
}
# ============================================================================
# Create Systemd Service (for production)
# ============================================================================
create_systemd_service() {
local network=$1
log_info "Creating systemd service for Geth..."
sudo tee /etc/systemd/system/morphguard-geth.service > /dev/null << EOF
[Unit]
Description=MorphGuard Ethereum Node (Geth)
After=network.target
[Service]
Type=simple
User=$USER
ExecStart=/usr/bin/geth --$network --http --http.addr 0.0.0.0 --http.port 8545 --http.api eth,net,web3,personal --http.corsdomain "*" --datadir $GETH_DATA_DIR --syncmode snap
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
log_info "Systemd service created: morphguard-geth.service"
}
# ============================================================================
# Main Setup Logic
# ============================================================================
case "$NETWORK" in
--sepolia|sepolia)
log_info "Setting up Ethereum Sepolia Testnet..."
check_disk_space 50
install_geth
generate_wallet
create_systemd_service "sepolia"
configure_env "http://localhost:8545" "ethereum-sepolia"
echo ""
echo -e "${GREEN}Setup Complete!${NC}"
echo ""
echo "To start the Ethereum node:"
echo " sudo systemctl start morphguard-geth"
echo " sudo systemctl enable morphguard-geth # Auto-start on boot"
echo ""
echo "To check sync status:"
echo " geth attach http://localhost:8545 --exec 'eth.syncing'"
echo ""
log_warn "Initial sync may take 2-6 hours depending on network speed."
;;
--mainnet|mainnet)
log_info "Setting up Ethereum Mainnet..."
log_warn "Mainnet requires real ETH for gas fees!"
check_disk_space 500
install_geth
generate_wallet
create_systemd_service "mainnet"
configure_env "http://localhost:8545" "ethereum-mainnet"
echo ""
echo -e "${GREEN}Setup Complete!${NC}"
echo ""
log_warn "Mainnet sync can take 1-3 days and requires 500GB+ storage."
;;
--local|local)
log_info "Setting up Local Development Chain (Ganache)..."
install_ganache
# Start Ganache in background
log_info "Starting Ganache..."
ganache --detach --port 8545 --mnemonic "morphguard test mnemonic do not use in production" > /dev/null 2>&1 &
# Use pre-funded test account from Ganache
local test_wallet="0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"
echo "$test_wallet" > "$MORPHGUARD_DIR/.eth_wallet_address"
configure_env "http://localhost:8545" "local-ganache"
echo ""
echo -e "${GREEN}Setup Complete!${NC}"
echo ""
echo "Ganache is running on http://localhost:8545"
echo "Test accounts are pre-funded with 1000 ETH each."
echo ""
echo "To restart Ganache:"
echo " ganache --port 8545"
;;
*)
echo "Usage: $0 [--sepolia|--mainnet|--local]"
echo ""
echo "Options:"
echo " --sepolia Ethereum Sepolia Testnet (default, free)"
echo " --mainnet Ethereum Mainnet (requires ETH)"
echo " --local Local Ganache development chain"
exit 1
;;
esac
echo ""
echo -e "${BLUE}Next Steps:${NC}"
echo "1. Start MorphGuard: ./start_morphguard.sh"
echo "2. Enable blockchain logging in Admin Panel > Forensic Evidence"
echo "3. Check connection status in the UI"
echo ""