Browse Source

Merge branch 'rename' of CoBox/cobox-config into development

kyphae 2 years ago
parent
commit
f328ef7ae0
5 changed files with 33 additions and 74 deletions
  1. 10
    20
      README.md
  2. 6
    8
      lib/key-handler.js
  3. 1
    1
      package.json
  4. 14
    43
      test/index.test.js
  5. 2
    2
      yarn.lock

+ 10
- 20
README.md View File

@@ -6,9 +6,9 @@ Stores and retrieves a YAML configuration file for use with the cobox stack.
6 6
 
7 7
 ```js
8 8
 const Config = require('cobox-config')
9
-const crypto = require('cobox-crypto') // or some other crypto scheme
9
+const crypto = require('cobox-crypto')() // or some other crypto scheme
10 10
 
11
-var storage = './storage' 
11
+var storage = './storage'
12 12
 var config = Config(storage)
13 13
 ```
14 14
 
@@ -27,38 +27,28 @@ config.load()
27 27
 Load from YAML, path set using the storage path when initialising. This is called automatically when initialising a `Config`.
28 28
 
29 29
 ```
30
-config.identities.add(identity)
30
+var key = group.name || group.address || group.address.toString('hex')
31
+config.groups.get(group)
31 32
 ```
32 33
 
33
-Append an identity to the config
34
+Get a group from the config
34 35
 
35 36
 ```
36
-var key = identity.name || identity.publicKey || identity.publicKey.toString('hex')
37
-config.identities.remove(key)
37
+var key = group.name || group.address || group.address.toString('hex')
38
+config.groups.set(group, { name, address, encryptionKey })
38 39
 ```
39 40
 
40
-Remove an identity from the config
41
+Add a group to the config
41 42
 
42 43
 ```
43
-var key = group.name || group.publicKey || group.publicKey.toString('hex')
44
-config.groups.remove(group)
44
+var key = group.name || group.address || group.address.toString('hex')
45
+config.groups.delete(group)
45 46
 ```
46 47
 
47 48
 Remove a group from the config
48 49
 
49
-```
50
-config.identities.list()
51
-```
52
-
53
-List saved identities
54
-
55 50
 ```
56 51
 config.groups.list()
57 52
 ```
58 53
 
59 54
 List saved groups
60
-
61
-## Todos
62
-
63
-* [ ] encode public keys as base64 urlsafe in YAML 
64
-* [ ] store the path to secret key file (base64 urlsafe binary encoding) instead of raw secret key in the config file 

+ 6
- 8
lib/key-handler.js View File

@@ -1,8 +1,7 @@
1
-const Crypto = require('cobox-crypto')
1
+const crypto = require('cobox-crypto')
2 2
 const { isPubKey, isName } = require('../util')
3 3
 
4
-const KEY_NAMES = ['publicKey', 'encryptionKey', 'secretKey', 'symmetricKey']
5
-const crypto = Crypto()
4
+const KEY_NAMES = ['name', 'address', 'encryptionKey']
6 5
 
7 6
 const KeyHandler = module.exports = (collection) => ({
8 7
   get: (id) => {
@@ -28,11 +27,11 @@ const KeyHandler = module.exports = (collection) => ({
28 27
       entry = collection.byKey[id.toString('hex')]
29 28
       if (!entry) return false
30 29
       if (entry.name) delete collection.byName[entry.name]
31
-      return delete collection.byKey[entry.publicKey.toString('hex')]
30
+      return delete collection.byKey[entry.address.toString('hex')]
32 31
     } else if (isName(id)) {
33 32
       entry = collection.byName[id]
34 33
       if (!entry) return false
35
-      return delete collection.byName[id] && delete collection.byKey[entry.publicKey.toString('hex')]
34
+      return delete collection.byName[id] && delete collection.byKey[entry.address.toString('hex')]
36 35
     }
37 36
   },
38 37
   list: () => Object.values(collection.byKey)
@@ -52,9 +51,8 @@ function compact (obj) {
52 51
 }
53 52
 
54 53
 function isValid (entry) {
55
- return entry.publicKey
56
-    && crypto.isKey(entry.publicKey)
54
+ return entry.address
55
+    && crypto.isKey(entry.address)
57 56
     && !entry.encryptionKey || crypto.isKey(entry.encryptionKey)
58
-    && !entry.symmetricKey || crypto.isKey(entry.symmetricKey)
59 57
     && !entry.name || isName(entry.name)
60 58
 }

+ 1
- 1
package.json View File

@@ -7,7 +7,7 @@
7 7
     "test": "test"
8 8
   },
9 9
   "dependencies": {
10
-    "cobox-crypto": "git+ssh://git@ledger-git.dyne.org:2240/CoBox/cobox-crypto.git",
10
+    "cobox-crypto": "git+ssh://git@ledger-git.dyne.org:2240/CoBox/cobox-crypto.git#development",
11 11
     "debug": "^4.1.1",
12 12
     "js-yaml": "^3.13.1",
13 13
     "mkdirp": "^0.5.1",

+ 14
- 43
test/index.test.js View File

@@ -1,7 +1,6 @@
1 1
 const { describe } = require('tape-plus')
2 2
 const Config = require('../')
3
-const crypto = require('cobox-crypto')()
4
-const { keyPair } = require('hypercore-crypto')
3
+const crypto = require('cobox-crypto')
5 4
 const fs = require('fs')
6 5
 const path = require('path')
7 6
 const yaml = require('js-yaml')
@@ -21,7 +20,6 @@ describe('load', (context) => {
21 20
 
22 21
   context('default', (assert, next) => {
23 22
     var config = Config(storage)
24
-    assert.ok(config.identities.list() instanceof Array, 'identities list defaults to empty Array')
25 23
     assert.ok(config.groups.list() instanceof Array, 'groups list defaults to empty Array')
26 24
     next()
27 25
   })
@@ -69,45 +67,35 @@ describe('set', (context) => {
69 67
   context('byKey', (assert, next) => {
70 68
     var config = Config(storage)
71 69
     var group = crypto.unpack(crypto.accessKey())
72
-    var identity = keyPair()
73 70
 
74
-    config.groups.set(group.publicKey, group)
75
-    config.identities.set(identity.publicKey, identity)
71
+    config.groups.set(group.address, group)
76 72
 
77 73
     config.save()
78 74
 
79 75
     var reload = yaml.safeLoad(fs.readFileSync(path.join(storage, 'config.yml')))
80
-    var rawGroup = reload.groups.byKey[group.publicKey.toString('hex')]
81
-    var rawIdentity = reload.identities.byKey[identity.publicKey.toString('hex')]
76
+    var rawGroup = reload.groups.byKey[group.address.toString('hex')]
82 77
 
83
-    assert.same(config.groups.get(group.publicKey), rawGroup, 'saves groups to storage')
84
-    assert.same(config.identities.get(identity.publicKey), rawIdentity, 'saves identities to storage')
78
+    assert.same(config.groups.get(group.address), rawGroup, 'saves groups to storage')
85 79
     next()
86 80
   })
87 81
 
88 82
   context('byName', (assert, next) => {
89 83
     var config = Config(storage)
90 84
     var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
91
-    var identity = Object.assign({ name: 'Alice' }, keyPair())
92 85
 
93
-    config.groups.set(group.publicKey, group)
94
-    config.identities.set(identity.publicKey, identity)
86
+    config.groups.set(group.address, group)
95 87
 
96 88
     config.save()
97 89
 
98 90
     var reload = yaml.safeLoad(fs.readFileSync(path.join(storage, 'config.yml')))
99
-    var rawGroup = reload.groups.byKey[group.publicKey.toString('hex')]
100
-    var rawIdentity = reload.identities.byKey[identity.publicKey.toString('hex')]
91
+    var rawGroup = reload.groups.byKey[group.address.toString('hex')]
101 92
 
102 93
     assert.ok(rawGroup.name, 'saves group with a name')
103 94
     assert.same(config.groups.get(group.name), rawGroup, 'gets the group using a name')
104
-    assert.ok(rawIdentity.name, 'saves identity with a name')
105
-    assert.same(config.identities.get(identity.name), rawIdentity, 'saves identities to storage')
106 95
 
107 96
     var reload = Config(storage)
108 97
 
109 98
     assert.same(reload._groups, config._groups, 'load groups from storage')
110
-    assert.same(reload._identities, config._identities, 'load identities from storage')
111 99
     next()
112 100
   })
113 101
 })
@@ -126,18 +114,14 @@ describe('get', (context) => {
126 114
   context('works with buffers or strings', (assert, next) => {
127 115
     var config = Config(storage)
128 116
     var group = crypto.unpack(crypto.accessKey())
129
-    var identity = keyPair()
130 117
 
131
-    config.groups.set(group.publicKey, group)
132
-    config.identities.set(identity.publicKey, identity)
118
+    config.groups.set(group.address, group)
133 119
 
134 120
     config.save()
135 121
 
136
-    assert.ok(config.groups.get(group.publicKey), 'get group works with a buffer')
137
-    assert.ok(config.groups.get(group.publicKey.toString('hex')), 'get group works with a hex')
122
+    assert.ok(config.groups.get(group.address), 'get group works with a buffer')
123
+    assert.ok(config.groups.get(group.address.toString('hex')), 'get group works with a hex')
138 124
 
139
-    assert.ok(config.identities.get(identity.publicKey), 'get identity works with a buffer')
140
-    assert.ok(config.identities.get(identity.publicKey.toString('hex')), 'get identity works with a hex')
141 125
     next()
142 126
   })
143 127
 })
@@ -156,63 +140,50 @@ describe('del', (context) => {
156 140
   context('deletes relevant entry using buffers', (assert, next) => {
157 141
     var config = Config(storage)
158 142
     var group = crypto.unpack(crypto.accessKey())
159
-    var identity = keyPair()
160 143
 
161
-    config.groups.set(group.publicKey, group)
162
-    config.identities.set(identity.publicKey, identity)
144
+    config.groups.set(group.address, group)
163 145
 
164 146
     config.save()
165 147
     config.load()
166 148
 
167
-    config.groups.delete(group.publicKey)
168
-    config.identities.delete(identity.publicKey)
149
+    config.groups.delete(group.address)
169 150
 
170 151
     assert.same(config.groups.list(), [], 'deleted the group')
171
-    assert.same(config.identities.list(), [], 'deleted the identity')
172 152
     next()
173 153
   })
174 154
 
175 155
   context('deletes relevant entry using buffers', (assert, next) => {
176 156
     var config = Config(storage)
177 157
     var group = crypto.unpack(crypto.accessKey())
178
-    var identity = keyPair()
179 158
 
180
-    config.groups.set(group.publicKey, group)
181
-    config.identities.set(identity.publicKey, identity)
159
+    config.groups.set(group.address, group)
182 160
 
183 161
     config.save()
184 162
     config.load()
185 163
 
186
-    config.groups.delete(group.publicKey.toString('hex'))
187
-    config.identities.delete(identity.publicKey.toString('hex'))
164
+    config.groups.delete(group.address.toString('hex'))
188 165
 
189 166
     assert.same(config.groups.list(), [], 'deleted the group')
190
-    assert.same(config.identities.list(), [], 'deleted the identity')
191 167
     next()
192 168
   })
193 169
 
194 170
   context('deletes relevant entry using names', (assert, next) => {
195 171
     var config = Config(storage)
196 172
     var group = Object.assign({ name: 'group-a' }, crypto.unpack(crypto.accessKey()))
197
-    var identity = Object.assign({ name: 'Alice' }, keyPair())
198 173
 
199
-    config.groups.set(group.publicKey, group)
200
-    config.identities.set(identity.publicKey, identity)
174
+    config.groups.set(group.address, group)
201 175
 
202 176
     config.save()
203 177
     config.load()
204 178
 
205 179
     assert.same(config.groups.list().length, 1, 'saved the group')
206
-    assert.same(config.identities.list().length, 1, 'saved the identity')
207 180
 
208 181
     config.groups.delete(group.name)
209
-    config.identities.delete(identity.name)
210 182
 
211 183
     config.save()
212 184
     config.load()
213 185
 
214 186
     assert.same(config.groups.list(), [], 'deleted the group')
215
-    assert.same(config.identities.list(), [], 'deleted the identity')
216 187
     next()
217 188
   })
218 189
 })

+ 2
- 2
yarn.lock View File

@@ -241,9 +241,9 @@ cliui@^5.0.0:
241 241
     strip-ansi "^5.2.0"
242 242
     wrap-ansi "^5.1.0"
243 243
 
244
-"cobox-crypto@git+ssh://git@ledger-git.dyne.org:2240/CoBox/cobox-crypto.git":
244
+"cobox-crypto@git+ssh://git@ledger-git.dyne.org:2240/CoBox/cobox-crypto.git#rename":
245 245
   version "1.1.0"
246
-  resolved "git+ssh://git@ledger-git.dyne.org:2240/CoBox/cobox-crypto.git#5a64d8b1ee3746f729cd0c69165d4417988903ab"
246
+  resolved "git+ssh://git@ledger-git.dyne.org:2240/CoBox/cobox-crypto.git#8c5c9e627dd84cbab7d78aa167723d466f05b96e"
247 247
   dependencies:
248 248
     assert "^2.0.0"
249 249
     hypercore-crypto "^1.0.0"