|
| 1 | +-- #689 Stellar DID Layer, Cross-Campaign Reputation Score & Privacy-Preserving KYC Attestations |
| 2 | +-- |
| 3 | +-- New tables: |
| 4 | +-- contributor_identities — links a user to their on-chain DID |
| 5 | +-- kyc_attestations — privacy-preserving off-chain KYC records (hash only, never raw docs) |
| 6 | +-- campaign_requirements — per-campaign contribution gate (min reputation + required attestations) |
| 7 | +-- reputation_events — append-only audit log of every reputation delta |
| 8 | + |
| 9 | +-- --------------------------------------------------------------------------- |
| 10 | +-- contributor_identities |
| 11 | +-- --------------------------------------------------------------------------- |
| 12 | +CREATE TABLE IF NOT EXISTS contributor_identities ( |
| 13 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 14 | + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, |
| 15 | + public_key TEXT NOT NULL, |
| 16 | + did TEXT NOT NULL, |
| 17 | + contract_registered_at TIMESTAMPTZ, |
| 18 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 19 | + CONSTRAINT contributor_identities_public_key_unique UNIQUE (public_key), |
| 20 | + CONSTRAINT contributor_identities_did_unique UNIQUE (did), |
| 21 | + CONSTRAINT contributor_identities_user_id_unique UNIQUE (user_id) |
| 22 | +); |
| 23 | + |
| 24 | +CREATE INDEX IF NOT EXISTS contributor_identities_user_id_idx |
| 25 | + ON contributor_identities (user_id); |
| 26 | + |
| 27 | +-- --------------------------------------------------------------------------- |
| 28 | +-- kyc_attestations |
| 29 | +-- Stores the off-chain record of a KYC-level attestation. |
| 30 | +-- proof_hash is the SHA-256 of the Persona inquiry ID — never the document. |
| 31 | +-- --------------------------------------------------------------------------- |
| 32 | +DO $$ BEGIN |
| 33 | + CREATE TYPE kyc_attestation_type AS ENUM ('kyc_basic', 'kyc_standard', 'kyc_enhanced'); |
| 34 | +EXCEPTION WHEN duplicate_object THEN NULL; |
| 35 | +END $$; |
| 36 | + |
| 37 | +CREATE TABLE IF NOT EXISTS kyc_attestations ( |
| 38 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 39 | + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, |
| 40 | + public_key TEXT NOT NULL, |
| 41 | + attestation_type kyc_attestation_type NOT NULL, |
| 42 | + kyc_level TEXT NOT NULL, |
| 43 | + persona_inquiry_id TEXT, |
| 44 | + proof_hash TEXT NOT NULL, |
| 45 | + issued_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 46 | + expires_at TIMESTAMPTZ, |
| 47 | + revoked_at TIMESTAMPTZ, |
| 48 | + on_chain_tx_hash TEXT, |
| 49 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() |
| 50 | +); |
| 51 | + |
| 52 | +CREATE INDEX IF NOT EXISTS kyc_attestations_user_id_idx |
| 53 | + ON kyc_attestations (user_id); |
| 54 | +CREATE INDEX IF NOT EXISTS kyc_attestations_public_key_idx |
| 55 | + ON kyc_attestations (public_key); |
| 56 | +CREATE INDEX IF NOT EXISTS kyc_attestations_active_idx |
| 57 | + ON kyc_attestations (user_id, attestation_type) |
| 58 | + WHERE revoked_at IS NULL; |
| 59 | + |
| 60 | +-- --------------------------------------------------------------------------- |
| 61 | +-- campaign_requirements |
| 62 | +-- One row per campaign; upsert on conflict to allow updates by the creator. |
| 63 | +-- required_attestations is a JSONB array of attestation type strings, |
| 64 | +-- e.g. ["kyc_basic", "kyc_standard"]. |
| 65 | +-- --------------------------------------------------------------------------- |
| 66 | +CREATE TABLE IF NOT EXISTS campaign_requirements ( |
| 67 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 68 | + campaign_id UUID NOT NULL REFERENCES campaigns(id) ON DELETE CASCADE, |
| 69 | + min_reputation_score INTEGER NOT NULL DEFAULT 0 |
| 70 | + CHECK (min_reputation_score >= 0 AND min_reputation_score <= 1000), |
| 71 | + required_attestations JSONB NOT NULL DEFAULT '[]'::jsonb, |
| 72 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 73 | + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 74 | + CONSTRAINT campaign_requirements_campaign_id_unique UNIQUE (campaign_id) |
| 75 | +); |
| 76 | + |
| 77 | +CREATE INDEX IF NOT EXISTS campaign_requirements_campaign_id_idx |
| 78 | + ON campaign_requirements (campaign_id); |
| 79 | + |
| 80 | +-- --------------------------------------------------------------------------- |
| 81 | +-- reputation_events |
| 82 | +-- Immutable audit log — rows are never updated or deleted. |
| 83 | +-- --------------------------------------------------------------------------- |
| 84 | +DO $$ BEGIN |
| 85 | + CREATE TYPE reputation_event_type AS ENUM ( |
| 86 | + 'contribution_made', |
| 87 | + 'contribution_to_successful_campaign', |
| 88 | + 'contribution_to_failed_campaign', |
| 89 | + 'dispute_raised_against_contributor' |
| 90 | + ); |
| 91 | +EXCEPTION WHEN duplicate_object THEN NULL; |
| 92 | +END $$; |
| 93 | + |
| 94 | +CREATE TABLE IF NOT EXISTS reputation_events ( |
| 95 | + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 96 | + public_key TEXT NOT NULL, |
| 97 | + event_type reputation_event_type NOT NULL, |
| 98 | + delta INTEGER NOT NULL DEFAULT 0, |
| 99 | + resulting_score INTEGER NOT NULL DEFAULT 0 |
| 100 | + CHECK (resulting_score >= 0 AND resulting_score <= 1000), |
| 101 | + related_campaign_id UUID REFERENCES campaigns(id) ON DELETE SET NULL, |
| 102 | + on_chain_tx_hash TEXT, |
| 103 | + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() |
| 104 | +); |
| 105 | + |
| 106 | +CREATE INDEX IF NOT EXISTS reputation_events_public_key_idx |
| 107 | + ON reputation_events (public_key); |
| 108 | +CREATE INDEX IF NOT EXISTS reputation_events_public_key_created_idx |
| 109 | + ON reputation_events (public_key, created_at DESC); |
| 110 | +CREATE INDEX IF NOT EXISTS reputation_events_campaign_idx |
| 111 | + ON reputation_events (related_campaign_id) |
| 112 | + WHERE related_campaign_id IS NOT NULL; |
0 commit comments