anproto personal data server
1import { apds } from './apds.js'
2
3await apds.start(Deno.args[0] || 'default')
4const pk = await apds.pubkey()
5
6const sockets = new Set()
7
8const send = async (hash) => {
9 const msg = await apds.get(hash)
10 const opened = await apds.open(msg)
11 const blob = await apds.get(opened.substring(13))
12 sockets.forEach(s => s.send(blob))
13 sockets.forEach(s => s.send(msg))
14 sockets.forEach(s => s.send(hash))
15}
16
17const render = async (h) => {
18 const get = await apds.get(h)
19 const opened = await apds.open(get)
20 const blob = await apds.get(opened.substring(13))
21 const yaml = await apds.parseYaml(blob)
22 //console.log(yaml)
23 const handle = yaml.name ? get.substring(0, 10) + ' ' + yaml.name : get.substring(0, 10)
24 console.log(`%c${handle} %c| ${yaml.body} -- %c${opened.substring(0, 13)}`, 'color: magenta', 'color: white', 'color: cyan')
25 //console.log(opened)
26 //console.log(blob)
27}
28
29const commands = async (c) => {
30 if (c.startsWith('nick')) {
31 const ar = c.split(' ')
32 if (ar[1]) {
33 await apds.put('name', ar[1])
34 console.log('You are now known as "' + ar[1] + '"')
35 } else {
36 console.log('ERROR: no nick to set')
37 }
38 }
39 if (c.startsWith('connect')) {
40 const ar = c.split(' ')
41 const s = ar[1]
42 console.log('Connecting to ' + s)
43 const ws = new WebSocket(s)
44 ws.onopen = () => {
45 console.log('Connected to: ' + s)
46 sockets.add(ws)
47 }
48 }
49 if (c.startsWith('clear')) {
50 const cleared = await apds.clear()
51 console.log('ERASING ALL DATA')
52 }
53 if (c.startsWith('exit')) {
54 console.log('EXITING APDS')
55 Deno.exit()
56 }
57}
58
59const flow = async () => {
60 const name = await apds.get('name')
61 const pub = await apds.pubkey()
62 const handle = name ? pub.substring(0, 10) + ' ' + name : pub.substring(0, 10)
63 const command = prompt(handle + '>')
64 if (command.startsWith('/')) {
65 await commands(command.substring(1))
66 await flow()
67 } else if (command.length > 0) {
68 const anmsg = await apds.compose(command)
69 await render(anmsg)
70 await send(anmsg)
71 await flow()
72 } else {
73 await flow()
74 }
75}
76
77if (pk) {
78 console.log('ANPROTO APDS ACTIVATED -- Your pubkey is: ' + pk)
79 await flow()
80} else {
81 const kp = await apds.generate()
82 await apds.put('keypair', kp)
83 console.log('ANPROTO APDS ACTIVATED -- No keypair found!')
84 console.log('New keypair generated! ' + await apds.pubkey())
85 await flow()
86}
87