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

Commit c54be1c

Browse files
feat: init creates the keychain with --pass
Creates the 'self' key, #1138 Fixes #1139
1 parent 9799d99 commit c54be1c

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

src/cli/commands/init.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = {
3838
node.init({
3939
bits: argv.bits,
4040
emptyRepo: argv.emptyRepo,
41+
pass: argv.pass,
4142
log: print
4243
}, (err) => {
4344
if (err) {

src/core/components/init.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const waterfall = require('async/waterfall')
55
const parallel = require('async/parallel')
66
const promisify = require('promisify-es6')
77
const config = require('../runtime/config-nodejs.json')
8+
const Keychain = require('libp2p-keychain')
89

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

@@ -36,7 +37,7 @@ module.exports = function init (self) {
3637
opts.emptyRepo = opts.emptyRepo || false
3738
opts.bits = Number(opts.bits) || 2048
3839
opts.log = opts.log || function () {}
39-
40+
let privateKey
4041
waterfall([
4142
// Verify repo does not yet exist.
4243
(cb) => self._repo.exists(cb),
@@ -57,6 +58,10 @@ module.exports = function init (self) {
5758
PeerID: keys.toB58String(),
5859
PrivKey: keys.privKey.bytes.toString('base64')
5960
}
61+
if (opts.pass) {
62+
privateKey = keys.privKey
63+
config.Keychain = Keychain.generateOptions()
64+
}
6065
opts.log('done')
6166
opts.log('peer identity: ' + config.Identity.PeerID)
6267

@@ -65,10 +70,21 @@ module.exports = function init (self) {
6570
(_, cb) => self._repo.open(cb),
6671
(cb) => {
6772
self.log('repo opened')
73+
if (opts.pass) {
74+
self.log('creating keychain')
75+
const keychainOptions = Object.assign({passPhrase: opts.pass}, config.Keychain)
76+
const keychain = new Keychain(self._repo.keys, keychainOptions)
77+
keychain.importPeer('self', { privKey: privateKey }, cb)
78+
} else {
79+
cb()
80+
}
81+
},
82+
(_, cb) => {
6883
if (opts.emptyRepo) {
6984
return cb(null, true)
7085
}
7186

87+
self.log('adding assets')
7288
const tasks = [
7389
// add empty unixfs dir object (go-ipfs assumes this exists)
7490
(cb) => self.object.new('unixfs-dir', cb)

src/core/components/pre-start.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ module.exports = function preStart (self) {
1313
return (callback) => {
1414
self.log('pre-start')
1515

16-
self._keychain = new Keychain(self._repo.keys, { passPhrase: self._options.pass || 'todo do not hardcode the pass phrase' })
17-
1816
waterfall([
1917
(cb) => self._repo.config.get(cb),
18+
(config, cb) => {
19+
const pass = self._options.pass || 'todo do not hardcode the pass phrase'
20+
const keychainOptions = Object.assign({passPhrase: pass}, config.Keychain)
21+
self._keychain = new Keychain(self._repo.keys, keychainOptions)
22+
cb(null, config)
23+
},
2024
(config, cb) => {
2125
const privKey = config.Identity.PrivKey
2226

test/core/init.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai')
77
const expect = chai.expect
88
chai.use(dirtyChai)
99
const isNode = require('detect-node')
10+
const hat = require('hat')
1011
const PeerId = require('peer-id')
1112
const PeerInfo = require('peer-info')
1213
const multiaddr = require('multiaddr')
@@ -36,7 +37,7 @@ describe('init', () => {
3637
afterEach((done) => repo.teardown(done))
3738

3839
it('basic', (done) => {
39-
ipfs.init({ bits: 512 }, (err) => {
40+
ipfs.init({ bits: 512, pass: hat() }, (err) => {
4041
expect(err).to.not.exist()
4142

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

4647
repo.config.get((err, config) => {
4748
expect(err).to.not.exist()
49+
console.log(config)
4850
expect(config.Identity).to.exist()
51+
expect(config.Keychain).to.exist()
4952
done()
5053
})
5154
})
@@ -55,7 +58,7 @@ describe('init', () => {
5558
it('set # of bits in key', function (done) {
5659
this.timeout(40 * 1000)
5760

58-
ipfs.init({ bits: 1024 }, (err) => {
61+
ipfs.init({ bits: 1024, pass: hat() }, (err) => {
5962
expect(err).to.not.exist()
6063

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

6972
it('init docs are written', (done) => {
70-
ipfs.init({ bits: 512 }, (err) => {
73+
ipfs.init({ bits: 512, pass: hat() }, (err) => {
7174
expect(err).to.not.exist()
7275
const multihash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
7376

0 commit comments

Comments
 (0)