1
import nacl from "tweetnacl";
2
import { TriggerClient } from "@trigger.dev/sdk";
3
const verifyRequestSignature = async (request: Request): Promise<any[]> => {
4
const body = await request.text();
5
const jsonBody = JSON.parse(body);
6
const signature = request.headers.get("x-signature-ed25519");
7
const timestamp = request.headers.get("x-signature-timestamp");
8
const discordKey = process.env.DISCORD_APPLICATION_KEY;
9
if (!discordKey || !signature || !timestamp) return [false, jsonBody];
11
nacl.sign.detached.verify(
12
Buffer.from(timestamp + body),
13
Buffer.from(signature, "hex"),
14
Buffer.from(discordKey, "hex")
20
const discord = client.defineHttpEndpoint({
22
source: "discord.com",
27
skipTriggeringRuns: true,
31
handler: async (request) => {
32
const success = await verifyRequestSignature(request);
34
if (!success[0]) return new Response("Unauthorized", { status: 401 });
36
const { type } = success[1];
38
if (Number(type) === 1)
39
return new Response(JSON.stringify({ type }), {
40
headers: { "Content-Type": "application/json" },
43
if (Number(type) === 2)
48
content: `Hello, New!`,
51
{ headers: { "Content-Type": "application/json" } }
54
return new Response(JSON.stringify({ error: "bad request" }), {
59
verify: async (request) => {
60
const success = await verifyRequestSignature(request);
61
if (success[0]) return { success: success[0] };
62
return { success: false, reason: "Failed ed25519 verification" };
73
trigger: discord.onRequest(),
74
run: async (request, io, ctx) => {
75
const body = await request.json();
76
await io.logger.info(`Body`, body);