-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
285 lines (244 loc) · 7.17 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
// the js module powering the mobile and desktop app
const Autobase = require('autobase')
const BlindPairing = require('blind-pairing')
const HyperDB = require('hyperdb')
const Hyperswarm = require('hyperswarm')
const ReadyResource = require('ready-resource')
const z32 = require('z32')
const b4a = require('b4a')
const { Router, dispatch } = require('./spec/hyperdispatch')
const db = require('./spec/db/index.js')
class AutopassPairer extends ReadyResource {
constructor (store, invite, opts = {}) {
super()
this.store = store
this.invite = invite
this.swarm = null
this.pairing = null
this.candidate = null
this.bootstrap = opts.bootstrap || null
this.onresolve = null
this.onreject = null
this.pass = null
this.ready().catch(noop)
}
async _open () {
await this.store.ready()
this.swarm = new Hyperswarm({
keyPair: await this.store.createKeyPair('hyperswarm'),
bootstrap: this.bootstrap
})
const store = this.store
this.swarm.on('connection', (connection, peerInfo) => {
store.replicate(connection)
})
this.pairing = new BlindPairing(this.swarm)
const core = Autobase.getLocalCore(this.store)
await core.ready()
const key = core.key
await core.close()
this.candidate = this.pairing.addCandidate({
invite: z32.decode(this.invite),
userData: key,
onadd: async (result) => {
if (this.pass === null) {
this.pass = new Autopass(this.store, {
swarm: this.swarm,
key: result.key,
encryptionKey: result.encryptionKey,
bootstrap: this.bootstrap
})
}
this.swarm = null
this.store = null
if (this.onresolve) this._whenWritable()
this.candidate.close().catch(noop)
}
})
}
_whenWritable () {
if (this.pass.base.writable) return
const check = () => {
if (this.pass.base.writable) {
this.pass.base.off('update', check)
this.onresolve(this.pass)
}
}
this.pass.base.on('update', check)
}
async _close () {
if (this.candidate !== null) {
await this.candidate.close()
}
if (this.swarm !== null) {
await this.swarm.destroy()
}
if (this.store !== null) {
await this.store.close()
}
if (this.onreject) {
this.onreject(new Error('Pairing closed'))
} else if (this.base) {
await this.base.close()
}
}
finished () {
return new Promise((resolve, reject) => {
this.onresolve = resolve
this.onreject = reject
})
}
}
class Autopass extends ReadyResource {
constructor (corestore, opts = {}) {
super()
this.router = new Router()
this.store = corestore
this.swarm = opts.swarm || null
this.base = null
this.bootstrap = opts.bootstrap || null
this.member = null
this.pairing = null
this.replicate = opts.replicate !== false
this.debug = !!opts.key
// Register handlers for commands
this.router.add('@autopass/remove-writer', async (data, context) => {
await context.base.removeWriter(data.key)
})
this.router.add('@autopass/add-writer', async (data, context) => {
await context.base.addWriter(data.key)
})
this.router.add('@autopass/put', async (data, context) => {
await context.view.insert('@autopass/records', data)
})
this.router.add('@autopass/del', async (data, context) => {
await context.view.delete('@autopass/records', { key: data.key })
})
this.router.add('@autopass/add-invite', async (data, context) => {
await context.view.insert('@autopass/invite', data)
})
this._boot(opts)
this.ready().catch(noop)
}
// Initialize autobase
_boot (opts = {}) {
const { encryptionKey, key } = opts
this.base = new Autobase(this.store, key, {
encrypt: true,
encryptionKey,
open (store) {
return HyperDB.bee(store.get('view'), db, {
extension: false,
autoUpdate: true
})
},
// New data blocks will be added using the apply function
apply: this._apply.bind(this)
})
this.base.on('update', () => {
if (!this.base._interrupting) this.emit('update')
})
}
async _apply (nodes, view, base) {
for (const node of nodes) {
await this.router.dispatch(node.value, { view, base })
}
await view.flush()
}
async _open () {
await this.base.ready()
if (this.replicate) await this._replicate()
}
async _close () {
if (this.swarm) {
await this.member.close()
await this.pairing.close()
await this.swarm.destroy()
}
await this.base.close()
}
get writerKey () {
return this.base.local.key
}
get key () {
return this.base.key
}
get discoveryKey () {
return this.base.discoveryKey
}
get encryptionKey () {
return this.base.encryptionKey
}
static pair (store, invite, opts) {
return new AutopassPairer(store, invite, opts)
}
async createInvite (opts) {
if (this.opened === false) await this.ready()
const existing = await this.base.view.findOne('@autopass/invite', {})
if (existing) {
return z32.encode(existing.invite)
}
const { id, invite, publicKey, expires } = BlindPairing.createInvite(this.base.key)
const record = { id, invite, publicKey, expires }
await this.base.append(dispatch('@autopass/add-invite', record))
return z32.encode(record.invite)
}
list (opts) {
return this.base.view.find('@autopass/records', {})
}
async get (key) {
const data = await this.base.view.get('@autopass/records', { key })
if (data === null) {
return null
}
return data.value
}
async addWriter (key) {
await this.base.append(dispatch('@autopass/add-writer', { key: b4a.isBuffer(key) ? key : b4a.from(key) }))
return true
}
async removeWriter (key) {
await this.base.append(dispatch('@autopass/remove-writer', { key: b4a.isBuffer(key) ? key : b4a.from(key) }))
}
get writable () {
return this.base.writable
}
async _replicate () {
await this.base.ready()
if (this.swarm === null) {
this.swarm = new Hyperswarm({
keyPair: await this.store.createKeyPair('hyperswarm'),
bootstrap: this.bootstrap
})
this.swarm.on('connection', (connection, peerInfo) => {
this.store.replicate(connection)
})
}
this.pairing = new BlindPairing(this.swarm)
this.member = this.pairing.addMember({
discoveryKey: this.base.discoveryKey,
onadd: async (candidate) => {
const id = candidate.inviteId
const inv = await this.base.view.findOne('@autopass/invite', {})
if (!b4a.equals(inv.id, id)) {
return
}
candidate.open(inv.publicKey)
await this.addWriter(candidate.userData)
candidate.confirm({
key: this.base.key,
encryptionKey: this.base.encryptionKey
})
}
})
this.swarm.join(this.base.discoveryKey)
}
async add (key, value) {
await this.base.append(dispatch('@autopass/put', { key, value }))
}
async remove (key) {
await this.base.append(dispatch('@autopass/del', { key }))
}
} // end class
function noop () {}
module.exports = Autopass