Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: init creates the keychain with --pass
Browse files Browse the repository at this point in the history
Creates the 'self' key, #1138
Fixes #1139
  • Loading branch information
richardschneider committed Dec 22, 2017
1 parent 3502d12 commit 864dfd8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
"build": "aegir build",
"test": "aegir test -t node -t browser --no-cors",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser -t webworker --no-cors",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser --no-cors",
"test:node:core": "aegir test -t node -f test/core/**.js",
"test:node:http": "aegir test -t node -f test/http-api/index.js",
"test:node:gateway": "aegir test -t node -f test/gateway/index.js",
"test:node:cli": "aegir test -t node -f test/cli/index.js",
"test:browser": "aegir test -t browser --no-cors",
"test:bootstrapers": "IPFS_TEST=bootstrapers aegir test -t browser -f test/bootstrapers.js",
"benchmark": "echo \"Error: no benchmarks yet\" && exit 1",
"benchmark:node": "echo \"Error: no benchmarks yet\" && exit 1",
Expand Down Expand Up @@ -60,7 +58,7 @@
},
"homepage": "https://github.com/ipfs/js-ipfs#readme",
"devDependencies": {
"aegir": "^12.2.0",
"aegir": "^12.3.0",
"buffer-loader": "0.0.1",
"chai": "^4.1.2",
"delay": "^2.0.0",
Expand Down Expand Up @@ -111,16 +109,16 @@
"ipfs-multipart": "~0.1.0",
"ipfs-repo": "~0.18.5",
"ipfs-unixfs": "~0.1.14",
"ipfs-unixfs-engine": "~0.24.1",
"ipfs-unixfs-engine": "~0.24.2",
"ipld-resolver": "~0.14.1",
"is-ipfs": "^0.3.2",
"is-stream": "^1.1.0",
"joi": "^13.0.2",
"libp2p": "~0.14.0",
"libp2p": "~0.14.3",
"libp2p-circuit": "~0.1.4",
"libp2p-floodsub": "~0.13.1",
"libp2p-kad-dht": "~0.6.0",
"libp2p-keychain": "~0.2.0",
"libp2p-keychain": "github:libp2p/js-libp2p-keychain#options",
"libp2p-mdns": "~0.9.1",
"libp2p-multiplex": "~0.5.1",
"libp2p-railing": "~0.7.1",
Expand All @@ -141,8 +139,8 @@
"once": "^1.4.0",
"path-exists": "^3.0.0",
"peer-book": "~0.5.2",
"peer-id": "~0.10.3",
"peer-info": "~0.11.3",
"peer-id": "~0.10.4",
"peer-info": "~0.11.4",
"progress": "^2.0.0",
"promisify-es6": "^1.0.3",
"pull-abortable": "^4.1.1",
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = {
node.init({
bits: argv.bits,
emptyRepo: argv.emptyRepo,
pass: argv.pass,
log: print
}, (err) => {
if (err) {
Expand Down
18 changes: 17 additions & 1 deletion src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const promisify = require('promisify-es6')
const config = require('../runtime/config-nodejs.json')
const Keychain = require('libp2p-keychain')

const addDefaultAssets = require('./init-assets')

Expand Down Expand Up @@ -36,7 +37,7 @@ module.exports = function init (self) {
opts.emptyRepo = opts.emptyRepo || false
opts.bits = Number(opts.bits) || 2048
opts.log = opts.log || function () {}

let privateKey
waterfall([
// Verify repo does not yet exist.
(cb) => self._repo.exists(cb),
Expand All @@ -57,6 +58,10 @@ module.exports = function init (self) {
PeerID: keys.toB58String(),
PrivKey: keys.privKey.bytes.toString('base64')
}
if (opts.pass) {
privateKey = keys.privKey
config.Keychain = Keychain.generateOptions()
}
opts.log('done')
opts.log('peer identity: ' + config.Identity.PeerID)

Expand All @@ -65,10 +70,21 @@ module.exports = function init (self) {
(_, cb) => self._repo.open(cb),
(cb) => {
self.log('repo opened')
if (opts.pass) {
self.log('creating keychain')
const keychainOptions = Object.assign({passPhrase: opts.pass}, config.Keychain)
const keychain = new Keychain(self._repo.keys, keychainOptions)
keychain.importPeer('self', { privKey: privateKey }, cb)
} else {
cb()
}
},
(_, cb) => {
if (opts.emptyRepo) {
return cb(null, true)
}

self.log('adding assets')
const tasks = [
// add empty unixfs dir object (go-ipfs assumes this exists)
(cb) => self.object.new('unixfs-dir', cb)
Expand Down
8 changes: 6 additions & 2 deletions src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ module.exports = function preStart (self) {
return (callback) => {
self.log('pre-start')

self._keychain = new Keychain(self._repo.keys, { passPhrase: self._options.pass || 'todo do not hardcode the pass phrase' })

waterfall([
(cb) => self._repo.config.get(cb),
(config, cb) => {
const pass = self._options.pass || 'todo do not hardcode the pass phrase'
const keychainOptions = Object.assign({passPhrase: pass}, config.Keychain)
self._keychain = new Keychain(self._repo.keys, keychainOptions)
cb(null, config)
},
(config, cb) => {
const privKey = config.Identity.PrivKey

Expand Down
9 changes: 6 additions & 3 deletions test/core/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const isNode = require('detect-node')
const hat = require('hat')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
Expand Down Expand Up @@ -36,7 +37,7 @@ describe('init', () => {
afterEach((done) => repo.teardown(done))

it('basic', (done) => {
ipfs.init({ bits: 512 }, (err) => {
ipfs.init({ bits: 512, pass: hat() }, (err) => {
expect(err).to.not.exist()

repo.exists((err, res) => {
Expand All @@ -45,7 +46,9 @@ describe('init', () => {

repo.config.get((err, config) => {
expect(err).to.not.exist()
console.log(config)
expect(config.Identity).to.exist()
expect(config.Keychain).to.exist()
done()
})
})
Expand All @@ -55,7 +58,7 @@ describe('init', () => {
it('set # of bits in key', function (done) {
this.timeout(40 * 1000)

ipfs.init({ bits: 1024 }, (err) => {
ipfs.init({ bits: 1024, pass: hat() }, (err) => {
expect(err).to.not.exist()

repo.config.get((err, config) => {
Expand All @@ -67,7 +70,7 @@ describe('init', () => {
})

it('init docs are written', (done) => {
ipfs.init({ bits: 512 }, (err) => {
ipfs.init({ bits: 512, pass: hat() }, (err) => {
expect(err).to.not.exist()
const multihash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'

Expand Down

0 comments on commit 864dfd8

Please sign in to comment.