-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
345 lines (287 loc) · 9.16 KB
/
index.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
const ssbSingleton = require('ssb-browser-core/ssb-singleton')
const pull = require('pull-stream')
const hkdf = require('futoin-hkdf')
const crypto = require('crypto')
const {
getGroupKeysFeed,
getChatFeed,
groupConfigChanges,
replicateSubfeeds,
dumpDB
} = require('./helpers')
const { monkeyPatchBox2Libs } = require('./browser-hack')
const { config, extraModules } = require('./ssb-config')
const Crut = require('ssb-crut')
const { spec } = require('./group-config-spec')
let crut
// setup ssb browser core
ssbSingleton.setup("/.groupies", config, extraModules)
ssbSingleton.getSimpleSSBEventually(
(err, SSB) => {
if (err) return console.error(err)
crut = new Crut(SSB, spec)
SSB.ebt.registerFormat(require('ssb-ebt/formats/bendy-butt'))
setupBox2(SSB)
}
)
const chatApp = require('./chat')
// load ssb-profile-link component
require('./ssb-profile-link')
let afterGroupSave = () => {}
const menu = new Vue({
el: '#menu',
data: function() {
return {
id: '',
groups: [],
peers: [],
stagedPeers: [],
activeId: '',
// group dialog
groupKey: '',
showGroupEdit: false,
groupSaveText: 'Create group',
groupTitle: '',
rooms: [],
newRoomAddress: ''
}
},
methods: {
dumpDB,
openGroup: function(group) {
this.activeId = group.id
const app = chatApp(ssbSingleton, group, crut, getChatFeed,
groupConfigChanges, this.editGroupConfig)
new Vue(app).$mount("#app")
},
addRoomToConfig: function() {
this.rooms.push(this.newRoomAddress)
this.newRoomAddress = ''
},
editGroupConfig: function(group, title, rooms) {
afterGroupSave = () => { this.showGroupEdit = false }
this.groupKey = group.key
this.groupSaveText = 'Save group config'
this.groupTitle = title
this.rooms = rooms
this.showGroupEdit = true
},
copyGroupKey: function() {
navigator.clipboard.writeText(this.groupKey)
},
saveGroupConfig: function() {
group = { id: this.groupKey + '.groupies' }
const groupKey = Buffer.from(this.groupKey, 'hex')
SSB.box2.addGroupKey(group.id, groupKey)
getChatFeed(SSB, group, (err, chatFeed) => {
if (err) return console.error("failed to get chat feed", err)
// this assumes that the root config msg will be available
// here be dragons
const crutWrite = new Crut(SSB, spec, {
feedId: chatFeed.keys.id,
publish: (content, cb) => SSB.db.publishAs(chatFeed.keys, content, cb)
})
const { where, type, toPullStream } = SSB.db.operators
pull(
SSB.db.query(
where(type('groupconfig')),
toPullStream()
),
pull.filter(msg => {
const { recps, tangles } = msg.value.content
const correctGroup = recps && recps[0] === group.id
const isRoot = tangles && tangles.groupconfig && tangles.groupconfig.root === null
return correctGroup && isRoot
}),
pull.collect((err, msgs) => {
if (err) return console.error(err)
const content = { title: this.groupTitle }
if (msgs.length > 0) {
crut.read(msgs[0].key, (err, record) => {
if (err) return console.error(err)
const currentRooms = new Set(record.states[0].rooms)
const newRooms = new Set(this.rooms)
const added = []
const removed = []
for (let room of currentRooms)
if (!newRooms.has(room))
removed.push(room)
for (let room of newRooms)
if (!currentRooms.has(room))
added.push(room)
content.rooms = {
add: added,
remove: removed
}
crutWrite.update(msgs[0].key, content, (err) => {
if (err) return console.error(err)
afterGroupSave()
})
})
} else {
content.rooms = { add: this.rooms }
content.recps = [group.id]
crutWrite.create(content, (err) => {
if (err) return console.error(err)
afterGroupSave()
})
}
})
)
})
},
addGroupKey: function() {
this.activeId = 'addGroupKey'
const addGroupKey = require('./add-group-key')
new Vue(addGroupKey(ssbSingleton, getGroupKeysFeed)).$mount("#app")
},
newGroup: function() {
const groupKey = crypto.randomBytes(32)
this.groupTitle = ''
this.groupKey = groupKey.toString('hex')
this.groupSaveText = 'Create group'
this.showGroupEdit = true
afterGroupSave = () => {
const groupId = this.groupKey + '.groupies'
ssbSingleton.getSimpleSSBEventually(
(err, SSB) => {
if (err) return console.error(err)
getGroupKeysFeed(SSB, (err, keysFeed) => {
SSB.db.publishAs(keysFeed.keys, {
type: 'groupkey',
key: this.groupKey,
id: groupId,
recps: [SSB.id]
}, (err, msg) => {
if (err) return console.error(err)
this.showGroupEdit = false
this.activeId = groupId
const group = { key: groupKey.toString('hex'), id: groupId }
const app = chatApp(ssbSingleton, group, crut, getChatFeed,
groupConfigChanges, this.editGroupConfig)
new Vue(app).$mount("#app")
})
})
}
)
}
},
openProfile: function() {
this.activeId = ''
const profile = require('./profile')
new Vue(profile(SSB.id)).$mount("#app")
},
}
})
function setupBox2(SSB) {
monkeyPatchBox2Libs()
// We can't encrypt the seed to ourself with a own DM key generated
// from the seed because we need to seed to read the private message
// with the seed. Instead we encrypt the seed from box1. Another
// option is to save the own DM key outside the system as in ahau,
// but that complicates backup.
SSB.box2.setReady()
SSB.metafeeds.findOrCreate((err, metafeed) => {
const KEY_LENGTH = 32
const derived_key = hkdf(metafeed.seed, KEY_LENGTH, {
salt: 'ssb',
info: 'box2:this-is-my-own-direct-message-key',
hash: 'SHA-256',
})
SSB.box2.addOwnDMKey(derived_key)
SSB.box2.registerIsGroup((recp) => recp.endsWith('.groupies'))
// now we can enable box2 (seed needs to be box1)
SSB.config.box2 = { alwaysbox2: true }
// add existing group keys
const { where, type, toPullStream } = SSB.db.operators
pull(
SSB.db.query(
where(type('groupkey')),
toPullStream()
),
pull.filter(msg => {
return msg.value.content.recps && msg.value.content.recps[0] === SSB.id
}),
pull.drain((msg) => {
const { key, id } = msg.value.content
if (key)
SSB.box2.addGroupKey(id, Buffer.from(key, 'hex'))
},
() => { setupApp(SSB) }
)
)
})
}
function setupApp(SSB) {
menu.id = SSB.id
const { where, type, live, toPullStream } = SSB.db.operators
// add groups to menu
pull(
SSB.db.query(
where(type('groupkey')),
live({ old: true }),
toPullStream()
),
pull.drain((msg) => {
const { key, id } = msg.value.content
const group = {
key, id, title: 'encrypted chat'
}
menu.groups.push(group)
groupConfigChanges(SSB, crut, id, (err, record) => {
if (!err) {
const state = record.states[0]
group.title = state.title
if (state.rooms.length > 0) {
state.rooms.forEach(room => {
console.log("connecting to", room)
SSB.conn.connect(room, () => {})
})
}
}
})
})
)
// show connection errors
pull(
SSB.conn.hub().listen(),
pull.drain((ev) => {
if (ev.type.indexOf("failed") >= 0)
console.warn("Connection error: ", ev)
})
)
// update list of peers
pull(
SSB.conn.peers(),
pull.drain((entries) => {
menu.peers = entries.filter(([, x]) => !!x.key).map(([address, data]) => ({ address, data }))
})
)
pull(
SSB.conn.stagedPeers(),
pull.drain((entries) => {
menu.stagedPeers = entries.filter(([, x]) => !!x.key).map(([address, data]) => ({ address, data }))
})
)
replicateSubfeeds(true, () => {
// timeout to make sure we get all feeds replicated
setTimeout(() => {
// auto connect to room
const roomKey = '@Px7ZxMT4mtpqiNH+PHyao9+o0RdQ/nzU5qznf7WMNIE=.ed25519'
const room = 'wss:between-two-worlds.dk:443~shs:Px7ZxMT4mtpqiNH+PHyao9+o0RdQ/nzU5qznf7WMNIE='
SSB.conn.connect(room, {
key: roomKey,
type: 'room'
}, () => {})
// auto reconnect on fail
setInterval(() => {
if (menu.peers.length === 0) {
SSB.conn.connect(room, {
key: roomKey,
type: 'room'
})
}
}, 1000)
}, 1500)
})
}