corestore 6.0.1-alpha.15 → 6.0.1-alpha.18

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/index.js +7 -9
  2. package/package.json +1 -1
  3. package/test/all.js +62 -0
package/index.js CHANGED
@@ -153,10 +153,10 @@ module.exports = class Corestore extends EventEmitter {
153
153
  while (this.cores.has(id)) {
154
154
  const existing = this.cores.get(id)
155
155
  if (existing.opened && !existing.closing) return { from: existing, keyPair, auth }
156
- if (!existing.opened) {
157
- await existing.ready().catch(safetyCatch)
158
- } else if (existing.closing) {
156
+ if (existing.closing) {
159
157
  await existing.close()
158
+ } else {
159
+ await existing.ready().catch(safetyCatch)
160
160
  }
161
161
  }
162
162
 
@@ -202,7 +202,7 @@ module.exports = class Corestore extends EventEmitter {
202
202
  return { from: core, keyPair, auth }
203
203
  }
204
204
 
205
- async createKeyPair (name) {
205
+ async createKeyPair (name, namespace = this._namespace) {
206
206
  if (!this.primaryKey) await this._opening
207
207
 
208
208
  const keyPair = {
@@ -211,12 +211,12 @@ module.exports = class Corestore extends EventEmitter {
211
211
  auth: {
212
212
  sign: (msg) => sign(keyPair, msg),
213
213
  verify: (signable, signature) => {
214
- return sodium.crypto_sign_detached(signature, signable, keyPair.publicKey)
214
+ return crypto.verify(signable, signature, keyPair.publicKey)
215
215
  }
216
216
  }
217
217
  }
218
218
 
219
- const seed = deriveSeed(this.primaryKey, this._namespace, name)
219
+ const seed = deriveSeed(this.primaryKey, namespace, name)
220
220
  sodium.crypto_sign_seed_keypair(keyPair.publicKey, keyPair.secretKey, seed)
221
221
 
222
222
  return keyPair
@@ -321,9 +321,7 @@ module.exports = class Corestore extends EventEmitter {
321
321
 
322
322
  function sign (keyPair, message) {
323
323
  if (!keyPair.secretKey) throw new Error('Invalid key pair')
324
- const signature = b4a.allocUnsafe(sodium.crypto_sign_BYTES)
325
- sodium.crypto_sign_detached(signature, message, keyPair.secretKey)
326
- return signature
324
+ return crypto.sign(message, keyPair.secretKey)
327
325
  }
328
326
 
329
327
  function validateGetOptions (opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.0.1-alpha.15",
3
+ "version": "6.0.1-alpha.18",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test/all.js CHANGED
@@ -168,6 +168,30 @@ test('writable core loaded from name userData', async function (t) {
168
168
  t.alike(await core.get(1), Buffer.from('world'))
169
169
  })
170
170
 
171
+ test('writable core loaded from name and namespace userData', async function (t) {
172
+ const dir = tmpdir()
173
+
174
+ let store = new Corestore(dir)
175
+ let core = store.namespace('ns1').get({ name: 'main' })
176
+ await core.ready()
177
+ const key = core.key
178
+
179
+ t.ok(core.writable)
180
+ await core.append('hello')
181
+ t.is(core.length, 1)
182
+
183
+ await store.close()
184
+ store = new Corestore(dir)
185
+ core = store.get(key)
186
+ await core.ready()
187
+
188
+ t.ok(core.writable)
189
+ await core.append('world')
190
+ t.is(core.length, 2)
191
+ t.alike(await core.get(0), Buffer.from('hello'))
192
+ t.alike(await core.get(1), Buffer.from('world'))
193
+ })
194
+
171
195
  test('storage locking', async function (t) {
172
196
  const dir = tmpdir()
173
197
 
@@ -263,6 +287,44 @@ test('different primary keys yield different keypairs', async function (t) {
263
287
  t.unlike(kp1.publicKey, kp2.publicKey)
264
288
  })
265
289
 
290
+ test('keypair auth sign', async function (t) {
291
+ const store = new Corestore(ram)
292
+ const keyPair = await store.createKeyPair('foo')
293
+ const message = b4a.from('hello world')
294
+
295
+ const sig = keyPair.auth.sign(message)
296
+
297
+ t.is(sig.length, 64)
298
+ t.ok(crypto.verify(message, sig, keyPair.publicKey))
299
+ t.absent(crypto.verify(message, b4a.alloc(64), keyPair.publicKey))
300
+ })
301
+
302
+ test('keypair auth verify', async function (t) {
303
+ const store = new Corestore(ram)
304
+ const keyPair = await store.createKeyPair('foo')
305
+ const message = b4a.from('hello world')
306
+
307
+ const sig = crypto.sign(message, keyPair.secretKey)
308
+
309
+ t.is(sig.length, 64)
310
+ t.ok(keyPair.auth.verify(message, sig))
311
+ t.absent(keyPair.auth.verify(message, b4a.alloc(64)))
312
+ })
313
+
314
+ test('core caching after reopen regression', async function (t) {
315
+ const store = new Corestore(ram)
316
+ const core = store.get({ name: 'test-core' })
317
+ await core.ready()
318
+
319
+ core.close()
320
+ await core.opening
321
+
322
+ const core2 = store.get({ name: 'test-core' })
323
+ await core2.ready()
324
+
325
+ t.pass('did not infinite loop')
326
+ })
327
+
266
328
  function tmpdir () {
267
329
  return path.join(os.tmpdir(), 'corestore-' + Math.random().toString(16).slice(2))
268
330
  }