Golang implementation ANProto: the Authenticated and Non-networked protocol or ANother protocol
1import { createRequire } from 'module';
2const require = createRequire(import.meta.url);
3
4// Provide PRNG for the bundled nacl library
5globalThis.self = {
6 crypto: {
7 getRandomValues: (buf) => {
8 require('crypto').randomFillSync(buf);
9 return buf;
10 }
11 }
12};
13
14// Polyfill atob for base64.decode used by lib/base64.js
15if (typeof globalThis.atob === 'undefined') {
16 globalThis.atob = (b64) => Buffer.from(b64, 'base64').toString('latin1');
17}
18
19// Ensure WebCrypto subtle exists
20if (typeof globalThis.crypto === 'undefined' || typeof globalThis.crypto.subtle === 'undefined') {
21 globalThis.crypto = globalThis.crypto || {};
22 try {
23 globalThis.crypto.subtle = require('crypto').webcrypto.subtle;
24 } catch (e) {
25 // let it error later if not available
26 }
27}
28
29const { an } = await import('./an.js');
30
31const [,,cmd, ...rest] = process.argv;
32
33async function main(){
34 if(cmd === 'gen'){
35 const k = await an.gen();
36 console.log(k);
37 return;
38 }
39 if(cmd === 'sign'){
40 const h = rest[0];
41 const k = rest[1];
42 const s = await an.sign(h, k);
43 console.log(s);
44 return;
45 }
46 if(cmd === 'open'){
47 const m = rest[0];
48 const o = await an.open(m);
49 console.log(o);
50 return;
51 }
52 console.error('unknown cmd');
53 process.exit(2);
54}
55
56main().catch((e)=>{ console.error(e); process.exit(1); });