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

Commit 864dfd8

Browse files
feat: init creates the keychain with --pass
Creates the 'self' key, #1138 Fixes #1139
1 parent 3502d12 commit 864dfd8

File tree

5 files changed

+37
-15
lines changed

5 files changed

+37
-15
lines changed

package.json

+7-9
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
"build": "aegir build",
2424
"test": "aegir test -t node -t browser --no-cors",
2525
"test:node": "aegir test -t node",
26-
"test:browser": "aegir test -t browser -t webworker --no-cors",
27-
"test:node": "aegir test -t node",
26+
"test:browser": "aegir test -t browser --no-cors",
2827
"test:node:core": "aegir test -t node -f test/core/**.js",
2928
"test:node:http": "aegir test -t node -f test/http-api/index.js",
3029
"test:node:gateway": "aegir test -t node -f test/gateway/index.js",
3130
"test:node:cli": "aegir test -t node -f test/cli/index.js",
32-
"test:browser": "aegir test -t browser --no-cors",
3331
"test:bootstrapers": "IPFS_TEST=bootstrapers aegir test -t browser -f test/bootstrapers.js",
3432
"benchmark": "echo \"Error: no benchmarks yet\" && exit 1",
3533
"benchmark:node": "echo \"Error: no benchmarks yet\" && exit 1",
@@ -60,7 +58,7 @@
6058
},
6159
"homepage": "https://github.com/ipfs/js-ipfs#readme",
6260
"devDependencies": {
63-
"aegir": "^12.2.0",
61+
"aegir": "^12.3.0",
6462
"buffer-loader": "0.0.1",
6563
"chai": "^4.1.2",
6664
"delay": "^2.0.0",
@@ -111,16 +109,16 @@
111109
"ipfs-multipart": "~0.1.0",
112110
"ipfs-repo": "~0.18.5",
113111
"ipfs-unixfs": "~0.1.14",
114-
"ipfs-unixfs-engine": "~0.24.1",
112+
"ipfs-unixfs-engine": "~0.24.2",
115113
"ipld-resolver": "~0.14.1",
116114
"is-ipfs": "^0.3.2",
117115
"is-stream": "^1.1.0",
118116
"joi": "^13.0.2",
119-
"libp2p": "~0.14.0",
117+
"libp2p": "~0.14.3",
120118
"libp2p-circuit": "~0.1.4",
121119
"libp2p-floodsub": "~0.13.1",
122120
"libp2p-kad-dht": "~0.6.0",
123-
"libp2p-keychain": "~0.2.0",
121+
"libp2p-keychain": "github:libp2p/js-libp2p-keychain#options",
124122
"libp2p-mdns": "~0.9.1",
125123
"libp2p-multiplex": "~0.5.1",
126124
"libp2p-railing": "~0.7.1",
@@ -141,8 +139,8 @@
141139
"once": "^1.4.0",
142140
"path-exists": "^3.0.0",
143141
"peer-book": "~0.5.2",
144-
"peer-id": "~0.10.3",
145-
"peer-info": "~0.11.3",
142+
"peer-id": "~0.10.4",
143+
"peer-info": "~0.11.4",
146144
"progress": "^2.0.0",
147145
"promisify-es6": "^1.0.3",
148146
"pull-abortable": "^4.1.1",

src/cli/commands/init.js

+1
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

+17-1
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

+6-2
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

+6-3
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)