corestore 6.11.0 → 6.13.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 +21 -29
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -27,6 +27,7 @@ module.exports = class Corestore extends ReadyResource {
27
27
  this.cores = root ? root.cores : new Map()
28
28
  this.cache = !!opts.cache
29
29
  this.primaryKey = opts.primaryKey || null
30
+ this.passive = !!opts.passive
30
31
 
31
32
  this._keyStorage = null
32
33
  this._bootstrap = opts._bootstrap || null
@@ -168,7 +169,6 @@ module.exports = class Corestore extends ReadyResource {
168
169
  if (opts._discoveryKey) {
169
170
  return {
170
171
  keyPair: null,
171
- auth: null,
172
172
  discoveryKey: opts._discoveryKey
173
173
  }
174
174
  }
@@ -178,18 +178,16 @@ module.exports = class Corestore extends ReadyResource {
178
178
  publicKey: opts.publicKey,
179
179
  secretKey: opts.secretKey
180
180
  },
181
- sign: opts.sign,
182
- auth: opts.auth,
183
181
  discoveryKey: crypto.discoveryKey(opts.publicKey)
184
182
  }
185
183
  }
186
- const { publicKey, auth } = await this.createKeyPair(opts.name)
184
+ const { publicKey, secretKey } = await this.createKeyPair(opts.name)
185
+
187
186
  return {
188
187
  keyPair: {
189
188
  publicKey,
190
- secretKey: null
189
+ secretKey
191
190
  },
192
- auth,
193
191
  discoveryKey: crypto.discoveryKey(publicKey)
194
192
  }
195
193
  }
@@ -207,13 +205,9 @@ module.exports = class Corestore extends ReadyResource {
207
205
  if (!name) return
208
206
 
209
207
  const namespace = this._getPrereadyUserData(core, USERDATA_NAMESPACE_KEY)
210
- const { publicKey, auth } = await this.createKeyPair(b4a.toString(name), namespace)
211
- if (!b4a.equals(publicKey, core.key)) throw new Error('Stored core key does not match the provided name')
208
+ const keyPair = await this.createKeyPair(b4a.toString(name), namespace)
212
209
 
213
- // TODO: Should Hypercore expose a helper for this, or should preready return keypair/auth?
214
- core.auth = auth
215
- core.key = publicKey
216
- core.writable = true
210
+ core.setKeyPair(keyPair)
217
211
  }
218
212
 
219
213
  _getLock (id) {
@@ -292,13 +286,7 @@ module.exports = class Corestore extends ReadyResource {
292
286
 
293
287
  const keyPair = {
294
288
  publicKey: b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES),
295
- secretKey: b4a.alloc(sodium.crypto_sign_SECRETKEYBYTES),
296
- auth: {
297
- sign: (msg) => sign(keyPair, msg),
298
- verify: (signable, signature) => {
299
- return crypto.verify(signable, signature, keyPair.publicKey)
300
- }
301
- }
289
+ secretKey: b4a.alloc(sodium.crypto_sign_SECRETKEYBYTES)
302
290
  }
303
291
 
304
292
  const seed = deriveSeed(this.primaryKey, namespace, name)
@@ -363,15 +351,24 @@ module.exports = class Corestore extends ReadyResource {
363
351
  const isExternal = isStream(isInitiator) || !!(opts && opts.stream)
364
352
  const stream = Hypercore.createProtocolStream(isInitiator, {
365
353
  ...opts,
366
- ondiscoverykey: discoveryKey => {
354
+ ondiscoverykey: async discoveryKey => {
367
355
  const core = this.get({ _discoveryKey: discoveryKey })
368
- return core.ready().catch(safetyCatch)
356
+
357
+ try {
358
+ await core.ready()
359
+ } catch {
360
+ return
361
+ }
362
+
363
+ if (this.passive && !core.closing) core.replicate(stream, { session: true })
369
364
  }
370
365
  })
371
366
 
372
- for (const core of this.cores.values()) {
373
- if (!core.opened || core.closing) continue // If the core is not opened, it will be replicated in preload.
374
- core.replicate(stream, { session: true })
367
+ if (!this.passive) {
368
+ for (const core of this.cores.values()) {
369
+ if (!core.opened || core.closing) continue // If the core is not opened, it will be replicated in preload.
370
+ core.replicate(stream, { session: true })
371
+ }
375
372
  }
376
373
 
377
374
  const streamRecord = { stream, isExternal }
@@ -444,11 +441,6 @@ module.exports = class Corestore extends ReadyResource {
444
441
  }
445
442
  }
446
443
 
447
- function sign (keyPair, message) {
448
- if (!keyPair.secretKey) throw new Error('Invalid key pair')
449
- return crypto.sign(message, keyPair.secretKey)
450
- }
451
-
452
444
  function validateGetOptions (opts) {
453
445
  const key = (b4a.isBuffer(opts) || typeof opts === 'string') ? hypercoreId.decode(opts) : null
454
446
  if (key) return { key, publicKey: key }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.11.0",
3
+ "version": "6.13.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.18.2",
34
+ "hypercore": "^10.21.0",
35
35
  "hypercore-crypto": "^3.4.0",
36
36
  "hypercore-id-encoding": "^1.2.0",
37
37
  "read-write-mutexify": "^2.1.0",