Repository navigation
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
108 lines (89 loc) · 3.92 KB
/
Copy pathsupabase_schema.sql
File metadata and controls
108 lines (89 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
-- PYUSD UPI MVP Database Schema
-- This schema supports wallet management, handle-based payments, and transaction tracking
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Wallets table - stores encrypted private keys and public addresses
CREATE TABLE IF NOT EXISTS wallets (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
public_address TEXT NOT NULL UNIQUE,
encrypted_private_key TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id)
);
-- Create index on public_address for faster lookups
CREATE INDEX IF NOT EXISTS idx_wallets_public_address ON wallets(public_address);
CREATE INDEX IF NOT EXISTS idx_wallets_user_id ON wallets(user_id);
-- Handles table - stores unique @pyusd handles for users
CREATE TABLE IF NOT EXISTS handles (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
handle TEXT NOT NULL UNIQUE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id),
CHECK (handle ~ '^[a-z0-9._-]+@pyusd$')
);
-- Create index on handle for faster lookups
CREATE INDEX IF NOT EXISTS idx_handles_handle ON handles(handle);
CREATE INDEX IF NOT EXISTS idx_handles_user_id ON handles(user_id);
-- Transactions table - stores payment history
CREATE TABLE IF NOT EXISTS transactions (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
from_user_id UUID NOT NULL REFERENCES auth.users(id),
to_user_id UUID NOT NULL REFERENCES auth.users(id),
from_handle TEXT NOT NULL,
to_handle TEXT NOT NULL,
amount TEXT NOT NULL,
tx_hash TEXT NOT NULL UNIQUE,
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'failed')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create indexes for transaction queries
CREATE INDEX IF NOT EXISTS idx_transactions_from_user ON transactions(from_user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_transactions_to_user ON transactions(to_user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_transactions_tx_hash ON transactions(tx_hash);
-- Row Level Security Policies
-- Enable RLS on all tables
ALTER TABLE wallets ENABLE ROW LEVEL SECURITY;
ALTER TABLE handles ENABLE ROW LEVEL SECURITY;
ALTER TABLE transactions ENABLE ROW LEVEL SECURITY;
-- Wallets policies: users can only read their own wallet
CREATE POLICY "Users can view their own wallet"
ON wallets FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own wallet"
ON wallets FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- Handles policies: users can read all handles (for lookups), but only insert/update their own
CREATE POLICY "Anyone can view handles"
ON handles FOR SELECT
TO authenticated
USING (true);
CREATE POLICY "Users can insert their own handle"
ON handles FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- Transactions policies: users can view transactions they're involved in
CREATE POLICY "Users can view their own transactions"
ON transactions FOR SELECT
USING (auth.uid() = from_user_id OR auth.uid() = to_user_id);
CREATE POLICY "Users can insert their own transactions"
ON transactions FOR INSERT
WITH CHECK (auth.uid() = from_user_id);
CREATE POLICY "Users can update their own transactions"
ON transactions FOR UPDATE
USING (auth.uid() = from_user_id);
-- Function to automatically update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Triggers for updated_at
CREATE TRIGGER update_wallets_updated_at BEFORE UPDATE ON wallets
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_transactions_updated_at BEFORE UPDATE ON transactions
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();