Extracted admin endpoints from chain_vault.py (2,178 lines) into
wallet_admin.py (768 lines). chain_vault.py is now 1,469 lines.
What moved to wallet_admin.py (29 routes):
- API keys: /api-keys, /api-keys/revoke
- Alerts: /alerts, /alerts/delete
- Webhooks: /webhooks, /webhooks/{id}, /webhooks/{id}/test,
/webhooks/{id}/retry, /webhooks/deliveries
- Audit trail: /audit-trail
- Bulk ops: /bulk/filter, /bulk/delete, /bulk/export, /export
- 2FA: /admin/2fa/{setup,verify-setup,disable,status}
- Config: /config
- Proof of Generation: /proof/{commit,provenance,verify,roots,stats}
What stayed in chain_vault.py (32 routes):
- Chain metadata: /chains, /stats, /healthz, /health-score
- RPC config: /rpc-chains
- Wallet gen: /generate, /generate/batch, /generate/all,
/import, /from-mnemonic, /derive-address, /hd-wallet
- Vault CRUD: /vault, /vault/{id}, /vault/{id}/full, DELETE
- Wallet ops: /tree, /cluster, /rotate, /rotate-sweep, /rotations,
/distribute, /sweep, /escrow, /escrow/release, /paper-wallet, /tx
- Validation: /validate/{chain}/{address}, /validate/all
- Balances: /balances, /balances/snapshot
- PDF: /paper-wallet/{id}/pdf, /wallet/{id}/birth-certificate
Helpers extracted to _persistent_store.py:
- _PersistentStore class (webhooks/alerts/payments SQLite store)
P3-7 fix: removed dead _webhooks global references in test/retry
endpoints — now uses _PersistentStore.all('webhooks')
Added to wallet_admin.py:
- _require_totp() helper (also kept in chain_vault.py for the
/vault/{id}/full endpoint that needs it)
- WebhookCreateRequest, BulkFilterRequest, BulkDeleteRequest models
(these were inlined in original chain_vault.py body — now in
the request schemas section)
P3-10 — WP plugin supported_chains() rewritten
Plugin used to advertise chains the backend doesn't support
('bitcoin' vs backend 'btc', 'ethereum' vs 'eth', etc.). Rewrote
to use correct backend keys + added a 'backend' field for clarity.
Now matches ADDRESS_GENERATION.md truth table.
main.py: updated import + include_router for wallet_admin.router.
Test results: 80 passed, 5 skipped (no regressions).
Refs: AUDIT.md P2-16, P3-7, P3-10, P3-17
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""Standardized API response shapes.
|
|
|
|
Every response follows::
|
|
|
|
Success: {"data": ..., "meta": {"request_id": "..."}}
|
|
Error: {"error": {"code": "...", "message": "...", "details": ...}}
|
|
|
|
This module provides helpers that all routers should use instead of
|
|
returning raw dicts. Migration is incremental — old endpoints that
|
|
return bare dicts still work via FastAPI's auto-serialisation.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
import secrets
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
|
|
def success(data, status_code: int = 200, meta: dict | None = None) -> JSONResponse:
|
|
body: dict = {"data": data}
|
|
if meta:
|
|
body["meta"] = {**meta, "timestamp": time.time()}
|
|
else:
|
|
body["meta"] = {"timestamp": time.time()}
|
|
return JSONResponse(content=body, status_code=status_code)
|
|
|
|
|
|
def error(code: str, message: str, status_code: int = 400, details: dict | None = None, request_id: str | None = None) -> JSONResponse:
|
|
body: dict = {
|
|
"error": {
|
|
"code": code,
|
|
"message": message,
|
|
}
|
|
}
|
|
if details:
|
|
body["error"]["details"] = details
|
|
body["meta"] = {"request_id": request_id or f"req_{secrets.token_hex(8)}", "timestamp": time.time()}
|
|
return JSONResponse(content=body, status_code=status_code)
|
|
|
|
|
|
def created(data) -> JSONResponse:
|
|
return success(data, status_code=201)
|
|
|
|
|
|
def no_content() -> JSONResponse:
|
|
return JSONResponse(content=None, status_code=204)
|
|
|
|
|
|
def bad_request(message: str, details: dict | None = None) -> JSONResponse:
|
|
return error("bad_request", message, 400, details)
|
|
|
|
|
|
def unauthorized(message: str = "Authentication required") -> JSONResponse:
|
|
return error("unauthorized", message, 401)
|
|
|
|
|
|
def forbidden(message: str = "Access denied") -> JSONResponse:
|
|
return error("forbidden", message, 403)
|
|
|
|
|
|
def not_found(message: str = "Resource not found") -> JSONResponse:
|
|
return error("not_found", message, 404)
|
|
|
|
|
|
def conflict(message: str, details: dict | None = None) -> JSONResponse:
|
|
return error("conflict", message, 409, details)
|
|
|
|
|
|
def too_many_requests(message: str = "Rate limit exceeded") -> JSONResponse:
|
|
return error("too_many_requests", message, 429)
|
|
|
|
|
|
def server_error(message: str = "Internal server error") -> JSONResponse:
|
|
return error("server_error", message, 500)
|