#!/usr/bin/env bash # Bootstrap local Z Chain (chainId 900002) with Hyperledger Besu via Docker. set -euo pipefail ROOT="$(cd "$(dirname "$0")/../.." && pwd)" NETWORK_DIR="${Z_CHAIN_NETWORK_DIR:-$ROOT/logs/z-chain-network}" GENESIS_CONFIG="$ROOT/config/z-chain-network-config.json" RPC_PORT="${Z_CHAIN_RPC_PORT:-8546}" CONTAINER_NAME="${Z_CHAIN_BESU_CONTAINER:-z-chain-besu}" cd "$ROOT" if ! command -v docker &>/dev/null; then echo "ERROR: docker is required. Install Docker and retry." exit 1 fi if [[ ! -f "$GENESIS_CONFIG" ]]; then echo "ERROR: missing $GENESIS_CONFIG" exit 1 fi mkdir -p "$NETWORK_DIR" if [[ ! -f "$NETWORK_DIR/genesis.json" ]]; then echo "Generating IBFT2 network files in $NETWORK_DIR ..." docker run --rm \ -v "$ROOT/config:/config:ro" \ -v "$NETWORK_DIR:/network" \ hyperledger/besu:latest \ operator generate-blockchain-config \ --config-file=/config/z-chain-network-config.json \ --to=/network fi if docker ps -a --format '{{.Names}}' | grep -qx "$CONTAINER_NAME"; then echo "Removing existing container $CONTAINER_NAME ..." docker rm -f "$CONTAINER_NAME" >/dev/null fi NODE_KEY="$NETWORK_DIR/keys/node1/key" GENESIS="$NETWORK_DIR/genesis.json" if [[ ! -f "$NODE_KEY" || ! -f "$GENESIS" ]]; then echo "ERROR: generated network files missing under $NETWORK_DIR" exit 1 fi echo "Starting Z Chain Besu on http://127.0.0.1:$RPC_PORT (chainId 900002) ..." docker run -d \ --name "$CONTAINER_NAME" \ -p "${RPC_PORT}:8545" \ -v "$NETWORK_DIR:/network:ro" \ hyperledger/besu:latest \ --data-path=/tmp/besu \ --genesis-file=/network/genesis.json \ --node-private-key-file=/network/keys/node1/key \ --rpc-http-enabled \ --rpc-http-api=ETH,NET,WEB3,IBFT,ADMIN,DEBUG \ --rpc-http-host=0.0.0.0 \ --rpc-http-port=8545 \ --rpc-http-cors-origins="*" \ --host-allowlist="*" \ --min-gas-price=0 echo "" echo "Z Chain local RPC: http://127.0.0.1:$RPC_PORT" echo "Export: CHAIN_900002_RPC_URL=http://127.0.0.1:$RPC_PORT" echo "Export: VITE_RPC_URL_900002=http://127.0.0.1:$RPC_PORT" echo "Verify: cast chain-id --rpc-url http://127.0.0.1:$RPC_PORT"