Browse Source

Merge branch 'load-and-save-encryption-keys' of CoBox/cobox-group into development

kyphae 2 years ago
parent
commit
3db5a35ae0
2 changed files with 67 additions and 5 deletions
  1. 33
    3
      index.js
  2. 34
    2
      test/index.test.js

+ 33
- 3
index.js View File

@@ -27,7 +27,12 @@ class Group extends Base {
27 27
     super(storage, address, opts)
28 28
 
29 29
     this.masterKey = opts.masterKey || crypto.masterKey()
30
-    this.encryptionKey = crypto.encryptionKey(opts.encryptionKey)
30
+
31
+    this.encryptionKey = opts.encryptionKey
32
+      ? crypto.encryptionKey(opts.encryptionKey)
33
+      : this._loadEncryptionKey() || crypto.encryptionKey()
34
+    assert(this._saveEncryptionKey(), 'Problem writing encryptionKey')
35
+
31 36
     if (!crypto.isKey(this.encryptionKey)) throw new Error('invalid: encryption key format')
32 37
 
33 38
     var groupOpts = this.config.groups.get(this.address)
@@ -121,10 +126,11 @@ class Group extends Base {
121 126
   save () {
122 127
     this.config.groups.set(this.address, {
123 128
       address: this.address,
124
-      encryptionKey: this.encryptionKey,
129
+      // encryptionKey: this.encryptionKey,
125 130
       name: this.name
126 131
     })
127
-    return this.config.save()
132
+
133
+    return this._saveEncryptionKey() && this.config.save()
128 134
   }
129 135
 
130 136
   // ------------------------------ PRIVATE FUNCTIONS ------------------------------
@@ -175,6 +181,30 @@ class Group extends Base {
175 181
 
176 182
     this.db.network.put(peer, payload, callback)
177 183
   }
184
+
185
+  _saveEncryptionKey () {
186
+    const encryptionKeyPath = path.join(this.config.root, this.address.toString('hex'), 'encryption_key')
187
+    try {
188
+      if (!fs.existsSync(encryptionKeyPath)) {
189
+        mkdirp.sync(path.dirname(encryptionKeyPath))
190
+        fs.writeFileSync(encryptionKeyPath, this.encryptionKey, { mode: fs.constants.S_IRUSR })
191
+      }
192
+    } catch (err) {
193
+      return false
194
+    }
195
+    return true
196
+  }
197
+
198
+  _loadEncryptionKey () {
199
+    const encryptionKeyPath = path.join(this.config.root, this.address.toString('hex'), 'encryption_key')
200
+    let encryptionKey
201
+    try {
202
+      encryptionKey = fs.readFileSync(encryptionKeyPath)
203
+    } catch (err) {
204
+      return false
205
+    }
206
+    return encryptionKey
207
+  }
178 208
 }
179 209
 
180 210
 function setupLevel (levelPath) {

+ 34
- 2
test/index.test.js View File

@@ -4,8 +4,9 @@ const crypto = require('cobox-crypto')
4 4
 const collect = require('collect-stream')
5 5
 const debug = require('debug')('cobox-group')
6 6
 const Config = require('cobox-config')
7
-const liveStream = require('level-live-stream')
8
-const util = require('util')
7
+const path = require('path')
8
+const fs = require('fs')
9
+const mkdirp = require('mkdirp')
9 10
 
10 11
 const Group = require('../')
11 12
 const query = require('../query')
@@ -36,6 +37,37 @@ describe('basic', (context) => {
36 37
     cleanup(storage, next)
37 38
   })
38 39
 
40
+  context('Group stores encryption key', (assert, next) => {
41
+    const config = Config(storage)
42
+    const address = crypto.address()
43
+    const encryptionKey = crypto.encryptionKey()
44
+    const group = Group(storage, address, { config, encryptionKey })
45
+    assert.ok(group, 'group loaded')
46
+    const encryptionKeyPath = path.join(config.root, address.toString('hex'), 'encryption_key')
47
+    assert.equal(
48
+      fs.readFileSync(encryptionKeyPath).toString('hex'),
49
+      encryptionKey.toString('hex'),
50
+      'encryption key stored correctly'
51
+    )
52
+    cleanup(storage, next)
53
+  })
54
+
55
+  context('Group loads encryption key', (assert, next) => {
56
+    const config = Config(storage)
57
+    const address = crypto.address()
58
+    const encryptionKey = crypto.encryptionKey()
59
+    const encryptionKeyPath = path.join(config.root, address.toString('hex'), 'encryption_key')
60
+    mkdirp.sync(path.dirname(encryptionKeyPath))
61
+    fs.writeFileSync(encryptionKeyPath, encryptionKey, { mode: fs.constants.S_IRUSR })
62
+    const group = Group(storage, address, { config })
63
+    assert.ok(group, 'group loaded')
64
+    assert.equal(
65
+      group.encryptionKey.toString('hex'),
66
+      encryptionKey.toString('hex'),
67
+      'encryption key retrieved correctly'
68
+    )
69
+    cleanup(storage, next)
70
+  })
39 71
   context('returns a name', (assert, next) => {
40 72
     var config = Config(storage)
41 73
     var address = crypto.address()