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 Jan 27, 2018
1 parent 9799d99 commit c54be1c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
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 c54be1c

Please sign in to comment.