33const log = require ( 'debug' ) ( 'ipfs:components:init' )
44const PeerId = require ( 'peer-id' )
55const { Buffer } = require ( 'buffer' )
6- const PeerInfo = require ( 'peer-info' )
6+
77const mergeOptions = require ( 'merge-options' )
88const getDefaultConfig = require ( '../runtime/config-nodejs.js' )
99const createRepo = require ( '../runtime/repo-nodejs' )
10- const Keychain = require ( 'libp2p-keychain' )
11- const NoKeychain = require ( './no-keychain' )
1210const mortice = require ( 'mortice' )
1311const { DAGNode } = require ( 'ipld-dag-pb' )
1412const UnixFs = require ( 'ipfs-unixfs' )
@@ -80,7 +78,7 @@ module.exports = ({
8078 : await initNewRepo ( repo , { ...options , print } )
8179
8280 log ( 'peer created' )
83- const peerInfo = new PeerInfo ( peerId )
81+
8482 const blockService = new BlockService ( repo )
8583 const ipld = new Ipld ( getDefaultIpldOptions ( blockService , constructorOptions . ipld , log ) )
8684
@@ -142,7 +140,7 @@ module.exports = ({
142140 // Setup the offline routing for IPNS.
143141 // This is primarily used for offline ipns modifications, such as the initializeKeyspace feature.
144142 const offlineDatastore = new OfflineDatastore ( repo )
145- const ipns = new IPNS ( offlineDatastore , repo . datastore , peerInfo , keychain , { pass : options . pass } )
143+ const ipns = new IPNS ( offlineDatastore , repo . datastore , peerId , keychain , { pass : options . pass } )
146144 await ipns . initializeKeyspace ( peerId . privKey , emptyDirCid . toString ( ) )
147145 }
148146
@@ -158,7 +156,7 @@ module.exports = ({
158156 ipld,
159157 keychain,
160158 object,
161- peerInfo ,
159+ peerId ,
162160 pin,
163161 pinManager,
164162 preload,
@@ -190,7 +188,6 @@ async function initNewRepo (repo, { privateKey, emptyRepo, bits, profiles, confi
190188 }
191189
192190 const peerId = await createPeerId ( { privateKey, bits, print } )
193- let keychain = new NoKeychain ( )
194191
195192 log ( 'identity generated' )
196193
@@ -201,23 +198,32 @@ async function initNewRepo (repo, { privateKey, emptyRepo, bits, profiles, confi
201198
202199 privateKey = peerId . privKey
203200
204- config . Keychain = Keychain . generateOptions ( )
205-
206201 log ( 'peer identity: %s' , config . Identity . PeerID )
207202
208203 await repo . init ( config )
209204 await repo . open ( )
210205
211206 log ( 'repo opened' )
212207
213- if ( pass ) {
214- log ( 'creating keychain' )
215- const keychainOptions = { passPhrase : pass , ...config . Keychain }
216- keychain = new Keychain ( repo . keys , keychainOptions )
217- await keychain . importPeer ( 'self' , { privKey : privateKey } )
208+ // Create libp2p for Keychain creation
209+ const libp2p = Components . libp2p ( {
210+ peerId,
211+ repo,
212+ config,
213+ keychainConfig : {
214+ pass
215+ }
216+ } )
217+
218+ if ( libp2p . keychain && libp2p . keychain . opts ) {
219+ await libp2p . loadKeychain ( )
220+
221+ await repo . config . set ( 'Keychain' , {
222+ dek : libp2p . keychain . opts . dek
223+ } )
218224 }
219225
220- return { peerId, keychain }
226+ return { peerId, keychain : libp2p . keychain }
221227}
222228
223229async function initExistingRepo ( repo , { config : newConfig , profiles, pass } ) {
@@ -233,27 +239,21 @@ async function initExistingRepo (repo, { config: newConfig, profiles, pass }) {
233239 await repo . config . set ( config )
234240 }
235241
236- let keychain = new NoKeychain ( )
237-
238- if ( pass ) {
239- const keychainOptions = { passPhrase : pass , ...config . Keychain }
240- keychain = new Keychain ( repo . keys , keychainOptions )
241- log ( 'keychain constructed' )
242- }
243-
244242 const peerId = await PeerId . createFromPrivKey ( config . Identity . PrivKey )
245243
246- // Import the private key as 'self', if needed.
247- if ( pass ) {
248- try {
249- await keychain . findKeyByName ( 'self' )
250- } catch ( err ) {
251- log ( 'Creating "self" key' )
252- await keychain . importPeer ( 'self' , peerId )
244+ const libp2p = Components . libp2p ( {
245+ peerId ,
246+ repo ,
247+ config ,
248+ keychainConfig : {
249+ pass ,
250+ ... config . Keychain
253251 }
254- }
252+ } )
253+
254+ libp2p . keychain && await libp2p . loadKeychain ( )
255255
256- return { peerId, keychain }
256+ return { peerId, keychain : libp2p . keychain }
257257}
258258
259259function createPeerId ( { privateKey, bits, print } ) {
@@ -303,7 +303,7 @@ function createApi ({
303303 ipld,
304304 keychain,
305305 object,
306- peerInfo ,
306+ peerId ,
307307 pin,
308308 pinManager,
309309 preload,
@@ -337,7 +337,7 @@ function createApi ({
337337 dns : Components . dns ( ) ,
338338 files : Components . files ( { ipld, block, blockService, repo, preload, options : constructorOptions } ) ,
339339 get : Components . get ( { ipld, preload } ) ,
340- id : Components . id ( { peerInfo } ) ,
340+ id : Components . id ( { peerId } ) ,
341341 init : async ( ) => { throw new AlreadyInitializedError ( ) } , // eslint-disable-line require-await
342342 isOnline : Components . isOnline ( { } ) ,
343343 key : {
@@ -367,7 +367,7 @@ function createApi ({
367367 initOptions,
368368 ipld,
369369 keychain,
370- peerInfo ,
370+ peerId ,
371371 pinManager,
372372 preload,
373373 print,
@@ -383,7 +383,7 @@ function createApi ({
383383 addrs : notStarted ,
384384 connect : notStarted ,
385385 disconnect : notStarted ,
386- localAddrs : Components . swarm . localAddrs ( { peerInfo } ) ,
386+ localAddrs : Components . swarm . localAddrs ( { multiaddrs : [ ] } ) ,
387387 peers : notStarted
388388 } ,
389389 version : Components . version ( { repo } )
0 commit comments