fix(interactions): deployment + handler

This commit is contained in:
41666 2021-08-01 16:59:47 -04:00
parent 3ca37cff17
commit 61b1146774
7 changed files with 58 additions and 12 deletions

View file

@ -17,12 +17,12 @@ const commands: Record<
};
export const interactionHandler = async (request: Request): Promise<Response> => {
if (!(await verifyRequest(request))) {
const interaction = (await request.json()) as InteractionRequest;
if (!verifyRequest(request, interaction)) {
return new Response('invalid request signature', { status: 401 });
}
const interaction = (await request.json()) as InteractionRequest;
if (interaction.type === InteractionType.PING) {
return respond({ type: 1 });
}

View file

@ -7,7 +7,7 @@ import { uiPublicURI } from './utils/config';
const router = new Router();
router.add('GET', '_healthz', healthz);
router.add('POST', 'interaction', interactionHandler);
router.add('POST', 'interactions', interactionHandler);
// Root Zen <3
router.addFallback('root', () => {

View file

@ -1,7 +1,11 @@
import { publicKey } from '@roleypoly/interactions/utils/config';
import { InteractionRequest } from '@roleypoly/types';
import nacl from 'tweetnacl';
export const verifyRequest = async (request: Request): Promise<boolean> => {
export const verifyRequest = (
request: Request,
interaction: InteractionRequest
): boolean => {
const timestamp = request.headers.get('x-signature-timestamp');
const signature = request.headers.get('x-signature-ed25519');
@ -9,11 +13,9 @@ export const verifyRequest = async (request: Request): Promise<boolean> => {
return false;
}
const body = await request.json();
if (
!nacl.sign.detached.verify(
Buffer.from(timestamp + JSON.stringify(body)),
Buffer.from(timestamp + JSON.stringify(interaction)),
Buffer.from(signature, 'hex'),
Buffer.from(publicKey, 'hex')
)