Files
smirk-backend-core/migrations/20260622000001_initial_schema.sql
T
jwinterm 586f681287 feat: Grin slatepack relay (non-custodial async mailbox, feature-flagged)
Final wallet-handler slice. An async store-and-forward mailbox for interactive
Grin transfers between two registered Smirk users, over the grin_slatepacks db.
Reworked clean from the legacy relay — NOT a port.

Endpoints (under /api/v1/wallet/grin/relay, JWT-gated):
- POST create   {recipient_user_id, slate_id, slatepack, amount_nanogrin}
- GET  pending  -> the caller's inbox
- POST get      {slate_id} -> poll a relay's state (sender/recipient)
- POST respond  {slate_id, response_slatepack} (recipient)
- POST finalize {slate_id, tx_hash} (sender; RECORDS the txid only)
- POST cancel   {slate_id} (sender or recipient)

Non-custodial: stores only opaque (encrypted) slatepacks + lifecycle status;
never holds keys, finalizes, or broadcasts. The wallet broadcasts locally (via
/wallet/grin/broadcast) and reports the txid here — diverging from the legacy
server-side post_tx (see docs/private/OWN_BACKEND_CONVERGENCE.md).

Controls + hardening (incl. fixes from the adversarial review workflow):
- Per-action authz: caller must be the slate's sender/recipient; a non-party
  gets 404 (no existence oracle).
- slate_id is UNIQUE (+ validated as a UUID) and mutations key on the
  authorized row's PRIMARY KEY, not slate_id — so a duplicate slate_id can't
  brick a relay (fetch_optional-on-2-rows 500) or let a slate_id-keyed UPDATE
  touch another row. create maps the unique violation to 409.
- State transitions are ATOMIC: the status + expiry preconditions live in the
  UPDATE WHERE clause (not a Rust read-check-write), so respond-twice and
  cancel-races-finalize cannot occur; a non-qualifying transition -> 409.
- TTL enforced at the action boundary (respond/finalize require expires_at >
  now) + a periodic expire_old_slatepacks sweep in main.rs (relay-enabled only).
- Recipient must be a registered user; slatepack sizes capped (64 KiB).
- Behind FEATURE_GRIN_RELAY (default on).

6 paths + schemas + grin_relay tag in openapi (drift-clean). 4 L1 integration
tests against live Postgres: full lifecycle, unregistered-recipient 400, auth
gate, non-party/non-recipient 404s.
2026-06-23 17:39:13 -04:00

124 lines
6.5 KiB
SQL

-- Initial schema for smirk-backend-core.
--
-- Nostr-native identity: a user is keyed by pubkey_hash (peppered), an optional
-- nostr_pubkey, and an optional reserved username. No platform-login columns.
-- pubkey_hash and seed_fingerprint are stored HMAC-peppered (the pepper lives in
-- config); UNIQUE constraints are on the peppered values. FK ON DELETE clauses
-- support self-service erasure. Requires PostgreSQL 13+ (gen_random_uuid).
-- ── Enum types ──────────────────────────────────────────────────────────────
CREATE TYPE asset_type AS ENUM ('btc', 'ltc', 'xmr', 'wow', 'grin');
CREATE TYPE slatepack_status AS ENUM (
'pending_recipient', 'pending_sender', 'finalized', 'expired', 'cancelled'
);
CREATE TYPE audit_action AS ENUM (
'user_created', 'user_login', 'wallet_created', 'wallet_registered',
'tx_broadcast', 'session_created', 'session_revoked'
);
-- ── users ───────────────────────────────────────────────────────────────────
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username TEXT UNIQUE,
pubkey_hash TEXT UNIQUE, -- peppered at rest
nostr_pubkey TEXT UNIQUE,
wallet_birthday TIMESTAMPTZ,
seed_fingerprint TEXT UNIQUE, -- peppered at rest
xmr_start_height BIGINT,
wow_start_height BIGINT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
last_seen_at TIMESTAMPTZ
);
-- UNIQUE constraints already index username/pubkey_hash/nostr_pubkey/seed_fingerprint.
-- ── wallets ─────────────────────────────────────────────────────────────────
CREATE TABLE wallets (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
asset asset_type NOT NULL,
address TEXT NOT NULL,
view_key TEXT,
derivation_index INTEGER,
registered_with_node BOOLEAN NOT NULL DEFAULT FALSE,
registration_error TEXT,
label TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_wallets_user_id ON wallets (user_id);
CREATE INDEX idx_wallets_address ON wallets (address);
-- ── sessions ────────────────────────────────────────────────────────────────
CREATE TABLE sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
refresh_token_hash TEXT NOT NULL, -- peppered at rest
platform TEXT NOT NULL, -- extension | web | nostr
device_info TEXT,
ip_address INET,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ,
last_used_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_sessions_user_id ON sessions (user_id);
CREATE INDEX idx_sessions_refresh_token_hash ON sessions (refresh_token_hash);
-- ── user_keys ───────────────────────────────────────────────────────────────
CREATE TABLE user_keys (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
asset asset_type NOT NULL,
public_key TEXT NOT NULL,
public_spend_key TEXT,
key_type TEXT NOT NULL DEFAULT 'primary',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
UNIQUE (user_id, asset, key_type)
);
CREATE INDEX idx_user_keys_user_id ON user_keys (user_id);
-- ── grin_slatepacks ─────────────────────────────────────────────────────────
CREATE TABLE grin_slatepacks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
-- UNIQUE: the relay's authorization + state machine key off slate_id, so it
-- must map to exactly one row (a collision would let mutations keyed on
-- slate_id touch another user's relay). Also serves as the lookup index.
slate_id TEXT NOT NULL UNIQUE,
sender_user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
recipient_user_id UUID REFERENCES users(id) ON DELETE SET NULL,
recipient_address TEXT,
slatepack_content TEXT NOT NULL,
amount_nanogrin BIGINT NOT NULL,
status slatepack_status NOT NULL DEFAULT 'pending_recipient',
response_slatepack TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
expires_at TIMESTAMPTZ NOT NULL,
finalized_at TIMESTAMPTZ,
tx_hash TEXT
);
-- (slate_id lookup index is provided by the UNIQUE constraint above.)
CREATE INDEX idx_grin_slatepacks_sender ON grin_slatepacks (sender_user_id);
CREATE INDEX idx_grin_slatepacks_recipient ON grin_slatepacks (recipient_user_id);
CREATE INDEX idx_grin_slatepacks_status ON grin_slatepacks (status);
-- ── audit_logs ──────────────────────────────────────────────────────────────
CREATE TABLE audit_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES users(id) ON DELETE SET NULL,
action audit_action NOT NULL,
resource_type TEXT,
resource_id UUID,
details JSONB,
ip_address INET,
user_agent TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_audit_logs_user_id ON audit_logs (user_id);
CREATE INDEX idx_audit_logs_action ON audit_logs (action);
CREATE INDEX idx_audit_logs_created_at ON audit_logs (created_at);