|
| 1 | +/** |
| 2 | + * POST /api/escrow/multisig — Create a new multisig escrow |
| 3 | + * GET /api/escrow/multisig — Get a multisig escrow by ID (query param) |
| 4 | + * |
| 5 | + * Non-custodial 2-of-3 multisig escrow. |
| 6 | + * CoinPay is a dispute mediator and co-signer — never a custodian. |
| 7 | + */ |
| 8 | + |
| 9 | +import { NextRequest, NextResponse } from 'next/server'; |
| 10 | +import { createClient } from '@supabase/supabase-js'; |
| 11 | +import { authenticateRequest } from '@/lib/auth/middleware'; |
| 12 | +import { |
| 13 | + createMultisigEscrow, |
| 14 | + getMultisigEscrow, |
| 15 | + createMultisigEscrowSchema, |
| 16 | + isMultisigEnabled, |
| 17 | +} from '@/lib/multisig'; |
| 18 | + |
| 19 | +function getSupabase() { |
| 20 | + const url = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 21 | + const key = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 22 | + if (!url || !key) throw new Error('Supabase not configured'); |
| 23 | + return createClient(url, key); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * POST /api/escrow/multisig |
| 28 | + * Create a new 2-of-3 multisig escrow |
| 29 | + */ |
| 30 | +export async function POST(request: NextRequest) { |
| 31 | + try { |
| 32 | + if (!isMultisigEnabled()) { |
| 33 | + return NextResponse.json( |
| 34 | + { error: 'Multisig escrow is not enabled' }, |
| 35 | + { status: 503 }, |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + const supabase = getSupabase(); |
| 40 | + |
| 41 | + // Authentication required |
| 42 | + const authHeader = request.headers.get('authorization'); |
| 43 | + const apiKeyHeader = request.headers.get('x-api-key'); |
| 44 | + |
| 45 | + if (!authHeader && !apiKeyHeader) { |
| 46 | + return NextResponse.json( |
| 47 | + { error: 'Authentication required. Provide Authorization header or X-API-Key.' }, |
| 48 | + { status: 401 }, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const authResult = await authenticateRequest(supabase, authHeader || apiKeyHeader); |
| 54 | + if (!authResult.success) { |
| 55 | + return NextResponse.json( |
| 56 | + { error: 'Invalid or expired authentication' }, |
| 57 | + { status: 401 }, |
| 58 | + ); |
| 59 | + } |
| 60 | + } catch { |
| 61 | + return NextResponse.json( |
| 62 | + { error: 'Authentication failed' }, |
| 63 | + { status: 401 }, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + const body = await request.json(); |
| 68 | + |
| 69 | + // Validate input |
| 70 | + const parsed = createMultisigEscrowSchema.safeParse(body); |
| 71 | + if (!parsed.success) { |
| 72 | + return NextResponse.json( |
| 73 | + { error: parsed.error.errors[0].message }, |
| 74 | + { status: 400 }, |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + const result = await createMultisigEscrow(supabase, parsed.data); |
| 79 | + |
| 80 | + if (!result.success) { |
| 81 | + return NextResponse.json({ error: result.error }, { status: 400 }); |
| 82 | + } |
| 83 | + |
| 84 | + return NextResponse.json(result.escrow, { status: 201 }); |
| 85 | + } catch (error) { |
| 86 | + console.error('Failed to create multisig escrow:', error); |
| 87 | + return NextResponse.json( |
| 88 | + { error: 'Internal server error' }, |
| 89 | + { status: 500 }, |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * GET /api/escrow/multisig?id=<escrow_id> |
| 96 | + * Get a multisig escrow by ID |
| 97 | + */ |
| 98 | +export async function GET(request: NextRequest) { |
| 99 | + try { |
| 100 | + const supabase = getSupabase(); |
| 101 | + const { searchParams } = new URL(request.url); |
| 102 | + const escrowId = searchParams.get('id'); |
| 103 | + |
| 104 | + if (!escrowId) { |
| 105 | + return NextResponse.json( |
| 106 | + { error: 'id query parameter is required' }, |
| 107 | + { status: 400 }, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + const result = await getMultisigEscrow(supabase, escrowId); |
| 112 | + |
| 113 | + if (!result.success) { |
| 114 | + return NextResponse.json({ error: result.error }, { status: 404 }); |
| 115 | + } |
| 116 | + |
| 117 | + return NextResponse.json(result.escrow); |
| 118 | + } catch (error) { |
| 119 | + console.error('Failed to get multisig escrow:', error); |
| 120 | + return NextResponse.json( |
| 121 | + { error: 'Internal server error' }, |
| 122 | + { status: 500 }, |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
0 commit comments