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
38
39
40
41
42
43
44
45
46
47
48
49
import { runVal } from "https://esm.town/v/std/runVal";
import { btoa } from "https://esm.town/v/stevekrouse/btoa";
import { importKey } from "https://esm.town/v/stevekrouse/importKey";
import { mainReference } from "https://esm.town/v/stevekrouse/mainReference";
import { signECDSA } from "https://esm.town/v/stevekrouse/signECDSA";
// Make an authenticated request to another user's val
// Example usage: https://www.val.town/v/stevekrouse.authRequestEx
export const runValAPIAuth = async ({ val, args, handle, privateKey, keys }: {
val: string;
args: any;
handle?: string;
privateKey?: CryptoKey;
keys: {
privateKey: {
kty: string;
crv: string;
alg: string;
x: string;
y: string;
d: string;
key_ops: string[];
ext: boolean;
};
publicKey: any;
};
}) => {
let t = Date.now();
handle = handle ?? mainReference().userHandle;
let toSign = {
val,
handle: handle.replace("@", ""),
args,
t,
};
if (keys && !privateKey) {
privateKey = await importKey(
keys.privateKey,
"privateKey",
);
}
let signature = await btoa(
await signECDSA(JSON.stringify(toSign), privateKey),
);
return runVal(val.replace("@", ""), ...[...args, {
...toSign,
signature,
}]);
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.