Skip to content

Commit

Permalink
feat: add swarm key getter method
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
jacobheun committed Sep 19, 2018
1 parent 92e858c commit faacb30
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/errors/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'

exports.ERR_REPO_NOT_INITIALIZED = 'ERR_REPO_NOT_INITIALIZED'
exports.SWARM_KEY_NOT_FOUND = 'No swarm key found'
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path')
const debug = require('debug')
const Big = require('big.js')
const pull = require('pull-stream')
const Key = require('interface-datastore').Key

const backends = require('./backends')
const version = require('./version')
Expand Down Expand Up @@ -337,6 +338,22 @@ class IpfsRepo {
})
})
}

/**
* Gets the swarm.key buffer, if it exists
*
* @param {function(Error, Buffer)} callback
* @returns {void}
*/
swarmKey (callback) {
const swarmKeyKey = new Key('swarm.key')
this.root.get(swarmKeyKey, (err, swarmKeyBuffer) => {
if (err) {
return callback(new Error(ERRORS.SWARM_KEY_NOT_FOUND))
}
callback(null, swarmKeyBuffer)
})
}
}

function getSize (queryFn, callback) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/pnet-repo/swarm.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/key/swarm/psk/1.0.0/
/base16/
a4ec738d91ab0fbce75dfc29e8f5b6d75830aff1b76afeb51cd602a4b764aee8
5 changes: 4 additions & 1 deletion test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const IPFSRepo = require('../src')

describe('IPFS Repo Tests onNode.js', () => {
require('./options-test')
require('./pnet-test')

const customLock = {
lockName: 'test.lock',
Expand Down Expand Up @@ -83,7 +84,9 @@ describe('IPFS Repo Tests onNode.js', () => {
}
},
(cb) => repo.open(cb)
], done)
], (err) => {
done(err)
})
})

after((done) => {
Expand Down
55 changes: 55 additions & 0 deletions test/pnet-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const path = require('path')
const ncp = require('ncp').ncp
const rimraf = require('rimraf')
const series = require('async/series')

const Repo = require('../')
const Errors = require('../').errors

describe('private network', () => {
describe('swarm key', () => {
const repoPath = path.join(__dirname, 'slash', 'path')
const swarmRepoPath = path.join(__dirname, 'fixtures/pnet-repo')

afterEach(() => {
rimraf.sync(repoPath)
})

it('returns an error if no swarm key is found', (done) => {
const repo = new Repo(repoPath)

repo.swarmKey((err, swarmKeyBuffer) => {
expect(swarmKeyBuffer).to.not.exist()
expect(err).to.include({
message: Errors.SWARM_KEY_NOT_FOUND
})
done()
})
})

it('gets the swarm.key', (done) => {
const repo = new Repo(repoPath)

series([
(cb) => {
// copy the swarm key to the repo root
ncp(swarmRepoPath, repoPath, cb)
},
(cb) => {
repo.swarmKey((err, swarmKeyBuffer) => {
expect(err).to.not.exist()
expect(Buffer.isBuffer(swarmKeyBuffer)).to.equal(true)
expect(swarmKeyBuffer.byteLength).to.equal(95)
cb()
})
}
], done)
})
})
})

0 comments on commit faacb30

Please sign in to comment.