1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
export let verify_discord_signature = async (
public_key: String,
body: any,
signature: String,
timestamp: any, // ugly hack to allow concatenation
) => {
const encoder = new TextEncoder();
const fromHexString = (hexString) => Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
const nacl = await import("https://cdn.skypack.dev/tweetnacl@v1.0.3?dts");
// This used to use node.js buffers...
const result = nacl.sign.detached.verify(
new Uint8Array(encoder.encode(timestamp + body)),
fromHexString(signature),
fromHexString(public_key),
);
return result;
};