Spaces:
Running
Running
File size: 10,449 Bytes
2978bba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | #!/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 ""
|