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
|
// 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) + '...'
return name
}
// 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) {
post.author = keys.publicKey
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)))
localforage.getItem('log').then(log => {
if (log) {
log.unshift(message)
localforage.setItem('log', log)
} else {
var feed = [message]
localforage.setItem('log', feed)
}
})
feed.unshift(message)
localforage.setItem(keys.publicKey, feed)
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)))
localforage.getItem('log').then(log => {
if (log) {
log.unshift(message)
localforage.setItem('log', log)
} else {
var feed = [message]
localforage.setItem('log', feed)
}
})
var feed = [message]
localforage.setItem(keys.publicKey, feed)
return message
}
}
|