22
33const log = require ( 'debug' ) ( 'ipfs:components:init' )
44const PeerId = require ( 'peer-id' )
5- const PeerInfo = require ( 'peer-info' )
65const mergeOptions = require ( 'merge-options' )
76const getDefaultConfig = require ( '../runtime/config-nodejs.js' )
87const createRepo = require ( '../runtime/repo-nodejs' )
9- const Keychain = require ( 'libp2p-keychain' )
10- const NoKeychain = require ( './no-keychain' )
118const mortice = require ( 'mortice' )
129const { DAGNode } = require ( 'ipld-dag-pb' )
1310const UnixFs = require ( 'ipfs-unixfs' )
@@ -79,7 +76,7 @@ module.exports = ({
7976 : await initNewRepo ( repo , { ...options , print } )
8077
8178 log ( 'peer created' )
82- const peerInfo = new PeerInfo ( peerId )
79+
8380 const blockService = new BlockService ( repo )
8481 const ipld = new Ipld ( getDefaultIpldOptions ( blockService , constructorOptions . ipld , log ) )
8582
@@ -141,7 +138,7 @@ module.exports = ({
141138 // Setup the offline routing for IPNS.
142139 // This is primarily used for offline ipns modifications, such as the initializeKeyspace feature.
143140 const offlineDatastore = new OfflineDatastore ( repo )
144- const ipns = new IPNS ( offlineDatastore , repo . datastore , peerInfo , keychain , { pass : options . pass } )
141+ const ipns = new IPNS ( offlineDatastore , repo . datastore , peerId , keychain , { pass : options . pass } )
145142 await ipns . initializeKeyspace ( peerId . privKey , emptyDirCid . toString ( ) )
146143 }
147144
@@ -157,7 +154,7 @@ module.exports = ({
157154 ipld,
158155 keychain,
159156 object,
160- peerInfo ,
157+ peerId ,
161158 pin,
162159 pinManager,
163160 preload,
@@ -189,7 +186,6 @@ async function initNewRepo (repo, { privateKey, emptyRepo, bits, profiles, confi
189186 }
190187
191188 const peerId = await createPeerId ( { privateKey, bits, print } )
192- let keychain = new NoKeychain ( )
193189
194190 log ( 'identity generated' )
195191
@@ -200,23 +196,32 @@ async function initNewRepo (repo, { privateKey, emptyRepo, bits, profiles, confi
200196
201197 privateKey = peerId . privKey
202198
203- config . Keychain = Keychain . generateOptions ( )
204-
205199 log ( 'peer identity: %s' , config . Identity . PeerID )
206200
207201 await repo . init ( config )
208202 await repo . open ( )
209203
210204 log ( 'repo opened' )
211205
212- if ( pass ) {
213- log ( 'creating keychain' )
214- const keychainOptions = { passPhrase : pass , ...config . Keychain }
215- keychain = new Keychain ( repo . keys , keychainOptions )
216- await keychain . importPeer ( 'self' , { privKey : privateKey } )
206+ // Create libp2p for Keychain creation
207+ const libp2p = Components . libp2p ( {
208+ peerId,
209+ repo,
210+ config,
211+ keychainConfig : {
212+ pass
213+ }
214+ } )
215+
216+ if ( libp2p . keychain && libp2p . keychain . opts ) {
217+ await libp2p . loadKeychain ( )
218+
219+ await repo . config . set ( 'Keychain' , {
220+ dek : libp2p . keychain . opts . dek
221+ } )
217222 }
218223
219- return { peerId, keychain }
224+ return { peerId, keychain : libp2p . keychain }
220225}
221226
222227async function initExistingRepo ( repo , { config : newConfig , profiles, pass } ) {
@@ -232,27 +237,21 @@ async function initExistingRepo (repo, { config: newConfig, profiles, pass }) {
232237 await repo . config . set ( config )
233238 }
234239
235- let keychain = new NoKeychain ( )
236-
237- if ( pass ) {
238- const keychainOptions = { passPhrase : pass , ...config . Keychain }
239- keychain = new Keychain ( repo . keys , keychainOptions )
240- log ( 'keychain constructed' )
241- }
242-
243240 const peerId = await PeerId . createFromPrivKey ( config . Identity . PrivKey )
244241
245- // Import the private key as 'self', if needed.
246- if ( pass ) {
247- try {
248- await keychain . findKeyByName ( 'self' )
249- } catch ( err ) {
250- log ( 'Creating "self" key' )
251- await keychain . importPeer ( 'self' , peerId )
242+ const libp2p = Components . libp2p ( {
243+ peerId ,
244+ repo ,
245+ config ,
246+ keychainConfig : {
247+ pass ,
248+ ... config . Keychain
252249 }
253- }
250+ } )
251+
252+ libp2p . keychain && await libp2p . loadKeychain ( )
254253
255- return { peerId, keychain }
254+ return { peerId, keychain : libp2p . keychain }
256255}
257256
258257function createPeerId ( { privateKey, bits, print } ) {
@@ -302,7 +301,7 @@ function createApi ({
302301 ipld,
303302 keychain,
304303 object,
305- peerInfo ,
304+ peerId ,
306305 pin,
307306 pinManager,
308307 preload,
@@ -336,7 +335,7 @@ function createApi ({
336335 dns : Components . dns ( ) ,
337336 files : Components . files ( { ipld, block, blockService, repo, preload, options : constructorOptions } ) ,
338337 get : Components . get ( { ipld, preload } ) ,
339- id : Components . id ( { peerInfo } ) ,
338+ id : Components . id ( { peerId } ) ,
340339 init : async ( ) => { throw new AlreadyInitializedError ( ) } , // eslint-disable-line require-await
341340 isOnline : Components . isOnline ( { } ) ,
342341 key : {
@@ -366,7 +365,7 @@ function createApi ({
366365 initOptions,
367366 ipld,
368367 keychain,
369- peerInfo ,
368+ peerId ,
370369 pinManager,
371370 preload,
372371 print,
@@ -382,7 +381,7 @@ function createApi ({
382381 addrs : notStarted ,
383382 connect : notStarted ,
384383 disconnect : notStarted ,
385- localAddrs : Components . swarm . localAddrs ( { peerInfo } ) ,
384+ localAddrs : Components . swarm . localAddrs ( { multiaddrs : [ ] } ) ,
386385 peers : notStarted
387386 } ,
388387 version : Components . version ( { repo } )
0 commit comments