1
- 'use strict'
2
-
3
1
const defaultRepo = require ( './default-repo' )
4
- // const bl = require('bl')
5
2
const blocks = require ( 'ipfs-blocks' )
6
3
const BlockService = blocks . BlockService
7
4
const Block = blocks . Block
8
5
const mDAG = require ( 'ipfs-merkle-dag' )
9
6
const DAGNode = mDAG . DAGNode
10
7
const DAGService = mDAG . DAGService
11
- const peerId = require ( 'peer-id' )
8
+ const PeerId = require ( 'peer-id' )
9
+ const PeerInfo = require ( 'peer-info' )
10
+ const libp2p = require ( 'libp2p-ipfs' )
11
+ const multiaddr = require ( 'multiaddr' )
12
12
13
13
exports = module . exports = IPFS
14
14
@@ -24,6 +24,8 @@ function IPFS (repo) {
24
24
const blockS = new BlockService ( repo )
25
25
const dagS = new DAGService ( blockS )
26
26
27
+ var libp2pNode
28
+
27
29
this . version = ( opts , callback ) => {
28
30
if ( typeof opts === 'function' ) {
29
31
callback = opts
@@ -53,7 +55,7 @@ function IPFS (repo) {
53
55
if ( err ) {
54
56
return callback ( err )
55
57
}
56
- var pid = peerId . createFromPrivKey ( config . Identity . PrivKey )
58
+ var pid = PeerId . createFromPrivKey ( config . Identity . PrivKey )
57
59
callback ( null , {
58
60
ID : config . Identity . PeerID ,
59
61
PublicKey : pid . pubKey ,
@@ -124,7 +126,7 @@ function IPFS (repo) {
124
126
replace : ( config , callback ) => {
125
127
repo . config . set ( config , callback )
126
128
} ,
127
- show : callback => {
129
+ show : ( callback ) => {
128
130
repo . config . get ( ( err , config ) => {
129
131
if ( err ) { return callback ( err ) }
130
132
callback ( null , config )
@@ -281,4 +283,28 @@ function IPFS (repo) {
281
283
} )
282
284
}
283
285
}
286
+
287
+ this . libp2p = {
288
+ start : ( options , callback ) => {
289
+ // create libp2pNode and make it to listen
290
+ // patch this.libp2p with .routing .records and .swarm
291
+ this . config . show ( ( err , config ) => {
292
+ if ( err ) {
293
+ return callback ( err )
294
+ }
295
+ options . multiaddrs = { tcp : multiaddr ( config . Addresses . Swarm [ 0 ] ) }
296
+ this . nodeId = PeerId . createFromPrivKey ( config . Identity . PrivKey )
297
+
298
+ this . nodeInfo = new PeerInfo ( this . nodeId , [ ] )
299
+ options . peer = this . nodeInfo
300
+
301
+ libp2pNode = libp2p ( options )
302
+ this . libp2p . swarm = libp2pNode . swarm
303
+ this . libp2p . routing = libp2pNode . routing
304
+ this . libp2p . records = libp2pNode . records
305
+ libp2pNode . listen ( callback )
306
+ } )
307
+ } ,
308
+ stop : ( options , callback ) => { }
309
+ }
284
310
}
0 commit comments