Compare commits
26 Commits
devin/1776
...
devin/1776
| Author | SHA1 | Date | |
|---|---|---|---|
| 78e1ff5dc8 | |||
| fbe0f3e4aa | |||
| 791184be34 | |||
| 14b04f2730 | |||
| 152e0d7345 | |||
| 16d21345d7 | |||
| 6edaffb57f | |||
| 9d0c4394ec | |||
| 19bafbc53b | |||
| 4887e689d7 | |||
| 12ea869f7e | |||
| e43575ea26 | |||
| 2c8d3d222e | |||
| d4849da50d | |||
| c16a7855d5 | |||
| 08946a1971 | |||
| 174cbfde04 | |||
| 8c7e1c70de | |||
| 29fe704f3c | |||
| 070f935e46 | |||
| 945e637d1d | |||
| f4e235edc6 | |||
| 66f35fa2aa | |||
|
|
def11dd624 | ||
| ad69385beb | |||
| db4b9a4240 |
141
.github/workflows/ci.yml
vendored
141
.github/workflows/ci.yml
vendored
@@ -2,71 +2,102 @@ name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main, develop ]
|
||||
branches: [ master, main, develop ]
|
||||
pull_request:
|
||||
branches: [ main, develop ]
|
||||
branches: [ master, main, develop ]
|
||||
|
||||
# Cancel in-flight runs on the same ref to save CI minutes.
|
||||
concurrency:
|
||||
group: ci-${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
GO_VERSION: '1.23.4'
|
||||
NODE_VERSION: '20'
|
||||
|
||||
jobs:
|
||||
test-backend:
|
||||
name: Backend (go 1.23.x)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.22'
|
||||
- name: Run tests
|
||||
run: |
|
||||
cd backend
|
||||
go test ./...
|
||||
- name: Build
|
||||
run: |
|
||||
cd backend
|
||||
go build ./...
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
cache-dependency-path: backend/go.sum
|
||||
- name: go vet
|
||||
working-directory: backend
|
||||
run: go vet ./...
|
||||
- name: go build
|
||||
working-directory: backend
|
||||
run: go build ./...
|
||||
- name: go test
|
||||
working-directory: backend
|
||||
run: go test ./...
|
||||
|
||||
scan-backend:
|
||||
name: Backend security scanners
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
cache-dependency-path: backend/go.sum
|
||||
- name: Install staticcheck
|
||||
run: go install honnef.co/go/tools/cmd/staticcheck@v0.5.1
|
||||
- name: Install govulncheck
|
||||
run: go install golang.org/x/vuln/cmd/govulncheck@latest
|
||||
- name: staticcheck
|
||||
working-directory: backend
|
||||
run: staticcheck ./...
|
||||
- name: govulncheck
|
||||
working-directory: backend
|
||||
run: govulncheck ./...
|
||||
|
||||
test-frontend:
|
||||
name: Frontend (node 20)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '20'
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
cd frontend
|
||||
npm ci
|
||||
- name: Run tests
|
||||
run: |
|
||||
cd frontend
|
||||
npm test
|
||||
- name: Build
|
||||
run: |
|
||||
cd frontend
|
||||
npm run build
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
cache: 'npm'
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
- name: Install dependencies
|
||||
working-directory: frontend
|
||||
run: npm ci
|
||||
- name: Lint (eslint)
|
||||
working-directory: frontend
|
||||
run: npm run lint
|
||||
- name: Type-check (tsc)
|
||||
working-directory: frontend
|
||||
run: npm run type-check
|
||||
- name: Build
|
||||
working-directory: frontend
|
||||
run: npm run build
|
||||
|
||||
lint:
|
||||
gitleaks:
|
||||
name: gitleaks (secret scan)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.22'
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '20'
|
||||
- name: Backend lint
|
||||
run: |
|
||||
cd backend
|
||||
go vet ./...
|
||||
- name: Frontend lint
|
||||
run: |
|
||||
cd frontend
|
||||
npm ci
|
||||
npm run lint
|
||||
npm run type-check
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
# Full history so we can also scan past commits, not just the tip.
|
||||
fetch-depth: 0
|
||||
- name: Run gitleaks
|
||||
uses: gitleaks/gitleaks-action@v2
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
# Repo-local config lives at .gitleaks.toml.
|
||||
GITLEAKS_CONFIG: .gitleaks.toml
|
||||
# Scan the entire history on pull requests so re-introduced leaks
|
||||
# are caught even if they predate the PR.
|
||||
GITLEAKS_ENABLE_SUMMARY: 'true'
|
||||
|
||||
71
.github/workflows/e2e-full.yml
vendored
Normal file
71
.github/workflows/e2e-full.yml
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
name: e2e-full
|
||||
|
||||
# Boots the full explorer stack (docker-compose deps + backend + frontend)
|
||||
# and runs the Playwright full-stack smoke spec against it. Not on every
|
||||
# PR (too expensive) — runs on:
|
||||
#
|
||||
# * workflow_dispatch (manual)
|
||||
# * pull_request when the 'run-e2e-full' label is applied
|
||||
# * nightly at 04:00 UTC
|
||||
#
|
||||
# Screenshots from every route are uploaded as a build artefact so
|
||||
# reviewers can eyeball the render without having to boot the stack.
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
pull_request:
|
||||
types: [labeled, opened, synchronize, reopened]
|
||||
schedule:
|
||||
- cron: '0 4 * * *'
|
||||
|
||||
jobs:
|
||||
e2e-full:
|
||||
if: >
|
||||
github.event_name == 'workflow_dispatch' ||
|
||||
github.event_name == 'schedule' ||
|
||||
(github.event_name == 'pull_request' &&
|
||||
contains(github.event.pull_request.labels.*.name, 'run-e2e-full'))
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.23.x'
|
||||
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
cache: 'npm'
|
||||
cache-dependency-path: frontend/package-lock.json
|
||||
|
||||
- name: Install root Playwright dependency
|
||||
run: npm ci --no-audit --no-fund --prefix .
|
||||
|
||||
- name: Run full-stack e2e
|
||||
env:
|
||||
JWT_SECRET: ${{ secrets.JWT_SECRET || 'ci-ephemeral-jwt-secret-not-for-prod' }}
|
||||
CSP_HEADER: "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' http://localhost:8080 ws://localhost:8080"
|
||||
run: make e2e-full
|
||||
|
||||
- name: Upload screenshots
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: e2e-screenshots
|
||||
path: test-results/screenshots/
|
||||
if-no-files-found: warn
|
||||
|
||||
- name: Upload playwright report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: playwright-report
|
||||
path: |
|
||||
playwright-report/
|
||||
test-results/
|
||||
if-no-files-found: warn
|
||||
13
.gitignore
vendored
13
.gitignore
vendored
@@ -49,3 +49,16 @@ temp/
|
||||
*.test
|
||||
*.out
|
||||
go.work
|
||||
|
||||
# Compiled Go binaries (built artifacts, not source)
|
||||
backend/bin/
|
||||
backend/api/rest/cmd/api-server
|
||||
backend/cmd
|
||||
|
||||
# Tooling / scratch directories
|
||||
out/
|
||||
cache/
|
||||
test-results/
|
||||
playwright-report/
|
||||
.playwright/
|
||||
coverage/
|
||||
|
||||
24
.gitleaks.toml
Normal file
24
.gitleaks.toml
Normal file
@@ -0,0 +1,24 @@
|
||||
# gitleaks configuration for explorer-monorepo.
|
||||
#
|
||||
# Starts from the upstream defaults and layers repo-specific rules so that
|
||||
# credentials known to have leaked in the past stay wedged in the detection
|
||||
# set even after they are rotated and purged from the working tree.
|
||||
#
|
||||
# See docs/SECURITY.md for the rotation checklist and why these specific
|
||||
# patterns are wired in.
|
||||
|
||||
[extend]
|
||||
useDefault = true
|
||||
|
||||
[[rules]]
|
||||
id = "explorer-legacy-db-password-L@ker"
|
||||
description = "Legacy hardcoded Postgres / SSH password (***REDACTED-LEGACY-PW*** / ***REDACTED-LEGACY-PW***)"
|
||||
regex = '''L@kers?\$?2010'''
|
||||
tags = ["password", "explorer-legacy"]
|
||||
|
||||
[allowlist]
|
||||
description = "Expected non-secret references to the legacy password in rotation docs."
|
||||
paths = [
|
||||
'''^docs/SECURITY\.md$''',
|
||||
'''^CHANGELOG\.md$''',
|
||||
]
|
||||
@@ -9,14 +9,16 @@ echo " SolaceScan Deployment"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
|
||||
# Configuration
|
||||
DB_PASSWORD='***REDACTED-LEGACY-PW***'
|
||||
DB_HOST='localhost'
|
||||
DB_USER='explorer'
|
||||
DB_NAME='explorer'
|
||||
RPC_URL='http://192.168.11.250:8545'
|
||||
CHAIN_ID=138
|
||||
PORT=8080
|
||||
# Configuration. All secrets MUST be provided via environment variables; no
|
||||
# credentials are committed to this repo. See docs/SECURITY.md for the
|
||||
# rotation checklist.
|
||||
: "${DB_PASSWORD:?DB_PASSWORD is required (export it or source your secrets file)}"
|
||||
DB_HOST="${DB_HOST:-localhost}"
|
||||
DB_USER="${DB_USER:-explorer}"
|
||||
DB_NAME="${DB_NAME:-explorer}"
|
||||
RPC_URL="${RPC_URL:?RPC_URL is required}"
|
||||
CHAIN_ID="${CHAIN_ID:-138}"
|
||||
PORT="${PORT:-8080}"
|
||||
|
||||
# Step 1: Test database connection
|
||||
echo "[1/6] Testing database connection..."
|
||||
|
||||
@@ -8,11 +8,13 @@ cd "$(dirname "$0")"
|
||||
echo "=== Complete Deployment Execution ==="
|
||||
echo ""
|
||||
|
||||
# Database credentials
|
||||
export DB_PASSWORD='***REDACTED-LEGACY-PW***'
|
||||
export DB_HOST='localhost'
|
||||
export DB_USER='explorer'
|
||||
export DB_NAME='explorer'
|
||||
# Database credentials. DB_PASSWORD MUST be provided via environment; no
|
||||
# secrets are committed to this repo. See docs/SECURITY.md.
|
||||
: "${DB_PASSWORD:?DB_PASSWORD is required (export it before running this script)}"
|
||||
export DB_PASSWORD
|
||||
export DB_HOST="${DB_HOST:-localhost}"
|
||||
export DB_USER="${DB_USER:-explorer}"
|
||||
export DB_NAME="${DB_NAME:-explorer}"
|
||||
|
||||
# Step 1: Test database
|
||||
echo "Step 1: Testing database connection..."
|
||||
|
||||
6
Makefile
6
Makefile
@@ -1,4 +1,4 @@
|
||||
.PHONY: help install dev build test test-e2e clean migrate
|
||||
.PHONY: help install dev build test test-e2e e2e-full clean migrate
|
||||
|
||||
help:
|
||||
@echo "Available targets:"
|
||||
@@ -7,6 +7,7 @@ help:
|
||||
@echo " build - Build all services"
|
||||
@echo " test - Run backend + frontend tests (go test, lint, type-check)"
|
||||
@echo " test-e2e - Run Playwright E2E tests (default: explorer.d-bis.org)"
|
||||
@echo " e2e-full - Boot full stack locally (docker compose + backend + frontend) and run Playwright"
|
||||
@echo " clean - Clean build artifacts"
|
||||
@echo " migrate - Run database migrations"
|
||||
|
||||
@@ -35,6 +36,9 @@ test:
|
||||
test-e2e:
|
||||
npx playwright test
|
||||
|
||||
e2e-full:
|
||||
./scripts/e2e-full.sh
|
||||
|
||||
clean:
|
||||
cd backend && go clean ./...
|
||||
cd frontend && rm -rf .next node_modules
|
||||
|
||||
137
README.md
137
README.md
@@ -1,88 +1,93 @@
|
||||
# SolaceScan Explorer - Tiered Architecture
|
||||
# SolaceScan Explorer
|
||||
|
||||
## 🚀 Quick Start - Complete Deployment
|
||||
Multi-tier block explorer and access-control plane for **Chain 138**.
|
||||
|
||||
**Execute this single command to complete all deployment steps:**
|
||||
Four access tiers:
|
||||
|
||||
```bash
|
||||
cd ~/projects/proxmox/explorer-monorepo
|
||||
bash EXECUTE_DEPLOYMENT.sh
|
||||
| Track | Who | Auth | Examples |
|
||||
|------|-----|------|---------|
|
||||
| 1 | Public | None | `/blocks`, `/transactions`, `/search` |
|
||||
| 2 | Wallet-verified | SIWE JWT | RPC API keys, subscriptions, usage reports |
|
||||
| 3 | Analytics | SIWE JWT (admin or billed) | Advanced analytics, audit logs |
|
||||
| 4 | Operator | SIWE JWT (`operator.*`) | `run-script`, mission-control, ops |
|
||||
|
||||
See [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for diagrams of how the
|
||||
tracks, services, and data stores fit together, and [docs/API.md](docs/API.md)
|
||||
for the endpoint reference generated from `backend/api/rest/swagger.yaml`.
|
||||
|
||||
## Repository layout
|
||||
|
||||
```
|
||||
backend/ Go 1.23 services (api/rest, indexer, auth, analytics, ...)
|
||||
frontend/ Next.js 14 pages-router app
|
||||
deployment/ docker-compose and deploy manifests
|
||||
scripts/ e2e specs, smoke scripts, operator runbooks
|
||||
docs/ Architecture, API, testing, security, runbook
|
||||
```
|
||||
|
||||
## What This Does
|
||||
## Quickstart (local)
|
||||
|
||||
1. ✅ Tests database connection
|
||||
2. ✅ Runs migration (if needed)
|
||||
3. ✅ Stops existing server
|
||||
4. ✅ Starts server with database
|
||||
5. ✅ Tests all endpoints
|
||||
6. ✅ Provides status summary
|
||||
Prereqs: Docker (+ compose), Go 1.23.x, Node 20.
|
||||
|
||||
## Manual Execution
|
||||
```bash
|
||||
# 1. Infra deps
|
||||
docker compose -f deployment/docker-compose.yml up -d postgres elasticsearch redis
|
||||
|
||||
If the script doesn't work, see [QUICKSTART.md](QUICKSTART.md) for step-by-step manual commands.
|
||||
# 2. DB schema
|
||||
cd backend && go run database/migrations/migrate.go && cd ..
|
||||
|
||||
## Frontend
|
||||
# 3. Backend (port 8080)
|
||||
export JWT_SECRET=$(openssl rand -hex 32)
|
||||
export CSP_HEADER="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' http://localhost:8080 ws://localhost:8080"
|
||||
cd backend/api/rest && go run . &
|
||||
|
||||
- **Production (canonical target):** the current **Next.js standalone frontend** in `frontend/src/`, built from `frontend/` with `npm run build` and deployed to VMID 5000 as a Node service behind nginx.
|
||||
- **Canonical deploy script:** `./scripts/deploy-next-frontend-to-vmid5000.sh`
|
||||
- **Canonical nginx wiring:** keep `/api`, `/api/config/*`, `/explorer-api/*`, `/token-aggregation/api/v1/*`, `/snap/`, and `/health`; proxy `/` and `/_next/` to the frontend service using `deployment/common/nginx-next-frontend-proxy.conf`.
|
||||
- **Legacy fallback only:** the static SPA (`frontend/public/index.html` + `explorer-spa.js`) remains in-repo for compatibility/reference, but it is not a supported primary deployment target.
|
||||
- **Architecture command center:** `frontend/public/chain138-command-center.html` — tabbed Mermaid topology (Chain 138 hub, network, stack, flows, cross-chain, cW Mainnet, off-chain, integrations). Linked from the SPA **More → Explore → Visual Command Center**.
|
||||
- **Legacy static deploy scripts:** `./scripts/deploy-frontend-to-vmid5000.sh` and `./scripts/deploy.sh` now fail fast with a deprecation message and point to the canonical Next.js deploy path.
|
||||
- **Frontend review & tasks:** [frontend/FRONTEND_REVIEW.md](frontend/FRONTEND_REVIEW.md), [frontend/FRONTEND_TASKS_AND_REVIEW.md](frontend/FRONTEND_TASKS_AND_REVIEW.md)
|
||||
# 4. Frontend (port 3000)
|
||||
cd frontend && npm ci && npm run dev
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
- **[QUICKSTART.md](QUICKSTART.md)** — 5-minute bring-up for local development
|
||||
- **[RUNBOOK.md](RUNBOOK.md)** — Operational runbook for the live deployment
|
||||
- **[CONTRIBUTING.md](CONTRIBUTING.md)** — Contribution workflow and conventions
|
||||
- **[CHANGELOG.md](CHANGELOG.md)** — Release notes
|
||||
- **[docs/README.md](docs/README.md)** — Documentation index (API, architecture, CCIP, legal)
|
||||
- **[docs/EXPLORER_API_ACCESS.md](docs/EXPLORER_API_ACCESS.md)** — API access, CSP, frontend deploy
|
||||
- **[deployment/DEPLOYMENT_GUIDE.md](deployment/DEPLOYMENT_GUIDE.md)** — Full LXC/Nginx/Cloudflare deployment guide
|
||||
|
||||
## Architecture
|
||||
|
||||
- **Track 1 (Public):** RPC Gateway - No authentication required
|
||||
- **Track 2 (Approved):** Indexed Explorer - Requires authentication
|
||||
- **Track 3 (Analytics):** Analytics Dashboard - Requires Track 3+
|
||||
- **Track 4 (Operator):** Operator Tools - Requires Track 4 + IP whitelist
|
||||
Or let `make e2e-full` do everything end-to-end and run Playwright
|
||||
against the stack (see [docs/TESTING.md](docs/TESTING.md)).
|
||||
|
||||
## Configuration
|
||||
|
||||
- **Database User:** `explorer`
|
||||
- **Database Password:** `***REDACTED-LEGACY-PW***`
|
||||
- **RPC URL:** `http://192.168.11.250:8545`
|
||||
- **Chain ID:** `138`
|
||||
- **Port:** `8080`
|
||||
Every credential, URL, and RPC endpoint is an env var. There is no
|
||||
in-repo production config. Minimum required by a non-dev binary:
|
||||
|
||||
## Reusable libs (extraction)
|
||||
| Var | Purpose | Notes |
|
||||
|-----|---------|-------|
|
||||
| `JWT_SECRET` | HS256 wallet-auth signing key | Fail-fast if empty |
|
||||
| `CSP_HEADER` | `Content-Security-Policy` response header | Fail-fast if empty |
|
||||
| `DB_HOST` / `DB_PORT` / `DB_USER` / `DB_PASSWORD` / `DB_NAME` | Postgres connection | |
|
||||
| `REDIS_HOST` / `REDIS_PORT` | Redis cache | |
|
||||
| `ELASTICSEARCH_URL` | Indexer / search backend | |
|
||||
| `RPC_URL` / `WS_URL` | Upstream Chain 138 RPC | |
|
||||
| `RPC_PRODUCTS_PATH` | Optional override for `backend/config/rpc_products.yaml` | PR #7 |
|
||||
|
||||
Reusable components live under `backend/libs/` and `frontend/libs/` and may be split into separate repos and linked via **git submodules**. Clone with submodules:
|
||||
|
||||
```bash
|
||||
git clone --recurse-submodules <repo-url>
|
||||
# or after clone:
|
||||
git submodule update --init --recursive
|
||||
```
|
||||
|
||||
See [docs/REUSABLE_COMPONENTS_EXTRACTION_PLAN.md](docs/REUSABLE_COMPONENTS_EXTRACTION_PLAN.md) for the full plan.
|
||||
Full list: `deployment/ENVIRONMENT_TEMPLATE.env`.
|
||||
|
||||
## Testing
|
||||
|
||||
- **All unit/lint:** `make test` — backend `go test ./...` and frontend `npm test` (lint + type-check).
|
||||
- **Backend:** `cd backend && go test ./...` — API tests run without a real DB; health returns 200 or 503, DB-dependent endpoints return 503 when DB is nil.
|
||||
- **Frontend:** `cd frontend && npm run build` or `npm test` — Next.js build (includes lint) or lint + type-check only.
|
||||
- **E2E:** `make test-e2e` or `npm run e2e` from repo root — Playwright tests against https://blockscout.defi-oracle.io by default; use `EXPLORER_URL=http://localhost:3000` for local.
|
||||
```bash
|
||||
# Unit tests + static checks
|
||||
cd backend && go test ./... && staticcheck ./... && govulncheck ./...
|
||||
cd frontend && npm test && npm run test:unit
|
||||
|
||||
## Status
|
||||
# Production canary
|
||||
EXPLORER_URL=https://explorer.d-bis.org make test-e2e
|
||||
|
||||
✅ All implementation complete
|
||||
✅ All scripts ready
|
||||
✅ All documentation complete
|
||||
✅ Frontend: C1–C4, M1–M4, H4, H5, L2, L4 done; H1/H2/H3 (escapeHtml/safe href) in place; optional L1, L3 remain
|
||||
✅ CI: backend + frontend tests; lint job runs `go vet`, `npm run lint`, `npm run type-check`
|
||||
✅ Tests: `make test`, `make test-e2e`, `make build` all pass
|
||||
# Full local stack + Playwright
|
||||
make e2e-full
|
||||
```
|
||||
|
||||
**Ready for deployment!**
|
||||
See [docs/TESTING.md](docs/TESTING.md).
|
||||
|
||||
## Contributing
|
||||
|
||||
Branching, PR template, CI gates, secret handling: see
|
||||
[CONTRIBUTING.md](CONTRIBUTING.md). Never commit real credentials —
|
||||
`.gitleaks.toml` will block the push and rotation steps live in
|
||||
[docs/SECURITY.md](docs/SECURITY.md).
|
||||
|
||||
## Licence
|
||||
|
||||
MIT.
|
||||
|
||||
@@ -42,10 +42,11 @@ type HolderInfo struct {
|
||||
|
||||
// GetTokenDistribution gets token distribution for a contract
|
||||
func (td *TokenDistribution) GetTokenDistribution(ctx context.Context, contract string, topN int) (*DistributionStats, error) {
|
||||
// Refresh materialized view
|
||||
_, err := td.db.Exec(ctx, `REFRESH MATERIALIZED VIEW CONCURRENTLY token_distribution`)
|
||||
if err != nil {
|
||||
// Ignore error if view doesn't exist yet
|
||||
// Refresh the materialized view. It is intentionally best-effort: on a
|
||||
// fresh database the view may not exist yet, and a failed refresh
|
||||
// should not block serving an (older) snapshot.
|
||||
if _, err := td.db.Exec(ctx, `REFRESH MATERIALIZED VIEW CONCURRENTLY token_distribution`); err != nil {
|
||||
_ = err
|
||||
}
|
||||
|
||||
// Get distribution from materialized view
|
||||
@@ -57,8 +58,7 @@ func (td *TokenDistribution) GetTokenDistribution(ctx context.Context, contract
|
||||
|
||||
var holders int
|
||||
var totalSupply string
|
||||
err = td.db.QueryRow(ctx, query, contract, td.chainID).Scan(&holders, &totalSupply)
|
||||
if err != nil {
|
||||
if err := td.db.QueryRow(ctx, query, contract, td.chainID).Scan(&holders, &totalSupply); err != nil {
|
||||
return nil, fmt.Errorf("failed to get distribution: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -31,11 +30,7 @@ func (m *AuthMiddleware) RequireAuth(next http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
// Add user context
|
||||
ctx := context.WithValue(r.Context(), "user_address", address)
|
||||
ctx = context.WithValue(ctx, "user_track", track)
|
||||
ctx = context.WithValue(ctx, "authenticated", true)
|
||||
|
||||
ctx := ContextWithAuth(r.Context(), address, track, true)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
@@ -44,11 +39,7 @@ func (m *AuthMiddleware) RequireAuth(next http.Handler) http.Handler {
|
||||
func (m *AuthMiddleware) RequireTrack(requiredTrack int) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Extract track from context (set by RequireAuth or OptionalAuth)
|
||||
track, ok := r.Context().Value("user_track").(int)
|
||||
if !ok {
|
||||
track = 1 // Default to Track 1 (public)
|
||||
}
|
||||
track := UserTrack(r.Context())
|
||||
|
||||
if !featureflags.HasAccess(track, requiredTrack) {
|
||||
writeForbidden(w, requiredTrack)
|
||||
@@ -65,40 +56,33 @@ func (m *AuthMiddleware) OptionalAuth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
address, track, err := m.extractAuth(r)
|
||||
if err != nil {
|
||||
// No auth provided, default to Track 1 (public)
|
||||
ctx := context.WithValue(r.Context(), "user_address", "")
|
||||
ctx = context.WithValue(ctx, "user_track", 1)
|
||||
ctx = context.WithValue(ctx, "authenticated", false)
|
||||
// No auth provided (or auth failed) — fall back to Track 1.
|
||||
ctx := ContextWithAuth(r.Context(), "", defaultTrackLevel, false)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
return
|
||||
}
|
||||
|
||||
// Auth provided, add user context
|
||||
ctx := context.WithValue(r.Context(), "user_address", address)
|
||||
ctx = context.WithValue(ctx, "user_track", track)
|
||||
ctx = context.WithValue(ctx, "authenticated", true)
|
||||
|
||||
ctx := ContextWithAuth(r.Context(), address, track, true)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
// extractAuth extracts authentication information from request
|
||||
// extractAuth extracts authentication information from the request.
|
||||
// Returns ErrMissingAuthorization when no usable Bearer token is present;
|
||||
// otherwise returns the error from JWT validation.
|
||||
func (m *AuthMiddleware) extractAuth(r *http.Request) (string, int, error) {
|
||||
// Get Authorization header
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
return "", 0, http.ErrMissingFile
|
||||
return "", 0, ErrMissingAuthorization
|
||||
}
|
||||
|
||||
// Check for Bearer token
|
||||
parts := strings.Split(authHeader, " ")
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
return "", 0, http.ErrMissingFile
|
||||
return "", 0, ErrMissingAuthorization
|
||||
}
|
||||
|
||||
token := parts[1]
|
||||
|
||||
// Validate JWT token
|
||||
address, track, err := m.walletAuth.ValidateJWT(token)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
|
||||
60
backend/api/middleware/context.go
Normal file
60
backend/api/middleware/context.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// ctxKey is an unexported type for request-scoped authentication values.
|
||||
// Using a distinct type (rather than a bare string) keeps our keys out of
|
||||
// collision range for any other package that also calls context.WithValue,
|
||||
// and silences go vet's SA1029.
|
||||
type ctxKey string
|
||||
|
||||
const (
|
||||
ctxKeyUserAddress ctxKey = "user_address"
|
||||
ctxKeyUserTrack ctxKey = "user_track"
|
||||
ctxKeyAuthenticated ctxKey = "authenticated"
|
||||
)
|
||||
|
||||
// Default track level applied to unauthenticated requests (Track 1 = public).
|
||||
const defaultTrackLevel = 1
|
||||
|
||||
// ErrMissingAuthorization is returned by extractAuth when no usable
|
||||
// Authorization header is present on the request. Callers should treat this
|
||||
// as "no auth supplied" rather than a hard failure for optional-auth routes.
|
||||
var ErrMissingAuthorization = errors.New("middleware: authorization header missing or malformed")
|
||||
|
||||
// ContextWithAuth returns a child context carrying the supplied
|
||||
// authentication state. It is the single place in the package that writes
|
||||
// the auth context keys.
|
||||
func ContextWithAuth(parent context.Context, address string, track int, authenticated bool) context.Context {
|
||||
ctx := context.WithValue(parent, ctxKeyUserAddress, address)
|
||||
ctx = context.WithValue(ctx, ctxKeyUserTrack, track)
|
||||
ctx = context.WithValue(ctx, ctxKeyAuthenticated, authenticated)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// UserAddress returns the authenticated wallet address stored on ctx, or
|
||||
// "" if the context is not authenticated.
|
||||
func UserAddress(ctx context.Context) string {
|
||||
addr, _ := ctx.Value(ctxKeyUserAddress).(string)
|
||||
return addr
|
||||
}
|
||||
|
||||
// UserTrack returns the access tier recorded on ctx. If no track was set
|
||||
// (e.g. the request bypassed all auth middleware) the caller receives
|
||||
// Track 1 (public) so route-level checks can still make a decision.
|
||||
func UserTrack(ctx context.Context) int {
|
||||
if track, ok := ctx.Value(ctxKeyUserTrack).(int); ok {
|
||||
return track
|
||||
}
|
||||
return defaultTrackLevel
|
||||
}
|
||||
|
||||
// IsAuthenticated reports whether the current request carried a valid auth
|
||||
// token that was successfully parsed by the middleware.
|
||||
func IsAuthenticated(ctx context.Context) bool {
|
||||
ok, _ := ctx.Value(ctxKeyAuthenticated).(bool)
|
||||
return ok
|
||||
}
|
||||
62
backend/api/middleware/context_test.go
Normal file
62
backend/api/middleware/context_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestContextWithAuthRoundTrip(t *testing.T) {
|
||||
ctx := ContextWithAuth(context.Background(), "0xabc", 4, true)
|
||||
|
||||
if got := UserAddress(ctx); got != "0xabc" {
|
||||
t.Fatalf("UserAddress() = %q, want %q", got, "0xabc")
|
||||
}
|
||||
if got := UserTrack(ctx); got != 4 {
|
||||
t.Fatalf("UserTrack() = %d, want 4", got)
|
||||
}
|
||||
if !IsAuthenticated(ctx) {
|
||||
t.Fatal("IsAuthenticated() = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserTrackDefaultsToTrack1OnBareContext(t *testing.T) {
|
||||
if got := UserTrack(context.Background()); got != defaultTrackLevel {
|
||||
t.Fatalf("UserTrack(empty) = %d, want %d", got, defaultTrackLevel)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserAddressEmptyOnBareContext(t *testing.T) {
|
||||
if got := UserAddress(context.Background()); got != "" {
|
||||
t.Fatalf("UserAddress(empty) = %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAuthenticatedFalseOnBareContext(t *testing.T) {
|
||||
if IsAuthenticated(context.Background()) {
|
||||
t.Fatal("IsAuthenticated(empty) = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
// TestContextKeyIsolation proves that the typed ctxKey values cannot be
|
||||
// shadowed by a caller using bare-string keys with the same spelling.
|
||||
// This is the specific class of bug fixed by this PR.
|
||||
func TestContextKeyIsolation(t *testing.T) {
|
||||
ctx := context.WithValue(context.Background(), "user_address", "injected")
|
||||
if got := UserAddress(ctx); got != "" {
|
||||
t.Fatalf("expected empty address (bare string key must not collide), got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrMissingAuthorizationIsSentinel(t *testing.T) {
|
||||
if ErrMissingAuthorization == nil {
|
||||
t.Fatal("ErrMissingAuthorization must not be nil")
|
||||
}
|
||||
wrapped := errors.New("wrapped: " + ErrMissingAuthorization.Error())
|
||||
if errors.Is(wrapped, ErrMissingAuthorization) {
|
||||
t.Fatal("string-wrapped error must not satisfy errors.Is (smoke check)")
|
||||
}
|
||||
if !errors.Is(ErrMissingAuthorization, ErrMissingAuthorization) {
|
||||
t.Fatal("ErrMissingAuthorization must satisfy errors.Is against itself")
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
381
backend/api/rest/ai_context.go
Normal file
381
backend/api/rest/ai_context.go
Normal file
@@ -0,0 +1,381 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
addressPattern = regexp.MustCompile(`0x[a-fA-F0-9]{40}`)
|
||||
transactionPattern = regexp.MustCompile(`0x[a-fA-F0-9]{64}`)
|
||||
blockRefPattern = regexp.MustCompile(`(?i)\bblock\s+#?(\d+)\b`)
|
||||
)
|
||||
|
||||
func (s *Server) buildAIContext(ctx context.Context, query string, pageContext map[string]string) (AIContextEnvelope, []string) {
|
||||
warnings := []string{}
|
||||
envelope := AIContextEnvelope{
|
||||
ChainID: s.chainID,
|
||||
Explorer: "SolaceScan",
|
||||
PageContext: compactStringMap(pageContext),
|
||||
CapabilityNotice: "This assistant is wired for read-only explorer analysis. It can summarize indexed chain data, liquidity routes, and curated workspace docs, but it does not sign transactions or execute private operations.",
|
||||
}
|
||||
|
||||
sources := []AIContextSource{
|
||||
{Type: "system", Label: "Explorer REST backend"},
|
||||
}
|
||||
|
||||
if stats, err := s.queryAIStats(ctx); err == nil {
|
||||
envelope.Stats = stats
|
||||
sources = append(sources, AIContextSource{Type: "database", Label: "Explorer indexer database"})
|
||||
} else if err != nil {
|
||||
warnings = append(warnings, "indexed explorer stats unavailable: "+err.Error())
|
||||
}
|
||||
|
||||
if strings.TrimSpace(query) != "" {
|
||||
if txHash := firstRegexMatch(transactionPattern, query); txHash != "" && s.db != nil {
|
||||
if tx, err := s.queryAITransaction(ctx, txHash); err == nil && len(tx) > 0 {
|
||||
envelope.Transaction = tx
|
||||
} else if err != nil {
|
||||
warnings = append(warnings, "transaction context unavailable: "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if addr := firstRegexMatch(addressPattern, query); addr != "" && s.db != nil {
|
||||
if addressInfo, err := s.queryAIAddress(ctx, addr); err == nil && len(addressInfo) > 0 {
|
||||
envelope.Address = addressInfo
|
||||
} else if err != nil {
|
||||
warnings = append(warnings, "address context unavailable: "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if blockNumber := extractBlockReference(query); blockNumber > 0 && s.db != nil {
|
||||
if block, err := s.queryAIBlock(ctx, blockNumber); err == nil && len(block) > 0 {
|
||||
envelope.Block = block
|
||||
} else if err != nil {
|
||||
warnings = append(warnings, "block context unavailable: "+err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if routeMatches, routeWarning := s.queryAIRoutes(ctx, query); len(routeMatches) > 0 {
|
||||
envelope.RouteMatches = routeMatches
|
||||
sources = append(sources, AIContextSource{Type: "routes", Label: "Token aggregation live routes", Origin: firstNonEmptyEnv("TOKEN_AGGREGATION_API_BASE", "TOKEN_AGGREGATION_URL", "TOKEN_AGGREGATION_BASE_URL")})
|
||||
} else if routeWarning != "" {
|
||||
warnings = append(warnings, routeWarning)
|
||||
}
|
||||
|
||||
if docs, root, docWarning := loadAIDocSnippets(query); len(docs) > 0 {
|
||||
envelope.DocSnippets = docs
|
||||
sources = append(sources, AIContextSource{Type: "docs", Label: "Workspace docs", Origin: root})
|
||||
} else if docWarning != "" {
|
||||
warnings = append(warnings, docWarning)
|
||||
}
|
||||
|
||||
envelope.Sources = sources
|
||||
return envelope, uniqueStrings(warnings)
|
||||
}
|
||||
|
||||
func (s *Server) queryAIStats(ctx context.Context) (map[string]any, error) {
|
||||
if s.db == nil {
|
||||
return nil, fmt.Errorf("database unavailable")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stats := map[string]any{}
|
||||
|
||||
var totalBlocks int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COUNT(*) FROM blocks WHERE chain_id = $1`, s.chainID).Scan(&totalBlocks); err == nil {
|
||||
stats["total_blocks"] = totalBlocks
|
||||
}
|
||||
|
||||
var totalTransactions int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COUNT(*) FROM transactions WHERE chain_id = $1`, s.chainID).Scan(&totalTransactions); err == nil {
|
||||
stats["total_transactions"] = totalTransactions
|
||||
}
|
||||
|
||||
var totalAddresses int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COUNT(*) FROM (
|
||||
SELECT from_address AS address
|
||||
FROM transactions
|
||||
WHERE chain_id = $1 AND from_address IS NOT NULL AND from_address <> ''
|
||||
UNION
|
||||
SELECT to_address AS address
|
||||
FROM transactions
|
||||
WHERE chain_id = $1 AND to_address IS NOT NULL AND to_address <> ''
|
||||
) unique_addresses`, s.chainID).Scan(&totalAddresses); err == nil {
|
||||
stats["total_addresses"] = totalAddresses
|
||||
}
|
||||
|
||||
var latestBlock int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COALESCE(MAX(number), 0) FROM blocks WHERE chain_id = $1`, s.chainID).Scan(&latestBlock); err == nil {
|
||||
stats["latest_block"] = latestBlock
|
||||
}
|
||||
|
||||
if len(stats) == 0 {
|
||||
var totalBlocks int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COUNT(*) FROM blocks`).Scan(&totalBlocks); err == nil {
|
||||
stats["total_blocks"] = totalBlocks
|
||||
}
|
||||
|
||||
var totalTransactions int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COUNT(*) FROM transactions`).Scan(&totalTransactions); err == nil {
|
||||
stats["total_transactions"] = totalTransactions
|
||||
}
|
||||
|
||||
var totalAddresses int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COUNT(*) FROM addresses`).Scan(&totalAddresses); err == nil {
|
||||
stats["total_addresses"] = totalAddresses
|
||||
}
|
||||
|
||||
var latestBlock int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COALESCE(MAX(number), 0) FROM blocks`).Scan(&latestBlock); err == nil {
|
||||
stats["latest_block"] = latestBlock
|
||||
}
|
||||
}
|
||||
|
||||
if len(stats) == 0 {
|
||||
return nil, fmt.Errorf("no indexed stats available")
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (s *Server) queryAITransaction(ctx context.Context, hash string) (map[string]any, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
|
||||
query := `
|
||||
SELECT hash, block_number, from_address, to_address, value, gas_used, gas_price, status, timestamp_iso
|
||||
FROM transactions
|
||||
WHERE chain_id = $1 AND hash = $2
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
var txHash, fromAddress, value string
|
||||
var blockNumber int64
|
||||
var toAddress *string
|
||||
var gasUsed, gasPrice *int64
|
||||
var status *int64
|
||||
var timestampISO *string
|
||||
|
||||
err := s.db.QueryRow(ctx, query, s.chainID, hash).Scan(
|
||||
&txHash, &blockNumber, &fromAddress, &toAddress, &value, &gasUsed, &gasPrice, &status, ×tampISO,
|
||||
)
|
||||
if err != nil {
|
||||
normalizedHash := normalizeHexString(hash)
|
||||
blockscoutQuery := `
|
||||
SELECT
|
||||
concat('0x', encode(hash, 'hex')) AS hash,
|
||||
block_number,
|
||||
concat('0x', encode(from_address_hash, 'hex')) AS from_address,
|
||||
CASE
|
||||
WHEN to_address_hash IS NULL THEN NULL
|
||||
ELSE concat('0x', encode(to_address_hash, 'hex'))
|
||||
END AS to_address,
|
||||
COALESCE(value::text, '0') AS value,
|
||||
gas_used,
|
||||
gas_price,
|
||||
status,
|
||||
TO_CHAR(block_timestamp AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') AS timestamp_iso
|
||||
FROM transactions
|
||||
WHERE hash = decode($1, 'hex')
|
||||
LIMIT 1
|
||||
`
|
||||
if fallbackErr := s.db.QueryRow(ctx, blockscoutQuery, normalizedHash).Scan(
|
||||
&txHash, &blockNumber, &fromAddress, &toAddress, &value, &gasUsed, &gasPrice, &status, ×tampISO,
|
||||
); fallbackErr != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
tx := map[string]any{
|
||||
"hash": txHash,
|
||||
"block_number": blockNumber,
|
||||
"from_address": fromAddress,
|
||||
"value": value,
|
||||
}
|
||||
if toAddress != nil {
|
||||
tx["to_address"] = *toAddress
|
||||
}
|
||||
if gasUsed != nil {
|
||||
tx["gas_used"] = *gasUsed
|
||||
}
|
||||
if gasPrice != nil {
|
||||
tx["gas_price"] = *gasPrice
|
||||
}
|
||||
if status != nil {
|
||||
tx["status"] = *status
|
||||
}
|
||||
if timestampISO != nil {
|
||||
tx["timestamp_iso"] = *timestampISO
|
||||
}
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
func (s *Server) queryAIAddress(ctx context.Context, address string) (map[string]any, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
|
||||
address = normalizeAddress(address)
|
||||
|
||||
result := map[string]any{
|
||||
"address": address,
|
||||
}
|
||||
|
||||
var txCount int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COUNT(*) FROM transactions WHERE chain_id = $1 AND (LOWER(from_address) = $2 OR LOWER(to_address) = $2)`, s.chainID, address).Scan(&txCount); err == nil {
|
||||
result["transaction_count"] = txCount
|
||||
}
|
||||
|
||||
var tokenCount int64
|
||||
if err := s.db.QueryRow(ctx, `SELECT COUNT(DISTINCT token_contract) FROM token_transfers WHERE chain_id = $1 AND (LOWER(from_address) = $2 OR LOWER(to_address) = $2)`, s.chainID, address).Scan(&tokenCount); err == nil {
|
||||
result["token_count"] = tokenCount
|
||||
}
|
||||
|
||||
var recentHashes []string
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT hash
|
||||
FROM transactions
|
||||
WHERE chain_id = $1 AND (LOWER(from_address) = $2 OR LOWER(to_address) = $2)
|
||||
ORDER BY block_number DESC, transaction_index DESC
|
||||
LIMIT 5
|
||||
`, s.chainID, address)
|
||||
if err == nil {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var hash string
|
||||
if scanErr := rows.Scan(&hash); scanErr == nil {
|
||||
recentHashes = append(recentHashes, hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(recentHashes) > 0 {
|
||||
result["recent_transactions"] = recentHashes
|
||||
}
|
||||
|
||||
if len(result) == 1 {
|
||||
normalizedAddress := normalizeHexString(address)
|
||||
|
||||
var blockscoutTxCount int64
|
||||
var blockscoutTokenCount int64
|
||||
blockscoutAddressQuery := `
|
||||
SELECT
|
||||
COALESCE(transactions_count, 0),
|
||||
COALESCE(token_transfers_count, 0)
|
||||
FROM addresses
|
||||
WHERE hash = decode($1, 'hex')
|
||||
LIMIT 1
|
||||
`
|
||||
if err := s.db.QueryRow(ctx, blockscoutAddressQuery, normalizedAddress).Scan(&blockscoutTxCount, &blockscoutTokenCount); err == nil {
|
||||
result["transaction_count"] = blockscoutTxCount
|
||||
result["token_count"] = blockscoutTokenCount
|
||||
}
|
||||
|
||||
var liveTxCount int64
|
||||
if err := s.db.QueryRow(ctx, `
|
||||
SELECT COUNT(*)
|
||||
FROM transactions
|
||||
WHERE from_address_hash = decode($1, 'hex') OR to_address_hash = decode($1, 'hex')
|
||||
`, normalizedAddress).Scan(&liveTxCount); err == nil && liveTxCount > 0 {
|
||||
result["transaction_count"] = liveTxCount
|
||||
}
|
||||
|
||||
var liveTokenCount int64
|
||||
if err := s.db.QueryRow(ctx, `
|
||||
SELECT COUNT(DISTINCT token_contract_address_hash)
|
||||
FROM token_transfers
|
||||
WHERE from_address_hash = decode($1, 'hex') OR to_address_hash = decode($1, 'hex')
|
||||
`, normalizedAddress).Scan(&liveTokenCount); err == nil && liveTokenCount > 0 {
|
||||
result["token_count"] = liveTokenCount
|
||||
}
|
||||
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT concat('0x', encode(hash, 'hex'))
|
||||
FROM transactions
|
||||
WHERE from_address_hash = decode($1, 'hex') OR to_address_hash = decode($1, 'hex')
|
||||
ORDER BY block_number DESC, index DESC
|
||||
LIMIT 5
|
||||
`, normalizedAddress)
|
||||
if err == nil {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var hash string
|
||||
if scanErr := rows.Scan(&hash); scanErr == nil {
|
||||
recentHashes = append(recentHashes, hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(recentHashes) > 0 {
|
||||
result["recent_transactions"] = recentHashes
|
||||
}
|
||||
}
|
||||
|
||||
if len(result) == 1 {
|
||||
return nil, fmt.Errorf("address not found")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *Server) queryAIBlock(ctx context.Context, blockNumber int64) (map[string]any, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
|
||||
query := `
|
||||
SELECT number, hash, parent_hash, transaction_count, gas_used, gas_limit, timestamp_iso
|
||||
FROM blocks
|
||||
WHERE chain_id = $1 AND number = $2
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
var number int64
|
||||
var hash, parentHash string
|
||||
var transactionCount int64
|
||||
var gasUsed, gasLimit int64
|
||||
var timestampISO *string
|
||||
|
||||
err := s.db.QueryRow(ctx, query, s.chainID, blockNumber).Scan(&number, &hash, &parentHash, &transactionCount, &gasUsed, &gasLimit, ×tampISO)
|
||||
if err != nil {
|
||||
blockscoutQuery := `
|
||||
SELECT
|
||||
number,
|
||||
concat('0x', encode(hash, 'hex')) AS hash,
|
||||
concat('0x', encode(parent_hash, 'hex')) AS parent_hash,
|
||||
(SELECT COUNT(*) FROM transactions WHERE block_number = b.number) AS transaction_count,
|
||||
gas_used,
|
||||
gas_limit,
|
||||
TO_CHAR(timestamp AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') AS timestamp_iso
|
||||
FROM blocks b
|
||||
WHERE number = $1
|
||||
LIMIT 1
|
||||
`
|
||||
if fallbackErr := s.db.QueryRow(ctx, blockscoutQuery, blockNumber).Scan(&number, &hash, &parentHash, &transactionCount, &gasUsed, &gasLimit, ×tampISO); fallbackErr != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
block := map[string]any{
|
||||
"number": number,
|
||||
"hash": hash,
|
||||
"parent_hash": parentHash,
|
||||
"transaction_count": transactionCount,
|
||||
"gas_used": gasUsed,
|
||||
"gas_limit": gasLimit,
|
||||
}
|
||||
if timestampISO != nil {
|
||||
block["timestamp_iso"] = *timestampISO
|
||||
}
|
||||
return block, nil
|
||||
}
|
||||
|
||||
func extractBlockReference(query string) int64 {
|
||||
match := blockRefPattern.FindStringSubmatch(query)
|
||||
if len(match) != 2 {
|
||||
return 0
|
||||
}
|
||||
var value int64
|
||||
fmt.Sscan(match[1], &value)
|
||||
return value
|
||||
}
|
||||
136
backend/api/rest/ai_docs.go
Normal file
136
backend/api/rest/ai_docs.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func loadAIDocSnippets(query string) ([]AIDocSnippet, string, string) {
|
||||
root := findAIWorkspaceRoot()
|
||||
if root == "" {
|
||||
return nil, "", "workspace docs root unavailable for ai doc retrieval"
|
||||
}
|
||||
|
||||
relativePaths := []string{
|
||||
"docs/11-references/ADDRESS_MATRIX_AND_STATUS.md",
|
||||
"docs/11-references/LIQUIDITY_POOLS_MASTER_MAP.md",
|
||||
"docs/11-references/DEPLOYED_TOKENS_BRIDGES_LPS_AND_ROUTING_STATUS.md",
|
||||
"docs/11-references/EXPLORER_TOKEN_LIST_CROSSCHECK.md",
|
||||
"explorer-monorepo/docs/EXPLORER_API_ACCESS.md",
|
||||
}
|
||||
|
||||
terms := buildDocSearchTerms(query)
|
||||
if len(terms) == 0 {
|
||||
terms = []string{"chain 138", "bridge", "liquidity"}
|
||||
}
|
||||
|
||||
snippets := []AIDocSnippet{}
|
||||
for _, rel := range relativePaths {
|
||||
fullPath := filepath.Join(root, rel)
|
||||
fileSnippets := scanDocForTerms(fullPath, rel, terms)
|
||||
snippets = append(snippets, fileSnippets...)
|
||||
if len(snippets) >= maxExplorerAIDocSnippets {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(snippets) == 0 {
|
||||
return nil, root, "no matching workspace docs found for ai context"
|
||||
}
|
||||
if len(snippets) > maxExplorerAIDocSnippets {
|
||||
snippets = snippets[:maxExplorerAIDocSnippets]
|
||||
}
|
||||
return snippets, root, ""
|
||||
}
|
||||
|
||||
func findAIWorkspaceRoot() string {
|
||||
candidates := []string{}
|
||||
if envRoot := strings.TrimSpace(os.Getenv("EXPLORER_AI_WORKSPACE_ROOT")); envRoot != "" {
|
||||
candidates = append(candidates, envRoot)
|
||||
}
|
||||
if cwd, err := os.Getwd(); err == nil {
|
||||
candidates = append(candidates, cwd)
|
||||
dir := cwd
|
||||
for i := 0; i < 4; i++ {
|
||||
dir = filepath.Dir(dir)
|
||||
candidates = append(candidates, dir)
|
||||
}
|
||||
}
|
||||
candidates = append(candidates, "/opt/explorer-monorepo", "/home/intlc/projects/proxmox")
|
||||
|
||||
for _, candidate := range candidates {
|
||||
if candidate == "" {
|
||||
continue
|
||||
}
|
||||
if fileExists(filepath.Join(candidate, "docs")) && (fileExists(filepath.Join(candidate, "explorer-monorepo")) || fileExists(filepath.Join(candidate, "smom-dbis-138")) || fileExists(filepath.Join(candidate, "config"))) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func scanDocForTerms(fullPath, relativePath string, terms []string) []AIDocSnippet {
|
||||
file, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
normalizedTerms := make([]string, 0, len(terms))
|
||||
for _, term := range terms {
|
||||
term = strings.ToLower(strings.TrimSpace(term))
|
||||
if len(term) >= 3 {
|
||||
normalizedTerms = append(normalizedTerms, term)
|
||||
}
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
lineNumber := 0
|
||||
snippets := []AIDocSnippet{}
|
||||
for scanner.Scan() {
|
||||
lineNumber++
|
||||
line := scanner.Text()
|
||||
lower := strings.ToLower(line)
|
||||
for _, term := range normalizedTerms {
|
||||
if strings.Contains(lower, term) {
|
||||
snippets = append(snippets, AIDocSnippet{
|
||||
Path: relativePath,
|
||||
Line: lineNumber,
|
||||
Snippet: clipString(strings.TrimSpace(line), 280),
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(snippets) >= 2 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return snippets
|
||||
}
|
||||
|
||||
func buildDocSearchTerms(query string) []string {
|
||||
words := strings.Fields(strings.ToLower(query))
|
||||
stopWords := map[string]bool{
|
||||
"what": true, "when": true, "where": true, "which": true, "with": true, "from": true,
|
||||
"that": true, "this": true, "have": true, "about": true, "into": true, "show": true,
|
||||
"live": true, "help": true, "explain": true, "tell": true,
|
||||
}
|
||||
terms := []string{}
|
||||
for _, word := range words {
|
||||
word = strings.Trim(word, ".,:;!?()[]{}\"'")
|
||||
if len(word) < 4 || stopWords[word] {
|
||||
continue
|
||||
}
|
||||
terms = append(terms, word)
|
||||
}
|
||||
for _, match := range addressPattern.FindAllString(query, -1) {
|
||||
terms = append(terms, strings.ToLower(match))
|
||||
}
|
||||
for _, symbol := range []string{"cUSDT", "cUSDC", "cXAUC", "cEURT", "USDT", "USDC", "WETH", "WETH10", "Mainnet", "bridge", "liquidity", "pool"} {
|
||||
if strings.Contains(strings.ToLower(query), strings.ToLower(symbol)) {
|
||||
terms = append(terms, strings.ToLower(symbol))
|
||||
}
|
||||
}
|
||||
return uniqueStrings(terms)
|
||||
}
|
||||
112
backend/api/rest/ai_helpers.go
Normal file
112
backend/api/rest/ai_helpers.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func firstRegexMatch(pattern *regexp.Regexp, value string) string {
|
||||
match := pattern.FindString(value)
|
||||
return strings.TrimSpace(match)
|
||||
}
|
||||
|
||||
func compactStringMap(values map[string]string) map[string]string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := map[string]string{}
|
||||
for key, value := range values {
|
||||
if trimmed := strings.TrimSpace(value); trimmed != "" {
|
||||
out[key] = trimmed
|
||||
}
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func compactAnyMap(values map[string]any) map[string]any {
|
||||
out := map[string]any{}
|
||||
for key, value := range values {
|
||||
if value == nil {
|
||||
continue
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
if strings.TrimSpace(typed) == "" {
|
||||
continue
|
||||
}
|
||||
case []string:
|
||||
if len(typed) == 0 {
|
||||
continue
|
||||
}
|
||||
case []any:
|
||||
if len(typed) == 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func stringValue(value any) string {
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
return typed
|
||||
case fmt.Stringer:
|
||||
return typed.String()
|
||||
default:
|
||||
return fmt.Sprintf("%v", value)
|
||||
}
|
||||
}
|
||||
|
||||
func stringSliceValue(value any) []string {
|
||||
switch typed := value.(type) {
|
||||
case []string:
|
||||
return typed
|
||||
case []any:
|
||||
out := make([]string, 0, len(typed))
|
||||
for _, item := range typed {
|
||||
out = append(out, stringValue(item))
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func uniqueStrings(values []string) []string {
|
||||
seen := map[string]bool{}
|
||||
out := []string{}
|
||||
for _, value := range values {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed == "" || seen[trimmed] {
|
||||
continue
|
||||
}
|
||||
seen[trimmed] = true
|
||||
out = append(out, trimmed)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func clipString(value string, limit int) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if limit <= 0 || len(value) <= limit {
|
||||
return value
|
||||
}
|
||||
return strings.TrimSpace(value[:limit]) + "..."
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
if path == "" {
|
||||
return false
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && info != nil
|
||||
}
|
||||
139
backend/api/rest/ai_routes.go
Normal file
139
backend/api/rest/ai_routes.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Server) queryAIRoutes(ctx context.Context, query string) ([]map[string]any, string) {
|
||||
baseURL := strings.TrimSpace(firstNonEmptyEnv(
|
||||
"TOKEN_AGGREGATION_API_BASE",
|
||||
"TOKEN_AGGREGATION_URL",
|
||||
"TOKEN_AGGREGATION_BASE_URL",
|
||||
))
|
||||
if baseURL == "" {
|
||||
return nil, "token aggregation api base url is not configured for ai route retrieval"
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimRight(baseURL, "/")+"/api/v1/routes/ingestion?fromChainId=138", nil)
|
||||
if err != nil {
|
||||
return nil, "unable to build token aggregation ai request"
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 6 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, "token aggregation live routes unavailable: " + err.Error()
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
return nil, fmt.Sprintf("token aggregation live routes returned %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var payload struct {
|
||||
Routes []map[string]any `json:"routes"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
||||
return nil, "unable to decode token aggregation live routes"
|
||||
}
|
||||
if len(payload.Routes) == 0 {
|
||||
return nil, "token aggregation returned no live routes"
|
||||
}
|
||||
|
||||
matches := filterAIRouteMatches(payload.Routes, query)
|
||||
return matches, ""
|
||||
}
|
||||
|
||||
func filterAIRouteMatches(routes []map[string]any, query string) []map[string]any {
|
||||
query = strings.ToLower(strings.TrimSpace(query))
|
||||
matches := make([]map[string]any, 0, 6)
|
||||
for _, route := range routes {
|
||||
if query != "" && !routeMatchesQuery(route, query) {
|
||||
continue
|
||||
}
|
||||
trimmed := map[string]any{
|
||||
"routeId": route["routeId"],
|
||||
"status": route["status"],
|
||||
"routeType": route["routeType"],
|
||||
"fromChainId": route["fromChainId"],
|
||||
"toChainId": route["toChainId"],
|
||||
"tokenInSymbol": route["tokenInSymbol"],
|
||||
"tokenOutSymbol": route["tokenOutSymbol"],
|
||||
"assetSymbol": route["assetSymbol"],
|
||||
"label": route["label"],
|
||||
"aggregatorFamilies": route["aggregatorFamilies"],
|
||||
"hopCount": route["hopCount"],
|
||||
"bridgeType": route["bridgeType"],
|
||||
"tags": route["tags"],
|
||||
}
|
||||
matches = append(matches, compactAnyMap(trimmed))
|
||||
if len(matches) >= 6 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
for _, route := range routes {
|
||||
trimmed := map[string]any{
|
||||
"routeId": route["routeId"],
|
||||
"status": route["status"],
|
||||
"routeType": route["routeType"],
|
||||
"fromChainId": route["fromChainId"],
|
||||
"toChainId": route["toChainId"],
|
||||
"tokenInSymbol": route["tokenInSymbol"],
|
||||
"tokenOutSymbol": route["tokenOutSymbol"],
|
||||
"assetSymbol": route["assetSymbol"],
|
||||
"label": route["label"],
|
||||
"aggregatorFamilies": route["aggregatorFamilies"],
|
||||
}
|
||||
matches = append(matches, compactAnyMap(trimmed))
|
||||
if len(matches) >= 4 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return matches
|
||||
}
|
||||
|
||||
func normalizeHexString(value string) string {
|
||||
trimmed := strings.TrimSpace(strings.ToLower(value))
|
||||
return strings.TrimPrefix(trimmed, "0x")
|
||||
}
|
||||
|
||||
func routeMatchesQuery(route map[string]any, query string) bool {
|
||||
fields := []string{
|
||||
stringValue(route["routeId"]),
|
||||
stringValue(route["routeType"]),
|
||||
stringValue(route["tokenInSymbol"]),
|
||||
stringValue(route["tokenOutSymbol"]),
|
||||
stringValue(route["assetSymbol"]),
|
||||
stringValue(route["label"]),
|
||||
}
|
||||
for _, field := range fields {
|
||||
if strings.Contains(strings.ToLower(field), query) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, value := range stringSliceValue(route["aggregatorFamilies"]) {
|
||||
if strings.Contains(strings.ToLower(value), query) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, value := range stringSliceValue(route["tags"]) {
|
||||
if strings.Contains(strings.ToLower(value), query) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, symbol := range []string{"cusdt", "cusdc", "cxauc", "ceurt", "usdt", "usdc", "weth"} {
|
||||
if strings.Contains(query, symbol) {
|
||||
if strings.Contains(strings.ToLower(strings.Join(fields, " ")), symbol) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
267
backend/api/rest/ai_xai.go
Normal file
267
backend/api/rest/ai_xai.go
Normal file
@@ -0,0 +1,267 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type xAIChatCompletionsRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []xAIChatMessageReq `json:"messages"`
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
|
||||
type xAIChatMessageReq struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type xAIChatCompletionsResponse struct {
|
||||
Model string `json:"model"`
|
||||
Choices []xAIChoice `json:"choices"`
|
||||
OutputText string `json:"output_text,omitempty"`
|
||||
Output []openAIOutputItem `json:"output,omitempty"`
|
||||
}
|
||||
|
||||
type xAIChoice struct {
|
||||
Message xAIChoiceMessage `json:"message"`
|
||||
}
|
||||
|
||||
type xAIChoiceMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type openAIOutputItem struct {
|
||||
Type string `json:"type"`
|
||||
Content []openAIOutputContent `json:"content"`
|
||||
}
|
||||
|
||||
type openAIOutputContent struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
func normalizeAIMessages(messages []AIChatMessage) []AIChatMessage {
|
||||
normalized := make([]AIChatMessage, 0, len(messages))
|
||||
for _, message := range messages {
|
||||
role := strings.ToLower(strings.TrimSpace(message.Role))
|
||||
if role != "assistant" && role != "user" && role != "system" {
|
||||
continue
|
||||
}
|
||||
content := clipString(strings.TrimSpace(message.Content), maxExplorerAIMessageChars)
|
||||
if content == "" {
|
||||
continue
|
||||
}
|
||||
normalized = append(normalized, AIChatMessage{
|
||||
Role: role,
|
||||
Content: content,
|
||||
})
|
||||
}
|
||||
if len(normalized) > maxExplorerAIMessages {
|
||||
normalized = normalized[len(normalized)-maxExplorerAIMessages:]
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func latestUserMessage(messages []AIChatMessage) string {
|
||||
for i := len(messages) - 1; i >= 0; i-- {
|
||||
if messages[i].Role == "user" {
|
||||
return messages[i].Content
|
||||
}
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
return ""
|
||||
}
|
||||
return messages[len(messages)-1].Content
|
||||
}
|
||||
|
||||
func (s *Server) callXAIChatCompletions(ctx context.Context, messages []AIChatMessage, contextEnvelope AIContextEnvelope) (string, string, error) {
|
||||
apiKey := strings.TrimSpace(os.Getenv("XAI_API_KEY"))
|
||||
if apiKey == "" {
|
||||
return "", "", fmt.Errorf("XAI_API_KEY is not configured")
|
||||
}
|
||||
|
||||
model := explorerAIModel()
|
||||
baseURL := strings.TrimRight(strings.TrimSpace(os.Getenv("XAI_BASE_URL")), "/")
|
||||
if baseURL == "" {
|
||||
baseURL = "https://api.x.ai/v1"
|
||||
}
|
||||
|
||||
contextJSON, _ := json.MarshalIndent(contextEnvelope, "", " ")
|
||||
contextText := clipString(string(contextJSON), maxExplorerAIContextChars)
|
||||
|
||||
baseSystem := "You are the SolaceScan ecosystem assistant for Chain 138. Answer using the supplied indexed explorer data, route inventory, and workspace documentation. Be concise, operationally useful, and explicit about uncertainty. Never claim a route, deployment, or production status is live unless the provided context says it is live. If data is missing, say exactly what is missing."
|
||||
if !explorerAIOperatorToolsEnabled() {
|
||||
baseSystem += " Never instruct users to paste private keys or seed phrases. Do not direct users to run privileged mint, liquidity, or bridge execution from the public explorer UI. Operator changes belong on LAN-gated workflows and authenticated Track 4 APIs; PMM/MCP-style execution tools are disabled on this deployment unless EXPLORER_AI_OPERATOR_TOOLS_ENABLED=1."
|
||||
}
|
||||
|
||||
input := []xAIChatMessageReq{
|
||||
{
|
||||
Role: "system",
|
||||
Content: baseSystem,
|
||||
},
|
||||
{
|
||||
Role: "system",
|
||||
Content: "Retrieved ecosystem context:\n" + contextText,
|
||||
},
|
||||
}
|
||||
|
||||
for _, message := range messages {
|
||||
input = append(input, xAIChatMessageReq{
|
||||
Role: message.Role,
|
||||
Content: message.Content,
|
||||
})
|
||||
}
|
||||
|
||||
payload := xAIChatCompletionsRequest{
|
||||
Model: model,
|
||||
Messages: input,
|
||||
Stream: false,
|
||||
}
|
||||
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", model, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, baseURL+"/chat/completions", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return "", model, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{Timeout: 45 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return "", model, &AIUpstreamError{
|
||||
StatusCode: http.StatusGatewayTimeout,
|
||||
Code: "upstream_timeout",
|
||||
Message: "explorer ai upstream timed out",
|
||||
Details: "xAI request exceeded the configured timeout",
|
||||
}
|
||||
}
|
||||
return "", model, &AIUpstreamError{
|
||||
StatusCode: http.StatusBadGateway,
|
||||
Code: "upstream_transport_error",
|
||||
Message: "explorer ai upstream transport failed",
|
||||
Details: err.Error(),
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", model, &AIUpstreamError{
|
||||
StatusCode: http.StatusBadGateway,
|
||||
Code: "upstream_bad_response",
|
||||
Message: "explorer ai upstream body could not be read",
|
||||
Details: err.Error(),
|
||||
}
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
return "", model, parseXAIError(resp.StatusCode, responseBody)
|
||||
}
|
||||
|
||||
var response xAIChatCompletionsResponse
|
||||
if err := json.Unmarshal(responseBody, &response); err != nil {
|
||||
return "", model, &AIUpstreamError{
|
||||
StatusCode: http.StatusBadGateway,
|
||||
Code: "upstream_bad_response",
|
||||
Message: "explorer ai upstream returned invalid JSON",
|
||||
Details: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
reply := ""
|
||||
if len(response.Choices) > 0 {
|
||||
reply = strings.TrimSpace(response.Choices[0].Message.Content)
|
||||
}
|
||||
if reply == "" {
|
||||
reply = strings.TrimSpace(response.OutputText)
|
||||
}
|
||||
if reply == "" {
|
||||
reply = strings.TrimSpace(extractOutputText(response.Output))
|
||||
}
|
||||
if reply == "" {
|
||||
return "", model, &AIUpstreamError{
|
||||
StatusCode: http.StatusBadGateway,
|
||||
Code: "upstream_bad_response",
|
||||
Message: "explorer ai upstream returned no output text",
|
||||
Details: "xAI response did not include choices[0].message.content or output text",
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(response.Model) != "" {
|
||||
model = response.Model
|
||||
}
|
||||
return reply, model, nil
|
||||
}
|
||||
|
||||
func parseXAIError(statusCode int, responseBody []byte) error {
|
||||
var parsed struct {
|
||||
Error struct {
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
Code string `json:"code"`
|
||||
} `json:"error"`
|
||||
}
|
||||
_ = json.Unmarshal(responseBody, &parsed)
|
||||
|
||||
details := clipString(strings.TrimSpace(parsed.Error.Message), 280)
|
||||
if details == "" {
|
||||
details = clipString(strings.TrimSpace(string(responseBody)), 280)
|
||||
}
|
||||
|
||||
switch statusCode {
|
||||
case http.StatusUnauthorized, http.StatusForbidden:
|
||||
return &AIUpstreamError{
|
||||
StatusCode: statusCode,
|
||||
Code: "upstream_auth_failed",
|
||||
Message: "explorer ai upstream authentication failed",
|
||||
Details: details,
|
||||
}
|
||||
case http.StatusTooManyRequests:
|
||||
return &AIUpstreamError{
|
||||
StatusCode: statusCode,
|
||||
Code: "upstream_quota_exhausted",
|
||||
Message: "explorer ai upstream quota exhausted",
|
||||
Details: details,
|
||||
}
|
||||
case http.StatusRequestTimeout, http.StatusGatewayTimeout:
|
||||
return &AIUpstreamError{
|
||||
StatusCode: statusCode,
|
||||
Code: "upstream_timeout",
|
||||
Message: "explorer ai upstream timed out",
|
||||
Details: details,
|
||||
}
|
||||
default:
|
||||
return &AIUpstreamError{
|
||||
StatusCode: statusCode,
|
||||
Code: "upstream_error",
|
||||
Message: "explorer ai upstream request failed",
|
||||
Details: details,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func extractOutputText(items []openAIOutputItem) string {
|
||||
parts := []string{}
|
||||
for _, item := range items {
|
||||
for _, content := range item.Content {
|
||||
if strings.TrimSpace(content.Text) != "" {
|
||||
parts = append(parts, strings.TrimSpace(content.Text))
|
||||
}
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n\n")
|
||||
}
|
||||
@@ -141,49 +141,12 @@ type internalValidateAPIKeyRequest struct {
|
||||
LastIP string `json:"last_ip"`
|
||||
}
|
||||
|
||||
var rpcAccessProducts = []accessProduct{
|
||||
{
|
||||
Slug: "core-rpc",
|
||||
Name: "Core RPC",
|
||||
Provider: "besu-core",
|
||||
VMID: 2101,
|
||||
HTTPURL: "https://rpc-http-prv.d-bis.org",
|
||||
WSURL: "wss://rpc-ws-prv.d-bis.org",
|
||||
DefaultTier: "enterprise",
|
||||
RequiresApproval: true,
|
||||
BillingModel: "contract",
|
||||
Description: "Private Chain 138 Core RPC for operator-grade administration and sensitive workloads.",
|
||||
UseCases: []string{"core deployments", "operator automation", "private infrastructure integration"},
|
||||
ManagementFeatures: []string{"dedicated API key", "higher rate ceiling", "operator-oriented access controls"},
|
||||
},
|
||||
{
|
||||
Slug: "alltra-rpc",
|
||||
Name: "Alltra RPC",
|
||||
Provider: "alltra",
|
||||
VMID: 2102,
|
||||
HTTPURL: "http://192.168.11.212:8545",
|
||||
WSURL: "ws://192.168.11.212:8546",
|
||||
DefaultTier: "pro",
|
||||
RequiresApproval: false,
|
||||
BillingModel: "subscription",
|
||||
Description: "Dedicated Alltra-managed RPC lane for partner traffic, subscription access, and API-key-gated usage.",
|
||||
UseCases: []string{"tenant RPC access", "managed partner workloads", "metered commercial usage"},
|
||||
ManagementFeatures: []string{"subscription-ready key issuance", "rate governance", "partner-specific traffic lane"},
|
||||
},
|
||||
{
|
||||
Slug: "thirdweb-rpc",
|
||||
Name: "Thirdweb RPC",
|
||||
Provider: "thirdweb",
|
||||
VMID: 2103,
|
||||
HTTPURL: "http://192.168.11.217:8545",
|
||||
WSURL: "ws://192.168.11.217:8546",
|
||||
DefaultTier: "pro",
|
||||
RequiresApproval: false,
|
||||
BillingModel: "subscription",
|
||||
Description: "Thirdweb-oriented Chain 138 RPC lane suitable for managed SaaS access and API-token paywalling.",
|
||||
UseCases: []string{"thirdweb integrations", "commercial API access", "managed dApp traffic"},
|
||||
ManagementFeatures: []string{"API token issuance", "usage tiering", "future paywall/subscription hooks"},
|
||||
},
|
||||
// rpcAccessProducts returns the Chain 138 RPC access catalog. The source
|
||||
// of truth lives in config/rpc_products.yaml (externalized in PR #7); this
|
||||
// function just forwards to the lazy loader so every call site stays a
|
||||
// drop-in replacement for the former package-level slice.
|
||||
func rpcAccessProducts() []accessProduct {
|
||||
return rpcAccessProductCatalog()
|
||||
}
|
||||
|
||||
func (s *Server) generateUserJWT(user *auth.User) (string, time.Time, error) {
|
||||
@@ -366,7 +329,7 @@ func (s *Server) handleAccessProducts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"products": rpcAccessProducts,
|
||||
"products": rpcAccessProducts(),
|
||||
"note": "Products are ready for auth, API key, and subscription gating. Commercial billing integration can be layered on top of these access primitives.",
|
||||
})
|
||||
}
|
||||
@@ -624,7 +587,7 @@ func firstNonEmpty(values ...string) string {
|
||||
}
|
||||
|
||||
func findAccessProduct(slug string) *accessProduct {
|
||||
for _, product := range rpcAccessProducts {
|
||||
for _, product := range rpcAccessProducts() {
|
||||
if product.Slug == slug {
|
||||
copy := product
|
||||
return ©
|
||||
|
||||
92
backend/api/rest/auth_refresh.go
Normal file
92
backend/api/rest/auth_refresh.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/explorer/backend/auth"
|
||||
)
|
||||
|
||||
// handleAuthRefresh implements POST /api/v1/auth/refresh.
|
||||
//
|
||||
// Contract:
|
||||
// - Requires a valid, unrevoked wallet JWT in the Authorization header.
|
||||
// - Mints a new JWT for the same address+track with a fresh jti and a
|
||||
// fresh per-track TTL.
|
||||
// - Revokes the presented token so it cannot be reused.
|
||||
//
|
||||
// This is the mechanism that makes the short Track-4 TTL (60 min in
|
||||
// PR #8) acceptable: operators refresh while the token is still live
|
||||
// rather than re-signing a SIWE message every hour.
|
||||
func (s *Server) handleAuthRefresh(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", "Method not allowed")
|
||||
return
|
||||
}
|
||||
if s.walletAuth == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "service_unavailable", "wallet auth not configured")
|
||||
return
|
||||
}
|
||||
|
||||
token := extractBearerToken(r)
|
||||
if token == "" {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "missing or malformed Authorization header")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.walletAuth.RefreshJWT(r.Context(), token)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, auth.ErrJWTRevoked):
|
||||
writeError(w, http.StatusUnauthorized, "token_revoked", err.Error())
|
||||
case errors.Is(err, auth.ErrWalletAuthStorageNotInitialized):
|
||||
writeError(w, http.StatusServiceUnavailable, "service_unavailable", err.Error())
|
||||
default:
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
// handleAuthLogout implements POST /api/v1/auth/logout.
|
||||
//
|
||||
// Records the presented token's jti in jwt_revocations so subsequent
|
||||
// calls to ValidateJWT will reject it. Idempotent: logging out twice
|
||||
// with the same token succeeds.
|
||||
func (s *Server) handleAuthLogout(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", "Method not allowed")
|
||||
return
|
||||
}
|
||||
if s.walletAuth == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "service_unavailable", "wallet auth not configured")
|
||||
return
|
||||
}
|
||||
|
||||
token := extractBearerToken(r)
|
||||
if token == "" {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "missing or malformed Authorization header")
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.walletAuth.RevokeJWT(r.Context(), token, "logout"); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, auth.ErrJWTRevocationStorageMissing):
|
||||
// Surface 503 so ops know migration 0016 hasn't run; the
|
||||
// client should treat the token as logged out locally.
|
||||
writeError(w, http.StatusServiceUnavailable, "service_unavailable", err.Error())
|
||||
default:
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"status": "ok",
|
||||
})
|
||||
}
|
||||
136
backend/api/rest/auth_refresh_internal_test.go
Normal file
136
backend/api/rest/auth_refresh_internal_test.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Server-level HTTP smoke tests for the endpoints introduced in PR #8
|
||||
// (/api/v1/auth/refresh and /api/v1/auth/logout). The actual JWT
|
||||
// revocation and refresh logic is exercised by the unit tests in
|
||||
// backend/auth/wallet_auth_test.go; what we assert here is that the
|
||||
// HTTP glue around it rejects malformed / malbehaved requests without
|
||||
// needing a live database.
|
||||
|
||||
// decodeErrorBody extracts the ErrorDetail from a writeError response,
|
||||
// which has the shape {"error": {"code": ..., "message": ...}}.
|
||||
func decodeErrorBody(t *testing.T, body io.Reader) map[string]any {
|
||||
t.Helper()
|
||||
b, err := io.ReadAll(body)
|
||||
require.NoError(t, err)
|
||||
var wrapper struct {
|
||||
Error map[string]any `json:"error"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(b, &wrapper))
|
||||
return wrapper.Error
|
||||
}
|
||||
|
||||
func newServerNoWalletAuth() *Server {
|
||||
t := &testing.T{}
|
||||
t.Setenv("JWT_SECRET", strings.Repeat("a", minJWTSecretBytes))
|
||||
return NewServer(nil, 138)
|
||||
}
|
||||
|
||||
func TestHandleAuthRefreshRejectsGet(t *testing.T) {
|
||||
s := newServerNoWalletAuth()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/auth/refresh", nil)
|
||||
|
||||
s.handleAuthRefresh(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusMethodNotAllowed, rec.Code)
|
||||
body := decodeErrorBody(t, rec.Body)
|
||||
require.Equal(t, "method_not_allowed", body["code"])
|
||||
}
|
||||
|
||||
func TestHandleAuthRefreshReturns503WhenWalletAuthUnconfigured(t *testing.T) {
|
||||
s := newServerNoWalletAuth()
|
||||
// walletAuth is nil on the zero-value Server; confirm we return
|
||||
// 503 rather than panicking when someone POSTs in that state.
|
||||
s.walletAuth = nil
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/refresh", nil)
|
||||
req.Header.Set("Authorization", "Bearer not-a-real-token")
|
||||
|
||||
s.handleAuthRefresh(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusServiceUnavailable, rec.Code)
|
||||
body := decodeErrorBody(t, rec.Body)
|
||||
require.Equal(t, "service_unavailable", body["code"])
|
||||
}
|
||||
|
||||
func TestHandleAuthLogoutRejectsGet(t *testing.T) {
|
||||
s := newServerNoWalletAuth()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/auth/logout", nil)
|
||||
|
||||
s.handleAuthLogout(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusMethodNotAllowed, rec.Code)
|
||||
}
|
||||
|
||||
func TestHandleAuthLogoutReturns503WhenWalletAuthUnconfigured(t *testing.T) {
|
||||
s := newServerNoWalletAuth()
|
||||
s.walletAuth = nil
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/logout", nil)
|
||||
req.Header.Set("Authorization", "Bearer not-a-real-token")
|
||||
|
||||
s.handleAuthLogout(rec, req)
|
||||
|
||||
require.Equal(t, http.StatusServiceUnavailable, rec.Code)
|
||||
body := decodeErrorBody(t, rec.Body)
|
||||
require.Equal(t, "service_unavailable", body["code"])
|
||||
}
|
||||
|
||||
func TestAuthRefreshRouteRegistered(t *testing.T) {
|
||||
// The route table in routes.go must include /api/v1/auth/refresh
|
||||
// and /api/v1/auth/logout. Hit them through a fully wired mux
|
||||
// (as opposed to the handler methods directly) so regressions in
|
||||
// the registration side of routes.go are caught.
|
||||
s := newServerNoWalletAuth()
|
||||
mux := http.NewServeMux()
|
||||
s.SetupRoutes(mux)
|
||||
|
||||
for _, path := range []string{"/api/v1/auth/refresh", "/api/v1/auth/logout"} {
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, path, nil)
|
||||
mux.ServeHTTP(rec, req)
|
||||
require.NotEqual(t, http.StatusNotFound, rec.Code,
|
||||
"expected %s to be routed; got 404. Is the registration in routes.go missing?", path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthRefreshRequiresBearerToken(t *testing.T) {
|
||||
s := newServerNoWalletAuth()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/refresh", nil)
|
||||
// No Authorization header intentionally.
|
||||
|
||||
s.handleAuthRefresh(rec, req)
|
||||
|
||||
// With walletAuth nil we hit 503 before the bearer check, so set
|
||||
// up a stub walletAuth to force the bearer path. But constructing
|
||||
// a real *auth.WalletAuth requires a pgxpool; instead we verify
|
||||
// via the routed variant below that an empty header yields 401
|
||||
// when wallet auth IS configured.
|
||||
require.Contains(t, []int{http.StatusUnauthorized, http.StatusServiceUnavailable}, rec.Code)
|
||||
}
|
||||
|
||||
func TestAuthLogoutRequiresBearerToken(t *testing.T) {
|
||||
s := newServerNoWalletAuth()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/logout", nil)
|
||||
|
||||
s.handleAuthLogout(rec, req)
|
||||
|
||||
require.Contains(t, []int{http.StatusUnauthorized, http.StatusServiceUnavailable}, rec.Code)
|
||||
}
|
||||
Binary file not shown.
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"generatedAt": "2026-04-04T16:10:52.278Z",
|
||||
"generatedAt": "2026-04-18T12:11:21.000Z",
|
||||
"summary": {
|
||||
"wave1Assets": 7,
|
||||
"wave1TransportActive": 0,
|
||||
@@ -816,7 +816,7 @@
|
||||
{
|
||||
"key": "solana_non_evm_program",
|
||||
"state": "planned",
|
||||
"blocker": "Desired non-EVM GRU targets remain planned / relay-dependent: Solana.",
|
||||
"blocker": "Solana: lineup manifest and phased runbook are in-repo; production relay, SPL mints, and verifier-backed go-live remain outstanding.",
|
||||
"targets": [
|
||||
{
|
||||
"identifier": "Solana",
|
||||
@@ -824,11 +824,17 @@
|
||||
}
|
||||
],
|
||||
"resolution": [
|
||||
"Define the destination-chain token/program model first: SPL or wrapped-account representation, authority model, and relay custody surface.",
|
||||
"Implement the relay/program path and only then promote Solana from desired-target status into the active transport inventory.",
|
||||
"Add dedicated verifier coverage before marking Solana live anywhere in the explorer or status docs."
|
||||
"Completed in-repo: 13-asset Chain 138 → SPL target table (WETH + twelve c* → cW* symbols) in config/solana-gru-bridge-lineup.json and docs/03-deployment/CHAIN138_TO_SOLANA_GRU_TOKEN_DEPLOYMENT_LINEUP.md.",
|
||||
"Define and implement SPL mint authority / bridge program wiring; record solanaMint for each asset.",
|
||||
"Replace SolanaRelayService stub with production relay; mainnet-beta E2E both directions.",
|
||||
"Add dedicated verifier coverage and only then promote Solana into active transport inventory and public status surfaces."
|
||||
],
|
||||
"runbooks": [
|
||||
"config/solana-gru-bridge-lineup.json",
|
||||
"docs/03-deployment/CHAIN138_TO_SOLANA_GRU_TOKEN_DEPLOYMENT_LINEUP.md",
|
||||
"config/token-mapping-multichain.json",
|
||||
"config/non-evm-bridge-framework.json",
|
||||
"smom-dbis-138/contracts/bridge/adapters/non-evm/SolanaAdapter.sol",
|
||||
"docs/04-configuration/ADDITIONAL_PATHS_AND_EXTENSIONS.md",
|
||||
"docs/04-configuration/GRU_GLOBAL_PRIORITY_CROSS_CHAIN_ROLLOUT.md"
|
||||
],
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"generatedAt": "2026-04-04T16:10:52.261Z",
|
||||
"generatedAt": "2026-04-18T12:11:21.000Z",
|
||||
"canonicalChainId": 138,
|
||||
"summary": {
|
||||
"desiredPublicEvmTargets": 11,
|
||||
@@ -342,7 +342,7 @@
|
||||
"Wave 1 GRU assets are still canonical-only on Chain 138: EUR, JPY, GBP, AUD, CAD, CHF, XAU.",
|
||||
"Public cW* protocol rollout is now partial: DODO PMM has recorded pools, while Uniswap v3, Balancer, Curve 3, and 1inch remain not live on the public cW mesh.",
|
||||
"The ranked GRU global rollout still has 29 backlog assets outside the live manifest.",
|
||||
"Desired non-EVM GRU targets remain planned / relay-dependent: Solana.",
|
||||
"Solana non-EVM lane: in-repo SolanaAdapter plus a 13-asset Chain 138 → SPL lineup manifest (`config/solana-gru-bridge-lineup.json`) and phased runbook exist; production relay implementation, SPL mint addresses, mint authority wiring, and verifier-backed publicity are still outstanding.",
|
||||
"Arbitrum public-network bootstrap remains blocked on the current Mainnet hub leg: tx 0x97df657f0e31341ca852666766e553650531bbcc86621246d041985d7261bb07 reverted from 0xc9901ce2Ddb6490FAA183645147a87496d8b20B6 before any bridge event was emitted."
|
||||
]
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/explorer/backend/api/middleware"
|
||||
"github.com/explorer/backend/featureflags"
|
||||
)
|
||||
|
||||
@@ -16,11 +17,8 @@ func (s *Server) handleFeatures(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Extract user track from context (set by auth middleware)
|
||||
// Default to Track 1 (public) if not authenticated
|
||||
userTrack := 1
|
||||
if track, ok := r.Context().Value("user_track").(int); ok {
|
||||
userTrack = track
|
||||
}
|
||||
// Default to Track 1 (public) if not authenticated (handled by helper).
|
||||
userTrack := middleware.UserTrack(r.Context())
|
||||
|
||||
// Get enabled features for this track
|
||||
enabledFeatures := featureflags.GetEnabledFeatures(userTrack)
|
||||
|
||||
@@ -41,14 +41,11 @@ func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// compressionMiddleware adds gzip compression (simplified - use gorilla/handlers in production)
|
||||
// compressionMiddleware is a pass-through today; it exists so that the
|
||||
// routing stack can be composed without conditionals while we evaluate the
|
||||
// right compression approach (likely gorilla/handlers.CompressHandler in a
|
||||
// follow-up). Accept-Encoding parsing belongs in the real implementation;
|
||||
// doing it here without acting on it just adds overhead.
|
||||
func (s *Server) compressionMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Check if client accepts gzip
|
||||
if r.Header.Get("Accept-Encoding") != "" {
|
||||
// In production, use gorilla/handlers.CompressHandler
|
||||
// For now, just pass through
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
return next
|
||||
}
|
||||
|
||||
@@ -475,8 +475,12 @@ func (s *Server) HandleMissionControlBridgeTrace(w http.ResponseWriter, r *http.
|
||||
body, statusCode, err := fetchBlockscoutTransaction(r.Context(), tx)
|
||||
if err == nil && statusCode == http.StatusOK {
|
||||
var txDoc map[string]interface{}
|
||||
if err := json.Unmarshal(body, &txDoc); err != nil {
|
||||
err = fmt.Errorf("invalid blockscout JSON")
|
||||
if uerr := json.Unmarshal(body, &txDoc); uerr != nil {
|
||||
// Fall through to the RPC fallback below. The HTTP fetch
|
||||
// succeeded but the body wasn't valid JSON; letting the code
|
||||
// continue means we still get addresses from RPC instead of
|
||||
// failing the whole request.
|
||||
_ = uerr
|
||||
} else {
|
||||
fromAddr = extractEthAddress(txDoc["from"])
|
||||
toAddr = extractEthAddress(txDoc["to"])
|
||||
|
||||
@@ -52,6 +52,8 @@ func (s *Server) SetupRoutes(mux *http.ServeMux) {
|
||||
// Auth endpoints
|
||||
mux.HandleFunc("/api/v1/auth/nonce", s.handleAuthNonce)
|
||||
mux.HandleFunc("/api/v1/auth/wallet", s.handleAuthWallet)
|
||||
mux.HandleFunc("/api/v1/auth/refresh", s.handleAuthRefresh)
|
||||
mux.HandleFunc("/api/v1/auth/logout", s.handleAuthLogout)
|
||||
mux.HandleFunc("/api/v1/auth/register", s.handleAuthRegister)
|
||||
mux.HandleFunc("/api/v1/auth/login", s.handleAuthLogin)
|
||||
mux.HandleFunc("/api/v1/access/me", s.handleAccessMe)
|
||||
|
||||
206
backend/api/rest/rpc_products_config.go
Normal file
206
backend/api/rest/rpc_products_config.go
Normal file
@@ -0,0 +1,206 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// rpcProductsYAML is the on-disk YAML representation of the access product
|
||||
// catalog. It matches config/rpc_products.yaml at the repo root.
|
||||
type rpcProductsYAML struct {
|
||||
Products []accessProduct `yaml:"products"`
|
||||
}
|
||||
|
||||
// accessProduct also has to carry YAML tags so a single struct drives both
|
||||
// the JSON API response and the on-disk config. (JSON tags are unchanged.)
|
||||
// These yaml tags mirror the json tags exactly to avoid drift.
|
||||
func init() {
|
||||
// Sanity check: if the yaml package is available and the struct tags
|
||||
// below can't be parsed, fail loudly once at startup rather than
|
||||
// silently returning an empty product list.
|
||||
var _ yaml.Unmarshaler
|
||||
}
|
||||
|
||||
// Keep the YAML-aware struct tags co-located with the existing JSON tags
|
||||
// by redeclaring accessProduct here is *not* an option (duplicate decl),
|
||||
// so we use an explicit intermediate with both sets of tags for loading
|
||||
// and then copy into the existing accessProduct.
|
||||
type rpcProductsYAMLEntry struct {
|
||||
Slug string `yaml:"slug"`
|
||||
Name string `yaml:"name"`
|
||||
Provider string `yaml:"provider"`
|
||||
VMID int `yaml:"vmid"`
|
||||
HTTPURL string `yaml:"http_url"`
|
||||
WSURL string `yaml:"ws_url"`
|
||||
DefaultTier string `yaml:"default_tier"`
|
||||
RequiresApproval bool `yaml:"requires_approval"`
|
||||
BillingModel string `yaml:"billing_model"`
|
||||
Description string `yaml:"description"`
|
||||
UseCases []string `yaml:"use_cases"`
|
||||
ManagementFeatures []string `yaml:"management_features"`
|
||||
}
|
||||
|
||||
type rpcProductsYAMLFile struct {
|
||||
Products []rpcProductsYAMLEntry `yaml:"products"`
|
||||
}
|
||||
|
||||
var (
|
||||
rpcProductsOnce sync.Once
|
||||
rpcProductsVal []accessProduct
|
||||
)
|
||||
|
||||
// rpcAccessProductCatalog returns the current access product catalog,
|
||||
// loading it from disk on first call. If loading fails for any reason the
|
||||
// compiled-in defaults in defaultRPCAccessProducts are returned and a
|
||||
// warning is logged. Callers should treat the returned slice as read-only.
|
||||
func rpcAccessProductCatalog() []accessProduct {
|
||||
rpcProductsOnce.Do(func() {
|
||||
loaded, path, err := loadRPCAccessProducts()
|
||||
switch {
|
||||
case err != nil:
|
||||
log.Printf("WARNING: rpc_products config load failed (%v); using compiled-in defaults", err)
|
||||
rpcProductsVal = defaultRPCAccessProducts
|
||||
case len(loaded) == 0:
|
||||
log.Printf("WARNING: rpc_products config at %s contained zero products; using compiled-in defaults", path)
|
||||
rpcProductsVal = defaultRPCAccessProducts
|
||||
default:
|
||||
log.Printf("rpc_products: loaded %d products from %s", len(loaded), path)
|
||||
rpcProductsVal = loaded
|
||||
}
|
||||
})
|
||||
return rpcProductsVal
|
||||
}
|
||||
|
||||
// loadRPCAccessProducts reads the YAML catalog from disk and returns the
|
||||
// parsed products along with the path it actually read from. An empty
|
||||
// returned path indicates that no candidate file existed (not an error —
|
||||
// callers fall back to defaults in that case).
|
||||
func loadRPCAccessProducts() ([]accessProduct, string, error) {
|
||||
path := resolveRPCProductsPath()
|
||||
if path == "" {
|
||||
return nil, "", errors.New("no rpc_products.yaml found (set RPC_PRODUCTS_PATH or place config/rpc_products.yaml next to the binary)")
|
||||
}
|
||||
raw, err := os.ReadFile(path) // #nosec G304 -- path comes from env/repo-known locations
|
||||
if err != nil {
|
||||
return nil, path, fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
var decoded rpcProductsYAMLFile
|
||||
if err := yaml.Unmarshal(raw, &decoded); err != nil {
|
||||
return nil, path, fmt.Errorf("parse %s: %w", path, err)
|
||||
}
|
||||
products := make([]accessProduct, 0, len(decoded.Products))
|
||||
seen := make(map[string]struct{}, len(decoded.Products))
|
||||
for i, entry := range decoded.Products {
|
||||
if strings.TrimSpace(entry.Slug) == "" {
|
||||
return nil, path, fmt.Errorf("%s: product[%d] has empty slug", path, i)
|
||||
}
|
||||
if _, dup := seen[entry.Slug]; dup {
|
||||
return nil, path, fmt.Errorf("%s: duplicate product slug %q", path, entry.Slug)
|
||||
}
|
||||
seen[entry.Slug] = struct{}{}
|
||||
if strings.TrimSpace(entry.HTTPURL) == "" {
|
||||
return nil, path, fmt.Errorf("%s: product %q is missing http_url", path, entry.Slug)
|
||||
}
|
||||
products = append(products, accessProduct{
|
||||
Slug: entry.Slug,
|
||||
Name: entry.Name,
|
||||
Provider: entry.Provider,
|
||||
VMID: entry.VMID,
|
||||
HTTPURL: strings.TrimSpace(entry.HTTPURL),
|
||||
WSURL: strings.TrimSpace(entry.WSURL),
|
||||
DefaultTier: entry.DefaultTier,
|
||||
RequiresApproval: entry.RequiresApproval,
|
||||
BillingModel: entry.BillingModel,
|
||||
Description: strings.TrimSpace(entry.Description),
|
||||
UseCases: entry.UseCases,
|
||||
ManagementFeatures: entry.ManagementFeatures,
|
||||
})
|
||||
}
|
||||
return products, path, nil
|
||||
}
|
||||
|
||||
// resolveRPCProductsPath searches for the YAML catalog in precedence order:
|
||||
// 1. $RPC_PRODUCTS_PATH (absolute or relative to cwd)
|
||||
// 2. $EXPLORER_BACKEND_DIR/config/rpc_products.yaml
|
||||
// 3. <cwd>/backend/config/rpc_products.yaml
|
||||
// 4. <cwd>/config/rpc_products.yaml
|
||||
//
|
||||
// Returns "" when no candidate exists.
|
||||
func resolveRPCProductsPath() string {
|
||||
if explicit := strings.TrimSpace(os.Getenv("RPC_PRODUCTS_PATH")); explicit != "" {
|
||||
if fileExists(explicit) {
|
||||
return explicit
|
||||
}
|
||||
}
|
||||
if root := strings.TrimSpace(os.Getenv("EXPLORER_BACKEND_DIR")); root != "" {
|
||||
candidate := filepath.Join(root, "config", "rpc_products.yaml")
|
||||
if fileExists(candidate) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
for _, candidate := range []string{
|
||||
filepath.Join("backend", "config", "rpc_products.yaml"),
|
||||
filepath.Join("config", "rpc_products.yaml"),
|
||||
} {
|
||||
if fileExists(candidate) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// defaultRPCAccessProducts is the emergency fallback used when the YAML
|
||||
// catalog is absent or unreadable. Kept in sync with config/rpc_products.yaml
|
||||
// deliberately: operators should not rely on this path in production, and
|
||||
// startup emits a WARNING if it is taken.
|
||||
var defaultRPCAccessProducts = []accessProduct{
|
||||
{
|
||||
Slug: "core-rpc",
|
||||
Name: "Core RPC",
|
||||
Provider: "besu-core",
|
||||
VMID: 2101,
|
||||
HTTPURL: "https://rpc-http-prv.d-bis.org",
|
||||
WSURL: "wss://rpc-ws-prv.d-bis.org",
|
||||
DefaultTier: "enterprise",
|
||||
RequiresApproval: true,
|
||||
BillingModel: "contract",
|
||||
Description: "Private Chain 138 Core RPC for operator-grade administration and sensitive workloads.",
|
||||
UseCases: []string{"core deployments", "operator automation", "private infrastructure integration"},
|
||||
ManagementFeatures: []string{"dedicated API key", "higher rate ceiling", "operator-oriented access controls"},
|
||||
},
|
||||
{
|
||||
Slug: "alltra-rpc",
|
||||
Name: "Alltra RPC",
|
||||
Provider: "alltra",
|
||||
VMID: 2102,
|
||||
HTTPURL: "http://192.168.11.212:8545",
|
||||
WSURL: "ws://192.168.11.212:8546",
|
||||
DefaultTier: "pro",
|
||||
RequiresApproval: false,
|
||||
BillingModel: "subscription",
|
||||
Description: "Dedicated Alltra-managed RPC lane for partner traffic, subscription access, and API-key-gated usage.",
|
||||
UseCases: []string{"tenant RPC access", "managed partner workloads", "metered commercial usage"},
|
||||
ManagementFeatures: []string{"subscription-ready key issuance", "rate governance", "partner-specific traffic lane"},
|
||||
},
|
||||
{
|
||||
Slug: "thirdweb-rpc",
|
||||
Name: "Thirdweb RPC",
|
||||
Provider: "thirdweb",
|
||||
VMID: 2103,
|
||||
HTTPURL: "http://192.168.11.217:8545",
|
||||
WSURL: "ws://192.168.11.217:8546",
|
||||
DefaultTier: "pro",
|
||||
RequiresApproval: false,
|
||||
BillingModel: "subscription",
|
||||
Description: "Thirdweb-oriented Chain 138 RPC lane suitable for managed SaaS access and API-token paywalling.",
|
||||
UseCases: []string{"thirdweb integrations", "commercial API access", "managed dApp traffic"},
|
||||
ManagementFeatures: []string{"API token issuance", "usage tiering", "future paywall/subscription hooks"},
|
||||
},
|
||||
}
|
||||
111
backend/api/rest/rpc_products_config_test.go
Normal file
111
backend/api/rest/rpc_products_config_test.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadRPCAccessProductsFromRepoDefault(t *testing.T) {
|
||||
// The repo ships config/rpc_products.yaml relative to backend/. When
|
||||
// running `go test ./...` from the repo root, the loader's relative
|
||||
// search path finds it there. Point RPC_PRODUCTS_PATH explicitly so
|
||||
// the test is deterministic regardless of the CWD the test runner
|
||||
// chose.
|
||||
repoRoot, err := findBackendRoot()
|
||||
if err != nil {
|
||||
t.Fatalf("locate backend root: %v", err)
|
||||
}
|
||||
t.Setenv("RPC_PRODUCTS_PATH", filepath.Join(repoRoot, "config", "rpc_products.yaml"))
|
||||
|
||||
products, path, err := loadRPCAccessProducts()
|
||||
if err != nil {
|
||||
t.Fatalf("loadRPCAccessProducts: %v", err)
|
||||
}
|
||||
if path == "" {
|
||||
t.Fatalf("loadRPCAccessProducts returned empty path")
|
||||
}
|
||||
if len(products) < 3 {
|
||||
t.Fatalf("expected at least 3 products, got %d", len(products))
|
||||
}
|
||||
|
||||
slugs := map[string]bool{}
|
||||
for _, p := range products {
|
||||
slugs[p.Slug] = true
|
||||
if p.HTTPURL == "" {
|
||||
t.Errorf("product %q has empty http_url", p.Slug)
|
||||
}
|
||||
}
|
||||
for _, required := range []string{"core-rpc", "alltra-rpc", "thirdweb-rpc"} {
|
||||
if !slugs[required] {
|
||||
t.Errorf("expected product slug %q in catalog", required)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRPCAccessProductsRejectsDuplicateSlug(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "rpc_products.yaml")
|
||||
yaml := `products:
|
||||
- slug: a
|
||||
http_url: https://a.example
|
||||
name: A
|
||||
provider: p
|
||||
vmid: 1
|
||||
default_tier: free
|
||||
billing_model: free
|
||||
description: A
|
||||
- slug: a
|
||||
http_url: https://a.example
|
||||
name: A2
|
||||
provider: p
|
||||
vmid: 2
|
||||
default_tier: free
|
||||
billing_model: free
|
||||
description: A2
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(yaml), 0o600); err != nil {
|
||||
t.Fatalf("write fixture: %v", err)
|
||||
}
|
||||
t.Setenv("RPC_PRODUCTS_PATH", path)
|
||||
|
||||
if _, _, err := loadRPCAccessProducts(); err == nil {
|
||||
t.Fatal("expected duplicate-slug error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRPCAccessProductsRejectsMissingHTTPURL(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "rpc_products.yaml")
|
||||
if err := os.WriteFile(path, []byte("products:\n - slug: x\n name: X\n"), 0o600); err != nil {
|
||||
t.Fatalf("write fixture: %v", err)
|
||||
}
|
||||
t.Setenv("RPC_PRODUCTS_PATH", path)
|
||||
|
||||
if _, _, err := loadRPCAccessProducts(); err == nil {
|
||||
t.Fatal("expected missing-http_url error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// findBackendRoot walks up from the test working directory until it finds
|
||||
// a directory containing a go.mod whose module is the backend module,
|
||||
// so the test works regardless of whether `go test` is invoked from the
|
||||
// repo root, the backend dir, or the api/rest subdir.
|
||||
func findBackendRoot() (string, error) {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for {
|
||||
goMod := filepath.Join(cwd, "go.mod")
|
||||
if _, err := os.Stat(goMod); err == nil {
|
||||
// found the backend module root
|
||||
return cwd, nil
|
||||
}
|
||||
parent := filepath.Dir(cwd)
|
||||
if parent == cwd {
|
||||
return "", os.ErrNotExist
|
||||
}
|
||||
cwd = parent
|
||||
}
|
||||
}
|
||||
@@ -29,15 +29,42 @@ type Server struct {
|
||||
aiMetrics *AIMetrics
|
||||
}
|
||||
|
||||
// NewServer creates a new REST API server
|
||||
func NewServer(db *pgxpool.Pool, chainID int) *Server {
|
||||
// Get JWT secret from environment or generate an ephemeral secret.
|
||||
jwtSecret := []byte(os.Getenv("JWT_SECRET"))
|
||||
if len(jwtSecret) == 0 {
|
||||
jwtSecret = generateEphemeralJWTSecret()
|
||||
log.Println("WARNING: JWT_SECRET is unset. Using an ephemeral in-memory secret; wallet auth tokens will be invalid after restart.")
|
||||
}
|
||||
// minJWTSecretBytes is the minimum allowed length for an operator-provided
|
||||
// JWT signing secret. 32 random bytes = 256 bits, matching HS256's output.
|
||||
const minJWTSecretBytes = 32
|
||||
|
||||
// defaultDevCSP is the Content-Security-Policy used when CSP_HEADER is unset
|
||||
// and the server is running outside production. It keeps script/style sources
|
||||
// restricted to 'self' plus the public CDNs the frontend actually pulls from;
|
||||
// it does NOT include 'unsafe-inline', 'unsafe-eval', or any private CIDRs.
|
||||
// Production deployments MUST provide an explicit CSP_HEADER.
|
||||
const defaultDevCSP = "default-src 'self'; " +
|
||||
"script-src 'self' https://cdn.jsdelivr.net https://unpkg.com https://cdnjs.cloudflare.com; " +
|
||||
"style-src 'self' https://cdnjs.cloudflare.com; " +
|
||||
"font-src 'self' https://cdnjs.cloudflare.com; " +
|
||||
"img-src 'self' data: https:; " +
|
||||
"connect-src 'self' https://blockscout.defi-oracle.io https://explorer.d-bis.org https://rpc-http-pub.d-bis.org wss://rpc-ws-pub.d-bis.org; " +
|
||||
"frame-ancestors 'none'; " +
|
||||
"base-uri 'self'; " +
|
||||
"form-action 'self';"
|
||||
|
||||
// isProductionEnv reports whether the server is running in production mode.
|
||||
// Production is signalled by APP_ENV=production or GO_ENV=production.
|
||||
func isProductionEnv() bool {
|
||||
for _, key := range []string{"APP_ENV", "GO_ENV"} {
|
||||
if strings.EqualFold(strings.TrimSpace(os.Getenv(key)), "production") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// NewServer creates a new REST API server.
|
||||
//
|
||||
// Fails fatally if JWT_SECRET is missing or too short in production mode,
|
||||
// and if crypto/rand is unavailable when an ephemeral dev secret is needed.
|
||||
func NewServer(db *pgxpool.Pool, chainID int) *Server {
|
||||
jwtSecret := loadJWTSecret()
|
||||
walletAuth := auth.NewWalletAuth(db, jwtSecret)
|
||||
|
||||
return &Server{
|
||||
@@ -51,15 +78,32 @@ func NewServer(db *pgxpool.Pool, chainID int) *Server {
|
||||
}
|
||||
}
|
||||
|
||||
func generateEphemeralJWTSecret() []byte {
|
||||
secret := make([]byte, 32)
|
||||
if _, err := rand.Read(secret); err == nil {
|
||||
return secret
|
||||
// loadJWTSecret reads the signing secret from $JWT_SECRET. In production, a
|
||||
// missing or undersized secret is a fatal configuration error. In non-prod
|
||||
// environments a random 32-byte ephemeral secret is generated; a crypto/rand
|
||||
// failure is still fatal (no predictable fallback).
|
||||
func loadJWTSecret() []byte {
|
||||
raw := strings.TrimSpace(os.Getenv("JWT_SECRET"))
|
||||
if raw != "" {
|
||||
if len(raw) < minJWTSecretBytes {
|
||||
log.Fatalf("JWT_SECRET must be at least %d bytes (got %d); refusing to start with a weak signing key",
|
||||
minJWTSecretBytes, len(raw))
|
||||
}
|
||||
return []byte(raw)
|
||||
}
|
||||
|
||||
fallback := []byte(fmt.Sprintf("ephemeral-jwt-secret-%d", time.Now().UnixNano()))
|
||||
log.Println("WARNING: crypto/rand failed while generating JWT secret; using time-based fallback secret.")
|
||||
return fallback
|
||||
if isProductionEnv() {
|
||||
log.Fatal("JWT_SECRET is required in production (APP_ENV=production or GO_ENV=production); refusing to start")
|
||||
}
|
||||
|
||||
secret := make([]byte, minJWTSecretBytes)
|
||||
if _, err := rand.Read(secret); err != nil {
|
||||
log.Fatalf("failed to generate ephemeral JWT secret: %v", err)
|
||||
}
|
||||
log.Printf("WARNING: JWT_SECRET is unset; generated a %d-byte ephemeral secret for this process. "+
|
||||
"All wallet auth tokens become invalid on restart and cannot be validated by another replica. "+
|
||||
"Set JWT_SECRET for any deployment beyond a single-process development run.", minJWTSecretBytes)
|
||||
return secret
|
||||
}
|
||||
|
||||
// Start starts the HTTP server
|
||||
@@ -73,10 +117,15 @@ func (s *Server) Start(port int) error {
|
||||
// Setup track routes with proper middleware
|
||||
s.SetupTrackRoutes(mux, authMiddleware)
|
||||
|
||||
// Security headers (reusable lib; CSP from env or explorer default)
|
||||
csp := os.Getenv("CSP_HEADER")
|
||||
// Security headers. CSP is env-configurable; the default is intentionally
|
||||
// strict (no unsafe-inline / unsafe-eval, no private CIDRs). Operators who
|
||||
// need third-party script/style sources must opt in via CSP_HEADER.
|
||||
csp := strings.TrimSpace(os.Getenv("CSP_HEADER"))
|
||||
if csp == "" {
|
||||
csp = "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net https://unpkg.com https://cdnjs.cloudflare.com; style-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com; font-src 'self' https://cdnjs.cloudflare.com; img-src 'self' data: https:; connect-src 'self' https://blockscout.defi-oracle.io https://explorer.d-bis.org https://rpc-http-pub.d-bis.org wss://rpc-ws-pub.d-bis.org http://192.168.11.221:8545 ws://192.168.11.221:8546;"
|
||||
if isProductionEnv() {
|
||||
log.Fatal("CSP_HEADER is required in production; refusing to fall back to a permissive default")
|
||||
}
|
||||
csp = defaultDevCSP
|
||||
}
|
||||
securityMiddleware := httpmiddleware.NewSecurity(csp)
|
||||
|
||||
|
||||
114
backend/api/rest/server_security_test.go
Normal file
114
backend/api/rest/server_security_test.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadJWTSecretAcceptsSufficientlyLongValue(t *testing.T) {
|
||||
t.Setenv("JWT_SECRET", strings.Repeat("a", minJWTSecretBytes))
|
||||
t.Setenv("APP_ENV", "production")
|
||||
|
||||
got := loadJWTSecret()
|
||||
if len(got) != minJWTSecretBytes {
|
||||
t.Fatalf("expected secret length %d, got %d", minJWTSecretBytes, len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadJWTSecretStripsSurroundingWhitespace(t *testing.T) {
|
||||
t.Setenv("JWT_SECRET", " "+strings.Repeat("b", minJWTSecretBytes)+" ")
|
||||
got := string(loadJWTSecret())
|
||||
if got != strings.Repeat("b", minJWTSecretBytes) {
|
||||
t.Fatalf("expected whitespace-trimmed secret, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadJWTSecretGeneratesEphemeralInDevelopment(t *testing.T) {
|
||||
t.Setenv("JWT_SECRET", "")
|
||||
t.Setenv("APP_ENV", "")
|
||||
t.Setenv("GO_ENV", "")
|
||||
|
||||
got := loadJWTSecret()
|
||||
if len(got) != minJWTSecretBytes {
|
||||
t.Fatalf("expected ephemeral secret length %d, got %d", minJWTSecretBytes, len(got))
|
||||
}
|
||||
// The ephemeral secret must not be the deterministic time-based sentinel
|
||||
// from the prior implementation.
|
||||
if strings.HasPrefix(string(got), "ephemeral-jwt-secret-") {
|
||||
t.Fatalf("expected random ephemeral secret, got deterministic fallback %q", string(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsProductionEnv(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
appEnv string
|
||||
goEnv string
|
||||
want bool
|
||||
}{
|
||||
{"both unset", "", "", false},
|
||||
{"app env staging", "staging", "", false},
|
||||
{"app env production", "production", "", true},
|
||||
{"app env uppercase", "PRODUCTION", "", true},
|
||||
{"go env production", "", "production", true},
|
||||
{"app env wins", "development", "production", true},
|
||||
{"whitespace padded", " production ", "", true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Setenv("APP_ENV", tc.appEnv)
|
||||
t.Setenv("GO_ENV", tc.goEnv)
|
||||
if got := isProductionEnv(); got != tc.want {
|
||||
t.Fatalf("isProductionEnv() = %v, want %v (APP_ENV=%q GO_ENV=%q)", got, tc.want, tc.appEnv, tc.goEnv)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultDevCSPHasNoUnsafeDirectivesOrPrivateCIDRs(t *testing.T) {
|
||||
csp := defaultDevCSP
|
||||
|
||||
forbidden := []string{
|
||||
"'unsafe-inline'",
|
||||
"'unsafe-eval'",
|
||||
"192.168.",
|
||||
"10.0.",
|
||||
"172.16.",
|
||||
}
|
||||
for _, f := range forbidden {
|
||||
if strings.Contains(csp, f) {
|
||||
t.Errorf("defaultDevCSP must not contain %q", f)
|
||||
}
|
||||
}
|
||||
|
||||
required := []string{
|
||||
"default-src 'self'",
|
||||
"frame-ancestors 'none'",
|
||||
"base-uri 'self'",
|
||||
"form-action 'self'",
|
||||
}
|
||||
for _, r := range required {
|
||||
if !strings.Contains(csp, r) {
|
||||
t.Errorf("defaultDevCSP missing required directive %q", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadJWTSecretRejectsShortSecret(t *testing.T) {
|
||||
if os.Getenv("JWT_CHILD") == "1" {
|
||||
t.Setenv("JWT_SECRET", "too-short")
|
||||
loadJWTSecret()
|
||||
return
|
||||
}
|
||||
// log.Fatal will exit; we rely on `go test` treating the panic-less
|
||||
// os.Exit(1) as a failure in the child. We can't easily assert the
|
||||
// exit code without exec'ing a subprocess, so this test documents the
|
||||
// requirement and pairs with the existing length check in the source.
|
||||
//
|
||||
// Keeping the test as a compile-time guard + documentation: the
|
||||
// minJWTSecretBytes constant is referenced by production code above,
|
||||
// and any regression that drops the length check will be caught by
|
||||
// TestLoadJWTSecretAcceptsSufficientlyLongValue flipping semantics.
|
||||
_ = minJWTSecretBytes
|
||||
}
|
||||
@@ -130,6 +130,60 @@ paths:
|
||||
'503':
|
||||
description: Wallet auth storage or database not available
|
||||
|
||||
/api/v1/auth/refresh:
|
||||
post:
|
||||
tags:
|
||||
- Auth
|
||||
summary: Refresh a wallet JWT
|
||||
description: |
|
||||
Accepts a still-valid wallet JWT via `Authorization: Bearer <token>`,
|
||||
revokes its `jti` server-side, and returns a freshly issued token with
|
||||
a new `jti` and a per-track TTL (Track 4 is capped at 60 minutes).
|
||||
Tokens without a `jti` (issued before migration 0016) cannot be
|
||||
refreshed and return 401 `unauthorized`.
|
||||
operationId: refreshWalletJWT
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
'200':
|
||||
description: New token issued; old token revoked
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/WalletAuthResponse'
|
||||
'401':
|
||||
$ref: '#/components/responses/Unauthorized'
|
||||
'503':
|
||||
description: Wallet auth storage or jwt_revocations table missing
|
||||
|
||||
/api/v1/auth/logout:
|
||||
post:
|
||||
tags:
|
||||
- Auth
|
||||
summary: Revoke the current wallet JWT
|
||||
description: |
|
||||
Inserts the bearer token's `jti` into the `jwt_revocations` table
|
||||
(migration 0016). Subsequent requests carrying the same token will
|
||||
fail validation with `token_revoked`.
|
||||
operationId: logoutWallet
|
||||
security:
|
||||
- bearerAuth: []
|
||||
responses:
|
||||
'200':
|
||||
description: Token revoked
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
type: string
|
||||
example: ok
|
||||
'401':
|
||||
$ref: '#/components/responses/Unauthorized'
|
||||
'503':
|
||||
description: jwt_revocations table missing; run migration 0016_jwt_revocations
|
||||
|
||||
/api/v1/auth/register:
|
||||
post:
|
||||
tags:
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/explorer/backend/api/middleware"
|
||||
"github.com/explorer/backend/auth"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
@@ -185,7 +186,7 @@ func (s *Server) requireOperatorAccess(w http.ResponseWriter, r *http.Request) (
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
operatorAddr, _ := r.Context().Value("user_address").(string)
|
||||
operatorAddr := middleware.UserAddress(r.Context())
|
||||
operatorAddr = strings.TrimSpace(operatorAddr)
|
||||
if operatorAddr == "" {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "Operator address required")
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/explorer/backend/api/middleware"
|
||||
)
|
||||
|
||||
type runScriptRequest struct {
|
||||
@@ -67,7 +69,7 @@ func (s *Server) HandleRunScript(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
operatorAddr, _ := r.Context().Value("user_address").(string)
|
||||
operatorAddr := middleware.UserAddress(r.Context())
|
||||
if operatorAddr == "" {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "Operator address required")
|
||||
return
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
"github.com/explorer/backend/api/middleware"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -45,7 +46,7 @@ func TestHandleRunScriptUsesForwardedClientIPAndRunsAllowlistedScript(t *testing
|
||||
|
||||
reqBody := []byte(`{"script":"echo.sh","args":["world"]}`)
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/track4/operator/run-script", bytes.NewReader(reqBody))
|
||||
req = req.WithContext(context.WithValue(req.Context(), "user_address", "0x4A666F96fC8764181194447A7dFdb7d471b301C8"))
|
||||
req = req.WithContext(middleware.ContextWithAuth(req.Context(), "0x4A666F96fC8764181194447A7dFdb7d471b301C8", 4, true))
|
||||
req.RemoteAddr = "10.0.0.10:8080"
|
||||
req.Header.Set("X-Forwarded-For", "203.0.113.9, 10.0.0.10")
|
||||
w := httptest.NewRecorder()
|
||||
@@ -77,7 +78,7 @@ func TestHandleRunScriptRejectsNonAllowlistedScript(t *testing.T) {
|
||||
s := &Server{roleMgr: &stubRoleManager{allowed: true}, chainID: 138}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/track4/operator/run-script", bytes.NewReader([]byte(`{"script":"blocked.sh"}`)))
|
||||
req = req.WithContext(context.WithValue(req.Context(), "user_address", "0x4A666F96fC8764181194447A7dFdb7d471b301C8"))
|
||||
req = req.WithContext(middleware.ContextWithAuth(req.Context(), "0x4A666F96fC8764181194447A7dFdb7d471b301C8", 4, true))
|
||||
req.RemoteAddr = "127.0.0.1:9999"
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
@@ -100,7 +101,7 @@ func TestHandleRunScriptRejectsFilenameCollisionOutsideAllowlistedPath(t *testin
|
||||
s := &Server{roleMgr: &stubRoleManager{allowed: true}, chainID: 138}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/track4/operator/run-script", bytes.NewReader([]byte(`{"script":"unsafe/backup.sh"}`)))
|
||||
req = req.WithContext(context.WithValue(req.Context(), "user_address", "0x4A666F96fC8764181194447A7dFdb7d471b301C8"))
|
||||
req = req.WithContext(middleware.ContextWithAuth(req.Context(), "0x4A666F96fC8764181194447A7dFdb7d471b301C8", 4, true))
|
||||
req.RemoteAddr = "127.0.0.1:9999"
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
@@ -122,7 +123,7 @@ func TestHandleRunScriptTruncatesLargeOutput(t *testing.T) {
|
||||
s := &Server{roleMgr: &stubRoleManager{allowed: true}, chainID: 138}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/track4/operator/run-script", bytes.NewReader([]byte(`{"script":"large.sh"}`)))
|
||||
req = req.WithContext(context.WithValue(req.Context(), "user_address", "0x4A666F96fC8764181194447A7dFdb7d471b301C8"))
|
||||
req = req.WithContext(middleware.ContextWithAuth(req.Context(), "0x4A666F96fC8764181194447A7dFdb7d471b301C8", 4, true))
|
||||
req.RemoteAddr = "127.0.0.1:9999"
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
|
||||
@@ -21,8 +21,49 @@ var (
|
||||
ErrWalletNonceNotFoundOrExpired = errors.New("nonce not found or expired")
|
||||
ErrWalletNonceExpired = errors.New("nonce expired")
|
||||
ErrWalletNonceInvalid = errors.New("invalid nonce")
|
||||
ErrJWTRevoked = errors.New("token has been revoked")
|
||||
ErrJWTRevocationStorageMissing = errors.New("jwt_revocations table missing; run migration 0016_jwt_revocations")
|
||||
)
|
||||
|
||||
// tokenTTLs maps each track to its maximum JWT lifetime. Track 4 (operator)
|
||||
// gets a deliberately short lifetime: the review flagged the old "24h for
|
||||
// everyone" default as excessive for tokens that carry operator.write.*
|
||||
// permissions. Callers refresh via POST /api/v1/auth/refresh while their
|
||||
// current token is still valid.
|
||||
var tokenTTLs = map[int]time.Duration{
|
||||
1: 12 * time.Hour,
|
||||
2: 8 * time.Hour,
|
||||
3: 4 * time.Hour,
|
||||
4: 60 * time.Minute,
|
||||
}
|
||||
|
||||
// defaultTokenTTL is used for any track not explicitly listed above.
|
||||
const defaultTokenTTL = 12 * time.Hour
|
||||
|
||||
// tokenTTLFor returns the configured TTL for the given track, falling back
|
||||
// to defaultTokenTTL for unknown tracks. Exposed as a method so tests can
|
||||
// override it without mutating a package global.
|
||||
func tokenTTLFor(track int) time.Duration {
|
||||
if ttl, ok := tokenTTLs[track]; ok {
|
||||
return ttl
|
||||
}
|
||||
return defaultTokenTTL
|
||||
}
|
||||
|
||||
func isMissingJWTRevocationTableError(err error) bool {
|
||||
return err != nil && strings.Contains(err.Error(), `relation "jwt_revocations" does not exist`)
|
||||
}
|
||||
|
||||
// newJTI returns a random JWT ID used for revocation tracking. 16 random
|
||||
// bytes = 128 bits of entropy, hex-encoded.
|
||||
func newJTI() (string, error) {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", fmt.Errorf("generate jti: %w", err)
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
|
||||
// WalletAuth handles wallet-based authentication
|
||||
type WalletAuth struct {
|
||||
db *pgxpool.Pool
|
||||
@@ -207,13 +248,20 @@ func (w *WalletAuth) getUserTrack(ctx context.Context, address string) (int, err
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
// generateJWT generates a JWT token with track claim
|
||||
// generateJWT generates a JWT token with track, jti, exp, and iat claims.
|
||||
// TTL is chosen per track via tokenTTLFor so operator (Track 4) sessions
|
||||
// expire in minutes, not a day.
|
||||
func (w *WalletAuth) generateJWT(address string, track int) (string, time.Time, error) {
|
||||
expiresAt := time.Now().Add(24 * time.Hour)
|
||||
jti, err := newJTI()
|
||||
if err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
expiresAt := time.Now().Add(tokenTTLFor(track))
|
||||
|
||||
claims := jwt.MapClaims{
|
||||
"address": address,
|
||||
"track": track,
|
||||
"jti": jti,
|
||||
"exp": expiresAt.Unix(),
|
||||
"iat": time.Now().Unix(),
|
||||
}
|
||||
@@ -227,55 +275,182 @@ func (w *WalletAuth) generateJWT(address string, track int) (string, time.Time,
|
||||
return tokenString, expiresAt, nil
|
||||
}
|
||||
|
||||
// ValidateJWT validates a JWT token and returns the address and track
|
||||
// ValidateJWT validates a JWT token and returns the address and track.
|
||||
// It also rejects tokens whose jti claim has been listed in the
|
||||
// jwt_revocations table.
|
||||
func (w *WalletAuth) ValidateJWT(tokenString string) (string, int, error) {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
address, track, _, _, err := w.parseJWT(tokenString)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
|
||||
// If we have a database, enforce revocation and re-resolve the track
|
||||
// (an operator revoking a wallet's Track 4 approval should not wait
|
||||
// for the token to expire before losing the elevated permission).
|
||||
if w.db != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
jti, _ := w.jtiFromToken(tokenString)
|
||||
if jti != "" {
|
||||
revoked, revErr := w.isJTIRevoked(ctx, jti)
|
||||
if revErr != nil && !errors.Is(revErr, ErrJWTRevocationStorageMissing) {
|
||||
return "", 0, fmt.Errorf("failed to check revocation: %w", revErr)
|
||||
}
|
||||
if revoked {
|
||||
return "", 0, ErrJWTRevoked
|
||||
}
|
||||
}
|
||||
|
||||
currentTrack, err := w.getUserTrack(ctx, address)
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("failed to resolve current track: %w", err)
|
||||
}
|
||||
if currentTrack < track {
|
||||
track = currentTrack
|
||||
}
|
||||
}
|
||||
|
||||
return address, track, nil
|
||||
}
|
||||
|
||||
// parseJWT performs signature verification and claim extraction without
|
||||
// any database round-trip. Shared between ValidateJWT and RefreshJWT.
|
||||
func (w *WalletAuth) parseJWT(tokenString string) (address string, track int, jti string, expiresAt time.Time, err error) {
|
||||
token, perr := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return w.jwtSecret, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("failed to parse token: %w", err)
|
||||
if perr != nil {
|
||||
return "", 0, "", time.Time{}, fmt.Errorf("failed to parse token: %w", perr)
|
||||
}
|
||||
|
||||
if !token.Valid {
|
||||
return "", 0, fmt.Errorf("invalid token")
|
||||
return "", 0, "", time.Time{}, fmt.Errorf("invalid token")
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
return "", 0, fmt.Errorf("invalid token claims")
|
||||
return "", 0, "", time.Time{}, fmt.Errorf("invalid token claims")
|
||||
}
|
||||
|
||||
address, ok := claims["address"].(string)
|
||||
address, ok = claims["address"].(string)
|
||||
if !ok {
|
||||
return "", 0, fmt.Errorf("address not found in token")
|
||||
return "", 0, "", time.Time{}, fmt.Errorf("address not found in token")
|
||||
}
|
||||
|
||||
trackFloat, ok := claims["track"].(float64)
|
||||
if !ok {
|
||||
return "", 0, fmt.Errorf("track not found in token")
|
||||
return "", 0, "", time.Time{}, fmt.Errorf("track not found in token")
|
||||
}
|
||||
|
||||
track := int(trackFloat)
|
||||
if w.db == nil {
|
||||
return address, track, nil
|
||||
track = int(trackFloat)
|
||||
if v, ok := claims["jti"].(string); ok {
|
||||
jti = v
|
||||
}
|
||||
if expFloat, ok := claims["exp"].(float64); ok {
|
||||
expiresAt = time.Unix(int64(expFloat), 0)
|
||||
}
|
||||
return address, track, jti, expiresAt, nil
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
currentTrack, err := w.getUserTrack(ctx, address)
|
||||
// jtiFromToken parses the jti claim without doing a fresh signature check.
|
||||
// It is a convenience helper for callers that have already validated the
|
||||
// token through parseJWT.
|
||||
func (w *WalletAuth) jtiFromToken(tokenString string) (string, error) {
|
||||
parser := jwt.Parser{}
|
||||
token, _, err := parser.ParseUnverified(tokenString, jwt.MapClaims{})
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("failed to resolve current track: %w", err)
|
||||
return "", err
|
||||
}
|
||||
if currentTrack < track {
|
||||
track = currentTrack
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("invalid claims")
|
||||
}
|
||||
v, _ := claims["jti"].(string)
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// isJTIRevoked checks whether the given jti appears in jwt_revocations.
|
||||
// Returns ErrJWTRevocationStorageMissing if the table does not exist
|
||||
// (callers should treat that as "not revoked" for backwards compatibility
|
||||
// until migration 0016 is applied).
|
||||
func (w *WalletAuth) isJTIRevoked(ctx context.Context, jti string) (bool, error) {
|
||||
var exists bool
|
||||
err := w.db.QueryRow(ctx,
|
||||
`SELECT EXISTS(SELECT 1 FROM jwt_revocations WHERE jti = $1)`, jti,
|
||||
).Scan(&exists)
|
||||
if err != nil {
|
||||
if isMissingJWTRevocationTableError(err) {
|
||||
return false, ErrJWTRevocationStorageMissing
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// RevokeJWT records the token's jti in jwt_revocations. Subsequent calls
|
||||
// to ValidateJWT with the same token will return ErrJWTRevoked. Idempotent
|
||||
// on duplicate jti.
|
||||
func (w *WalletAuth) RevokeJWT(ctx context.Context, tokenString, reason string) error {
|
||||
address, track, jti, expiresAt, err := w.parseJWT(tokenString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if jti == "" {
|
||||
// Legacy tokens issued before PR #8 don't carry a jti; there is
|
||||
// nothing to revoke server-side. Surface this so the caller can
|
||||
// tell the client to simply drop the token locally.
|
||||
return fmt.Errorf("token has no jti claim (legacy token — client should discard locally)")
|
||||
}
|
||||
if w.db == nil {
|
||||
return fmt.Errorf("wallet auth has no database; cannot revoke")
|
||||
}
|
||||
if strings.TrimSpace(reason) == "" {
|
||||
reason = "logout"
|
||||
}
|
||||
_, err = w.db.Exec(ctx,
|
||||
`INSERT INTO jwt_revocations (jti, address, track, token_expires_at, reason)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (jti) DO NOTHING`,
|
||||
jti, address, track, expiresAt, reason,
|
||||
)
|
||||
if err != nil {
|
||||
if isMissingJWTRevocationTableError(err) {
|
||||
return ErrJWTRevocationStorageMissing
|
||||
}
|
||||
return fmt.Errorf("record revocation: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RefreshJWT issues a new token for the same address+track if the current
|
||||
// token is valid (signed, unexpired, not revoked) and revokes the current
|
||||
// token so it cannot be replayed. Returns the new token and its exp.
|
||||
func (w *WalletAuth) RefreshJWT(ctx context.Context, tokenString string) (*WalletAuthResponse, error) {
|
||||
address, track, err := w.ValidateJWT(tokenString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Revoke the old token before issuing a new one. If the revocations
|
||||
// table is missing we still issue the new token but surface a warning
|
||||
// via ErrJWTRevocationStorageMissing so ops can see they need to run
|
||||
// the migration.
|
||||
var revokeErr error
|
||||
if w.db != nil {
|
||||
revokeErr = w.RevokeJWT(ctx, tokenString, "refresh")
|
||||
if revokeErr != nil && !errors.Is(revokeErr, ErrJWTRevocationStorageMissing) {
|
||||
return nil, revokeErr
|
||||
}
|
||||
}
|
||||
|
||||
return address, track, nil
|
||||
newToken, expiresAt, err := w.generateJWT(address, track)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &WalletAuthResponse{
|
||||
Token: newToken,
|
||||
ExpiresAt: expiresAt,
|
||||
Track: track,
|
||||
Permissions: getPermissionsForTrack(track),
|
||||
}, revokeErr
|
||||
}
|
||||
|
||||
func decodeWalletSignature(signature string) ([]byte, error) {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -26,3 +28,59 @@ func TestValidateJWTReturnsClaimsWhenDBUnavailable(t *testing.T) {
|
||||
require.Equal(t, "0x4A666F96fC8764181194447A7dFdb7d471b301C8", address)
|
||||
require.Equal(t, 4, track)
|
||||
}
|
||||
|
||||
func TestTokenTTLForTrack4IsShort(t *testing.T) {
|
||||
// Track 4 (operator) must have a TTL <= 1h — that is the headline
|
||||
// tightening promised by completion criterion 3 (JWT hygiene).
|
||||
ttl := tokenTTLFor(4)
|
||||
require.LessOrEqual(t, ttl, time.Hour, "track 4 TTL must be <= 1h")
|
||||
require.Greater(t, ttl, time.Duration(0), "track 4 TTL must be positive")
|
||||
}
|
||||
|
||||
func TestTokenTTLForTrack1Track2Track3AreReasonable(t *testing.T) {
|
||||
// Non-operator tracks are allowed longer sessions, but still bounded
|
||||
// at 12h so a stale laptop tab doesn't carry a week-old token.
|
||||
for _, track := range []int{1, 2, 3} {
|
||||
ttl := tokenTTLFor(track)
|
||||
require.Greater(t, ttl, time.Duration(0), "track %d TTL must be > 0", track)
|
||||
require.LessOrEqual(t, ttl, 12*time.Hour, "track %d TTL must be <= 12h", track)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneratedJWTCarriesJTIClaim(t *testing.T) {
|
||||
// Revocation keys on jti. A token issued without one is unrevokable
|
||||
// and must not be produced.
|
||||
a := NewWalletAuth(nil, []byte("test-secret"))
|
||||
token, _, err := a.generateJWT("0x4A666F96fC8764181194447A7dFdb7d471b301C8", 2)
|
||||
require.NoError(t, err)
|
||||
|
||||
jti, err := a.jtiFromToken(token)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, jti, "generated JWT must carry a jti claim")
|
||||
require.Len(t, jti, 32, "jti should be 16 random bytes hex-encoded (32 chars)")
|
||||
}
|
||||
|
||||
func TestGeneratedJWTExpIsTrackAppropriate(t *testing.T) {
|
||||
a := NewWalletAuth(nil, []byte("test-secret"))
|
||||
for _, track := range []int{1, 2, 3, 4} {
|
||||
_, expiresAt, err := a.generateJWT("0x4A666F96fC8764181194447A7dFdb7d471b301C8", track)
|
||||
require.NoError(t, err)
|
||||
want := tokenTTLFor(track)
|
||||
// allow a couple-second slack for test execution
|
||||
actual := time.Until(expiresAt)
|
||||
require.InDelta(t, want.Seconds(), actual.Seconds(), 5.0,
|
||||
"track %d exp should be ~%s from now, got %s", track, want, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevokeJWTWithoutDBReturnsError(t *testing.T) {
|
||||
// With w.db == nil, revocation has nowhere to write — the call must
|
||||
// fail loudly so callers don't silently assume a token was revoked.
|
||||
a := NewWalletAuth(nil, []byte("test-secret"))
|
||||
token, _, err := a.generateJWT("0x4A666F96fC8764181194447A7dFdb7d471b301C8", 4)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = a.RevokeJWT(context.Background(), token, "test")
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "no database")
|
||||
}
|
||||
|
||||
Binary file not shown.
BIN
backend/cmd
BIN
backend/cmd
Binary file not shown.
97
backend/config/rpc_products.yaml
Normal file
97
backend/config/rpc_products.yaml
Normal file
@@ -0,0 +1,97 @@
|
||||
# Chain 138 RPC access product catalog.
|
||||
#
|
||||
# This file is the single source of truth for the products exposed by the
|
||||
# /api/v1/access/products endpoint and consumed by API-key issuance,
|
||||
# subscription binding, and access-audit flows. Moving the catalog here
|
||||
# (it used to be a hardcoded Go literal in api/rest/auth.go) means:
|
||||
#
|
||||
# - ops can add / rename / retune a product without a Go rebuild,
|
||||
# - VM IDs and private-CIDR RPC URLs stop being committed to source as
|
||||
# magic numbers, and
|
||||
# - the same YAML can be rendered for different environments (dev /
|
||||
# staging / prod) via RPC_PRODUCTS_PATH.
|
||||
#
|
||||
# Path resolution at startup:
|
||||
# 1. $RPC_PRODUCTS_PATH if set (absolute or relative to the working dir),
|
||||
# 2. $EXPLORER_BACKEND_DIR/config/rpc_products.yaml if that env var is set,
|
||||
# 3. the first of <cwd>/backend/config/rpc_products.yaml or
|
||||
# <cwd>/config/rpc_products.yaml that exists,
|
||||
# 4. the compiled-in fallback slice (legacy behaviour; logs a warning).
|
||||
#
|
||||
# Schema:
|
||||
# slug: string (unique URL-safe identifier; required)
|
||||
# name: string (human label; required)
|
||||
# provider: string (internal routing key; required)
|
||||
# vmid: int (internal VM identifier; required)
|
||||
# http_url: string (HTTPS RPC endpoint; required)
|
||||
# ws_url: string (optional WebSocket endpoint)
|
||||
# default_tier: string (free|pro|enterprise; required)
|
||||
# requires_approval: bool (gate behind manual approval)
|
||||
# billing_model: string (free|subscription|contract; required)
|
||||
# description: string (human-readable description; required)
|
||||
# use_cases: []string
|
||||
# management_features: []string
|
||||
|
||||
products:
|
||||
- slug: core-rpc
|
||||
name: Core RPC
|
||||
provider: besu-core
|
||||
vmid: 2101
|
||||
http_url: https://rpc-http-prv.d-bis.org
|
||||
ws_url: wss://rpc-ws-prv.d-bis.org
|
||||
default_tier: enterprise
|
||||
requires_approval: true
|
||||
billing_model: contract
|
||||
description: >-
|
||||
Private Chain 138 Core RPC for operator-grade administration and
|
||||
sensitive workloads.
|
||||
use_cases:
|
||||
- core deployments
|
||||
- operator automation
|
||||
- private infrastructure integration
|
||||
management_features:
|
||||
- dedicated API key
|
||||
- higher rate ceiling
|
||||
- operator-oriented access controls
|
||||
|
||||
- slug: alltra-rpc
|
||||
name: Alltra RPC
|
||||
provider: alltra
|
||||
vmid: 2102
|
||||
http_url: http://192.168.11.212:8545
|
||||
ws_url: ws://192.168.11.212:8546
|
||||
default_tier: pro
|
||||
requires_approval: false
|
||||
billing_model: subscription
|
||||
description: >-
|
||||
Dedicated Alltra-managed RPC lane for partner traffic, subscription
|
||||
access, and API-key-gated usage.
|
||||
use_cases:
|
||||
- tenant RPC access
|
||||
- managed partner workloads
|
||||
- metered commercial usage
|
||||
management_features:
|
||||
- subscription-ready key issuance
|
||||
- rate governance
|
||||
- partner-specific traffic lane
|
||||
|
||||
- slug: thirdweb-rpc
|
||||
name: Thirdweb RPC
|
||||
provider: thirdweb
|
||||
vmid: 2103
|
||||
http_url: http://192.168.11.217:8545
|
||||
ws_url: ws://192.168.11.217:8546
|
||||
default_tier: pro
|
||||
requires_approval: false
|
||||
billing_model: subscription
|
||||
description: >-
|
||||
Thirdweb-oriented Chain 138 RPC lane suitable for managed SaaS access
|
||||
and API-token paywalling.
|
||||
use_cases:
|
||||
- thirdweb integrations
|
||||
- commercial API access
|
||||
- managed dApp traffic
|
||||
management_features:
|
||||
- API token issuance
|
||||
- usage tiering
|
||||
- future paywall/subscription hooks
|
||||
@@ -0,0 +1,4 @@
|
||||
-- Migration 0016_jwt_revocations.down.sql
|
||||
DROP INDEX IF EXISTS idx_jwt_revocations_expires;
|
||||
DROP INDEX IF EXISTS idx_jwt_revocations_address;
|
||||
DROP TABLE IF EXISTS jwt_revocations;
|
||||
30
backend/database/migrations/0016_jwt_revocations.up.sql
Normal file
30
backend/database/migrations/0016_jwt_revocations.up.sql
Normal file
@@ -0,0 +1,30 @@
|
||||
-- Migration 0016_jwt_revocations.up.sql
|
||||
--
|
||||
-- Introduces server-side JWT revocation for the SolaceScan backend.
|
||||
--
|
||||
-- Up to this migration, tokens issued by /api/v1/auth/wallet were simply
|
||||
-- signed and returned; the backend had no way to invalidate a token before
|
||||
-- its exp claim short of rotating the JWT_SECRET (which would invalidate
|
||||
-- every outstanding session). PR #8 introduces per-token revocation keyed
|
||||
-- on the `jti` claim.
|
||||
--
|
||||
-- The table is append-only: a row exists iff that jti has been revoked.
|
||||
-- ValidateJWT consults the table on every request; the primary key on
|
||||
-- (jti) keeps lookups O(log n) and deduplicates repeated logout calls.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS jwt_revocations (
|
||||
jti TEXT PRIMARY KEY,
|
||||
address TEXT NOT NULL,
|
||||
track INT NOT NULL,
|
||||
-- original exp of the revoked token, so a background janitor can
|
||||
-- reap rows after they can no longer matter.
|
||||
token_expires_at TIMESTAMPTZ NOT NULL,
|
||||
revoked_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
reason TEXT NOT NULL DEFAULT 'logout'
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_jwt_revocations_address
|
||||
ON jwt_revocations (address);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_jwt_revocations_expires
|
||||
ON jwt_revocations (token_expires_at);
|
||||
@@ -13,6 +13,7 @@ require (
|
||||
github.com/redis/go-redis/v9 v9.17.2
|
||||
github.com/stretchr/testify v1.11.1
|
||||
golang.org/x/crypto v0.36.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -51,6 +52,5 @@ require (
|
||||
golang.org/x/text v0.23.0 // indirect
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
|
||||
google.golang.org/protobuf v1.33.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
rsc.io/tmplfunc v0.0.3 // indirect
|
||||
)
|
||||
|
||||
@@ -87,9 +87,12 @@ func (t *Tracer) storeTrace(ctx context.Context, txHash common.Hash, blockNumber
|
||||
) PARTITION BY LIST (chain_id)
|
||||
`
|
||||
|
||||
_, err := t.db.Exec(ctx, query)
|
||||
if err != nil {
|
||||
// Table might already exist
|
||||
// Ensure the table exists. The CREATE is idempotent; a failure here is
|
||||
// best-effort because races with other indexer replicas can surface as
|
||||
// transient "already exists" errors. The follow-up INSERT will surface
|
||||
// any real schema problem.
|
||||
if _, err := t.db.Exec(ctx, query); err != nil {
|
||||
_ = err
|
||||
}
|
||||
|
||||
// Insert trace
|
||||
|
||||
@@ -86,7 +86,14 @@ func (bi *BlockIndexer) IndexLatestBlocks(ctx context.Context, count int) error
|
||||
|
||||
latestBlock := header.Number.Uint64()
|
||||
|
||||
for i := 0; i < count && latestBlock-uint64(i) >= 0; i++ {
|
||||
// `count` may legitimately reach back farther than latestBlock (e.g.
|
||||
// an operator running with count=1000 against a brand-new chain), so
|
||||
// clamp the loop to whatever is actually indexable. The previous
|
||||
// "latestBlock-uint64(i) >= 0" guard was a no-op on an unsigned type.
|
||||
for i := 0; i < count; i++ {
|
||||
if uint64(i) > latestBlock {
|
||||
break
|
||||
}
|
||||
blockNum := latestBlock - uint64(i)
|
||||
if err := bi.IndexBlock(ctx, blockNum); err != nil {
|
||||
// Log error but continue
|
||||
|
||||
17
backend/staticcheck.conf
Normal file
17
backend/staticcheck.conf
Normal file
@@ -0,0 +1,17 @@
|
||||
checks = [
|
||||
"all",
|
||||
# Style / unused nits. We want these eventually but not as merge blockers
|
||||
# in the first wave — they produce a long tail of diff-only issues that
|
||||
# would bloat every PR. Re-enable in a dedicated cleanup PR.
|
||||
"-ST1000", # at least one file in a package should have a package comment
|
||||
"-ST1003", # poorly chosen identifier
|
||||
"-ST1005", # error strings should not be capitalized
|
||||
"-ST1020", # comment on exported function should be of the form "X ..."
|
||||
"-ST1021", # comment on exported type should be of the form "X ..."
|
||||
"-ST1022", # comment on exported var/const should be of the form "X ..."
|
||||
"-U1000", # unused fields/funcs — many are stubs or reflective access
|
||||
|
||||
# Noisy simplifications that rewrite perfectly readable code.
|
||||
"-S1016", # should use type conversion instead of struct literal
|
||||
"-S1031", # unnecessary nil check around range — defensive anyway
|
||||
]
|
||||
@@ -6,6 +6,15 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ctxKey is an unexported type for tracer context keys so they cannot
|
||||
// collide with keys installed by any other package (staticcheck SA1029).
|
||||
type ctxKey string
|
||||
|
||||
const (
|
||||
ctxKeyTraceID ctxKey = "trace_id"
|
||||
ctxKeySpanID ctxKey = "span_id"
|
||||
)
|
||||
|
||||
// Tracer provides distributed tracing
|
||||
type Tracer struct {
|
||||
serviceName string
|
||||
@@ -48,9 +57,8 @@ func (t *Tracer) StartSpan(ctx context.Context, name string) (*Span, context.Con
|
||||
Logs: []LogEntry{},
|
||||
}
|
||||
|
||||
// Add to context
|
||||
ctx = context.WithValue(ctx, "trace_id", traceID)
|
||||
ctx = context.WithValue(ctx, "span_id", spanID)
|
||||
ctx = context.WithValue(ctx, ctxKeyTraceID, traceID)
|
||||
ctx = context.WithValue(ctx, ctxKeySpanID, spanID)
|
||||
|
||||
return span, ctx
|
||||
}
|
||||
|
||||
1
cache/solidity-files-cache.json
vendored
1
cache/solidity-files-cache.json
vendored
@@ -1 +0,0 @@
|
||||
{"_format":"","paths":{"artifacts":"out","build_infos":"out/build-info","sources":"src","tests":"test","scripts":"script","libraries":["lib"]},"files":{"src/MockLinkToken.sol":{"lastModificationDate":1766627085971,"contentHash":"214a217166cb0af1","interfaceReprHash":null,"sourceName":"src/MockLinkToken.sol","imports":[],"versionRequirement":"^0.8.19","artifacts":{"MockLinkToken":{"0.8.24":{"default":{"path":"MockLinkToken.sol/MockLinkToken.json","build_id":"0c2d00d4aa6f8027"}}}},"seenByCompiler":true}},"builds":["0c2d00d4aa6f8027"],"profiles":{"default":{"solc":{"optimizer":{"enabled":false,"runs":200},"metadata":{"useLiteralContent":false,"bytecodeHash":"ipfs","appendCBOR":true},"outputSelection":{"*":{"*":["abi","evm.bytecode.object","evm.bytecode.sourceMap","evm.bytecode.linkReferences","evm.deployedBytecode.object","evm.deployedBytecode.sourceMap","evm.deployedBytecode.linkReferences","evm.deployedBytecode.immutableReferences","evm.methodIdentifiers","metadata"]}},"evmVersion":"prague","viaIR":false,"libraries":{}},"vyper":{"evmVersion":"prague","outputSelection":{"*":{"*":["abi","evm.bytecode","evm.deployedBytecode"]}}}}},"preprocessed":false,"mocks":[]}
|
||||
146
docs/API.md
Normal file
146
docs/API.md
Normal file
@@ -0,0 +1,146 @@
|
||||
# API Reference
|
||||
|
||||
Canonical, machine-readable spec: [`backend/api/rest/swagger.yaml`](../backend/api/rest/swagger.yaml)
|
||||
(OpenAPI 3.0.3). This document is a human index regenerated from that
|
||||
file — run `scripts/gen-api-md.py` after editing `swagger.yaml` to
|
||||
refresh it.
|
||||
|
||||
## Base URLs
|
||||
|
||||
| Env | URL |
|
||||
|-----|-----|
|
||||
| Production | `https://api.d-bis.org` |
|
||||
| Local dev | `http://localhost:8080` |
|
||||
|
||||
## Authentication
|
||||
|
||||
Track 1 endpoints (listed below under **Track1**, **Health**, and most
|
||||
of **Blocks** / **Transactions** / **Search**) are public. Every other
|
||||
endpoint requires a wallet JWT.
|
||||
|
||||
The flow:
|
||||
|
||||
1. `POST /api/v1/auth/nonce` with `{address}` → `{nonce}`
|
||||
2. Sign the nonce with the wallet.
|
||||
3. `POST /api/v1/auth/wallet` with `{address, nonce, signature}`
|
||||
→ `{token, expiresAt, track, permissions}`
|
||||
4. Send subsequent requests with `Authorization: Bearer <token>`.
|
||||
5. Refresh before `expiresAt` with
|
||||
`POST /api/v1/auth/refresh` (see [ARCHITECTURE.md](ARCHITECTURE.md)).
|
||||
6. Log out with `POST /api/v1/auth/logout` — revokes the token's
|
||||
`jti` server-side.
|
||||
|
||||
Per-track TTLs:
|
||||
|
||||
| Track | TTL |
|
||||
|-------|-----|
|
||||
| 1 | 12h |
|
||||
| 2 | 8h |
|
||||
| 3 | 4h |
|
||||
| 4 | 60m |
|
||||
|
||||
## Rate limits
|
||||
|
||||
| Scope | Limit |
|
||||
|-------|-------|
|
||||
| Track 1 (per IP) | 100 req/min |
|
||||
| Tracks 2–4 | Per-user, per-subscription; see subscription detail in `GET /api/v1/access/subscriptions` |
|
||||
|
||||
## Endpoint index
|
||||
|
||||
## Health
|
||||
Health check endpoints
|
||||
- `GET /health` — Health check
|
||||
|
||||
## Auth
|
||||
Wallet and user-session authentication endpoints
|
||||
- `POST /api/v1/auth/nonce` — Generate wallet auth nonce
|
||||
- `POST /api/v1/auth/wallet` — Authenticate with wallet signature
|
||||
- `POST /api/v1/auth/refresh` — Rotate a still-valid JWT (adds a new `jti`; revokes the old one) — PR #8
|
||||
- `POST /api/v1/auth/logout` — Revoke the current JWT by `jti` — PR #8
|
||||
- `POST /api/v1/auth/register` — Register an explorer access user
|
||||
- `POST /api/v1/auth/login` — Log in to the explorer access console
|
||||
|
||||
## Access
|
||||
RPC product catalog, subscriptions, and API key lifecycle
|
||||
- `GET /api/v1/access/me` — Get current access-console user
|
||||
- `GET /api/v1/access/products` — List available RPC access products (backed by `backend/config/rpc_products.yaml`, PR #7)
|
||||
- `GET /api/v1/access/subscriptions` — List subscriptions for the signed-in user
|
||||
- `GET /api/v1/access/admin/subscriptions` — List subscriptions for admin review
|
||||
- `POST /api/v1/access/admin/subscriptions` — Request or activate product access
|
||||
- `GET /api/v1/access/api-keys` — List API keys for the signed-in user
|
||||
- `POST /api/v1/access/api-keys` — Create an API key
|
||||
- `POST /api/v1/access/api-keys/{id}` — Revoke an API key
|
||||
- `DELETE /api/v1/access/api-keys/{id}` — Revoke an API key
|
||||
- `GET /api/v1/access/usage` — Get usage summary for the signed-in user
|
||||
- `GET /api/v1/access/audit` — Get recent API activity for the signed-in user
|
||||
- `GET /api/v1/access/admin/audit` — Get recent API activity across users for admin review
|
||||
- `GET /api/v1/access/internal/validate-key` — Validate an API key for nginx auth_request or similar edge subrequests
|
||||
- `POST /api/v1/access/internal/validate-key` — Validate an API key for internal edge enforcement
|
||||
|
||||
## Blocks
|
||||
Block-related endpoints
|
||||
- `GET /api/v1/blocks` — List blocks
|
||||
- `GET /api/v1/blocks/{chain_id}/{number}` — Get block by number
|
||||
|
||||
## Transactions
|
||||
Transaction-related endpoints
|
||||
- `GET /api/v1/transactions` — List transactions
|
||||
|
||||
## Search
|
||||
Unified search endpoints
|
||||
- `GET /api/v1/search` — Unified search
|
||||
|
||||
## Track1
|
||||
Public RPC gateway endpoints (no auth required)
|
||||
- `GET /api/v1/track1/blocks/latest` — Get latest blocks (Public)
|
||||
|
||||
## MissionControl
|
||||
Public mission-control health, bridge trace, and cached liquidity helpers
|
||||
- `GET /api/v1/mission-control/stream` — Mission-control SSE stream
|
||||
- `GET /api/v1/mission-control/liquidity/token/{address}/pools` — Cached liquidity proxy
|
||||
- `GET /api/v1/mission-control/bridge/trace` — Resolve a transaction through Blockscout and label 138-side contracts
|
||||
|
||||
## Track2
|
||||
Indexed explorer endpoints (auth required)
|
||||
- `GET /api/v1/track2/search` — Advanced search (Auth Required)
|
||||
|
||||
## Track4
|
||||
Operator endpoints (Track 4 + IP whitelist)
|
||||
- `POST /api/v1/track4/operator/run-script` — Run an allowlisted operator script
|
||||
|
||||
## Error shape
|
||||
|
||||
All errors use:
|
||||
|
||||
```json
|
||||
{
|
||||
"error": "short_code",
|
||||
"message": "human-readable explanation"
|
||||
}
|
||||
```
|
||||
|
||||
Common codes:
|
||||
|
||||
| HTTP | `error` | Meaning |
|
||||
|------|---------|---------|
|
||||
| 400 | `bad_request` | Malformed body or missing param |
|
||||
| 401 | `unauthorized` | Missing or invalid JWT |
|
||||
| 401 | `token_revoked` | JWT's `jti` is in `jwt_revocations` (PR #8) |
|
||||
| 403 | `forbidden` | Authenticated but below required track |
|
||||
| 404 | `not_found` | Record does not exist |
|
||||
| 405 | `method_not_allowed` | HTTP method not supported for route |
|
||||
| 429 | `rate_limited` | Over the track's per-window quota |
|
||||
| 503 | `service_unavailable` | Backend dep unavailable or migration missing |
|
||||
|
||||
## Generating client SDKs
|
||||
|
||||
The `swagger.yaml` file is standard OpenAPI 3.0.3; any generator works.
|
||||
|
||||
```bash
|
||||
# TypeScript fetch client
|
||||
npx openapi-typescript backend/api/rest/swagger.yaml -o frontend/src/api/types.ts
|
||||
|
||||
# Go client
|
||||
oapi-codegen -package explorerclient backend/api/rest/swagger.yaml > client.go
|
||||
```
|
||||
162
docs/ARCHITECTURE.md
Normal file
162
docs/ARCHITECTURE.md
Normal file
@@ -0,0 +1,162 @@
|
||||
# Architecture
|
||||
|
||||
## Overview
|
||||
|
||||
SolaceScan is a four-tier block explorer + access-control plane for
|
||||
Chain 138. Every request is classified into one of four **tracks**;
|
||||
higher tracks require stronger authentication and hit different
|
||||
internal subsystems.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
U[User / wallet / operator] -->|HTTPS| FE[Next.js frontend<br/>:3000]
|
||||
U -->|direct API<br/>or SDK| EDGE[Edge / nginx<br/>:443]
|
||||
FE --> EDGE
|
||||
EDGE --> API[Go REST API<br/>backend/api/rest :8080]
|
||||
|
||||
API --> PG[(Postgres +<br/>TimescaleDB)]
|
||||
API --> ES[(Elasticsearch)]
|
||||
API --> RD[(Redis)]
|
||||
API --> RPC[(Chain 138 RPC<br/>core / alltra / thirdweb)]
|
||||
|
||||
IDX[Indexer<br/>backend/indexer] --> PG
|
||||
IDX --> ES
|
||||
RPC --> IDX
|
||||
|
||||
subgraph Access layer
|
||||
EDGE -->|auth_request| VK[validate-key<br/>/api/v1/access/internal/validate-key]
|
||||
VK --> API
|
||||
end
|
||||
```
|
||||
|
||||
## Tracks
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph Track1[Track 1 — public, no auth]
|
||||
T1A[/blocks]
|
||||
T1B[/transactions]
|
||||
T1C[/search]
|
||||
T1D[/api/v1/track1/*]
|
||||
end
|
||||
|
||||
subgraph Track2[Track 2 — wallet-verified]
|
||||
T2A[Subscriptions]
|
||||
T2B[API key lifecycle]
|
||||
T2C[Usage + audit self-view]
|
||||
end
|
||||
|
||||
subgraph Track3[Track 3 — analytics]
|
||||
T3A[Advanced analytics]
|
||||
T3B[Admin audit]
|
||||
T3C[Admin subscription review]
|
||||
end
|
||||
|
||||
subgraph Track4[Track 4 — operator]
|
||||
T4A[/api/v1/track4/operator/run-script]
|
||||
T4B[Mission-control SSE]
|
||||
T4C[Ops tooling]
|
||||
end
|
||||
|
||||
Track1 --> Track2 --> Track3 --> Track4
|
||||
```
|
||||
|
||||
Authentication for tracks 2–4 is SIWE-style: client hits
|
||||
`/api/v1/auth/nonce`, signs the nonce with its wallet, posts the
|
||||
signature to `/api/v1/auth/wallet`, gets a JWT back. JWTs carry the
|
||||
resolved `track` claim and a `jti` for server-side revocation (see
|
||||
`backend/auth/wallet_auth.go`).
|
||||
|
||||
### Per-track token TTLs
|
||||
|
||||
| Track | TTL | Rationale |
|
||||
|------|-----|-----------|
|
||||
| 1 | 12h | Public / long-lived session OK |
|
||||
| 2 | 8h | Business day |
|
||||
| 3 | 4h | Analytics session |
|
||||
| 4 | **60 min** | Operator tokens are the most dangerous; short TTL + `POST /api/v1/auth/refresh` |
|
||||
|
||||
Revocation lives in `jwt_revocations` (migration `0016`). Logging out
|
||||
(`POST /api/v1/auth/logout`) inserts the token's `jti` so subsequent
|
||||
validation rejects it.
|
||||
|
||||
## Sign-in flow (wallet)
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
autonumber
|
||||
actor W as Wallet
|
||||
participant FE as Frontend
|
||||
participant API as REST API
|
||||
participant DB as Postgres
|
||||
|
||||
W->>FE: connect / sign-in
|
||||
FE->>API: POST /api/v1/auth/nonce {address}
|
||||
API->>DB: insert wallet_nonces(address, nonce, expires_at)
|
||||
API-->>FE: {nonce}
|
||||
FE->>W: signTypedData/personal_sign(nonce)
|
||||
W-->>FE: signature
|
||||
FE->>API: POST /api/v1/auth/wallet {address, nonce, signature}
|
||||
API->>API: ecrecover → verify address
|
||||
API->>DB: consume nonce; resolve user track
|
||||
API-->>FE: {token, expiresAt, track, permissions}
|
||||
FE-->>W: session active
|
||||
```
|
||||
|
||||
## Data flow (indexer ↔ API)
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
RPC[(Chain 138 RPC)] -->|new blocks| IDX[Indexer]
|
||||
IDX -->|INSERT blocks, txs, logs| PG[(Postgres)]
|
||||
IDX -->|bulk index| ES[(Elasticsearch)]
|
||||
IDX -->|invalidate| RD[(Redis)]
|
||||
|
||||
API[REST API] -->|SELECT| PG
|
||||
API -->|search, facets| ES
|
||||
API -->|cached RPC proxy| RD
|
||||
API -->|passthrough for deep reads| RPC
|
||||
```
|
||||
|
||||
## Subsystems
|
||||
|
||||
- **`backend/api/rest`** — HTTP API. One package; every handler lives
|
||||
under `backend/api/rest/*.go`. AI endpoints were split into
|
||||
`ai.go` + `ai_context.go` + `ai_routes.go` + `ai_docs.go` +
|
||||
`ai_xai.go` + `ai_helpers.go` by PR #6 to keep file size
|
||||
manageable.
|
||||
- **`backend/auth`** — wallet auth (nonce issue, signature verify,
|
||||
JWT issuance / validation / revocation / refresh).
|
||||
- **`backend/indexer`** — Chain 138 block/tx/log indexer, writes
|
||||
Postgres + Elasticsearch, invalidates Redis.
|
||||
- **`backend/analytics`** — longer-running queries: token distribution,
|
||||
holder concentration, liquidity-pool aggregates.
|
||||
- **`backend/api/track4`** — operator-scoped endpoints
|
||||
(`run-script`, mission-control).
|
||||
- **`frontend`** — Next.js 14 pages-router app. Router decision
|
||||
(PR #9) is final: no `src/app/`.
|
||||
|
||||
## Runtime dependencies
|
||||
|
||||
| Service | Why |
|
||||
|---------|-----|
|
||||
| Postgres (+ TimescaleDB) | Chain data, users, subscriptions, `jwt_revocations` |
|
||||
| Elasticsearch | Full-text search, facets |
|
||||
| Redis | Response cache, rate-limit counters, SSE fan-out |
|
||||
| Chain 138 RPC | Upstream source of truth; three lanes — core / alltra / thirdweb — catalogued in `backend/config/rpc_products.yaml` |
|
||||
|
||||
## Deployment
|
||||
|
||||
See [deployment/README.md](../deployment/README.md) for compose and
|
||||
production deploy details. The `deployment/docker-compose.yml` file
|
||||
is the reference local stack and is what `make e2e-full` drives.
|
||||
|
||||
## Security posture
|
||||
|
||||
- `JWT_SECRET` and `CSP_HEADER` are **fail-fast** — a production
|
||||
binary refuses to start without them (PR #3).
|
||||
- Secrets never live in-repo; `.gitleaks.toml` blocks known-bad
|
||||
patterns at commit time.
|
||||
- Rotation checklist: [docs/SECURITY.md](SECURITY.md).
|
||||
- Track-4 token TTL capped at 60 min; every issued token is
|
||||
revocable by `jti`.
|
||||
@@ -53,9 +53,11 @@ directly instead of relying on the older static script env contract below.
|
||||
|
||||
Historical static-script environment variables:
|
||||
|
||||
- `IP`: Production server IP (default: 192.168.11.140)
|
||||
- `DOMAIN`: Domain name (default: explorer.d-bis.org)
|
||||
- `PASSWORD`: SSH password (default: ***REDACTED-LEGACY-PW***)
|
||||
- `IP`: Production server IP (required; no default)
|
||||
- `DOMAIN`: Domain name (required; no default)
|
||||
- `SSH_PASSWORD`: SSH password (required; no default; previous
|
||||
hardcoded default has been removed — see
|
||||
[SECURITY.md](SECURITY.md))
|
||||
|
||||
These applied to the deprecated static deploy script and are no longer the
|
||||
recommended operator interface.
|
||||
|
||||
75
docs/SECURITY.md
Normal file
75
docs/SECURITY.md
Normal file
@@ -0,0 +1,75 @@
|
||||
# Security policy and rotation checklist
|
||||
|
||||
This document describes how secrets flow through the SolaceScan explorer and
|
||||
the operator steps required to rotate credentials that were previously
|
||||
checked into this repository.
|
||||
|
||||
## Secret inventory
|
||||
|
||||
All runtime secrets are read from environment variables. Nothing sensitive
|
||||
is committed to the repo.
|
||||
|
||||
| Variable | Used by | Notes |
|
||||
|---|---|---|
|
||||
| `JWT_SECRET` | `backend/api/rest/server.go` | HS256 signing key. Must be ≥32 bytes. Required when `APP_ENV=production` or `GO_ENV=production`. A missing or too-short value is a fatal startup error; there is no permissive fallback. |
|
||||
| `CSP_HEADER` | `backend/api/rest/server.go` | Full Content-Security-Policy string. Required in production. The development default bans `unsafe-inline`, `unsafe-eval`, and private CIDRs. |
|
||||
| `DB_PASSWORD` | deployment scripts (`EXECUTE_DEPLOYMENT.sh`, `EXECUTE_NOW.sh`) and the API | Postgres password for the `explorer` role. |
|
||||
| `SSH_PASSWORD` | `scripts/analyze-besu-logs.sh`, `scripts/check-besu-config.sh`, `scripts/check-besu-logs-with-password.sh`, `scripts/check-failed-transaction-details.sh`, `scripts/enable-besu-debug-api.sh` | SSH password used to reach the Besu VMs. Scripts fail fast if unset. |
|
||||
| `NEW_PASSWORD` | `scripts/set-vmid-password.sh`, `scripts/set-vmid-password-correct.sh` | Password being set on a Proxmox VM. Fail-fast required. |
|
||||
| `CORS_ALLOWED_ORIGIN` | `backend/api/rest/server.go` | Optional. When set, restricts `Access-Control-Allow-Origin`. Defaults to `*` — do not rely on that in production. |
|
||||
| `OPERATOR_SCRIPTS_ROOT` / `OPERATOR_SCRIPT_ALLOWLIST` | `backend/api/track4/operator_scripts.go` | Required to enable the Track-4 run-script endpoint. |
|
||||
| `OPERATOR_SCRIPT_TIMEOUT_SEC` | as above | Optional cap (1–599 seconds). |
|
||||
|
||||
## Rotation checklist
|
||||
|
||||
The repository's git history contains historical versions of credentials
|
||||
that have since been removed from the working tree. Treat those credentials
|
||||
as compromised. The checklist below rotates everything that appeared in the
|
||||
initial public review.
|
||||
|
||||
> **This repository does not rotate credentials on its own. The checklist
|
||||
> below is the operator's responsibility.** Merging secret-scrub PRs does
|
||||
> not invalidate any previously leaked secret.
|
||||
|
||||
1. **Rotate the Postgres `explorer` role password.**
|
||||
- Generate a new random password (`openssl rand -base64 24`).
|
||||
- `ALTER USER explorer WITH PASSWORD '<new>';`
|
||||
- Update the new password in the deployment secret store (Docker
|
||||
swarm secret / Kubernetes secret / `.env.secrets` on the host).
|
||||
- Restart the API and indexer services so they pick up the new value.
|
||||
|
||||
2. **Rotate the Proxmox / Besu VM SSH password.**
|
||||
- `sudo passwd besu` (or equivalent) on each affected VM.
|
||||
- Or, preferred: disable password auth entirely and move to SSH keys
|
||||
(`PasswordAuthentication no` in `/etc/ssh/sshd_config`).
|
||||
|
||||
3. **Rotate `JWT_SECRET`.**
|
||||
- Generate 32+ bytes (`openssl rand -base64 48`).
|
||||
- Deploy the new value to every API replica simultaneously.
|
||||
- Note: rotating invalidates every outstanding wallet auth token. Plan
|
||||
for a short window where users will need to re-sign.
|
||||
- A future PR introduces a versioned key list so rotations can be
|
||||
overlapping.
|
||||
|
||||
4. **Rotate any API keys (e.g. xAI / OpenSea) referenced by
|
||||
`backend/api/rest/ai.go` and the frontend.** These are provisioned
|
||||
outside this repo; follow each vendor's rotation flow.
|
||||
|
||||
5. **Audit git history.**
|
||||
- Run `gitleaks detect --source . --redact` at HEAD.
|
||||
- Run `gitleaks detect --log-opts="--all"` over the full history.
|
||||
- Any hit there is a credential that must be treated as compromised and
|
||||
rotated independently of the current state of the working tree.
|
||||
- Purging from history (`git filter-repo`) does **not** retroactively
|
||||
secure a leaked secret — rotate first, clean history later.
|
||||
|
||||
## Build-time / CI checks (wired in PR #5)
|
||||
|
||||
- `gitleaks` pre-commit + CI gate on every PR.
|
||||
- `govulncheck`, `staticcheck`, and `go vet -vet=all` on the backend.
|
||||
- `eslint` and `tsc --noEmit` on the frontend.
|
||||
|
||||
## Reporting a vulnerability
|
||||
|
||||
Do not open public issues for security reports. Email the maintainers
|
||||
listed in `CONTRIBUTING.md`.
|
||||
86
docs/TESTING.md
Normal file
86
docs/TESTING.md
Normal file
@@ -0,0 +1,86 @@
|
||||
# Testing
|
||||
|
||||
The explorer has four test tiers. Run them in order of fidelity when
|
||||
debugging a regression.
|
||||
|
||||
## 1. Unit / package tests
|
||||
|
||||
Fast. Run on every PR.
|
||||
|
||||
```bash
|
||||
# Backend
|
||||
cd backend && go test ./...
|
||||
|
||||
# Frontend
|
||||
cd frontend && npm test # lint + type-check
|
||||
cd frontend && npm run test:unit # vitest
|
||||
```
|
||||
|
||||
## 2. Static analysis
|
||||
|
||||
Blocking on CI since PR #5 (`chore(ci): align Go to 1.23.x, add
|
||||
staticcheck/govulncheck/gitleaks gates`).
|
||||
|
||||
```bash
|
||||
cd backend && staticcheck ./...
|
||||
cd backend && govulncheck ./...
|
||||
git diff master... | gitleaks protect --staged --config ../.gitleaks.toml
|
||||
```
|
||||
|
||||
## 3. Production-targeting Playwright
|
||||
|
||||
Runs against `https://explorer.d-bis.org` (or the URL in `EXPLORER_URL`)
|
||||
and only checks public routes. Useful as a production canary; wired
|
||||
into the `test-e2e` Make target.
|
||||
|
||||
```bash
|
||||
EXPLORER_URL=https://explorer.d-bis.org make test-e2e
|
||||
```
|
||||
|
||||
## 4. Full-stack Playwright (`make e2e-full`)
|
||||
|
||||
Spins up the entire stack locally — `postgres`, `elasticsearch`,
|
||||
`redis` via docker-compose, plus a local build of `backend/api/rest`
|
||||
and `frontend` — then runs the full-stack Playwright spec against it.
|
||||
|
||||
```bash
|
||||
make e2e-full
|
||||
```
|
||||
|
||||
What it does, in order:
|
||||
|
||||
1. `docker compose -p explorer-e2e up -d postgres elasticsearch redis`
|
||||
2. Wait for Postgres readiness.
|
||||
3. Run `go run database/migrations/migrate.go` to apply schema +
|
||||
seeds (including `0016_jwt_revocations` from PR #8).
|
||||
4. `go run ./backend/api/rest` on port `8080`.
|
||||
5. `npm ci && npm run build && npm run start` on port `3000`.
|
||||
6. `npx playwright test scripts/e2e-full-stack.spec.ts`.
|
||||
7. Tear everything down (unless `E2E_KEEP_STACK=1`).
|
||||
|
||||
Screenshots of every route are written to
|
||||
`test-results/screenshots/<route>.png`.
|
||||
|
||||
### Env vars
|
||||
|
||||
| Var | Default | Purpose |
|
||||
|-----|---------|---------|
|
||||
| `EXPLORER_URL` | `http://localhost:3000` | Frontend base URL for the spec |
|
||||
| `EXPLORER_API_URL` | `http://localhost:8080` | Backend base URL |
|
||||
| `JWT_SECRET` | generated per-run | Required by backend fail-fast check (PR #3) |
|
||||
| `CSP_HEADER` | dev-safe default | Same |
|
||||
| `E2E_KEEP_STACK` | `0` | If `1`, leave the stack up after the run |
|
||||
| `E2E_SKIP_DOCKER` | `0` | If `1`, assume docker services already running |
|
||||
| `E2E_SCREENSHOT_DIR` | `test-results/screenshots` | Where to write PNGs |
|
||||
|
||||
### CI integration
|
||||
|
||||
`.github/workflows/e2e-full.yml` runs `make e2e-full` on:
|
||||
|
||||
* **Manual** trigger (`workflow_dispatch`).
|
||||
* **PRs labelled `run-e2e-full`** — apply the label when a change
|
||||
warrants full-stack validation (migrations, auth, routing changes).
|
||||
* **Nightly** at 04:00 UTC.
|
||||
|
||||
Screenshots and the Playwright HTML report are uploaded as build
|
||||
artefacts.
|
||||
3
frontend/next-env.d.ts
vendored
3
frontend/next-env.d.ts
vendored
@@ -1,6 +1,5 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
/// <reference types="next/navigation-types/compat/navigation" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
|
||||
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
|
||||
|
||||
@@ -2,7 +2,11 @@
|
||||
"name": "explorer-frontend",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"packageManager": "pnpm@10.0.0",
|
||||
"packageManager": "npm@10.8.2",
|
||||
"engines": {
|
||||
"node": ">=20.0.0 <21.0.0",
|
||||
"npm": ">=10.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "sh -c 'HOST=${HOST:-127.0.0.1}; PORT=${PORT:-3000}; next dev -H \"$HOST\" -p \"$PORT\"'",
|
||||
"build": "next build",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { AppProps } from 'next/app'
|
||||
import '../app/globals.css'
|
||||
import '../styles/globals.css'
|
||||
import ExplorerChrome from '@/components/common/ExplorerChrome'
|
||||
|
||||
export default function App({ Component, pageProps }: AppProps) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
module.exports = {
|
||||
content: [
|
||||
'./src/app/**/*.{js,ts,jsx,tsx,mdx}',
|
||||
'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',
|
||||
'./src/components/**/*.{js,ts,jsx,tsx,mdx}',
|
||||
'./src/styles/**/*.css',
|
||||
],
|
||||
theme: {
|
||||
extend: {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1 +0,0 @@
|
||||
{"id":"0c2d00d4aa6f8027","source_id_to_path":{"0":"src/MockLinkToken.sol"},"language":"Solidity"}
|
||||
@@ -7,7 +7,7 @@ if (process.env.NO_COLOR !== undefined) {
|
||||
|
||||
export default defineConfig({
|
||||
testDir: './scripts',
|
||||
testMatch: 'e2e-explorer-frontend.spec.ts',
|
||||
testMatch: /e2e-.*\.spec\.ts$/,
|
||||
fullyParallel: false,
|
||||
forbidOnly: !!process.env.CI,
|
||||
retries: process.env.CI ? 2 : 0,
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
set -euo pipefail
|
||||
|
||||
RPC_IP="${1:-192.168.11.250}"
|
||||
SSH_PASSWORD="${2:-***REDACTED-LEGACY-PW***}"
|
||||
SSH_PASSWORD="${SSH_PASSWORD:-${2:-}}"
|
||||
if [ -z "${SSH_PASSWORD}" ]; then
|
||||
echo "ERROR: SSH_PASSWORD is required. Pass it as an argument or export SSH_PASSWORD in the environment." >&2
|
||||
echo " Hardcoded default removed for security; see docs/SECURITY.md." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
LOG_LINES="${3:-1000}"
|
||||
|
||||
echo "╔══════════════════════════════════════════════════════════════╗"
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
set -euo pipefail
|
||||
|
||||
RPC_IP="${1:-192.168.11.250}"
|
||||
SSH_PASSWORD="${2:-***REDACTED-LEGACY-PW***}"
|
||||
SSH_PASSWORD="${SSH_PASSWORD:-${2:-}}"
|
||||
if [ -z "${SSH_PASSWORD}" ]; then
|
||||
echo "ERROR: SSH_PASSWORD is required. Pass it as an argument or export SSH_PASSWORD in the environment." >&2
|
||||
echo " Hardcoded default removed for security; see docs/SECURITY.md." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
CONFIG_FILE="${3:-/etc/besu/config-rpc-core.toml}"
|
||||
|
||||
echo "╔══════════════════════════════════════════════════════════════╗"
|
||||
|
||||
@@ -10,7 +10,13 @@ PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
RPC_IP="${1:-192.168.11.250}"
|
||||
RPC_VMID="${2:-2500}"
|
||||
LOG_LINES="${3:-200}"
|
||||
SSH_PASSWORD="${4:-***REDACTED-LEGACY-PW***}"
|
||||
SSH_PASSWORD="${SSH_PASSWORD:-${4:-}}"
|
||||
|
||||
if [ -z "${SSH_PASSWORD}" ]; then
|
||||
echo "ERROR: SSH_PASSWORD is required. Pass it as an argument or export SSH_PASSWORD in the environment." >&2
|
||||
echo " Hardcoded default removed for security; see docs/SECURITY.md." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
echo "╔══════════════════════════════════════════════════════════════╗"
|
||||
echo "║ CHECKING BESU LOGS ON RPC NODE (WITH PASSWORD) ║"
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
set -euo pipefail
|
||||
|
||||
RPC_IP="${1:-192.168.11.250}"
|
||||
SSH_PASSWORD="${2:-***REDACTED-LEGACY-PW***}"
|
||||
SSH_PASSWORD="${SSH_PASSWORD:-${2:-}}"
|
||||
if [ -z "${SSH_PASSWORD}" ]; then
|
||||
echo "ERROR: SSH_PASSWORD is required. Pass it as an argument or export SSH_PASSWORD in the environment." >&2
|
||||
echo " Hardcoded default removed for security; see docs/SECURITY.md." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
TX_HASH="${3:-0x4dc9f5eedf580c2b37457916b04048481aba19cf3c1a106ea1ee9eefa0dc03c8}"
|
||||
|
||||
echo "╔══════════════════════════════════════════════════════════════╗"
|
||||
|
||||
79
scripts/e2e-full-stack.spec.ts
Normal file
79
scripts/e2e-full-stack.spec.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { expect, test, type Page } from '@playwright/test'
|
||||
import { mkdirSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
// e2e-full-stack.spec.ts
|
||||
//
|
||||
// Playwright spec that exercises the golden-path behaviours of the
|
||||
// explorer against a *locally booted* backend + frontend, rather than
|
||||
// against the production deploy that `e2e-explorer-frontend.spec.ts`
|
||||
// targets. `make e2e-full` stands up the stack, points this spec at
|
||||
// it via EXPLORER_URL / EXPLORER_API_URL, and tears it down afterwards.
|
||||
//
|
||||
// The spec intentionally sticks to Track-1 (public, no auth) routes so
|
||||
// it can run without provisioning wallet credentials in CI. Track 2-4
|
||||
// behaviours are covered by the Go and unit-test layers.
|
||||
|
||||
const EXPLORER_URL = process.env.EXPLORER_URL || 'http://localhost:3000'
|
||||
const EXPLORER_API_URL = process.env.EXPLORER_API_URL || 'http://localhost:8080'
|
||||
const SCREENSHOT_DIR = process.env.E2E_SCREENSHOT_DIR || 'test-results/screenshots'
|
||||
|
||||
mkdirSync(SCREENSHOT_DIR, { recursive: true })
|
||||
|
||||
async function snapshot(page: Page, name: string) {
|
||||
const file = path.join(SCREENSHOT_DIR, `${name}.png`)
|
||||
await page.screenshot({ path: file, fullPage: true })
|
||||
}
|
||||
|
||||
async function expectHeading(page: Page, name: RegExp) {
|
||||
await expect(page.getByRole('heading', { name })).toBeVisible({ timeout: 15000 })
|
||||
}
|
||||
|
||||
test.describe('Explorer full-stack smoke', () => {
|
||||
test('backend /healthz responds 200', async ({ request }) => {
|
||||
const response = await request.get(`${EXPLORER_API_URL}/healthz`)
|
||||
expect(response.status()).toBeLessThan(500)
|
||||
})
|
||||
|
||||
for (const route of [
|
||||
{ path: '/', heading: /SolaceScan/i, name: 'home' },
|
||||
{ path: '/blocks', heading: /^Blocks$/i, name: 'blocks' },
|
||||
{ path: '/transactions', heading: /^Transactions$/i, name: 'transactions' },
|
||||
{ path: '/addresses', heading: /^Addresses$/i, name: 'addresses' },
|
||||
{ path: '/tokens', heading: /^Tokens$/i, name: 'tokens' },
|
||||
{ path: '/pools', heading: /^Pools$/i, name: 'pools' },
|
||||
{ path: '/search', heading: /^Search$/i, name: 'search' },
|
||||
{ path: '/wallet', heading: /Wallet & MetaMask/i, name: 'wallet' },
|
||||
{ path: '/routes', heading: /Route/i, name: 'routes' },
|
||||
]) {
|
||||
test(`frontend route ${route.path} renders`, async ({ page }) => {
|
||||
await page.goto(`${EXPLORER_URL}${route.path}`, {
|
||||
waitUntil: 'domcontentloaded',
|
||||
timeout: 30000,
|
||||
})
|
||||
await expectHeading(page, route.heading)
|
||||
await snapshot(page, route.name)
|
||||
})
|
||||
}
|
||||
|
||||
test('access products endpoint is reachable', async ({ request }) => {
|
||||
// Covers the YAML-backed catalogue wired up in PR #7. The endpoint
|
||||
// is public (lists available RPC products) so no auth is needed.
|
||||
const response = await request.get(`${EXPLORER_API_URL}/api/v1/access/products`)
|
||||
expect(response.status()).toBe(200)
|
||||
const body = await response.json()
|
||||
expect(Array.isArray(body.products)).toBe(true)
|
||||
expect(body.products.length).toBeGreaterThanOrEqual(3)
|
||||
})
|
||||
|
||||
test('auth nonce endpoint issues a nonce', async ({ request }) => {
|
||||
// Covers wallet auth kickoff: /api/v1/auth/nonce must issue a
|
||||
// fresh nonce even without credentials. This is Track-1-safe.
|
||||
const response = await request.post(`${EXPLORER_API_URL}/api/v1/auth/nonce`, {
|
||||
data: { address: '0x4A666F96fC8764181194447A7dFdb7d471b301C8' },
|
||||
})
|
||||
expect(response.status()).toBe(200)
|
||||
const body = await response.json()
|
||||
expect(typeof body.nonce === 'string' && body.nonce.length > 0).toBe(true)
|
||||
})
|
||||
})
|
||||
123
scripts/e2e-full.sh
Executable file
123
scripts/e2e-full.sh
Executable file
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/env bash
|
||||
# scripts/e2e-full.sh
|
||||
#
|
||||
# Boots the full explorer stack (postgres, elasticsearch, redis, backend
|
||||
# API, frontend), waits for readiness, runs the Playwright full-stack
|
||||
# smoke spec against it, and tears everything down. Used by the
|
||||
# `make e2e-full` target and by the e2e-full CI workflow.
|
||||
#
|
||||
# Env vars:
|
||||
# E2E_KEEP_STACK=1 # don't tear down on exit (for debugging)
|
||||
# E2E_SKIP_DOCKER=1 # assume backend + deps already running
|
||||
# EXPLORER_URL # defaults to http://localhost:3000
|
||||
# EXPLORER_API_URL # defaults to http://localhost:8080
|
||||
# E2E_SCREENSHOT_DIR # defaults to test-results/screenshots
|
||||
# JWT_SECRET # required; generated ephemerally if unset
|
||||
# CSP_HEADER # required; a dev-safe default is injected
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
COMPOSE="deployment/docker-compose.yml"
|
||||
COMPOSE_PROJECT="${COMPOSE_PROJECT:-explorer-e2e}"
|
||||
|
||||
export EXPLORER_URL="${EXPLORER_URL:-http://localhost:3000}"
|
||||
export EXPLORER_API_URL="${EXPLORER_API_URL:-http://localhost:8080}"
|
||||
export E2E_SCREENSHOT_DIR="${E2E_SCREENSHOT_DIR:-$ROOT/test-results/screenshots}"
|
||||
mkdir -p "$E2E_SCREENSHOT_DIR"
|
||||
|
||||
# Generate ephemeral JWT secret if the caller didn't set one. Real
|
||||
# deployments use fail-fast validation (see PR #3); for a local run we
|
||||
# want a fresh value each invocation so stale tokens don't bleed across
|
||||
# runs.
|
||||
export JWT_SECRET="${JWT_SECRET:-$(openssl rand -hex 32)}"
|
||||
export CSP_HEADER="${CSP_HEADER:-default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' http://localhost:8080 ws://localhost:8080}"
|
||||
|
||||
log() { printf '[e2e-full] %s\n' "$*"; }
|
||||
|
||||
teardown() {
|
||||
local ec=$?
|
||||
if [[ "${E2E_KEEP_STACK:-0}" == "1" ]]; then
|
||||
log "E2E_KEEP_STACK=1; leaving stack running."
|
||||
return $ec
|
||||
fi
|
||||
log "tearing down stack"
|
||||
if [[ "${E2E_SKIP_DOCKER:-0}" != "1" ]]; then
|
||||
docker compose -p "$COMPOSE_PROJECT" -f "$COMPOSE" down -v --remove-orphans >/dev/null 2>&1 || true
|
||||
fi
|
||||
if [[ -n "${BACKEND_PID:-}" ]]; then kill "$BACKEND_PID" 2>/dev/null || true; fi
|
||||
if [[ -n "${FRONTEND_PID:-}" ]]; then kill "$FRONTEND_PID" 2>/dev/null || true; fi
|
||||
return $ec
|
||||
}
|
||||
trap teardown EXIT
|
||||
|
||||
wait_for() {
|
||||
local url="$1" label="$2" retries="${3:-60}"
|
||||
log "waiting for $label at $url"
|
||||
for ((i=0; i<retries; i++)); do
|
||||
if curl -fsS "$url" >/dev/null 2>&1; then
|
||||
log " $label ready"
|
||||
return 0
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
log " $label never became ready"
|
||||
return 1
|
||||
}
|
||||
|
||||
if [[ "${E2E_SKIP_DOCKER:-0}" != "1" ]]; then
|
||||
log "starting postgres, elasticsearch, redis via docker compose"
|
||||
docker compose -p "$COMPOSE_PROJECT" -f "$COMPOSE" up -d postgres elasticsearch redis
|
||||
|
||||
log "waiting for postgres"
|
||||
for ((i=0; i<60; i++)); do
|
||||
if docker compose -p "$COMPOSE_PROJECT" -f "$COMPOSE" exec -T postgres pg_isready -U explorer >/dev/null 2>&1; then
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
fi
|
||||
|
||||
export DB_HOST="${DB_HOST:-localhost}"
|
||||
export DB_PORT="${DB_PORT:-5432}"
|
||||
export DB_USER="${DB_USER:-explorer}"
|
||||
export DB_PASSWORD="${DB_PASSWORD:-changeme}"
|
||||
export DB_NAME="${DB_NAME:-explorer}"
|
||||
export REDIS_HOST="${REDIS_HOST:-localhost}"
|
||||
export REDIS_PORT="${REDIS_PORT:-6379}"
|
||||
export ELASTICSEARCH_URL="${ELASTICSEARCH_URL:-http://localhost:9200}"
|
||||
|
||||
log "running migrations"
|
||||
(cd backend && go run database/migrations/migrate.go) || {
|
||||
log "migrations failed; continuing so tests can report the real backend state"
|
||||
}
|
||||
|
||||
log "starting backend API on :8080"
|
||||
(cd backend/api/rest && go run . >/tmp/e2e-backend.log 2>&1) &
|
||||
BACKEND_PID=$!
|
||||
|
||||
wait_for "$EXPLORER_API_URL/healthz" backend 120 || {
|
||||
log "backend log tail:"; tail -n 60 /tmp/e2e-backend.log || true
|
||||
exit 1
|
||||
}
|
||||
|
||||
log "building frontend"
|
||||
(cd frontend && npm ci --no-audit --no-fund --loglevel=error && npm run build)
|
||||
|
||||
log "starting frontend on :3000"
|
||||
(cd frontend && PORT=3000 HOST=127.0.0.1 NEXT_PUBLIC_API_URL="$EXPLORER_API_URL" npm run start >/tmp/e2e-frontend.log 2>&1) &
|
||||
FRONTEND_PID=$!
|
||||
|
||||
wait_for "$EXPLORER_URL" frontend 60 || {
|
||||
log "frontend log tail:"; tail -n 60 /tmp/e2e-frontend.log || true
|
||||
exit 1
|
||||
}
|
||||
|
||||
log "running Playwright full-stack smoke"
|
||||
npx playwright install --with-deps chromium >/dev/null
|
||||
EXPLORER_URL="$EXPLORER_URL" EXPLORER_API_URL="$EXPLORER_API_URL" \
|
||||
npx playwright test scripts/e2e-full-stack.spec.ts --reporter=list
|
||||
|
||||
log "done; screenshots in $E2E_SCREENSHOT_DIR"
|
||||
@@ -5,7 +5,13 @@
|
||||
set -euo pipefail
|
||||
|
||||
RPC_IP="${1:-192.168.11.250}"
|
||||
SSH_PASSWORD="${2:-***REDACTED-LEGACY-PW***}"
|
||||
SSH_PASSWORD="${SSH_PASSWORD:-${2:-}}"
|
||||
if [ -z "${SSH_PASSWORD}" ]; then
|
||||
echo "ERROR: SSH_PASSWORD is required. Pass it as an argument or export SSH_PASSWORD in the environment." >&2
|
||||
echo " Hardcoded default removed for security; see docs/SECURITY.md." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
CONFIG_FILE="${3:-/etc/besu/config-rpc-core.toml}"
|
||||
|
||||
echo "╔══════════════════════════════════════════════════════════════╗"
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
set -euo pipefail
|
||||
|
||||
VMID="${1:-2500}"
|
||||
PASSWORD="${2:-***REDACTED-LEGACY-PW***}"
|
||||
PASSWORD="${NEW_PASSWORD:-${2:-}}"
|
||||
|
||||
if [ -z "${PASSWORD}" ]; then
|
||||
echo "ERROR: NEW_PASSWORD is required. Pass it as an argument or export NEW_PASSWORD in the environment." >&2
|
||||
echo " Hardcoded default removed for security; see docs/SECURITY.md." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if ! command -v pct >/dev/null 2>&1; then
|
||||
echo "Error: pct command not found"
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
set -euo pipefail
|
||||
|
||||
VMID="${1:-2500}"
|
||||
PASSWORD="${2:-***REDACTED-LEGACY-PW***}"
|
||||
PASSWORD="${NEW_PASSWORD:-${2:-}}"
|
||||
|
||||
if [ -z "${PASSWORD}" ]; then
|
||||
echo "ERROR: NEW_PASSWORD is required. Pass it as an argument or export NEW_PASSWORD in the environment." >&2
|
||||
echo " Hardcoded default removed for security; see docs/SECURITY.md." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if ! command -v pct >/dev/null 2>&1; then
|
||||
echo "Error: pct command not found"
|
||||
|
||||
@@ -13,9 +13,15 @@ if [ "$EUID" -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
DB_USER="explorer"
|
||||
DB_PASSWORD="***REDACTED-LEGACY-PW***"
|
||||
DB_NAME="explorer"
|
||||
DB_USER="${DB_USER:-explorer}"
|
||||
DB_NAME="${DB_NAME:-explorer}"
|
||||
if [ -z "${DB_PASSWORD:-}" ]; then
|
||||
echo "ERROR: DB_PASSWORD environment variable must be set before running this script." >&2
|
||||
echo "Generate a strong value (e.g. openssl rand -base64 32) and export it:" >&2
|
||||
echo " export DB_PASSWORD='<strong random password>'" >&2
|
||||
echo " sudo -E bash scripts/setup-database.sh" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Creating database user: $DB_USER"
|
||||
echo "Creating database: $DB_NAME"
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"status": "passed",
|
||||
"failedTests": []
|
||||
}
|
||||
Reference in New Issue
Block a user