walletpress/backend/alembic/versions/001_initial_schema.py
Rug Munch Media LLC 85d8ef5eac
refactor(routers): split chain_vault.py god router — WP-085..WP-087
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
2026-06-30 21:40:45 +07:00

68 lines
2.2 KiB
Python

"""Initial vault schema — wallets, rotations, FTS, version tracking.
Revises: None (first migration)
Create Date: 2026-06-30
"""
from typing import Sequence, Union
from alembic import op
revision: str = "001_initial_schema"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.execute("""
CREATE TABLE IF NOT EXISTS wallets (
id TEXT PRIMARY KEY,
chain TEXT NOT NULL,
address TEXT NOT NULL,
label TEXT DEFAULT '',
tags TEXT DEFAULT '[]',
wallet_group TEXT DEFAULT '',
created_at REAL NOT NULL,
encrypted_key TEXT DEFAULT '',
public_key TEXT DEFAULT '',
derivation_path TEXT DEFAULT '',
hd_path TEXT DEFAULT '',
mnemonic_encrypted TEXT DEFAULT '',
encrypted INTEGER DEFAULT 0,
balance_usd REAL DEFAULT 0.0,
notes TEXT DEFAULT '',
updated_at REAL DEFAULT 0
)
""")
op.execute("CREATE INDEX IF NOT EXISTS idx_wallets_chain ON wallets(chain)")
op.execute("CREATE INDEX IF NOT EXISTS idx_wallets_address ON wallets(address)")
op.execute("""
CREATE VIRTUAL TABLE IF NOT EXISTS wallets_fts USING fts5(
id, chain, address, label, notes,
content='wallets',
content_rowid='rowid'
)
""")
op.execute("""
CREATE TABLE IF NOT EXISTS rotations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
wallet_id TEXT NOT NULL,
new_address TEXT NOT NULL,
rotated_at REAL NOT NULL,
reason TEXT DEFAULT '',
FOREIGN KEY(wallet_id) REFERENCES wallets(id)
)
""")
op.execute("""
CREATE TABLE IF NOT EXISTS _schema_version (
version INTEGER PRIMARY KEY,
applied_at REAL NOT NULL
)
""")
def downgrade() -> None:
op.execute("DROP TABLE IF EXISTS rotations")
op.execute("DROP TABLE IF EXISTS wallets_fts")
op.execute("DROP TABLE IF EXISTS wallets")
op.execute("DROP TABLE IF EXISTS _schema_version")