-- ── API tokens ──────────────────────────────────────────────────────────────── -- Conformance: "API tokens and NIP-98 replay". token_hash uniqueness scoped to -- (community_id, token_hash); channel claims reference channels in same community. CREATE TABLE api_tokens ( community_id UUID NOT NULL REFERENCES communities(id), id UUID NOT NULL DEFAULT gen_random_uuid(), token_hash BYTEA NOT NULL, owner_pubkey BYTEA NOT NULL, name VARCHAR(255) NOT NULL, scopes JSONB NOT NULL, channel_ids JSONB, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), expires_at TIMESTAMPTZ, last_used_at TIMESTAMPTZ, revoked_at TIMESTAMPTZ, revoked_by BYTEA, created_by_self_mint BOOLEAN NOT NULL DEFAULT FALSE, PRIMARY KEY (community_id, id), FOREIGN KEY (community_id, owner_pubkey) REFERENCES users (community_id, pubkey), CONSTRAINT chk_api_tokens_hash_len CHECK (LENGTH(token_hash) = 32) ); CREATE UNIQUE INDEX idx_api_tokens_hash ON api_tokens (community_id, token_hash);