Browse Source

use cobox-keys to load and save encKey, default derive from parent_key

Kieran Gibb 2 years ago
parent
commit
0dc40958ee
No known key found for this signature in database
5 changed files with 88 additions and 60 deletions
  1. 11
    9
      index.js
  2. 6
    3
      lib/handlers/drive.js
  3. 6
    5
      package.json
  4. 17
    13
      test/index.test.js
  5. 48
    30
      yarn.lock

+ 11
- 9
index.js View File

@@ -7,6 +7,9 @@ const maybe = require('call-me-maybe')
7 7
 const crypto = require('cobox-crypto')
8 8
 const { Replicator } = require('cobox-replicator')
9 9
 const constants = require('cobox-constants')
10
+const { deriveFromStoredParent, loadKey, saveKey } = require('cobox-keys')
11
+const { keyIds } = constants
12
+const { log: LOG_ID } = keyIds
10 13
 
11 14
 const Kappa = require('kappa-core')
12 15
 const Log = require('cobox-log')
@@ -14,10 +17,8 @@ const Drive = require('./lib/handlers/drive')
14 17
 const State = require('./lib/handlers/state')
15 18
 
16 19
 const { setupLevel } = require('cobox-replicator/lib/level')
17
-const { loadEncryptionKey, saveEncryptionKey } = require('./lib/keys')
18 20
 
19
-// TODO: import from cobox-constants
20
-const LOG_ID = 3
21
+const ENC_KEY = 'encryption_key'
21 22
 
22 23
 class Group extends Replicator {
23 24
   /**
@@ -27,10 +28,10 @@ class Group extends Replicator {
27 28
   constructor (storage, address, opts = {}) {
28 29
     super(storage, address, opts)
29 30
 
30
-    var key = loadEncryptionKey(this.path) || opts.encryptionKey
31
+    var key = loadKey(this.path, ENC_KEY) || opts.encryptionKey
31 32
     var encryptionKey = crypto.encryptionKey(key)
32
-    assert(crypto.isKey(encryptionKey), 'invalid: encryption_key')
33
-    saveEncryptionKey(this.path, encryptionKey)
33
+    assert(crypto.isKey(encryptionKey), `invalid: ${ENC_KEY}`)
34
+    saveKey(this.path, ENC_KEY, encryptionKey)
34 35
 
35 36
     this._initFeeds({
36 37
       valueEncoding: crypto.encoder(encryptionKey, Object.assign({
@@ -42,6 +43,7 @@ class Group extends Replicator {
42 43
     this.name = opts.name || (groupOpts && groupOpts.name)
43 44
     this.core = new Kappa()
44 45
 
46
+    this._deriveKeyPair = opts.deriveKeyPair || deriveFromStoredParent(this.config.root, crypto.keyPair)
45 47
     this.location = null
46 48
     this._unmount = null
47 49
   }
@@ -88,8 +90,8 @@ class Group extends Replicator {
88 90
       this.log = Log({
89 91
         core: this.core,
90 92
         feeds: this.feeds,
91
-        author: this.config.identity.publicKey.toString('hex'),
92
-        keyPair: this.config.deriveKeyPair(LOG_ID, this.address),
93
+        author: this.identity.publicKey.toString('hex'),
94
+        keyPair: this._deriveKeyPair(LOG_ID, this.address),
93 95
         db: setupLevel(path.join(this.path, 'views', 'log'))
94 96
       })
95 97
 
@@ -100,7 +102,7 @@ class Group extends Replicator {
100 102
         core: this.core,
101 103
         logger: this.config.logger ? this.config.logger('kappa-drive') : null,
102 104
         db: setupLevel(path.join(this.path, 'views', 'drive')),
103
-        deriveKeyPair: this.config.deriveKeyPair
105
+        deriveKeyPair: this._deriveKeyPair
104 106
       })
105 107
 
106 108
       this.state = State({

+ 6
- 3
lib/handlers/drive.js View File

@@ -2,9 +2,12 @@ const { KappaDrive } = require('kappa-drive')
2 2
 const mount = require('kappa-drive-mount')
3 3
 const assert = require('assert')
4 4
 const maybe = require('call-me-maybe')
5
+const { keyIds } = require('cobox-constants')
5 6
 
6
-// TODO: import from cobox-constants
7
-const DRIVE_IDS = [1, 2]
7
+const {
8
+  metadata: METADATA_ID,
9
+  content: CONTENT_ID
10
+} = keyIds
8 11
 
9 12
 class DriveHandler extends KappaDrive {
10 13
   /**
@@ -13,7 +16,7 @@ class DriveHandler extends KappaDrive {
13 16
    */
14 17
   constructor (opts = {}) {
15 18
     super(opts.storage, opts.address, {
16
-      feedIds: DRIVE_IDS,
19
+      feedIds: [METADATA_ID, CONTENT_ID],
17 20
       multifeed: opts.feeds,
18 21
       core: opts.core,
19 22
       db: opts.db,

+ 6
- 5
package.json View File

@@ -9,10 +9,11 @@
9 9
   "dependencies": {
10 10
     "assert": "^2.0.0",
11 11
     "call-me-maybe": "^1.0.1",
12
-    "cobox-constants": "^1.0.0",
13
-    "cobox-crypto": "^1.2.2",
14
-    "cobox-log": "^1.0.1-alpha1",
15
-    "cobox-replicator": "git+https://ledger-git.dyne.org/cobox/cobox-replicator.git#development",
12
+    "cobox-constants": "^1.0.3",
13
+    "cobox-crypto": "^1.2.5",
14
+    "cobox-keys": "^1.0.0",
15
+    "cobox-log": "^1.0.2-alpha1",
16
+    "cobox-replicator": "2.0.0-alpha1",
16 17
     "collect-stream": "^1.2.1",
17 18
     "hypertrie": "^4.2.3",
18 19
     "kappa-core": "github:Frando/kappa-core#15777919bf820c7eaac6b76d1947cce93f922dac",
@@ -26,7 +27,7 @@
26 27
     "subleveldown": "^4.1.4"
27 28
   },
28 29
   "devDependencies": {
29
-    "cobox-config": "git+https://ledger-git.dyne.org/cobox/cobox-config.git#development",
30
+    "cobox-config": "^3.0.1-alpha1",
30 31
     "collect-stream": "^1.2.1",
31 32
     "nanoresource": "^1.3.0",
32 33
     "nyc": "^14.1.1",

+ 17
- 13
test/index.test.js View File

@@ -48,16 +48,16 @@ describe('cobox-group: Group', (context) => {
48 48
     var loadStub = sinon.stub().returns(encryptionKey)
49 49
     var saveStub = sinon.stub().returns(true)
50 50
 
51
-    var ProxyGroup = proxyquire('../', { './lib/keys': {
52
-        loadEncryptionKey: loadStub,
53
-        saveEncryptionKey: saveStub
51
+    var ProxyGroup = proxyquire('../', { 'cobox-keys': {
52
+        loadKey: loadStub,
53
+        saveKey: saveStub
54 54
       }
55 55
     })
56 56
 
57 57
     var group = ProxyGroup(storage, address)
58 58
 
59 59
     assert.ok(loadStub.calledWith(group.path), 'key is loaded')
60
-    assert.ok(saveStub.calledWith(group.path, encryptionKey), 'key is saved')
60
+    assert.ok(saveStub.calledWith(group.path, 'encryption_key', encryptionKey), 'key is saved')
61 61
     cleanup(storage, next)
62 62
   })
63 63
 
@@ -96,16 +96,18 @@ describe('cobox-group: Group', (context) => {
96 96
     var address = crypto.address()
97 97
     var config = Config(storage)
98 98
     var group = Group(path.join(storage, 'groups'), address, { config, name: randomWords(1).pop() })
99
+    var deriveKeyPair = require('cobox-keys').deriveFromStoredParent(config.root, crypto.keyPair)
99 100
 
100 101
     group.ready(() => {
101
-      assert.same(group.drive.metadata.key, config.deriveKeyPair(1, group.address).publicKey, 'Metadata key derived correctly')
102
-      assert.same(group.drive.metadata.secretKey, config.deriveKeyPair(1, group.address).secretKey, 'Metadata secret key derived correctly')
102
+      assert.same(group.log._feed.key, deriveKeyPair(0, group.address).publicKey, 'Log key derived correctly')
103
+      assert.same(group.log._feed.secretKey, deriveKeyPair(0, group.address).secretKey, 'Log secret key derived correctly')
103 104
 
104
-      assert.same(group.drive.content.key, config.deriveKeyPair(2, group.address).publicKey, 'Content key derived correctly')
105
-      assert.same(group.drive.content.secretKey, config.deriveKeyPair(2, group.address).secretKey, 'Content secret key derived correctly')
105
+      assert.same(group.drive.metadata.key, deriveKeyPair(1, group.address).publicKey, 'Metadata key derived correctly')
106
+      assert.same(group.drive.metadata.secretKey, deriveKeyPair(1, group.address).secretKey, 'Metadata secret key derived correctly')
107
+
108
+      assert.same(group.drive.content.key, deriveKeyPair(2, group.address).publicKey, 'Content key derived correctly')
109
+      assert.same(group.drive.content.secretKey, deriveKeyPair(2, group.address).secretKey, 'Content secret key derived correctly')
106 110
 
107
-      assert.same(group.log._feed.key, config.deriveKeyPair(3, group.address).publicKey, 'Log key derived correctly')
108
-      assert.same(group.log._feed.secretKey, config.deriveKeyPair(3, group.address).secretKey, 'Log secret key derived correctly')
109 111
       cleanup(storage, next)
110 112
     })
111 113
   })
@@ -224,7 +226,9 @@ describe('cobox-group: Group', (context) => {
224 226
       address = crypto.address(),
225 227
       encryptionKey = crypto.encryptionKey(),
226 228
       config1 = Config(storage1),
227
-      config2 = Config(storage2)
229
+      config2 = Config(storage2),
230
+      identity1 = crypto.boxKeyPair(),
231
+      identity2 = crypto.boxKeyPair()
228 232
 
229 233
     var group1 = Group(storage1, address, { encryptionKey, config: config1 }),
230 234
       group2 = Group(storage2, address, { encryptionKey, config: config2 })
@@ -238,12 +242,12 @@ describe('cobox-group: Group', (context) => {
238 242
 
239 243
     var msgs = [{
240 244
       type: 'group/about',
241
-      author: config1.identity.publicKey.toString('hex'),
245
+      author: identity1.publicKey.toString('hex'),
242 246
       timestamp: Date.now(),
243 247
       content: { name: 'Blockades' }
244 248
     }, {
245 249
       type: 'group/about',
246
-      author: config2.identity.publicKey.toString('hex'),
250
+      author: identity2.publicKey.toString('hex'),
247 251
       timestamp: Date.now() + 1,
248 252
       content: { name: 'Magma' }
249 253
     }]

+ 48
- 30
yarn.lock View File

@@ -423,12 +423,13 @@ clone@^2.1.2:
423 423
   resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f"
424 424
   integrity sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=
425 425
 
426
-"cobox-config@git+https://ledger-git.dyne.org/cobox/cobox-config.git#development":
427
-  version "2.1.0"
428
-  resolved "git+https://ledger-git.dyne.org/cobox/cobox-config.git#5d4ce09e3a32da19a27d631b09761c09c3bd3341"
426
+cobox-config@^3.0.1-alpha1:
427
+  version "3.0.1-alpha1"
428
+  resolved "https://registry.yarnpkg.com/cobox-config/-/cobox-config-3.0.1-alpha1.tgz#56792b7e1dda563b89af9250ba2b6e8c9893b9fb"
429
+  integrity sha512-9Z1sz56YIiM3M/1cDlYr+6YAmi9dV/tZnq4A04yz3/PGnGXpqE1cYJ5dr27YFrVw8ghnDu2eVVvLjkhPc8M9hQ==
429 430
   dependencies:
430 431
     cobox-constants "^1.0.1"
431
-    cobox-crypto "^1.2.3"
432
+    cobox-crypto "^1.2.4"
432 433
     debug "^4.1.1"
433 434
     js-yaml "^3.13.1"
434 435
     mkdirp "^0.5.1"
@@ -436,19 +437,20 @@ clone@^2.1.2:
436 437
     pino "^5.13.2"
437 438
     proxyquire "^2.1.3"
438 439
     sinon "^8.1.1"
440
+    sodium-native "^2.4.9"
439 441
     urlsafe-base64 "^1.0.0"
440 442
 
441
-cobox-constants@^1.0.0, cobox-constants@^1.0.1:
442
-  version "1.0.1"
443
-  resolved "https://registry.yarnpkg.com/cobox-constants/-/cobox-constants-1.0.1.tgz#251a0f6ca2cef802682d59749a017d78fa2e3594"
444
-  integrity sha512-Vt+jjed7eDB/5VZ3XSbUm6eQTN8LIdeL4sCRPXjCAjmDnZQMckYz1I7rhMi14h8+1Ztjio4eZpYnNmcMjco2QA==
443
+cobox-constants@^1.0.1, cobox-constants@^1.0.3:
444
+  version "1.0.3"
445
+  resolved "https://registry.yarnpkg.com/cobox-constants/-/cobox-constants-1.0.3.tgz#16900ae818d5f490b58353531f4b0a04d5717298"
446
+  integrity sha512-kgu8U2OGV3NRTszfl4vwtR4HTqNLMe9f56vFmUQr4etPDsaMh0x5VPup6Etvt4EpFb6Pt5t2SjC7ZKjQO9vntg==
445 447
   dependencies:
446 448
     os "^0.1.1"
447 449
 
448
-cobox-crypto@^1.2.2, cobox-crypto@^1.2.3:
449
-  version "1.2.3"
450
-  resolved "https://registry.yarnpkg.com/cobox-crypto/-/cobox-crypto-1.2.3.tgz#13132181aa45e413a35e094d249855e8a0ad4e4c"
451
-  integrity sha512-gZ6FCo+TvWdyfckstF/FOhja1PeG1eGyZsFLHHVmanPlQGHKBx8N2NKiXDBKxK44QnptZi5A2yWGTrINai+rmw==
450
+cobox-crypto@^1.2.4, cobox-crypto@^1.2.5:
451
+  version "1.2.5"
452
+  resolved "https://registry.yarnpkg.com/cobox-crypto/-/cobox-crypto-1.2.5.tgz#fe97f06f0d4389786958ca176ab96eb14932bf0d"
453
+  integrity sha512-N/qXNYdoy9QeOfZaghEClhpIbTuQZMrDcLjytv2EbCcCZSr3buvuI/CrelJuYV2fyW4a3CyETm3oKVNkGBZFqQ==
452 454
   dependencies:
453 455
     assert "^2.0.0"
454 456
     bip39 "^3.0.2"
@@ -456,13 +458,23 @@ cobox-crypto@^1.2.2, cobox-crypto@^1.2.3:
456 458
     hypercore-crypto "^1.0.0"
457 459
     sodium-native "^2.4.2"
458 460
 
459
-cobox-log@^1.0.1-alpha1:
460
-  version "1.0.1-alpha1"
461
-  resolved "https://registry.yarnpkg.com/cobox-log/-/cobox-log-1.0.1-alpha1.tgz#29e682cfcdbf15f3f656c449b25333138a8d7739"
462
-  integrity sha512-gmtatudIXROXoXxDQqnq9n/hQx6GlJ/ZC+oZtJTkjEVkkRdc2iceEgb/ZT4OIb5QVqzAgfwRd5q+FGhoQPa0CQ==
461
+cobox-keys@^1.0.0:
462
+  version "1.0.0"
463
+  resolved "https://registry.yarnpkg.com/cobox-keys/-/cobox-keys-1.0.0.tgz#fbb32fb193310c667d1b24a16928c039777856ab"
464
+  integrity sha512-Oxh5a+p9UWw/VFZo4Uw3LPMdk4nqNpcsRvjSskNPhaE+ja6F+0zIiN968TT4AsdMAk3r9gh9kPiawzajxYJIuQ==
465
+  dependencies:
466
+    assert "^2.0.0"
467
+    cobox-crypto "^1.2.4"
468
+    mkdirp "^1.0.3"
469
+    sodium-native "^2.4.9"
470
+
471
+cobox-log@^1.0.2-alpha1:
472
+  version "1.0.2-alpha1"
473
+  resolved "https://registry.yarnpkg.com/cobox-log/-/cobox-log-1.0.2-alpha1.tgz#8d14099582b5cff2cf308996fa013c2d9214be7f"
474
+  integrity sha512-1ttHGagvWsZUCZtzMVbnB0b1wK6tZMkQuYq/S9PV5lAbJSdFk1/htuPXvX81nYLx1wzOF3e8iwpPGlh3+KFwkA==
463 475
   dependencies:
464 476
     call-me-maybe "^1.0.1"
465
-    cobox-crypto "^1.2.3"
477
+    cobox-crypto "^1.2.5"
466 478
     cobox-schemas "^1.0.0-alpha1"
467 479
     collect-stream "^1.2.1"
468 480
     debug "^4.1.1"
@@ -474,15 +486,16 @@ cobox-log@^1.0.1-alpha1:
474 486
     nanoresource "^1.3.0"
475 487
     subleveldown "^4.1.4"
476 488
 
477
-"cobox-replicator@git+https://ledger-git.dyne.org/cobox/cobox-replicator.git#development":
478
-  version "1.0.3"
479
-  resolved "git+https://ledger-git.dyne.org/cobox/cobox-replicator.git#5b1430f4ba6ce72001e0a6cdee52198adb1fb19f"
489
+cobox-replicator@2.0.0-alpha1:
490
+  version "2.0.0-alpha1"
491
+  resolved "https://registry.yarnpkg.com/cobox-replicator/-/cobox-replicator-2.0.0-alpha1.tgz#8857caf7d859c102d3dacad5c106f4ff97eeb201"
492
+  integrity sha512-TsmGMP8AHOzbvySA85ampmlumoLWWsrgX/AtWJ3xmWg9UBi409/6Uvh/dkZYyIs2AW6AdpXGCcpFpfrhNBLVrg==
480 493
   dependencies:
481 494
     assert "^2.0.0"
482 495
     call-me-maybe "^1.0.1"
483
-    cobox-config "git+https://ledger-git.dyne.org/cobox/cobox-config.git#development"
484
-    cobox-constants "^1.0.0"
485
-    cobox-crypto "^1.2.3"
496
+    cobox-config "^3.0.1-alpha1"
497
+    cobox-constants "^1.0.3"
498
+    cobox-crypto "^1.2.5"
486 499
     debug "^4.1.1"
487 500
     hyperswarm "^2.4.0"
488 501
     level "^6.0.0"
@@ -1288,9 +1301,9 @@ hyperdrive-schemas@beta:
1288 1301
   integrity sha512-eZbIZAeiJlRd1rPNl7jBeE8aiWfx3odcOWeexvd1GaN8Tr0k0edXbKrOvnp1gI0tmUrFoDb1ypS9PmkE3jB9WA==
1289 1302
 
1290 1303
 hyperdrive@^10.3.2:
1291
-  version "10.8.11"
1292
-  resolved "https://registry.yarnpkg.com/hyperdrive/-/hyperdrive-10.8.11.tgz#7f7879afc0816d592847aca55063ce2dcf3b1016"
1293
-  integrity sha512-q15lDI1l+xmPK6mGK5bMquoQo7kmGkb8uUNpcamtSFGc8SAju9lWkv8GuZ/gr1DgWLmc1y3O90TL5TfU7Rv0VQ==
1304
+  version "10.8.12"
1305
+  resolved "https://registry.yarnpkg.com/hyperdrive/-/hyperdrive-10.8.12.tgz#2029c0707a7b42513600be585097222d55e330e9"
1306
+  integrity sha512-Cx6gXfxirbV8/N+C8DFLjkLEYhdcqxMCL4K1izdZIgwXtdcSZx5tsORUVYfOYsgnZcFVXe4DkFaDywyb/rbOUw==
1294 1307
   dependencies:
1295 1308
     byte-stream "^2.1.0"
1296 1309
     corestore "^5.0.0"
@@ -1351,9 +1364,9 @@ hypertrie@^3.6.0:
1351 1364
     varint "^5.0.0"
1352 1365
 
1353 1366
 hypertrie@^4.0.0, hypertrie@^4.2.2, hypertrie@^4.2.3, hypertrie@^4.3.0:
1354
-  version "4.3.0"
1355
-  resolved "https://registry.yarnpkg.com/hypertrie/-/hypertrie-4.3.0.tgz#14d1449ece0db2bff2d71ffb89762f2393772201"
1356
-  integrity sha512-Q4XQogiW1zCiyuv78JTd3EVJ56nwCCqKoPazJ+fOVPKoGfDD4qqmmm+0+lxGnKaPq5XHs8MEzWItUBT0ryl0rQ==
1367
+  version "4.4.0"
1368
+  resolved "https://registry.yarnpkg.com/hypertrie/-/hypertrie-4.4.0.tgz#00faae795ecde1c9cae65a419abf48a1d07d6237"
1369
+  integrity sha512-BRlyNnB0IPPaxNemU3W4H+aHKK1Zizhf6hICldRXbD95dng7oFoKzW+eJHw9jCeFLMrHdpN2N3oprkHO06/sXQ==
1357 1370
   dependencies:
1358 1371
     array-lru "^1.1.1"
1359 1372
     bulk-write-stream "^1.1.4"
@@ -2050,6 +2063,11 @@ mkdirp@^0.5.0, mkdirp@^0.5.1:
2050 2063
   dependencies:
2051 2064
     minimist "0.0.8"
2052 2065
 
2066
+mkdirp@^1.0.3:
2067
+  version "1.0.3"
2068
+  resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.3.tgz#4cf2e30ad45959dddea53ad97d518b6c8205e1ea"
2069
+  integrity sha512-6uCP4Qc0sWsgMLy1EOqqS/3rjDHOEnsStVr/4vtAIK2Y5i2kA7lFFejYrpIyiN9w0pYf4ckeCYT9f1r1P9KX5g==
2070
+
2053 2071
 module-not-found-error@^1.0.1:
2054 2072
   version "1.0.1"
2055 2073
   resolved "https://registry.yarnpkg.com/module-not-found-error/-/module-not-found-error-1.0.1.tgz#cf8b4ff4f29640674d6cdd02b0e3bc523c2bbdc0"
@@ -2931,7 +2949,7 @@ sodium-javascript@~0.5.0:
2931 2949
     siphash24 "^1.0.1"
2932 2950
     xsalsa20 "^1.0.0"
2933 2951
 
2934
-sodium-native@^2.0.0, sodium-native@^2.2.1, sodium-native@^2.4.2:
2952
+sodium-native@^2.0.0, sodium-native@^2.2.1, sodium-native@^2.4.2, sodium-native@^2.4.9:
2935 2953
   version "2.4.9"
2936 2954
   resolved "https://registry.yarnpkg.com/sodium-native/-/sodium-native-2.4.9.tgz#7a7beb997efdbd2c773a385fb959f0cead5f5162"
2937 2955
   integrity sha512-mbkiyA2clyfwAyOFIzMvsV6ny2KrKEIhFVASJxWfsmgfUEymgLIS2MLHHcGIQMkrcKhPErRaMR5Dzv0EEn+BWg==