Skip to content

Instantly share code, notes, and snippets.

@MeesJ
Last active August 23, 2023 16:35
Show Gist options
  • Save MeesJ/fd9294b6b28c2e2f95bf55874b8c0f89 to your computer and use it in GitHub Desktop.
Save MeesJ/fd9294b6b28c2e2f95bf55874b8c0f89 to your computer and use it in GitHub Desktop.
Code to make Discord interactions endpoints created in n8n work. Set the environmental variable NODE_FUNCTION_ALLOW_EXTERNAL to contain tweetnacl, otherwise it won't work!
const nacl = require('tweetnacl');
const PUBLIC_KEY = 'APPLICATIONPUBLICKEY';
if (!items || items.length === 0) {
return [
{
json: {
error: 'Invalid payload'
}
}
];
}
const interaction = items[0].json;
if (!interaction.headers) {
return [
{
json: {
error: 'Missing headers'
}
}
];
}
const signature = interaction.headers['x-signature-ed25519'];
const timestamp = interaction.headers['x-signature-timestamp'];
const body = JSON.stringify(interaction.body);
if (!signature || !timestamp) {
return [
{
json: {
error: 'Missing signature or timestamp'
}
}
];
}
const isVerified = nacl.sign.detached.verify(
Buffer.from(timestamp + body),
Buffer.from(signature, 'hex'),
Buffer.from(PUBLIC_KEY, 'hex')
);
if (!isVerified) {
return [
{
json: {
error: 'Invalid request signature'
}
}
];
}
return items;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment