forked from tetherto/lib-pear-pass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
294 lines (246 loc) · 7.08 KB
/
index.js
File metadata and controls
294 lines (246 loc) · 7.08 KB
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
// the js module powering the mobile and desktop app
const Autobase = require('autobase')
const BlindPairing = require('blind-pairing')
const Hyperbee = require('hyperbee')
const Hyperswarm = require('hyperswarm')
const ReadyResource = require('ready-resource')
const z32 = require('z32')
const b4a = require('b4a')
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 // we null this when passing it on, so avoid a nullptr
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.onresolve(this.pass)
this.candidate.close().catch(noop)
}
})
}
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.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
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,
valueEncoding: 'json',
open (store) {
return new Hyperbee(store.get('view'), {
extension: false,
keyEncoding: 'utf-8',
valueEncoding: 'json'
})
},
// New data blocks will be added using the apply function
async apply (nodes, view, base) {
for (const node of nodes) {
const op = node.value
// Add support for adding other peers as a writer to the base
if (op.type === 'addWriter') {
await base.addWriter(z32.decode(op.key))
} else if (op.type === 'removeWriter') {
await base.removeWriter(z32.decode(op.key))
} else if (op.type === 'addRecord') {
// This adds a new record
await view.put(op.key, op.value)
} else if (op.type === 'removeRecord') {
// Remove an existing record
await view.del(op.key)
}
}
}
})
this.base.on('update', () => this.emit('update'))
}
async _open () {
await this.base.ready()
if (this.replicate) await this._replicate()
}
// Close the base
async _close () {
if (this.swarm) {
await this.member.close()
await this.pairing.close()
await this.swarm.destroy()
}
await this.base.close()
}
// Need this key to become a writer
get writerKey () {
return this.base.local.key
}
// Return bootstrap key of the base
// This is what other peers should use to bootstrap the base from
get key () {
return this.base.key
}
// Find peers in Hyperswarm using this
get discoveryKey () {
return this.base.discoveryKey
}
// Encryption key for the base
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.get('autopass/invite')
if (existing) return existing.invite
const { id, invite, publicKey, expires } = BlindPairing.createInvite(this.base.key)
const record = { id: z32.encode(id), invite: z32.encode(invite), publicKey: z32.encode(publicKey), expires }
await this.add('autopass/invite', record)
return record.invite
}
// Get data of all indexes in the base
list (opts) {
return this.base.view.createReadStream(opts)
}
// Get data stored in a specific key
async get (key) {
const node = await this.base.view.get(key)
if (node === null) return null
return node.value
}
// Add a peer as a writer
async addWriter (key) {
await this.base.append({
type: 'addWriter',
key: b4a.isBuffer(key) ? z32.encode(key) : key
})
return true
}
// To later add removeWriter
async removeWriter (key) {
await this.base.append({
type: 'removeWriter',
key: b4a.isBuffer(key) ? z32.encode(key) : key
})
}
// Check if the base is writable
get writable () {
return this.base.writable
}
// Start Replicating the base across peers
async _replicate () {
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 = z32.encode(candidate.inviteId)
const inv = await this.get('autopass/invite')
if (inv.id !== id) return
candidate.open(z32.decode(inv.publicKey))
await this.addWriter(candidate.userData)
candidate.confirm({
key: this.base.key,
encryptionKey: this.base.encryptionKey
})
}
})
this.swarm.join(this.base.discoveryKey)
}
// Append a key/value to the base
async add (key, value) {
await this.base.append({
type: 'addRecord',
key,
value
})
}
// Remove a key pair
async remove (key) {
await this.base.append({
type: 'removeRecord',
key,
value: null
})
}
} // end class
function noop () {}
module.exports = Autopass