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

Commit 2ee8003

Browse files
feat: auto upgrade to keychain
Older repos are automatically upgraded to a keychain when the '--pass' is present. Helps #1138
1 parent 75b83c8 commit 2ee8003

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/core/components/no-keychain.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
function fail () {
4-
throw new Error('Key management requires the daemon to run with \'--pass ...\'')
4+
throw new Error('Key management requires \'--pass ...\' option')
55
}
66

77
class NoKeychain {

src/core/components/pre-start.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,28 @@ module.exports = function preStart (self) {
1313
return (callback) => {
1414
self.log('pre-start')
1515

16+
const pass = self._options.pass
17+
let importSelf = false
1618
waterfall([
1719
(cb) => self._repo.config.get(cb),
1820
(config, cb) => {
19-
const pass = self._options.pass
21+
// Upgrade to keychain?
22+
if (!pass || config.Keychain) {
23+
return cb(null, config)
24+
}
25+
config.Keychain = Keychain.generateOptions()
26+
self.config.set('Keychain', config.Keychain, (err) => {
27+
if (err) return cb(err)
28+
const keychainOptions = Object.assign({passPhrase: pass}, config.Keychain)
29+
self._keychain = new Keychain(self._repo.keys, keychainOptions)
30+
importSelf = true
31+
self.log('Upgrade repo for a keychain')
32+
cb(null, config)
33+
})
34+
},
35+
(config, cb) => {
2036
if (self._keychain) {
21-
// most likely an init has happened
37+
// most likely an init or upgrade has happened
2238
} else if (pass) {
2339
const keychainOptions = Object.assign({passPhrase: pass}, config.Keychain)
2440
self._keychain = new Keychain(self._repo.keys, keychainOptions)
@@ -32,7 +48,12 @@ module.exports = function preStart (self) {
3248
(config, cb) => {
3349
const privKey = config.Identity.PrivKey
3450

35-
peerId.createFromPrivKey(privKey, (err, id) => cb(err, config, id))
51+
peerId.createFromPrivKey(privKey, (err, id) => {
52+
if (!err && importSelf) {
53+
return self._keychain.importPeer('self', id, (err) => cb(err, config, id))
54+
}
55+
cb(err, config, id)
56+
})
3657
},
3758
(config, id, cb) => {
3859
self.log('peer created')

0 commit comments

Comments
 (0)