This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.js
244 lines (224 loc) · 6.88 KB
/
messages.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
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen
//
// SPDX-License-Identifier: LGPL-3.0-only
const crypto = require('crypto')
const ssbKeys = require('ssb-keys')
const bb = require('ssb-bendy-butt')
const {
where,
author,
and,
or,
type,
toCallback,
} = require('ssb-db2/operators')
const keys = require('./keys')
// FIXME: define and use json schema
/**
* Low level API for generating messages
*/
exports.init = function init(sbot) {
function add(feedpurpose, nonce, previousMsg, subKeys, mfKeys, metadata) {
const content = nonce
? {
type: 'metafeed/add/derived',
feedpurpose,
subfeed: subKeys.id,
metafeed: mfKeys.id,
nonce,
tangles: {
metafeed: { root: null, previous: null },
},
}
: {
type: 'metafeed/add/existing',
feedpurpose,
subfeed: subKeys.id,
metafeed: mfKeys.id,
tangles: {
metafeed: { root: null, previous: null },
},
}
if (metadata) Object.assign(content, metadata)
const sequence = previousMsg ? previousMsg.value.sequence + 1 : 1
const previous = previousMsg ? previousMsg.key : null
const timestamp = Date.now()
if (content.recps && !sbot.box2)
throw new Error('Not able to encrypt without ssb-db2-box2 module loaded')
const bbmsg = bb.encodeNew(
content,
subKeys,
mfKeys,
sequence,
previous,
timestamp,
null,
content.recps ? sbot.box2.encryptBendyButt : null
)
const msgVal = bb.decode(bbmsg)
return msgVal
}
function getNonce() {
return crypto.randomBytes(32)
}
return {
/**
* Generate a message linking an existing feed to a meta feed. `previous`
* is the previous message on the meta feed in KVT form. `metadata` is an
* optional object to be included (object spread) in `msg.value.content`.
*
* ```js
* const msg = sbot.metafeeds.messages.getMsgValAddExisting(metafeedKeys, null, 'main', mainKeys)
* ```
*/
getMsgValAddExisting(mfKeys, previous, feedpurpose, feedKeys, metadata) {
return add(feedpurpose, undefined, previous, feedKeys, mfKeys, metadata)
},
/**
* Generate a message to be posted on meta feed linking feed to a meta feed.
* Similar to `deriveFeedKeyFromSeed`, `feedformat` can be either
* `bendybutt-v1` for a meta feed or `classic`. `metadata` is an optional
* object to be included (object spread) in `msg.value.content`.
*
* ```js
* const msg = sbot.metafeeds.messages.getMsgValAddDerived(metafeedKeys, null, 'main', seed, 'classic')
* ```
*/
getMsgValAddDerived(
mfKeys,
previous,
feedpurpose,
seed,
feedformat,
metadata
) {
if (feedformat !== 'classic' && feedformat !== 'bendybutt-v1') {
throw new Error('Unknown feed format: ' + feedformat)
}
const nonce = getNonce()
const feedKeys = keys.deriveFeedKeyFromSeed(
seed,
nonce.toString('base64'),
feedformat
)
return add(feedpurpose, nonce, previous, feedKeys, mfKeys, metadata)
},
/**
* Generate a message to be posted on meta feed tombstoning a feed on a meta
* feed. `Previous` is the previous message on the meta feed in KVT form.
*
* ```js
* const previous = {
* key: '%vv/XLo8lYgFjX9sM44I5F6la2FAp6iREuZ0AVJFp0pU=.bbmsg-v1',
* value: {
* previous: '%jv9hs2es5Pkw85vSOmLvzQh4HtosbCrVjhT+fR6GPr4=.bbmsg-v1',
* // ...
* }
* }
*
* sbot.metafeeds.messages.getMsgValTombstone(metafeedKeys, previous, mainKeys, 'No longer used', (err, tombstoneMsg) => {
* sbot.db.add(tombstoneMsg, (err) => {
* console.log("main is now tombstoned on meta feed")
* })
* })
* ```
*/
getMsgValTombstone(mfKeys, previousMsg, feedKeys, reason, cb) {
sbot.db.query(
where(
and(
author(mfKeys.id),
or(type('metafeed/add/derived'), type('metafeed/add/existing'))
)
),
toCallback((err, msgs) => {
if (err) return cb(err)
msgs = msgs.filter((x) => x.value.content.subfeed === feedKeys.id)
if (msgs.length === 0) {
return cb(new Error('no add message found on meta feed'))
}
const content = {
type: 'metafeed/tombstone',
subfeed: feedKeys.id,
metafeed: mfKeys.id,
reason,
tangles: {
metafeed: {
root: msgs[0].key,
previous: msgs[msgs.length - 1].key,
},
},
}
const sequence = previousMsg ? previousMsg.value.sequence + 1 : 1
const previous = previousMsg ? previousMsg.key : null
const timestamp = Date.now()
const bbmsg = bb.encodeNew(
content,
feedKeys,
mfKeys,
sequence,
previous,
timestamp
)
const msgVal = bb.decode(bbmsg)
cb(null, msgVal)
})
)
},
/**
* Generate the content of a message to be published on a main feed linking
* it to a meta feed.
*
* ```js
* sbot.metafeeds.messages.getContentAnnounce(metafeedKeys, (err, content) => {
* sbot.db.publish(content, (err) => {
* console.log("main feed is now linked to meta feed")
* })
* })
* ```
*/
getContentAnnounce(metafeedKeys, cb) {
sbot.db.query(
where(and(author(sbot.id), type('metafeed/announce'))),
toCallback((err, msgs) => {
if (err) return cb(err)
const rootAnnounceId = msgs.length > 0 ? msgs[0].key : null
const previousAnnounceId =
msgs.length > 0 ? msgs[msgs.length - 1].key : null
const content = {
type: 'metafeed/announce',
metafeed: metafeedKeys.id,
subfeed: sbot.id,
tangles: {
metafeed: {
root: rootAnnounceId,
previous: previousAnnounceId,
},
},
}
const signedContent = ssbKeys.signObj(metafeedKeys, content)
cb(null, signedContent)
})
)
},
/**
* Generate the content of a message to save your seed value as a private
* message on a main feed.
*
* ```js
* const seedContent = sbot.metafeeds.messages.getContentSeed(metafeedKeys.id, sbot.id, seed)
* sbot.db.publish(seedContent, (err) => {
* console.log("seed has now been saved, all feed keys generated from this can be restored from the seed")
* })
* ```
*/
getContentSeed(metafeedId, mainfeedId, seed) {
return {
type: 'metafeed/seed',
metafeed: metafeedId,
seed: seed.toString('hex'),
recps: [mainfeedId],
}
},
}
}