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

fix: allow keychain interactions without a password #1548

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/commands/key/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
if (err) {
throw err
}
print(`generated ${key.id} ${key.name}`)
print(key.id)
})
}
}
15 changes: 13 additions & 2 deletions src/cli/commands/key/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ module.exports = {

describe: 'List all local keys',

builder: {},
builder: {
long: {
alias: 'l',
describe: 'Show extra information about keys',
default: false
}
},

handler (argv) {
argv.ipfs.key.list((err, keys) => {
if (err) {
throw err
}
keys.forEach((ki) => print(`${ki.id} ${ki.name}`))

if (argv.long) {
keys.forEach((ki) => print(`${ki.id} ${ki.name}`))
} else {
keys.forEach((ki) => print(ki.name))
}
})
}
}
18 changes: 7 additions & 11 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ module.exports = function init (self) {
PrivKey: peerId.privKey.bytes.toString('base64')
}
privateKey = peerId.privKey
if (opts.pass) {
config.Keychain = Keychain.generateOptions()
}

config.Keychain = Keychain.generateOptions()

opts.log('done')
opts.log('peer identity: ' + config.Identity.PeerID)

Expand All @@ -94,14 +94,10 @@ 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)
self._keychain = new Keychain(self._repo.keys, keychainOptions)
self._keychain.importPeer('self', { privKey: privateKey }, cb)
} else {
cb(null, true)
}
self.log('creating keychain')
const keychainOptions = Object.assign({passPhrase: opts.pass}, config.Keychain)
self._keychain = new Keychain(self._repo.keys, keychainOptions)
self._keychain.importPeer('self', { privKey: privateKey }, cb)
},
// add empty unixfs dir object (go-ipfs assumes this exists)
(_, cb) => {
Expand Down
24 changes: 0 additions & 24 deletions src/core/components/no-keychain.js

This file was deleted.

8 changes: 1 addition & 7 deletions src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const multiaddr = require('multiaddr')
const waterfall = require('async/waterfall')
const Keychain = require('libp2p-keychain')
const extend = require('deep-extend')
const NoKeychain = require('./no-keychain')
/*
* Load stuff from Repo into memory
*/
Expand Down Expand Up @@ -45,15 +44,10 @@ module.exports = function preStart (self) {
},
(config, cb) => {
// Construct the keychain
if (self._keychain) {
// most likely an init or upgrade has happened
} else if (pass) {
if (!self._keychain) {
const keychainOptions = Object.assign({passPhrase: pass}, config.Keychain)
self._keychain = new Keychain(self._repo.keys, keychainOptions)
self.log('keychain constructed')
} else {
self._keychain = new NoKeychain()
self.log('no keychain, use --pass')
}
cb(null, config)
},
Expand Down
12 changes: 11 additions & 1 deletion test/cli/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const expect = require('chai').expect
const runOnAndOff = require('../utils/on-and-off')
const hat = require('hat')

describe('key', () => runOnAndOff.off((thing) => {
describe.only('key', () => runOnAndOff.off((thing) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder to remove this before merge ;)

const name = 'test-key-' + hat()
const newName = 'test-key-' + hat()
const pass = '--pass ' + hat()
Expand All @@ -19,6 +19,7 @@ describe('key', () => runOnAndOff.off((thing) => {
this.timeout(40 * 1000)

return ipfs(`${pass} key gen ${name} --type rsa --size 2048`)
.then(() => ipfs(`${pass} key list -l`))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change changes the assertion as now it's testing on the output of key list rather that the name is included in the output when running key gen

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js-ipfs used to do:

$ jsipfs key gen foobar
generated Qmb1Wj3YR4PGgscvXJ3JgaiX6GCQFXLwP6Qo7c5kNR6PQK foobar

but to bring it inline with go-ipfs it now outputs:

$ jsipfs key gen foobar
Qmb1Wj3YR4PGgscvXJ3JgaiX6GCQFXLwP6Qo7c5kNR6PQK

so the name is no longer in the output and the test would fail. In order to verify that we have generated a key with the passed name, I changed the test to examine the output of jsipfs key list after generating the key which includes the name.

.then((out) => {
expect(out).to.include(name)
})
Expand All @@ -33,6 +34,15 @@ describe('key', () => runOnAndOff.off((thing) => {
})
})

it('list without password', function () {
this.timeout(20 * 1000)

return ipfs(`key list`)
.then((out) => {
expect(out).to.include(name)
})
})

it('rename', function () {
this.timeout(20 * 1000)

Expand Down