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
|
// bog.open -- opens a signature and returns content if you pass a signature and a public key
// EX: open(msg).then(content => { console.log(content) })
async function open (msg) {
var pubkey = nacl.util.decodeBase64(msg.author.substring(1))
var sig = nacl.util.decodeBase64(msg.signature)
var opened = await JSON.parse(nacl.util.encodeUTF8(nacl.sign.open(sig, pubkey)))
opened.key = msg.key
return opened
}
// bog.keys -- gets your public/private keypair, generates one if there is none
// EX: keys().then(key => { console.log(key)})
async function keys (key) {
var keypair = await localforage.getItem('id')
if (keypair != null) {
return keypair
} else {
var genkey = nacl.sign.keyPair()
var keypair = {
publicKey: '@' + nacl.util.encodeBase64(genkey.publicKey),
privateKey: nacl.util.encodeBase64(genkey.secretKey)
}
if (keypair.publicKey.includes('/')) {
console.log('TRYING AGAIN')
setTimeout(function () {
location.reload()
}, 10)
} else {
localforage.setItem('id', keypair)
}
return keypair
}
}
// bog.get -- iterates over log and returns a post.
// EX: get('%x5T7KZ5haR2F59ynUuCggwEdFXlLHEtFoBQIyKYppZYerq9oMoIqH76YzXQpw2DnYiM0ugEjePXv61g3E4l/Gw==').then(msg => { console.log(msg)})
async function get (key) {
var log = await localforage.getItem('log')
if (log != null) {
for (var i = log.length - 1; i >= 0; --i) {
if (log[i].key === key) {
return log[i]
}
}
}
}
// bog.getName -- iterates over a feed and returns a person's name
function getName (id) {
var name = h('span')
name.textContent = id.substring(0, 10) + '...'
bog().then(log => {
if (log) {
for (var i = 0; i < log.length; i++ ) {
if (log[i].named == id) {
console.log(log[i].name)
return name.textContent = '@' + log[i].name
}
}
}
})
return name
}
// bog.regenerate -- regenerates main log by taking all of the feed logs, combinging them, and then sorting them
function regenerate (home) {
var newlog = []
var openedlog = []
localforage.iterate(function(value, key, i) {
if (key[0] == '@') {
newlog = newlog.concat(value)
}
console.log(newlog)
}).then(function () {
newlog.forEach(function (msg) {
var pubkey = nacl.util.decodeBase64(msg.author.substring(1))
var sig = nacl.util.decodeBase64(msg.signature)
var opened = JSON.parse(nacl.util.encodeUTF8(nacl.sign.open(sig, pubkey)))
opened.key = msg.key
openedlog.push(opened)
})
console.log(openedlog)
openedlog.sort((a, b) => a.timestamp - b.timestamp)
var reversed = openedlog.reverse()
console.log('REGENERATE')
localforage.setItem('log', reversed).then(function () {
if (home) {
location.hash = ''
}
location.reload()
})
})
}
// bog.log (feed) -- returns a specific feed if a parameter is passed, if not returns the entire log
// EX: bog().then(log => { console.log(log)})
// EX: bog('@ExE3QXmBhYQlGVA3WM2BD851turNzwhruWbIpMd7rbQ=').then(log => { console.log(log)})
async function bog (feed) {
if (feed) {
var log = await localforage.getItem(feed)
return log
} else {
var log = await localforage.getItem('log')
return log
}
}
// bog.publish -- publishes a new bog post and updates the feeds
// EX: publish({type: 'post', timestamp: Date.now(), text: 'Hello World'}).then(msg => { console.log(msg)})
async function publish (post, keys, preview) {
post.author = keys.publicKey
post.timestamp = Date.now()
var message = {
author: keys.publicKey
}
var feed = await localforage.getItem(keys.publicKey)
if (feed) {
var firstMsg = await open(feed[0])
post.seq = ++firstMsg.seq
message.key = '%' + nacl.util.encodeBase64(nacl.hash(nacl.util.decodeUTF8(JSON.stringify(post)))),
message.signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(post)), nacl.util.decodeBase64(keys.privateKey)))
var openedMsg = await open(message)
if (!preview) {
console.log('ADDING TO LOG AND FEED')
localforage.getItem('log').then(log => {
if (log) {
log.unshift(openedMsg)
localforage.setItem('log', log)
} else {
var feed = [openedMsg]
localforage.setItem('log', feed)
}
})
var subs = [keys.publicKey]
feed.unshift(message)
localforage.setItem(keys.publicKey, feed).then(function () {
sync(subs, keys)
})
}
return message
} else {
post.seq = 1
message.key = '%' + nacl.util.encodeBase64(nacl.hash(nacl.util.decodeUTF8(JSON.stringify(post)))),
message.signature = nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(post)), nacl.util.decodeBase64(keys.privateKey)))
var openedMsg = await open(message)
if (!preview) {
localforage.getItem('log').then(log => {
if (log) {
log.unshift(openedMsg)
localforage.setItem('log', log)
} else {
var feed = [openedMsg]
localforage.setItem('log', feed)
}
})
var feed = [message]
var subs = [keys.publicKey]
localforage.setItem(keys.publicKey, feed).then(function () {
sync(subs, keys)
})
}
return message
}
}
|