corestore 6.14.1 → 6.15.1

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 +56 -26
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -168,26 +168,57 @@ 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
+
187
+ return {
188
+ manifest: opts.manifest,
189
+ keyPair,
190
+ key,
191
+ discoveryKey: crypto.discoveryKey(key)
192
+ }
193
+ }
194
+
195
+ if (opts.key) {
176
196
  return {
177
- keyPair: {
178
- publicKey: opts.publicKey,
179
- secretKey: opts.secretKey
180
- },
181
- discoveryKey: crypto.discoveryKey(opts.publicKey)
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
205
+
206
+ if (opts.compat === false) {
207
+ const manifest = { signer: { publicKey } } // default manifest
208
+ const key = Hypercore.key(manifest)
209
+
210
+ return {
211
+ manifest,
212
+ keyPair,
213
+ key,
214
+ discoveryKey: crypto.discoveryKey(key)
182
215
  }
183
216
  }
184
- const { publicKey, secretKey } = await this.createKeyPair(opts.name)
185
217
 
186
218
  return {
187
- keyPair: {
188
- publicKey,
189
- secretKey
190
- },
219
+ manifest: null,
220
+ keyPair,
221
+ key: publicKey,
191
222
  discoveryKey: crypto.discoveryKey(publicKey)
192
223
  }
193
224
  }
@@ -221,11 +252,11 @@ module.exports = class Corestore extends ReadyResource {
221
252
  }
222
253
 
223
254
  async _preload (id, keys, opts) {
224
- const { keyPair, auth } = keys
255
+ const { manifest, keyPair, key } = keys
225
256
 
226
257
  while (this.cores.has(id)) {
227
258
  const existing = this.cores.get(id)
228
- if (existing.opened && !existing.closing) return { from: existing, keyPair, auth }
259
+ if (existing.opened && !existing.closing) return { from: existing, keyPair, manifest }
229
260
  if (existing.closing) {
230
261
  await existing.close()
231
262
  } else {
@@ -233,6 +264,7 @@ module.exports = class Corestore extends ReadyResource {
233
264
  }
234
265
  }
235
266
 
267
+ const hasKeyPair = !!(keyPair && keyPair.secretKey)
236
268
  const userData = {}
237
269
  if (opts.name) {
238
270
  userData[USERDATA_NAME_KEY] = b4a.from(opts.name)
@@ -247,22 +279,19 @@ module.exports = class Corestore extends ReadyResource {
247
279
  autoClose: true,
248
280
  encryptionKey: opts.encryptionKey || null,
249
281
  userData,
250
- auth,
282
+ manifest,
283
+ key,
284
+ compat: opts.compat,
251
285
  cache: opts.cache,
252
286
  createIfMissing: opts.createIfMissing === false ? false : !opts._discoveryKey,
253
- keyPair: keyPair && keyPair.publicKey
254
- ? {
255
- publicKey: keyPair.publicKey,
256
- secretKey: null
257
- }
258
- : null
287
+ keyPair: hasKeyPair ? keyPair : null
259
288
  })
260
289
 
261
290
  if (this._root.closing) throw new Error('The corestore is closed')
262
291
  this.cores.set(id, core)
263
292
  core.ready().then(() => {
264
293
  if (core.closing) return // extra safety here as ready is a tick after open
265
- if (keyPair && keyPair.secretKey) core.setKeyPair(keyPair)
294
+ if (hasKeyPair) core.setKeyPair(keyPair)
266
295
  this._emitCore('core-open', core)
267
296
  for (const { stream } of this._replicationStreams) {
268
297
  core.replicate(stream, { session: true })
@@ -278,7 +307,7 @@ module.exports = class Corestore extends ReadyResource {
278
307
  this.emit('conflict', core, len, fork, proof)
279
308
  })
280
309
 
281
- return { from: core, keyPair, auth }
310
+ return { from: core, keyPair, manifest }
282
311
  }
283
312
 
284
313
  async createKeyPair (name, namespace = this._namespace) {
@@ -318,7 +347,7 @@ module.exports = class Corestore extends ReadyResource {
318
347
  const keys = await this._generateKeys(opts)
319
348
 
320
349
  id = b4a.toString(keys.discoveryKey, 'hex')
321
- rw = (opts.exclusive && opts.writable !== false && keys.keyPair) ? this._getLock(id) : null
350
+ rw = (opts.exclusive && opts.writable !== false) ? this._getLock(id) : null
322
351
 
323
352
  if (rw) await rw.write.lock()
324
353
  return await this._preload(id, keys, opts)
@@ -452,20 +481,21 @@ module.exports = class Corestore extends ReadyResource {
452
481
 
453
482
  function validateGetOptions (opts) {
454
483
  const key = (b4a.isBuffer(opts) || typeof opts === 'string') ? hypercoreId.decode(opts) : null
455
- if (key) return { key, publicKey: key }
484
+ if (key) return { key }
456
485
 
457
486
  if (opts.key) {
458
- opts.key = opts.publicKey = hypercoreId.decode(opts.key)
487
+ opts.key = hypercoreId.decode(opts.key)
459
488
  }
460
489
  if (opts.keyPair) {
461
490
  opts.publicKey = opts.keyPair.publicKey
462
491
  opts.secretKey = opts.keyPair.secretKey
463
492
  }
493
+
464
494
  if (opts.name && typeof opts.name !== 'string') throw new Error('name option must be a String')
465
495
  if (opts.name && opts.secretKey) throw new Error('Cannot provide both a name and a secret key')
466
496
  if (opts.publicKey && !b4a.isBuffer(opts.publicKey)) throw new Error('publicKey option must be a Buffer or Uint8Array')
467
497
  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')
498
+ if (!opts._discoveryKey && (!opts.name && !opts.publicKey && !opts.manifest && !opts.key)) throw new Error('Must provide either a name or a publicKey')
469
499
  return opts
470
500
  }
471
501
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.14.1",
3
+ "version": "6.15.1",
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",