| … | … | @@ -4,17 +4,68 @@ import { cachekv } from './lib/cachekv.js' |
| 4 | 4 | import { human } from './lib/human.js' |
| 5 | 5 | import { vb } from './lib/vb.js' |
| 6 | 6 | |
| 7 | let db |
| 8 | let log = [] |
| 9 | let newMessages = false |
| 10 | let sort = true |
| 11 | |
| 7 | 12 | export const bogbot = {} |
| 8 | 13 | |
| 14 | bogbot.start = async (appId) => { |
| 15 | console.log(appId) |
| 16 | |
| 17 | db = await cachekv(appId) |
| 18 | |
| 19 | setInterval(async () => { |
| 20 | if (newMessages) { |
| 21 | await db.put('log', JSON.stringify(log)) |
| 22 | newMessages = false |
| 23 | } |
| 24 | }, 1000) |
| 25 | |
| 26 | const getLog = await db.get('log') |
| 27 | if (getLog) { |
| 28 | log = JSON.parse(getLog) |
| 29 | } |
| 30 | |
| 31 | |
| 32 | setInterval(async () => { |
| 33 | if (sort) { |
| 34 | const newArray = [] |
| 35 | |
| 36 | await Promise.all(log.map(async (hash) => { |
| 37 | const obj = { |
| 38 | hash, |
| 39 | sig: await bogbot.find(hash) |
| 40 | } |
| 41 | obj.opened = await bogbot.open(obj.sig) |
| 42 | obj.ts = obj.opened.substring(0, 13) |
| 43 | newArray.push(obj) |
| 44 | })) |
| 45 | |
| 46 | await newArray.sort((a,b) => a.ts - b.ts) |
| 47 | |
| 48 | const newLog = [] |
| 49 | |
| 50 | await newArray.forEach(msg => { |
| 51 | newLog.push(msg.hash) |
| 52 | }) |
| 53 | |
| 54 | log = newLog |
| 55 | newMessages = true |
| 56 | sort = false |
| 57 | } |
| 58 | }, 20000) |
| 59 | } |
| 60 | |
| 9 | 61 | bogbot.generate = async () => { |
| 10 | 62 | const genkey = nacl.sign.keyPair() |
| 11 | 63 | const keygen = encode(genkey.publicKey) + encode(genkey.secretKey) |
| 12 | | //await localStorage.setItem('keypair', keygen) |
| 13 | 64 | return keygen |
| 14 | 65 | } |
| 15 | 66 | |
| 16 | 67 | bogbot.keypair = async () => { |
| 17 | | const keypair = await localStorage.getItem('keypair') |
| 68 | const keypair = await db.get('keypair') |
| 18 | 69 | if (keypair) { |
| 19 | 70 | return keypair |
| 20 | 71 | } |
| … | … | @@ -35,7 +86,7 @@ bogbot.privkey = async () => { |
| 35 | 86 | } |
| 36 | 87 | |
| 37 | 88 | bogbot.deletekey = async () => { |
| 38 | | localStorage.removeItem('keypair') |
| 89 | db.rm('keypair') |
| 39 | 90 | } |
| 40 | 91 | |
| 41 | 92 | bogbot.hash = async (data) => { |
| … | … | @@ -56,7 +107,7 @@ bogbot.sign = async (data) => { |
| 56 | 107 | const sig = encode(nacl.sign(new TextEncoder().encode(timestamp + hash), decode(await bogbot.privkey()))) |
| 57 | 108 | await bogbot.add(await bogbot.pubkey() + sig) |
| 58 | 109 | const protocolMsg = await bogbot.make(await bogbot.pubkey() + sig) |
| 59 | | localStorage.setItem('previous', protocolMsg) |
| 110 | db.put('previous', protocolMsg) |
| 60 | 111 | return protocolMsg |
| 61 | 112 | } |
| 62 | 113 | |
| … | … | @@ -86,9 +137,9 @@ bogbot.createYaml = async (obj, content) => { |
| 86 | 137 | bogbot.compose = async (content) => { |
| 87 | 138 | const obj = {} |
| 88 | 139 | |
| 89 | | const name = localStorage.getItem('name') |
| 90 | | const image = localStorage.getItem('image') |
| 91 | | const previous = localStorage.getItem('previous') |
| 140 | const name = await db.get('name') |
| 141 | const image = await db.get('image') |
| 142 | const previous = await db.get('previous') |
| 92 | 143 | |
| 93 | 144 | if (name) { obj.name = name} |
| 94 | 145 | if (image) { obj.image = image} |
| … | … | @@ -105,30 +156,21 @@ bogbot.compose = async (content) => { |
| 105 | 156 | bogbot.make = async (data) => { |
| 106 | 157 | const hash = await bogbot.hash(data) |
| 107 | 158 | |
| 108 | | await cachekv.put(hash, data) |
| 159 | await db.put(hash, data) |
| 109 | 160 | |
| 110 | 161 | return hash |
| 111 | 162 | } |
| 112 | 163 | |
| 113 | 164 | bogbot.find = async (hash) => { |
| 114 | | const blob = await cachekv.get(hash) |
| 165 | const blob = await db.get(hash) |
| 115 | 166 | |
| 116 | 167 | return blob |
| 117 | 168 | } |
| 118 | 169 | |
| 119 | | let newMessages = false |
| 120 | | |
| 121 | | setInterval(async () => { |
| 122 | | if (newMessages) { |
| 123 | | await cachekv.put('log', JSON.stringify(log)) |
| 124 | | newMessages = false |
| 125 | | } |
| 126 | | }, 1000) |
| 127 | | |
| 128 | | let log = [] |
| 129 | | const getLog = await cachekv.get('log') |
| 130 | | if (getLog) { |
| 131 | | log = JSON.parse(getLog) |
| 170 | bogbot.save = async (key, value) => { |
| 171 | console.log(key) |
| 172 | console.log(value) |
| 173 | await db.put(key, value) |
| 132 | 174 | } |
| 133 | 175 | |
| 134 | 176 | bogbot.add = async (msg) => { |
| … | … | @@ -155,32 +197,3 @@ bogbot.visual = async (pubkey) => { |
| 155 | 197 | return vb(decode(pubkey), 256) |
| 156 | 198 | } |
| 157 | 199 | |
| 158 | | let sort = true |
| 159 | | |
| 160 | | setInterval(async () => { |
| 161 | | if (sort) { |
| 162 | | const newArray = [] |
| 163 | | |
| 164 | | await Promise.all(log.map(async (hash) => { |
| 165 | | const obj = { |
| 166 | | hash, |
| 167 | | sig: await bogbot.find(hash) |
| 168 | | } |
| 169 | | obj.opened = await bogbot.open(obj.sig) |
| 170 | | obj.ts = obj.opened.substring(0, 13) |
| 171 | | newArray.push(obj) |
| 172 | | })) |
| 173 | | |
| 174 | | await newArray.sort((a,b) => a.ts - b.ts) |
| 175 | | |
| 176 | | const newLog = [] |
| 177 | | |
| 178 | | await newArray.forEach(msg => { |
| 179 | | newLog.push(msg.hash) |
| 180 | | }) |
| 181 | | |
| 182 | | log = newLog |
| 183 | | newMessages = true |
| 184 | | sort = false |
| 185 | | } |
| 186 | | }, 20000) |