corestore 6.14.0 → 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 (3) hide show
  1. package/LICENSE +21 -0
  2. package/index.js +44 -25
  3. package/package.json +2 -2
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Holepunch Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
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,21 +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
282
+ if (hasKeyPair) core.setKeyPair(keyPair)
265
283
  this._emitCore('core-open', core)
266
284
  for (const { stream } of this._replicationStreams) {
267
285
  core.replicate(stream, { session: true })
@@ -277,7 +295,7 @@ module.exports = class Corestore extends ReadyResource {
277
295
  this.emit('conflict', core, len, fork, proof)
278
296
  })
279
297
 
280
- return { from: core, keyPair, auth }
298
+ return { from: core, keyPair, manifest }
281
299
  }
282
300
 
283
301
  async createKeyPair (name, namespace = this._namespace) {
@@ -317,7 +335,7 @@ module.exports = class Corestore extends ReadyResource {
317
335
  const keys = await this._generateKeys(opts)
318
336
 
319
337
  id = b4a.toString(keys.discoveryKey, 'hex')
320
- rw = (opts.exclusive && opts.writable !== false && keys.keyPair) ? this._getLock(id) : null
338
+ rw = (opts.exclusive && opts.writable !== false) ? this._getLock(id) : null
321
339
 
322
340
  if (rw) await rw.write.lock()
323
341
  return await this._preload(id, keys, opts)
@@ -451,20 +469,21 @@ module.exports = class Corestore extends ReadyResource {
451
469
 
452
470
  function validateGetOptions (opts) {
453
471
  const key = (b4a.isBuffer(opts) || typeof opts === 'string') ? hypercoreId.decode(opts) : null
454
- if (key) return { key, publicKey: key }
472
+ if (key) return { key }
455
473
 
456
474
  if (opts.key) {
457
- opts.key = opts.publicKey = hypercoreId.decode(opts.key)
475
+ opts.key = hypercoreId.decode(opts.key)
458
476
  }
459
477
  if (opts.keyPair) {
460
478
  opts.publicKey = opts.keyPair.publicKey
461
479
  opts.secretKey = opts.keyPair.secretKey
462
480
  }
481
+
463
482
  if (opts.name && typeof opts.name !== 'string') throw new Error('name option must be a String')
464
483
  if (opts.name && opts.secretKey) throw new Error('Cannot provide both a name and a secret key')
465
484
  if (opts.publicKey && !b4a.isBuffer(opts.publicKey)) throw new Error('publicKey option must be a Buffer or Uint8Array')
466
485
  if (opts.secretKey && !b4a.isBuffer(opts.secretKey)) throw new Error('secretKey option must be a Buffer or Uint8Array')
467
- 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')
468
487
  return opts
469
488
  }
470
489
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.14.0",
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.22.2",
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",