|
| 1 | +import { |
| 2 | + setUserNotificationDetails, |
| 3 | + deleteUserNotificationDetails, |
| 4 | +} from "@/lib/notification"; |
| 5 | +import { sendFrameNotification } from "@/lib/notification-client"; |
| 6 | +import { http } from "viem"; |
| 7 | +import { createPublicClient } from "viem"; |
| 8 | +import { optimism } from "viem/chains"; |
| 9 | + |
| 10 | +const appName = process.env.NEXT_PUBLIC_ONCHAINKIT_PROJECT_NAME; |
| 11 | + |
| 12 | +const KEY_REGISTRY_ADDRESS = "0x00000000Fc1237824fb747aBDE0FF18990E59b7e"; |
| 13 | + |
| 14 | +const KEY_REGISTRY_ABI = [ |
| 15 | + { |
| 16 | + inputs: [ |
| 17 | + { name: "fid", type: "uint256" }, |
| 18 | + { name: "key", type: "bytes" }, |
| 19 | + ], |
| 20 | + name: "keyDataOf", |
| 21 | + outputs: [ |
| 22 | + { |
| 23 | + components: [ |
| 24 | + { name: "state", type: "uint8" }, |
| 25 | + { name: "keyType", type: "uint32" }, |
| 26 | + ], |
| 27 | + name: "", |
| 28 | + type: "tuple", |
| 29 | + }, |
| 30 | + ], |
| 31 | + stateMutability: "view", |
| 32 | + type: "function", |
| 33 | + }, |
| 34 | +] as const; |
| 35 | + |
| 36 | +async function verifyFidOwnership(fid: number, appKey: `0x${string}`) { |
| 37 | + const client = createPublicClient({ |
| 38 | + chain: optimism, |
| 39 | + transport: http(), |
| 40 | + }); |
| 41 | + |
| 42 | + try { |
| 43 | + const result = await client.readContract({ |
| 44 | + address: KEY_REGISTRY_ADDRESS, |
| 45 | + abi: KEY_REGISTRY_ABI, |
| 46 | + functionName: "keyDataOf", |
| 47 | + args: [BigInt(fid), appKey], |
| 48 | + }); |
| 49 | + |
| 50 | + return result.state === 1 && result.keyType === 1; |
| 51 | + } catch (error) { |
| 52 | + console.error("Key Registry verification failed:", error); |
| 53 | + return false; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function decode(encoded: string) { |
| 58 | + return JSON.parse(Buffer.from(encoded, "base64url").toString("utf-8")); |
| 59 | +} |
| 60 | + |
| 61 | +export async function POST(request: Request) { |
| 62 | + const requestJson = await request.json(); |
| 63 | + |
| 64 | + const { header: encodedHeader, payload: encodedPayload } = requestJson; |
| 65 | + |
| 66 | + const headerData = decode(encodedHeader); |
| 67 | + const event = decode(encodedPayload); |
| 68 | + |
| 69 | + const { fid, key } = headerData; |
| 70 | + |
| 71 | + const valid = await verifyFidOwnership(fid, key); |
| 72 | + |
| 73 | + if (!valid) { |
| 74 | + return Response.json( |
| 75 | + { success: false, error: "Invalid FID ownership" }, |
| 76 | + { status: 401 }, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + switch (event.event) { |
| 81 | + case "frame_added": |
| 82 | + console.log( |
| 83 | + "frame_added", |
| 84 | + "event.notificationDetails", |
| 85 | + event.notificationDetails, |
| 86 | + ); |
| 87 | + if (event.notificationDetails) { |
| 88 | + await setUserNotificationDetails(fid, event.notificationDetails); |
| 89 | + await sendFrameNotification({ |
| 90 | + fid, |
| 91 | + title: `Welcome to ${appName}`, |
| 92 | + body: `Thank you for adding ${appName}`, |
| 93 | + }); |
| 94 | + } else { |
| 95 | + await deleteUserNotificationDetails(fid); |
| 96 | + } |
| 97 | + |
| 98 | + break; |
| 99 | + case "frame_removed": { |
| 100 | + console.log("frame_removed"); |
| 101 | + await deleteUserNotificationDetails(fid); |
| 102 | + break; |
| 103 | + } |
| 104 | + case "notifications_enabled": { |
| 105 | + console.log("notifications_enabled", event.notificationDetails); |
| 106 | + await setUserNotificationDetails(fid, event.notificationDetails); |
| 107 | + await sendFrameNotification({ |
| 108 | + fid, |
| 109 | + title: `Welcome to ${appName}`, |
| 110 | + body: `Thank you for enabling notifications for ${appName}`, |
| 111 | + }); |
| 112 | + |
| 113 | + break; |
| 114 | + } |
| 115 | + case "notifications_disabled": { |
| 116 | + console.log("notifications_disabled"); |
| 117 | + await deleteUserNotificationDetails(fid); |
| 118 | + |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return Response.json({ success: true }); |
| 124 | +} |
0 commit comments