Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/app/api/ratings/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { recoverMessageAddress, type Address } from "viem";
import { type Address } from "viem";
import { publicClient } from "../../../../lib/rpc";
import { createServerClient, supabase } from "../../../../lib/supabase";
import { erc20Abi } from "../../../../lib/price";
Expand Down Expand Up @@ -52,6 +52,7 @@ interface RatingBody {
storylineId: number;
rating: number;
comment?: string;
address: string;
signature: string;
message: string;
}
Expand All @@ -64,7 +65,7 @@ export async function POST(req: NextRequest) {
return error("Invalid JSON body");
}

const { storylineId, rating, comment, signature, message } = body;
const { storylineId, rating, comment, address, signature, message } = body;

// Validate inputs
if (!storylineId || typeof storylineId !== "number") {
Expand All @@ -73,8 +74,8 @@ export async function POST(req: NextRequest) {
if (!rating || typeof rating !== "number" || !Number.isInteger(rating) || rating < 1 || rating > 5) {
return error("Rating must be an integer between 1 and 5");
}
if (!signature || !message) {
return error("Missing signature or message");
if (!address || !signature || !message) {
return error("Missing address, signature, or message");
}

// Validate signed message binds to this specific action
Expand All @@ -85,13 +86,17 @@ export async function POST(req: NextRequest) {
);
}

// 1. Recover rater address from signature
let raterAddress: Address;
// 1. Verify signature (supports both EOA and EIP-1271 contract wallets)
const raterAddress = address as Address;
try {
raterAddress = await recoverMessageAddress({
const valid = await publicClient.verifyMessage({
address: raterAddress,
message,
signature: signature as `0x${string}`,
});
if (!valid) {
return error("Invalid signature");
}
} catch {
return error("Failed to verify signature");
}
Expand Down
1 change: 1 addition & 0 deletions src/components/RatingWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export function RatingWidget({ storylineId, tokenAddress }: RatingWidgetProps) {
storylineId,
rating: selectedRating,
comment: comment || undefined,
address,
signature,
message,
}),
Expand Down
Loading