Public
Script
October 23, 2023
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
import { Buffer } from "node:buffer";
export async function valSignVerify(encoded: string) {
const matches = (encoded + "").match(/^@([^\.]+)\.(.+)$/);
if (!matches)
throw new Error("invalid signed data");
const [handle, encodedSignedMessage] = matches.slice(1);
const publicKey = Buffer.from(
await __utils__.api(handle + ".vsPublicKey"),
"base64",
);
const signedMessage = Buffer.from(encodedSignedMessage, "base64");
const { default: nacl } = await import("npm:tweetnacl@1.0.3");
const message = nacl.sign.open(signedMessage, publicKey);
if (!message)
throw new Error("invalid signature");
const { data, user, expr } = JSON.parse(new TextDecoder().decode(message));
if (user !== handle)
throw new Error("mismatched handle");
if (expr !== null && Date.now() > expr) {
throw new Error("signature expired");
}
return {
data,
handle,
expiresAt: expr === null ? null : new Date(expr),
};
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023