-
Notifications
You must be signed in to change notification settings - Fork 0
/
bogbot.js
222 lines (181 loc) · 4.8 KB
/
bogbot.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import nacl from './lib/nacl-fast-es.js'
import { decode, encode } from './lib/base64.js'
import { cachekv } from './lib/cachekv.js'
export const bogbot = {}
const generate = async () => {
const genkey = nacl.sign.keyPair()
const keygen = encode(genkey.publicKey) + encode(genkey.secretKey)
return keygen
}
bogbot.keypair = async () => {
const keypair = await localStorage.getItem('keypair')
if (!keypair) {
const keypair = await generate()
await localStorage.setItem('keypair', keypair)
location.reload()
//return keypair
} else {
return keypair
}
}
bogbot.pubkey = async () => {
const keypair = await bogbot.keypair()
return keypair.substring(0, 44)
}
bogbot.privkey = async () => {
const keypair = await bogbot.keypair()
return keypair.substring(44)
}
bogbot.deletekey = async () => {
localStorage.removeItem('keypair')
}
bogbot.publish = async (text) => {
const pubkey = await bogbot.pubkey()
const privkey = await bogbot.privkey()
const datahash = await bogbot.make(text)
const timestamp = Date.now()
const msg = timestamp + pubkey + datahash
const hash = encode(
Array.from(
new Uint8Array(
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(msg))
)
)
)
let previous
const getLatest = await bogbot.getLatest(pubkey)
previous = hash
if (getLatest) {
previous = getLatest.hash
} else {
previous = hash
}
const next = msg + previous + hash
const sig = encode(nacl.sign(new TextEncoder().encode(next), decode(privkey)))
return pubkey + sig
}
bogbot.open = async (msg) => {
const opened = new TextDecoder().decode(nacl.sign.open(decode(msg.substring(44)), decode(msg.substring(0, 44))))
const obj = {
timestamp: parseInt(opened.substring(0, 13)),
author: opened.substring(13, 57),
data: opened.substring(57, 101),
previous: opened.substring(101, 145),
hash : opened.substring(145),
raw: msg
}
const blob = await bogbot.find(obj.data)
if (blob) {
obj.txt = blob
}
return obj
}
let arraystore = []
const file = await cachekv.get('arraystore')
let newData = false
if (file) {
arraystore = JSON.parse(file)
newData = true
}
const sorter = setInterval(async () => {
if (newData) {
console.log(newData)
console.log('sorting')
const rawset = new Set()
for await (const msg of arraystore) {
rawset.add(msg.raw)
}
const newarray = []
for await (const msg of rawset) {
const opened = await bogbot.open(msg)
const blob = await bogbot.find(opened.data)
if (blob) {
opened.txt = blob
}
newarray.push(opened)
}
await newarray.sort((a,b) => a.timestamp - b.timestamp)
arraystore = newarray
await save()
newData = false
console.log(newData)
}
}, 10000)
bogbot.query = async (query) => {
if (arraystore[0] && !query) { return arraystore }
if (arraystore[0] && query.startsWith('?')) {
const search = query.substring(1).replace(/%20/g, ' ').toUpperCase()
const result = arraystore.filter(msg => msg.txt && msg.txt.toUpperCase().includes(search))
return result
} else if (arraystore[0]) {
const result = arraystore.filter(msg => msg.author == query || msg.hash == query)
return result
}
}
bogbot.getLatest = async (query) => {
if (arraystore[0]) {
const querylog = arraystore.filter(msg => msg.author == query)
if (querylog[0]) {
return querylog[querylog.length - 1]
}
}
}
bogbot.getFeeds = async () => {
const feeds = []
arraystore.map(msg => {
if (!feeds.includes(msg.author)) {
feeds.push(msg.author)
}
})
return feeds
}
const save = async function () {
const stringedarray = JSON.stringify(arraystore)
await cachekv.put('arraystore', stringedarray)
}
bogbot.add = async (msg) => {
const opened = await bogbot.open(msg)
const dupe = arraystore.filter(message => message.hash === opened.hash)
if (opened && !dupe[0]) {
const found = await bogbot.find(opened.data)
if (found) {
opened.txt = found
}
arraystore.push(opened)
newData = true
}
}
bogbot.make = async (file) => {
const hash = encode(
Array.from(
new Uint8Array(
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(file))
)
)
)
await cachekv.put(hash, file)
return hash
}
bogbot.find = async (hash) => {
const file = await cachekv.get(hash)
if (file) {
return file
}
}
const info = new Map()
bogbot.getInfo = async (id) => {
if (info.has(id)) {
return info.get(id)
} else if (await cachekv.get(id)) {
const gotInfo = JSON.parse(await cachekv.get(id))
info.set(id, gotInfo)
return gotInfo
} else {
const gotInfo = {}
return gotInfo
}
}
bogbot.saveInfo = async (pubkey, data) => {
info.set(pubkey, data)
await cachekv.put(pubkey, JSON.stringify(data))
}