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
|
// generate a public.private keypair with TweetNaCl.js
function requestFeed (src, server) {
var ws = new WebSocket(server + src)
localforage.getItem(src, function (err, log) {
if (log) {
ws.onopen = function () {
var clientLog = { publicKey: src, log: log }
ws.send(JSON.stringify(clientLog))
}
ws.onmessage = function (ev) {
var serverLog = JSON.parse(ev.data)
if (serverLog.log.length > log.length) {
localforage.setItem(src, serverLog.log)
localforage.getItem('log', function (err, publicLog) {
if (publicLog) {
var num = serverLog.log.length - publicLog.length
var diff = serverLog.log.slice(0, num)
newLog = diff.concat(publicLog)
localforage.setItem('log', newLog)
} else {
localforage.setItem('log', serverLog.log)
}
})
}
}
} else {
ws.onopen = function () {
var clientLog = {
publicKey: src,
log: []
}
ws.send(JSON.stringify(clientLog))
}
ws.onmessage = function (ev) {
serverLog = JSON.parse(ev.data)
localforage.setItem(src, serverLog.log)
localforage.getItem('log', function (err, publicLog) {
if (publicLog) {
newLog = serverLog.log.concat(publicLog)
localforage.setItem('log', newLog)
} else {
localforage.setItem('log', serverLog.log)
}
})
}
}
})
}
// publish new messages to your log
function publish (content, keys) {
console.log(content)
console.log(keys)
localforage.getItem(keys.publicKey, function (err, log) {
if (log) {
var lastPost = log[0]
var pubkey = nacl.util.decodeBase64(keys.publicKey.substring(1))
var sig = nacl.util.decodeBase64(lastPost.signature)
var opened = JSON.parse(nacl.util.encodeUTF8(nacl.sign.open(sig, pubkey)))
console.log(opened)
var seq = opened.sequence
content.sequence = ++seq
content.previous = nacl.util.encodeBase64(nacl.hash(nacl.util.decodeUTF8(JSON.stringify(log[0]))))
var post = {
author: keys.publicKey,
key: '%' + nacl.util.encodeBase64(nacl.hash(nacl.util.decodeUTF8(JSON.stringify(content)))),
signature: nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(content)), nacl.util.decodeBase64(keys.privateKey)))
}
// update the log
updateLog(keys.publicKey, post)
var scroller = document.getElementById('scroller')
if (scroller.firstChild) {
scroller.insertBefore(renderMessage(post), scroller.childNodes[1])
} else {
scroller.appendChild(renderMessage(post))
}
} else {
content.sequence = 0
var post = {
author: keys.publicKey,
key: '%' + nacl.util.encodeBase64(nacl.hash(nacl.util.decodeUTF8(JSON.stringify(content)))),
signature: nacl.util.encodeBase64(nacl.sign(nacl.util.decodeUTF8(JSON.stringify(content)), nacl.util.decodeBase64(keys.privateKey)))
}
updateLog(keys.publicKey, post)
var scroller = document.getElementById('scroller')
if (scroller.firstChild) {
scroller.insertBefore(renderMessage(post), scroller.childNodes[1])
} else {
scroller.appendChild(renderMessage(post))
}
}
})
}
// update your log in the browser
function updateLog (feed, post) {
console.log('UPDATE LOG')
console.log(feed)
console.log(post)
localforage.getItem(feed, function (err, log) {
if (log) {
log.unshift(post)
localforage.setItem(feed, log)
} else {
log = []
log.unshift(post)
localforage.setItem(feed, log)
}
})
localforage.getItem('log', function (err, log) {
if (log) {
log.unshift(post)
localforage.setItem('log', log)
} else {
log = []
log.unshift(post)
localforage.setItem('log', log)
}
})
}
function getName (id) {
var name = h('span')
name.textContent = id.substring(0, 10) + '...'
localforage.getItem(id, function (err, log) {
if (log) {
for (var i=0; i < log.length; i++) {
var post = log[i]
var pubkey = nacl.util.decodeBase64(post.author.substring(1))
var sig = nacl.util.decodeBase64(post.signature)
post.content = JSON.parse(nacl.util.encodeUTF8(nacl.sign.open(sig, pubkey)))
//if (post.content) {
if (post.content.type == 'name') {
console.log(post.content.name)
name.textContent = '@' + post.content.name
}
//}
}
}
})
return name
}
// human-time by Dave Eddy https://github.com/bahamas10/human
function human(seconds) {
if (seconds instanceof Date)
seconds = Math.round((Date.now() - seconds) / 1000);
var suffix = seconds < 0 ? 'from now' : 'ago';
seconds = Math.abs(seconds);
var times = [
seconds / 60 / 60 / 24 / 365, // years
seconds / 60 / 60 / 24 / 30, // months
seconds / 60 / 60 / 24 / 7, // weeks
seconds / 60 / 60 / 24, // days
seconds / 60 / 60, // hours
seconds / 60, // minutes
seconds // seconds
];
var names = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second'];
for (var i = 0; i < names.length; i++) {
var time = Math.floor(times[i]);
var name = names[i];
if (time > 1)
name += 's';
if (time >= 1)
return time + ' ' + name + ' ' + suffix;
}
return '0 seconds ' + suffix;
}
// hscrpt by Dominic Tarr https://github.com/dominictarr/hscrpt/blob/master/LICENSE
function h (tag, attrs, content) {
if(Array.isArray(attrs)) content = attrs, attrs = {}
var el = document.createElement(tag)
for(var k in attrs) el[k] = attrs[k]
if(content) content.forEach(function (e) {
if(e) el.appendChild('string' == typeof e ? document.createTextNode(e) : e)
})
return el
}
|