1
import nacl from "tweetnacl";
2
import { TriggerClient } from "@trigger.dev/sdk";
4
const verifyRequestSignature = async (request: Request): Promise<any[]> => {
5
const body = await request.text();
6
const jsonBody = JSON.parse(body);
7
const signature = request.headers.get("x-signature-ed25519");
8
const timestamp = request.headers.get("x-signature-timestamp");
9
const discordKey = process.env.DISCORD_APPLICATION_KEY;
10
if (!discordKey || !signature || !timestamp) return [false, jsonBody];
12
nacl.sign.detached.verify(
13
Buffer.from(timestamp + body),
14
Buffer.from(signature, "hex"),
15
Buffer.from(discordKey, "hex")
21
const discord = client.defineHttpEndpoint({
23
source: "discord.com",
28
skipTriggeringRuns: true,
32
handler: async (request) => {
33
const success = await verifyRequestSignature(request);
35
if (!success[0]) return new Response("Unauthorized", { status: 401 });
37
const { type } = success[1];
39
if (Number(type) === 1)
40
return new Response(JSON.stringify({ type }), {
41
headers: { "Content-Type": "application/json" },
44
if (Number(type) === 2)
49
content: `Hello, New!`,
52
{ headers: { "Content-Type": "application/json" } }
55
return new Response(JSON.stringify({ error: "bad request" }), {
60
verify: async (request) => {
61
const success = await verifyRequestSignature(request);
62
if (success[0]) return { success: success[0] };
63
return { success: false, reason: "Failed ed25519 verification" };
74
trigger: discord.onRequest(),
75
run: async (request, io, ctx) => {
76
const body = await request.json();
77
await io.logger.info(`Body`, body);