import { NextRequest, NextResponse } from 'next/server' import { verifySignatureInTweet } from '../../../utils/verifyTweet'; export async function POST(req: NextRequest): Promise { try { const { tweetUrl } = await req.json(); const url = `https://publish.twitter.com/oembed?url=${tweetUrl}&maxwidth=600`; const response = await fetch(url); const data = await response.json(); const { handle, memeUrl, txHash } = extractData(data.html); if (!handle || !memeUrl || !txHash) { return NextResponse.json( { error: 'Verification failed' }, { status: 500 } ) } const isVerified = await verifySignatureInTweet(txHash); const isHandleCorrect = handle === process.env.ACCOUNT_HANDLE; return NextResponse.json({isVerified: isVerified && isHandleCorrect}) } catch (error) { console.error('Error while verifying tweet:', error) return NextResponse.json( { error: 'Failed to verify tweet' }, { status: 500 } ) } } const extractData = (tweet: string | object) => { const tweetText = typeof tweet === 'string' ? tweet : JSON.stringify(tweet); const decodedTweet = tweetText.replace(/'/g, "'").replace(/"/g, '"'); const urlMatch = decodedTweet.match(//); const txHashMatch = decodedTweet.match(/TX Hash: '([^']+)'/); const handleMatch = decodedTweet.match(/@([A-Za-z0-9_]+)/); return { memeUrl: urlMatch ? urlMatch[1] : null, txHash: txHashMatch ? txHashMatch[1].trim() : null, handle: handleMatch ? handleMatch[1] : null, }; };