Last active
August 23, 2023 16:35
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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