Repository Public
1#!/usr/bin/env bash2# SP Wallet Core3# File: Scripts/dev-stack.sh4# Description: Throwaway local SP Wallet stack for the native clients' integration tests: a NEW random signer keystore (no real5# funds, ever), its own Postgres database, broadcast OFF (a signed transaction is never relayed), http://localhost.6# usage: dev-stack.sh up | up-fork | down | status | env (SPW_NATIVE_PORT, default 8750; signer on port+1)7# up-fork: Base is served by a LOCAL anvil fork (port+2) where the throwaway wallet is given 1 fake ETH; broadcasting8# is on, but the only funded chain is that local fork — the whole send path runs with no real funds anywhere.9#10# Author: Simon-Pierre Boucher11# Contact: contact@spboucher.ai12set -euo pipefail13# the backend build to run: the worktree of the production main (it returns the canonical object on every approval route)14REPO="${SPW_REPO:-$HOME/Desktop/Cluster/projets/sp-wallet-native}"15P="${SPW_NATIVE_PORT:-8750}"; SP=$((P + 1))16[[ "$P" =~ ^[0-9]{4,5}$ ]] || { echo "bad port"; exit 2; }17# the stack directory is always /tmp/spw-native-<port>/<run id>: a fresh one per start, nothing is ever deleted by this script18BASE="/tmp/spw-native-$P"; DB="spw_native_$P"19cmd="${1:-status}"2021down() {22 local f pid port23 for f in api signer anvil; do24 if [[ -s "$BASE/current/$f.pid" ]]; then kill "$(cat "$BASE/current/$f.pid")" 2>/dev/null || true; : > "$BASE/current/$f.pid"; fi25 done26 # whatever still listens on this stack's three ports is a previous run of this stack: stop it by the PID of the port27 for port in "$P" "$SP" "$((P + 2))"; do28 for pid in $(lsof -nP -tiTCP:"$port" -sTCP:LISTEN 2>/dev/null); do kill "$pid" 2>/dev/null || true; done29 done30 sleep 0.531}3233case "$cmd" in34 up|up-fork)35 down36 [[ -f "$REPO/dist/api.mjs" && -f "$REPO/dist/signer.mjs" ]] || { echo "build the backend first (node scripts/build.mjs in $REPO)"; exit 1; }37 D="$BASE/run-$(date +%Y%m%d-%H%M%S)"; mkdir -p "$D"; ln -sfn "$D" "$BASE/current"38 psql -q -d postgres -tc "select 1 from pg_database where datname='$DB'" | grep -q 1 || createdb "$DB"39 psql -q -d "$DB" -c 'set client_min_messages = warning; drop schema public cascade; create schema public;' >/dev/null 2>&140 export SPW_SIGNER_DIR="$D/signer" SPW_SIGNER_WRAP_KEY="$D/wrap/wrap.key" SPW_SIGNER_SECRET="$(openssl rand -hex 32)"41 export SPW_ORIGIN="http://localhost:$P" SPW_RP_ID=localhost SPW_SIGNER_PORT="$SP"42 ( cd "$REPO" && node dist/signer.mjs init > "$D/init.json" && node dist/signer.mjs setup-token > "$D/setup-token" )43 ( cd "$REPO" && nohup node dist/signer.mjs serve > "$D/signer.log" 2>&1 & echo $! > "$D/signer.pid" )44 BROADCAST=off; FORK=""45 if [[ "$cmd" == up-fork ]]; then46 FP=$((P + 2)); FORK="http://127.0.0.1:$FP"47 ( nohup anvil --fork-url "${SPW_NATIVE_BASE_RPC:-https://mainnet.base.org}" --port "$FP" --chain-id 8453 --block-time 2 --silent > "$D/anvil.log" 2>&1 & echo $! > "$D/anvil.pid" )48 for _ in $(seq 1 80); do curl -sf -X POST -H 'content-type: application/json' -d '{"jsonrpc":"2.0","id":1,"method":"eth_chainId","params":[]}' "$FORK" >/dev/null && break; sleep 0.25; done49 ME=$(cd "$REPO" && node dist/signer.mjs status | python3 -c 'import sys,json; print(json.load(sys.stdin)["addresses"]["base"])')50 curl -sf -X POST -H 'content-type: application/json' -d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"anvil_setBalance\",\"params\":[\"$ME\",\"0xde0b6b3a7640000\"]}" "$FORK" >/dev/null51 echo "$ME" > "$D/fork-address"52 export SPW_RPC_OVERRIDE="{\"base\":[\"$FORK\",\"$FORK\"]}"53 BROADCAST=on54 fi55 # SPW_BROADCAST=off (plain `up`): signed bytes never leave this machine. Every live-trading lock stays at its default (off).56 ( cd "$REPO" && DATABASE_URL="postgres:///$DB" SPW_SIGNER_URL="http://127.0.0.1:$SP" SPW_WEB_DIR="$REPO/dist/web" PORT="$P" SPW_BROADCAST="$BROADCAST" \57 SPW_WORKERS="${SPW_NATIVE_WORKERS:-on}" nohup node dist/api.mjs > "$D/api.log" 2>&1 & echo $! > "$D/api.pid" )58 for _ in $(seq 1 80); do curl -sf "http://localhost:$P/api/v1/health/live" >/dev/null && break; sleep 0.25; done59 curl -sf "http://localhost:$P/api/v1/health/live" >/dev/null || { echo "API did not start; see $D/api.log"; tail -5 "$D/api.log"; exit 1; }60 echo "stack up: http://localhost:$P (dir $BASE/current, db $DB, broadcast $BROADCAST${FORK:+, Base = local fork $FORK with 1 fake ETH})"61 ;;62 down) down; echo "stack down" ;;63 status) curl -sf "http://localhost:$P/api/v1/auth/state" && echo || echo "down" ;;64 env) echo "SPW_LIVE_ORIGIN=http://localhost:$P SPW_LIVE_DIR=$BASE/current" ;;65 *) echo "usage: $0 up|up-fork|down|status|env"; exit 2 ;;66esac67