Skip to content
Closed
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
33 changes: 33 additions & 0 deletions app/api/attestations/[attestationId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextRequest, NextResponse } from "next/server";
import {
snippetAttestationService,
SnippetAttestationValidationError,
} from "@/lib/snippet-attestation-service";

/**
* GET /api/attestations/[attestationId]
* Retrieve a specific snippet attestation record by ID.
*/
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ attestationId: string }> },
) {
try {
const { attestationId } = await params;
const attestation =
await snippetAttestationService.getAttestationById(attestationId);

return NextResponse.json({ attestation }, { status: 200 });
} catch (error) {
if (error instanceof SnippetAttestationValidationError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode },
);
}
return NextResponse.json(
{ error: "Failed to retrieve attestation." },
{ status: 500 },
);
}
}
60 changes: 60 additions & 0 deletions app/api/snippets/[id]/attest/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { NextRequest, NextResponse } from "next/server";
import {
snippetAttestationService,
SnippetAttestationAuthError,
SnippetAttestationValidationError,
} from "@/lib/snippet-attestation-service";

/**
* POST /api/snippets/[id]/attest
* Create a Stellar-backed attestation for the current snippet content.
*/
export async function POST(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
try {
const { id } = await params;
let body: Record<string, any> = {};
try {
body = (await req.json()) || {};
} catch {
return NextResponse.json(
{ error: "Request body must be valid JSON." },
{ status: 400 },
);
}

const headerWallet = req.headers.get("x-wallet-address");
const attestedBy = body.attestedBy || body.walletAddress || headerWallet || "";

const attestation = await snippetAttestationService.createAttestation({
snippetId: id,
code: body.code,
attestedBy,
stellarTransactionHash: body.stellarTransactionHash || body.transactionHash,
network: body.network,
ledger: body.ledger,
clientProvidedHash: body.contentHash,
});

return NextResponse.json({ attestation }, { status: 201 });
} catch (error) {
if (error instanceof SnippetAttestationAuthError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode },
);
}
if (error instanceof SnippetAttestationValidationError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode },
);
}
return NextResponse.json(
{ error: "Failed to create snippet attestation." },
{ status: 500 },
);
}
}
31 changes: 31 additions & 0 deletions app/api/snippets/[id]/attestation/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NextRequest, NextResponse } from "next/server";
import {
snippetAttestationService,
SnippetAttestationValidationError,
} from "@/lib/snippet-attestation-service";

/**
* GET /api/snippets/[id]/attestation
* Retrieve the latest attestation and historical revision attestations for a snippet.
*/
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
try {
const { id } = await params;
const result = await snippetAttestationService.getSnippetAttestations(id);
return NextResponse.json(result, { status: 200 });
} catch (error) {
if (error instanceof SnippetAttestationValidationError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode },
);
}
return NextResponse.json(
{ error: "Failed to retrieve snippet attestations." },
{ status: 500 },
);
}
}
Loading