Skip to content

Commit

Permalink
feat: keystore (#155)
Browse files Browse the repository at this point in the history
* feat: add a key store container

* feat: open and close the key store

* docs(readme): mention the key store
  • Loading branch information
richardschneider authored and daviddias committed Dec 5, 2017
1 parent f4e0ccf commit 27df24d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ This now has created the following structure, either on disk or as an in memory
│ └── _README
├── config
├── datastore
├── keys
└── version
```

Expand All @@ -145,6 +146,7 @@ Arguments:
* `storageBackends` (object, optional): may contain the following values, which should each be a class implementing the [datastore interface](https://github.com/ipfs/interface-datastore#readme):
* `root` (defaults to [`datastore-fs`](https://github.com/ipfs/js-datastore-fs#readme) in Node.js and [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme) in the browser). Defines the back-end type used for gets and puts of values at the root (`repo.set()`, `repo.get()`)
* `blocks` (defaults to [`datastore-fs`](https://github.com/ipfs/js-datastore-fs#readme) in Node.js and [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme) in the browser). Defines the back-end type used for gets and puts of values at `repo.blocks`.
* `keys` (defaults to [`datastore-fs`](https://github.com/ipfs/js-datastore-fs#readme) in Node.js and [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme) in the browser). Defines the back-end type used for gets and puts of encrypted keys at `repo.keys`
* `datastore` (defaults to [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme)). Defines the back-end type used as the key-valye store used for gets and puts of values at `repo.datastore`.

```js
Expand Down
5 changes: 5 additions & 0 deletions src/default-options-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
storageBackends: {
root: require('datastore-level'),
blocks: require('datastore-level'),
keys: require('datastore-level'),
datastore: require('datastore-level')
},
storageBackendOptions: {
Expand All @@ -17,6 +18,10 @@ module.exports = {
sharding: false,
db: require('level-js')
},
keys: {
sharding: false,
db: require('level-js')
},
datastore: {
db: require('level-js')
}
Expand Down
3 changes: 3 additions & 0 deletions src/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
storageBackends: {
root: require('datastore-fs'),
blocks: require('datastore-fs'),
keys: require('datastore-fs'),
datastore: require('datastore-level')
},
storageBackendOptions: {
Expand All @@ -15,6 +16,8 @@ module.exports = {
blocks: {
sharding: true,
extension: '.data'
},
keys: {
}
}
}
15 changes: 14 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ class IpfsRepo {
this.blocks = blocks
cb()
},
(cb) => {
log('creating keystore')
const keysBaseStore = backends.create('keys', path.join(this.path, 'keys'), this.options)
blockstore(
keysBaseStore,
this.options.storageBackendOptions.keys,
cb)
},
(keys, cb) => {
this.keys = keys
cb()
},

(cb) => {
this.closed = false
log('all opened')
Expand Down Expand Up @@ -169,7 +182,7 @@ class IpfsRepo {
(cb) => this.apiAddr.delete(ignoringNotFound(cb)),
(cb) => {
each(
[this.blocks, this.datastore],
[this.blocks, this.keys, this.datastore],
(store, callback) => store.close(callback),
cb)
},
Expand Down
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ describe('IPFS Repo Tests on the Browser', () => {
require('./repo-test')(repo)
require('./blockstore-test')(repo)
require('./datastore-test')(repo)
require('./keystore-test')(repo)
})
15 changes: 15 additions & 0 deletions test/keystore-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect

module.exports = (repo) => {
describe('keystore', () => {
it('exists', () => {
expect(repo).to.have.property('keys')
})
})
}
1 change: 1 addition & 0 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('IPFS Repo Tests onNode.js', () => {
require('./repo-test')(repo)
require('./blockstore-test')(repo)
require('./datastore-test')(repo)
require('./keystore-test')(repo)
if (!r.init) {
require('./interop-test')(repo)
}
Expand Down
4 changes: 4 additions & 0 deletions test/options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ function expectedRepoOptions () {
// equivalents via package.browser
root: require('datastore-fs'),
blocks: require('datastore-fs'),
keys: require('datastore-fs'),
datastore: require('datastore-level')
},
storageBackendOptions: {
root: {
extension: ''
},
keys: {},
blocks: {
sharding: true,
extension: '.data'
Expand All @@ -55,6 +57,8 @@ function expectedRepoOptions () {

if (process.browser) {
options.storageBackendOptions.root.db = require('leveldown')
options.storageBackendOptions.keys.db = require('leveldown')
options.storageBackendOptions.keys.sharding = false
options.storageBackendOptions.blocks.db = require('leveldown')
delete options.storageBackendOptions.blocks.extension
options.storageBackendOptions.blocks.sharding = false
Expand Down

0 comments on commit 27df24d

Please sign in to comment.