Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for swarm keys #172

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* text=auto
test/test-repo/** text eol=lf
test/test-repo/** text eol=lf
test/fixtures/** text eol=lf
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('bignumber.js')
const pull = require('pull-stream')
const Key = require('interface-datastore').Key

const backends = require('./backends')
const version = require('./version')
Expand Down Expand Up @@ -323,6 +324,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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All keys should be on the keychain

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richardschneider this implementation is based off of the current implementation for private network keys in go, https://github.com/ipfs/go-ipfs/blob/07feeec9c4f40f59efb57d7e16739a35b4c42d5b/repo/fsrepo/fsrepo.go#L681-L695, which are not store in the key store.

Looking at the keychain it doesn't look like it currently has the ability to store this key type. Here is the working private network spec for reference https://github.com/libp2p/specs/blob/b1365bedcd46442074fbf96610f66b0663be088a/pnet/Private-Networks-PSK-V1.md

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 @@ -88,7 +89,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)
})
})
})