1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| import nacl from 'tweetnacl'
function verifyDiscordSignature(
body: string,
signature: string,
timestamp: string,
publicKey: string
): boolean {
const isValid = nacl.sign.detached.verify(
Buffer.from(timestamp + body),
Buffer.from(signature, 'hex'),
Buffer.from(publicKey, 'hex')
)
return isValid
}
app.post('/webhook/discord', express.raw({ type: '*/*' }), (req, res) => {
const signature = req.headers['x-signature-ed25519'] as string
const timestamp = req.headers['x-signature-timestamp'] as string
if (!verifyDiscordSignature(
req.body.toString(),
signature,
timestamp,
process.env.DISCORD_PUBLIC_KEY!
)) {
return res.status(401).send('Invalid signature')
}
// Interactionの確認応答
const body = JSON.parse(req.body)
if (body.type === 1) {
return res.json({ type: 1 })
}
// 処理...
})
|