From c54be1cb25334821e1a6cda127f02721b4e6673a Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 23 Dec 2017 11:13:29 +1300 Subject: [PATCH] feat: init creates the keychain with --pass Creates the 'self' key, #1138 Fixes #1139 --- src/cli/commands/init.js | 1 + src/core/components/init.js | 18 +++++++++++++++++- src/core/components/pre-start.js | 8 ++++++-- test/core/init.spec.js | 9 ++++++--- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index b53639fec0..741a313750 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -38,6 +38,7 @@ module.exports = { node.init({ bits: argv.bits, emptyRepo: argv.emptyRepo, + pass: argv.pass, log: print }, (err) => { if (err) { diff --git a/src/core/components/init.js b/src/core/components/init.js index 87e9072811..1364aee782 100644 --- a/src/core/components/init.js +++ b/src/core/components/init.js @@ -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') @@ -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), @@ -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) @@ -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) diff --git a/src/core/components/pre-start.js b/src/core/components/pre-start.js index 8e478b5d3d..ff7c8c7032 100644 --- a/src/core/components/pre-start.js +++ b/src/core/components/pre-start.js @@ -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 diff --git a/test/core/init.spec.js b/test/core/init.spec.js index 07990027f2..8adaad85e8 100644 --- a/test/core/init.spec.js +++ b/test/core/init.spec.js @@ -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') @@ -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) => { @@ -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() }) }) @@ -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) => { @@ -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'