Skip to content
Open
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
35 changes: 31 additions & 4 deletions app/api/governance/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
} from '@/lib/voteSignature';
import { verifyAdminAccess } from '@/lib/adminAuth';
import { verifyWalletAccess } from '@/lib/walletAuth';
import { checkStarOwnershipBatched } from '@/lib/starSkrumpey';

/**
* GET /api/governance
Expand Down Expand Up @@ -331,6 +332,15 @@ export async function POST(request: NextRequest) {
);
}

// Verify proposer holds at least one Star Skrumpey
const proposerStars = await checkStarOwnershipBatched(proposerAddress);
if (proposerStars.length === 0) {
return NextResponse.json(
{ success: false, error: 'You must hold at least one Star Skrumpey to create proposals' },
{ status: 403 }
);
}

// Validate voting duration (1-4 weeks)
const duration = parseInt(votingDurationWeeks, 10) || 1;
if (duration < 1 || duration > 4) {
Expand Down Expand Up @@ -415,7 +425,7 @@ export async function POST(request: NextRequest) {

// Cast a vote
if (action === 'vote') {
const { proposalId, voterAddress, support, votingPower, reason, signature, nonce, signatureVersion, typedData } = body;
const { proposalId, voterAddress, support, reason, signature, nonce, signatureVersion } = body;

// Strict input validation
if (!proposalId || !voterAddress || support === undefined) {
Expand Down Expand Up @@ -535,18 +545,35 @@ export async function POST(request: NextRequest) {
}

// Signature is valid, proceed with vote
logger.info('Governance: Valid signature verified', {
proposalId,
logger.info('Governance: Valid signature verified', {
proposalId,
voterAddress: normalizedVoterAddress.slice(0, 10) + '...',
version
});

// Server-side voting power verification: 1 Star Skrumpey = 1 Vote
// NEVER trust client-supplied votingPower
const voterStars = await checkStarOwnershipBatched(normalizedVoterAddress);
const verifiedVotingPower = voterStars.length;

if (verifiedVotingPower === 0) {
return NextResponse.json(
{ success: false, error: 'You must hold at least one Star Skrumpey to vote' },
{ status: 403 }
);
}

logger.info('Governance: Voting power verified on-chain', {
voterAddress: normalizedVoterAddress.slice(0, 10) + '...',
verifiedVotingPower,
});

// Store vote with signature data
const result = castGovernanceVote({
proposalId,
voterAddress: normalizedVoterAddress,
support: supportValue,
votingPower: parseInt(votingPower, 10) || 1,
votingPower: verifiedVotingPower,
reason,
signature,
signatureVersion: version,
Expand Down