-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathroute.ts
More file actions
74 lines (62 loc) · 1.98 KB
/
route.ts
File metadata and controls
74 lines (62 loc) · 1.98 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
import { randomBytes } from 'crypto';
import { NextRequest, NextResponse } from 'next/server';
import { storeNonce } from '@/lib/auth/nonce-store';
import { StrKey } from '@stellar/stellar-sdk';
// Force dynamic rendering to ensure fresh nonces
export const dynamic = 'force-dynamic';
export const runtime = 'nodejs';
/**
* Validates a Stellar address
*/
function isValidStellarAddress(address: string): boolean {
return StrKey.isValidEd25519PublicKey(address);
}
/**
* Extracts address from query params or request body
*/
async function resolveAddressFromRequest(request: NextRequest): Promise<string | null> {
const queryAddress = request.nextUrl.searchParams.get('address')?.trim();
if (queryAddress) return queryAddress;
if (request.method === 'POST') {
try {
const body = await request.clone().json();
const address = (body.publicKey || body.address);
if (typeof address === 'string') return address.trim();
} catch {
// Ignore body parsing errors
}
}
return null;
}
async function handleNonceRequest(request: NextRequest) {
try {
const address = await resolveAddressFromRequest(request);
if (!address || !isValidStellarAddress(address)) {
return NextResponse.json(
{ error: 'Valid Stellar address is required (e.g., ?address=G...)' },
{ status: 400 }
);
}
// Generate a 32-byte random nonce and convert to hex
const nonce = randomBytes(32).toString('hex');
// Store nonce
storeNonce(address, nonce);
return NextResponse.json({
nonce,
address,
expiresAt: new Date(Date.now() + 5 * 60 * 1000).toISOString(),
});
} catch (error) {
console.error('Error generating nonce:', error);
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 }
);
}
}
export async function GET(request: NextRequest) {
return handleNonceRequest(request);
}
export async function POST(request: NextRequest) {
return handleNonceRequest(request);
}