corestore 6.0.1-alpha.10 → 6.0.1-alpha.13

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.
@@ -1,23 +1,22 @@
1
- name: Test on Node.js
2
-
1
+ name: Build Status
3
2
  on:
4
3
  push:
5
4
  branches:
6
- - main
5
+ - master
7
6
  pull_request:
8
7
  branches:
9
- - main
8
+ - master
10
9
  jobs:
11
10
  build:
12
11
  strategy:
13
12
  matrix:
14
- node-version: [12.x, 14.x, 16.x]
13
+ node-version: [lts/*]
15
14
  os: [ubuntu-latest, macos-latest, windows-latest]
16
15
  runs-on: ${{ matrix.os }}
17
16
  steps:
18
17
  - uses: actions/checkout@v2
19
18
  - name: Use Node.js ${{ matrix.node-version }}
20
- uses: actions/setup-node@v1
19
+ uses: actions/setup-node@v2
21
20
  with:
22
21
  node-version: ${{ matrix.node-version }}
23
22
  - run: npm install
package/index.js CHANGED
@@ -43,7 +43,7 @@ module.exports = class Corestore extends EventEmitter {
43
43
  if (opts._discoveryKey) {
44
44
  return {
45
45
  keyPair: null,
46
- sign: null,
46
+ auth: null,
47
47
  discoveryKey: opts._discoveryKey
48
48
  }
49
49
  }
@@ -54,16 +54,17 @@ module.exports = class Corestore extends EventEmitter {
54
54
  secretKey: opts.secretKey
55
55
  },
56
56
  sign: opts.sign,
57
+ auth: opts.auth,
57
58
  discoveryKey: crypto.discoveryKey(opts.publicKey)
58
59
  }
59
60
  }
60
- const { publicKey, sign } = await this.keys.createHypercoreKeyPair(opts.name, this._namespace)
61
+ const { publicKey, auth } = await this.keys.createHypercoreKeyPair(opts.name, this._namespace)
61
62
  return {
62
63
  keyPair: {
63
64
  publicKey,
64
65
  secretKey: null
65
66
  },
66
- sign,
67
+ auth,
67
68
  discoveryKey: crypto.discoveryKey(publicKey)
68
69
  }
69
70
  }
@@ -80,11 +81,11 @@ module.exports = class Corestore extends EventEmitter {
80
81
  if (!name) return
81
82
 
82
83
  const namespace = this._getPrereadyUserData(core, USERDATA_NAMESPACE_KEY)
83
- const { publicKey, sign } = await this.keys.createHypercoreKeyPair(b4a.toString(name), namespace)
84
- if (!publicKey.equals(core.key)) throw new Error('Stored core key does not match the provided name')
84
+ const { publicKey, auth } = await this.keys.createHypercoreKeyPair(b4a.toString(name), namespace)
85
+ if (!b4a.equals(publicKey, core.key)) throw new Error('Stored core key does not match the provided name')
85
86
 
86
- // TODO: Should Hypercore expose a helper for this, or should preready return keypair/sign?
87
- core.sign = sign
87
+ // TODO: Should Hypercore expose a helper for this, or should preready return keypair/auth?
88
+ core.auth = auth
88
89
  core.key = publicKey
89
90
  core.writable = true
90
91
  }
@@ -92,12 +93,12 @@ module.exports = class Corestore extends EventEmitter {
92
93
  async _preload (opts) {
93
94
  await this.ready()
94
95
 
95
- const { discoveryKey, keyPair, sign } = await this._generateKeys(opts)
96
+ const { discoveryKey, keyPair, auth } = await this._generateKeys(opts)
96
97
  const id = b4a.toString(discoveryKey, 'hex')
97
98
 
98
99
  while (this.cores.has(id)) {
99
100
  const existing = this.cores.get(id)
100
- if (existing.opened && !existing.closing) return { from: existing, keyPair, sign }
101
+ if (existing.opened && !existing.closing) return { from: existing, keyPair, auth }
101
102
  if (!existing.opened) {
102
103
  await existing.ready().catch(safetyCatch)
103
104
  } else if (existing.closing) {
@@ -119,7 +120,7 @@ module.exports = class Corestore extends EventEmitter {
119
120
  autoClose: true,
120
121
  encryptionKey: opts.encryptionKey || null,
121
122
  userData,
122
- sign: null,
123
+ auth,
123
124
  createIfMissing: !opts._discoveryKey,
124
125
  keyPair: keyPair && keyPair.publicKey
125
126
  ? {
@@ -144,7 +145,7 @@ module.exports = class Corestore extends EventEmitter {
144
145
  this.cores.delete(id)
145
146
  })
146
147
 
147
- return { from: core, keyPair, sign }
148
+ return { from: core, keyPair, auth }
148
149
  }
149
150
 
150
151
  get (opts = {}) {
@@ -201,7 +202,7 @@ module.exports = class Corestore extends EventEmitter {
201
202
 
202
203
  async _close () {
203
204
  await this._opening
204
- if (!this._namespace.equals(DEFAULT_NAMESPACE)) return // namespaces should not release resources on close
205
+ if (!b4a.equals(this._namespace, DEFAULT_NAMESPACE)) return // namespaces should not release resources on close
205
206
  const closePromises = []
206
207
  for (const core of this.cores.values()) {
207
208
  closePromises.push(core.close())
package/lib/keys.js CHANGED
@@ -28,7 +28,12 @@ module.exports = class KeyManager {
28
28
  const keyPair = {
29
29
  publicKey: b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES),
30
30
  _secretKey: b4a.alloc(sodium.crypto_sign_SECRETKEYBYTES),
31
- sign: (msg) => this._sign(keyPair, msg)
31
+ auth: {
32
+ sign: (msg) => this._sign(keyPair, msg),
33
+ verify: (signable, signature) => {
34
+ return sodium.crypto_sign_detached(signature, signable, keyPair.publicKey)
35
+ }
36
+ }
32
37
  }
33
38
 
34
39
  sodium.crypto_sign_seed_keypair(keyPair.publicKey, keyPair._secretKey, this.createSecret(name, token))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.0.1-alpha.10",
3
+ "version": "6.0.1-alpha.13",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,16 +22,15 @@
22
22
  "devDependencies": {
23
23
  "brittle": "^1.6.0",
24
24
  "random-access-file": "^2.2.0",
25
- "random-access-memory": "^3.1.2",
26
- "standardx": "^7.0.0",
27
- "tmp-promise": "^3.0.2"
25
+ "random-access-memory": "^4.0.0",
26
+ "standardx": "^7.0.0"
28
27
  },
29
28
  "dependencies": {
30
29
  "b4a": "^1.3.1",
31
- "hypercore": "next",
32
30
  "blake2b-universal": "^1.0.1",
33
31
  "derive-key": "^1.0.1",
34
- "hypercore-crypto": "^2.3.0",
32
+ "hypercore": "next",
33
+ "hypercore-crypto": "^3.2.1",
35
34
  "safety-catch": "^1.0.1",
36
35
  "sodium-universal": "^3.0.4"
37
36
  }
package/test/all.js CHANGED
@@ -1,7 +1,8 @@
1
1
  const { test, configure } = require('brittle')
2
2
  const crypto = require('hypercore-crypto')
3
3
  const ram = require('random-access-memory')
4
- const tmp = require('tmp-promise')
4
+ const os = require('os')
5
+ const path = require('path')
5
6
 
6
7
  const Corestore = require('..')
7
8
 
@@ -99,8 +100,8 @@ test('replicating cores created after replication begins', async function (t) {
99
100
  })
100
101
 
101
102
  test('replicating cores using discovery key hook', async function (t) {
102
- const dir = await tmp.dir({ unsafeCleanup: true })
103
- let store1 = new Corestore(dir.path)
103
+ const dir = tmpdir()
104
+ let store1 = new Corestore(dir)
104
105
  const store2 = new Corestore(ram)
105
106
 
106
107
  const core = store1.get({ name: 'main' })
@@ -108,15 +109,13 @@ test('replicating cores using discovery key hook', async function (t) {
108
109
  const key = core.key
109
110
 
110
111
  await store1.close()
111
- store1 = new Corestore(dir.path)
112
+ store1 = new Corestore(dir)
112
113
 
113
114
  const s = store1.replicate(true, { live: true })
114
115
  s.pipe(store2.replicate(false, { live: true })).pipe(s)
115
116
 
116
117
  const core2 = store2.get(key)
117
118
  t.alike(await core2.get(0), Buffer.from('hello'))
118
-
119
- await dir.cleanup()
120
119
  })
121
120
 
122
121
  test('nested namespaces', async function (t) {
@@ -144,9 +143,9 @@ test('core uncached when all sessions close', async function (t) {
144
143
  })
145
144
 
146
145
  test('writable core loaded from name userData', async function (t) {
147
- const dir = await tmp.dir({ unsafeCleanup: true })
146
+ const dir = tmpdir()
148
147
 
149
- let store = new Corestore(dir.path)
148
+ let store = new Corestore(dir)
150
149
  let core = store.get({ name: 'main' })
151
150
  await core.ready()
152
151
  const key = core.key
@@ -156,7 +155,7 @@ test('writable core loaded from name userData', async function (t) {
156
155
  t.is(core.length, 1)
157
156
 
158
157
  await store.close()
159
- store = new Corestore(dir.path)
158
+ store = new Corestore(dir)
160
159
  core = store.get(key)
161
160
  await core.ready()
162
161
 
@@ -165,25 +164,21 @@ test('writable core loaded from name userData', async function (t) {
165
164
  t.is(core.length, 2)
166
165
  t.alike(await core.get(0), Buffer.from('hello'))
167
166
  t.alike(await core.get(1), Buffer.from('world'))
168
-
169
- await dir.cleanup()
170
167
  })
171
168
 
172
169
  test('storage locking', async function (t) {
173
- const dir = await tmp.dir({ unsafeCleanup: true })
170
+ const dir = tmpdir()
174
171
 
175
- const store1 = new Corestore(dir.path)
172
+ const store1 = new Corestore(dir)
176
173
  await store1.ready()
177
174
 
178
- const store2 = new Corestore(dir.path)
175
+ const store2 = new Corestore(dir)
179
176
  try {
180
177
  await store2.ready()
181
178
  t.fail('dir should have been locked')
182
179
  } catch {
183
180
  t.pass('dir was locked')
184
181
  }
185
-
186
- await dir.cleanup()
187
182
  })
188
183
 
189
184
  test('closing a namespace does not close cores', async function (t) {
@@ -205,3 +200,7 @@ test('closing a namespace does not close cores', async function (t) {
205
200
  t.ok(core1.closed)
206
201
  t.ok(core2.closed)
207
202
  })
203
+
204
+ function tmpdir () {
205
+ return path.join(os.tmpdir(), 'corestore-' + Math.random().toString(16).slice(2))
206
+ }
package/test/keys.js CHANGED
@@ -41,7 +41,7 @@ test('short user-provided token will throw', async t => {
41
41
  })
42
42
 
43
43
  test('persistent storage regenerates keys correctly', async t => {
44
- const testPath = p.resolve(__dirname, 'test-data')
44
+ const testPath = p.join(__dirname, 'test-data')
45
45
 
46
46
  const keys1 = await KeyManager.fromStorage((name) => raf(testPath, { directory: testPath }))
47
47
  const kp1 = await keys1.createHypercoreKeyPair('core1')
@@ -51,6 +51,9 @@ test('persistent storage regenerates keys correctly', async t => {
51
51
 
52
52
  t.alike(kp1.publicKey, kp2.publicKey)
53
53
 
54
+ await keys1.close()
55
+ await keys2.close()
56
+
54
57
  await fs.promises.rm(testPath, { recursive: true })
55
58
  })
56
59