corestore 6.14.1 → 6.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +44 -26
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -168,26 +168,45 @@ module.exports = class Corestore extends ReadyResource {
168
168
  async _generateKeys (opts) {
169
169
  if (opts._discoveryKey) {
170
170
  return {
171
+ manifest: null,
171
172
  keyPair: null,
173
+ key: null,
172
174
  discoveryKey: opts._discoveryKey
173
175
  }
174
176
  }
175
- if (!opts.name) {
177
+
178
+ const keyPair = opts.name
179
+ ? await this.createKeyPair(opts.name)
180
+ : (opts.secretKey)
181
+ ? { secretKey: opts.secretKey, publicKey: opts.publicKey }
182
+ : null
183
+
184
+ if (opts.manifest) {
185
+ const key = Hypercore.key(opts.manifest)
186
+
176
187
  return {
177
- keyPair: {
178
- publicKey: opts.publicKey,
179
- secretKey: opts.secretKey
180
- },
181
- discoveryKey: crypto.discoveryKey(opts.publicKey)
188
+ manifest: opts.manifest,
189
+ keyPair,
190
+ key,
191
+ discoveryKey: crypto.discoveryKey(key)
182
192
  }
183
193
  }
184
- const { publicKey, secretKey } = await this.createKeyPair(opts.name)
194
+
195
+ if (opts.key) {
196
+ return {
197
+ manifest: null,
198
+ keyPair,
199
+ key: opts.key,
200
+ discoveryKey: crypto.discoveryKey(opts.key)
201
+ }
202
+ }
203
+
204
+ const publicKey = opts.publicKey || keyPair.publicKey
185
205
 
186
206
  return {
187
- keyPair: {
188
- publicKey,
189
- secretKey
190
- },
207
+ manifest: null,
208
+ keyPair,
209
+ key: publicKey,
191
210
  discoveryKey: crypto.discoveryKey(publicKey)
192
211
  }
193
212
  }
@@ -221,11 +240,11 @@ module.exports = class Corestore extends ReadyResource {
221
240
  }
222
241
 
223
242
  async _preload (id, keys, opts) {
224
- const { keyPair, auth } = keys
243
+ const { manifest, keyPair, key } = keys
225
244
 
226
245
  while (this.cores.has(id)) {
227
246
  const existing = this.cores.get(id)
228
- if (existing.opened && !existing.closing) return { from: existing, keyPair, auth }
247
+ if (existing.opened && !existing.closing) return { from: existing, keyPair, manifest }
229
248
  if (existing.closing) {
230
249
  await existing.close()
231
250
  } else {
@@ -233,6 +252,7 @@ module.exports = class Corestore extends ReadyResource {
233
252
  }
234
253
  }
235
254
 
255
+ const hasKeyPair = !!(keyPair && keyPair.secretKey)
236
256
  const userData = {}
237
257
  if (opts.name) {
238
258
  userData[USERDATA_NAME_KEY] = b4a.from(opts.name)
@@ -247,22 +267,19 @@ module.exports = class Corestore extends ReadyResource {
247
267
  autoClose: true,
248
268
  encryptionKey: opts.encryptionKey || null,
249
269
  userData,
250
- auth,
270
+ manifest,
271
+ key,
272
+ compat: opts.compat,
251
273
  cache: opts.cache,
252
274
  createIfMissing: opts.createIfMissing === false ? false : !opts._discoveryKey,
253
- keyPair: keyPair && keyPair.publicKey
254
- ? {
255
- publicKey: keyPair.publicKey,
256
- secretKey: null
257
- }
258
- : null
275
+ keyPair: hasKeyPair ? keyPair : null
259
276
  })
260
277
 
261
278
  if (this._root.closing) throw new Error('The corestore is closed')
262
279
  this.cores.set(id, core)
263
280
  core.ready().then(() => {
264
281
  if (core.closing) return // extra safety here as ready is a tick after open
265
- if (keyPair && keyPair.secretKey) core.setKeyPair(keyPair)
282
+ if (hasKeyPair) core.setKeyPair(keyPair)
266
283
  this._emitCore('core-open', core)
267
284
  for (const { stream } of this._replicationStreams) {
268
285
  core.replicate(stream, { session: true })
@@ -278,7 +295,7 @@ module.exports = class Corestore extends ReadyResource {
278
295
  this.emit('conflict', core, len, fork, proof)
279
296
  })
280
297
 
281
- return { from: core, keyPair, auth }
298
+ return { from: core, keyPair, manifest }
282
299
  }
283
300
 
284
301
  async createKeyPair (name, namespace = this._namespace) {
@@ -318,7 +335,7 @@ module.exports = class Corestore extends ReadyResource {
318
335
  const keys = await this._generateKeys(opts)
319
336
 
320
337
  id = b4a.toString(keys.discoveryKey, 'hex')
321
- rw = (opts.exclusive && opts.writable !== false && keys.keyPair) ? this._getLock(id) : null
338
+ rw = (opts.exclusive && opts.writable !== false) ? this._getLock(id) : null
322
339
 
323
340
  if (rw) await rw.write.lock()
324
341
  return await this._preload(id, keys, opts)
@@ -452,20 +469,21 @@ module.exports = class Corestore extends ReadyResource {
452
469
 
453
470
  function validateGetOptions (opts) {
454
471
  const key = (b4a.isBuffer(opts) || typeof opts === 'string') ? hypercoreId.decode(opts) : null
455
- if (key) return { key, publicKey: key }
472
+ if (key) return { key }
456
473
 
457
474
  if (opts.key) {
458
- opts.key = opts.publicKey = hypercoreId.decode(opts.key)
475
+ opts.key = hypercoreId.decode(opts.key)
459
476
  }
460
477
  if (opts.keyPair) {
461
478
  opts.publicKey = opts.keyPair.publicKey
462
479
  opts.secretKey = opts.keyPair.secretKey
463
480
  }
481
+
464
482
  if (opts.name && typeof opts.name !== 'string') throw new Error('name option must be a String')
465
483
  if (opts.name && opts.secretKey) throw new Error('Cannot provide both a name and a secret key')
466
484
  if (opts.publicKey && !b4a.isBuffer(opts.publicKey)) throw new Error('publicKey option must be a Buffer or Uint8Array')
467
485
  if (opts.secretKey && !b4a.isBuffer(opts.secretKey)) throw new Error('secretKey option must be a Buffer or Uint8Array')
468
- if (!opts._discoveryKey && (!opts.name && !opts.publicKey)) throw new Error('Must provide either a name or a publicKey')
486
+ if (!opts._discoveryKey && (!opts.name && !opts.publicKey && !opts.manifest && !opts.key)) throw new Error('Must provide either a name or a publicKey')
469
487
  return opts
470
488
  }
471
489
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.14.1",
3
+ "version": "6.15.0",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "b4a": "^1.6.4",
34
- "hypercore": "^10.24.1",
34
+ "hypercore": "^10.24.2",
35
35
  "hypercore-crypto": "^3.4.0",
36
36
  "hypercore-id-encoding": "^1.2.0",
37
37
  "read-write-mutexify": "^2.1.0",