mirror of
https://github.com/pezkuwichain/pezkuwi-telegram-miniapp.git
synced 2026-04-22 03:07:55 +00:00
31e768de45
- Add 8 Supabase edge functions for P2P operations (get-internal-balance, get-payment-methods, get-p2p-offers, accept-p2p-offer, get-p2p-trades, trade-action, p2p-messages, p2p-dispute) - Add frontend P2P API layer (src/lib/p2p-api.ts) - Add 8 P2P components (BalanceCard, OfferList, TradeModal, CreateOfferModal, TradeView, TradeChat, DisputeModal, P2P section) - Embed P2P as internal section in App.tsx instead of external link - Remove old P2PModal component - Add ~70 P2P translation keys across all 6 languages
213 lines
6.5 KiB
TypeScript
213 lines
6.5 KiB
TypeScript
import { serve } from 'https://deno.land/std@0.177.0/http/server.ts';
|
|
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2';
|
|
import { createHmac } from 'https://deno.land/std@0.177.0/node/crypto.ts';
|
|
|
|
// CORS - Production domain only
|
|
const ALLOWED_ORIGINS = [
|
|
'https://telegram.pezkuwichain.io',
|
|
'https://telegram.pezkiwi.app',
|
|
'https://t.me',
|
|
];
|
|
|
|
function getCorsHeaders(origin: string | null): Record<string, string> {
|
|
const allowedOrigin =
|
|
origin && ALLOWED_ORIGINS.some((o) => origin.startsWith(o)) ? origin : ALLOWED_ORIGINS[0];
|
|
|
|
return {
|
|
'Access-Control-Allow-Origin': allowedOrigin,
|
|
'Access-Control-Allow-Headers':
|
|
'authorization, x-client-info, apikey, content-type, x-supabase-client-platform',
|
|
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
|
};
|
|
}
|
|
|
|
interface GetP2PTradesRequest {
|
|
sessionToken: string;
|
|
status?: 'active' | 'completed' | 'all';
|
|
}
|
|
|
|
// Session token secret (derived from bot token)
|
|
function getSessionSecret(botToken: string): Uint8Array {
|
|
return createHmac('sha256', 'SessionTokenSecret').update(botToken).digest();
|
|
}
|
|
|
|
// Verify HMAC-signed session token
|
|
function verifySessionToken(token: string, botToken: string): number | null {
|
|
try {
|
|
const parts = token.split('.');
|
|
if (parts.length !== 2) {
|
|
// Try legacy format for backwards compatibility
|
|
return verifyLegacyToken(token);
|
|
}
|
|
|
|
const [payloadB64, signature] = parts;
|
|
|
|
// Verify signature
|
|
const secret = getSessionSecret(botToken);
|
|
const expectedSig = createHmac('sha256', secret).update(payloadB64).digest('hex');
|
|
|
|
if (signature !== expectedSig) {
|
|
return null;
|
|
}
|
|
|
|
// Parse payload
|
|
const payload = JSON.parse(atob(payloadB64));
|
|
|
|
// Check expiration
|
|
if (Date.now() > payload.exp) {
|
|
return null;
|
|
}
|
|
|
|
return payload.tgId;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Legacy token format (Base64 only) - for backwards compatibility
|
|
function verifyLegacyToken(token: string): number | null {
|
|
try {
|
|
const decoded = atob(token);
|
|
const [telegramId, timestamp] = decoded.split(':');
|
|
const ts = parseInt(timestamp);
|
|
// Token valid for 7 days
|
|
if (Date.now() - ts > 7 * 24 * 60 * 60 * 1000) {
|
|
return null;
|
|
}
|
|
return parseInt(telegramId);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
serve(async (req) => {
|
|
const origin = req.headers.get('origin');
|
|
const corsHeaders = getCorsHeaders(origin);
|
|
|
|
// Handle CORS
|
|
if (req.method === 'OPTIONS') {
|
|
return new Response('ok', { headers: corsHeaders });
|
|
}
|
|
|
|
try {
|
|
const body: GetP2PTradesRequest = await req.json();
|
|
const { sessionToken, status } = body;
|
|
|
|
// Get bot token for session verification
|
|
const botToken = Deno.env.get('TELEGRAM_BOT_TOKEN');
|
|
if (!botToken) {
|
|
return new Response(JSON.stringify({ error: 'Server configuration error' }), {
|
|
status: 500,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
// Validate session token
|
|
if (!sessionToken) {
|
|
return new Response(JSON.stringify({ error: 'Missing session token' }), {
|
|
status: 401,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
const telegramId = verifySessionToken(sessionToken, botToken);
|
|
if (!telegramId) {
|
|
return new Response(JSON.stringify({ error: 'Invalid or expired session' }), {
|
|
status: 401,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
// Create Supabase admin client (bypasses RLS)
|
|
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
|
|
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
|
|
const supabase = createClient(supabaseUrl, supabaseServiceKey);
|
|
|
|
// Get auth user ID for this telegram user
|
|
const telegramEmail = `telegram_${telegramId}@pezkuwichain.io`;
|
|
const {
|
|
data: { users: authUsers },
|
|
} = await supabase.auth.admin.listUsers();
|
|
const authUser = authUsers?.find((u) => u.email === telegramEmail);
|
|
|
|
if (!authUser) {
|
|
return new Response(JSON.stringify({ error: 'User not found. Please authenticate first.' }), {
|
|
status: 404,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
const userId = authUser.id;
|
|
|
|
// Build query: trades where user is seller OR buyer
|
|
let query = supabase
|
|
.from('p2p_fiat_trades')
|
|
.select('*')
|
|
.or(`seller_id.eq.${userId},buyer_id.eq.${userId}`)
|
|
.order('created_at', { ascending: false });
|
|
|
|
// Apply status filter
|
|
if (status === 'active') {
|
|
query = query.in('status', ['pending', 'payment_sent', 'disputed']);
|
|
} else if (status === 'completed') {
|
|
query = query.in('status', ['completed', 'cancelled', 'refunded']);
|
|
}
|
|
// 'all' or not provided: no additional filter
|
|
|
|
const { data: trades, error: tradesError } = await query;
|
|
|
|
if (tradesError) {
|
|
console.error('Fetch trades error:', tradesError);
|
|
return new Response(
|
|
JSON.stringify({ error: 'Failed to fetch trades: ' + tradesError.message }),
|
|
{
|
|
status: 500,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
}
|
|
);
|
|
}
|
|
|
|
// Fetch offer details for each trade
|
|
const offerIds = [...new Set((trades || []).map((t) => t.offer_id).filter(Boolean))];
|
|
|
|
let offersMap: Record<string, { token: string; fiat_currency: string; ad_type: string }> = {};
|
|
|
|
if (offerIds.length > 0) {
|
|
const { data: offers, error: offersError } = await supabase
|
|
.from('p2p_fiat_offers')
|
|
.select('id, token, fiat_currency, ad_type')
|
|
.in('id', offerIds);
|
|
|
|
if (!offersError && offers) {
|
|
offersMap = Object.fromEntries(
|
|
offers.map((o) => [o.id, { token: o.token, fiat_currency: o.fiat_currency, ad_type: o.ad_type }])
|
|
);
|
|
}
|
|
}
|
|
|
|
// Enrich trades with offer details
|
|
const enrichedTrades = (trades || []).map((trade) => ({
|
|
...trade,
|
|
offer: trade.offer_id ? offersMap[trade.offer_id] || null : null,
|
|
}));
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
trades: enrichedTrades,
|
|
}),
|
|
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
);
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
const origin = req.headers.get('origin');
|
|
return new Response(
|
|
JSON.stringify({ error: error instanceof Error ? error.message : 'Internal server error' }),
|
|
{
|
|
status: 500,
|
|
headers: { ...getCorsHeaders(origin), 'Content-Type': 'application/json' },
|
|
}
|
|
);
|
|
}
|
|
});
|