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

Commit d88de11

Browse files
committed
start integrating libp2p into the mix, start the swarm listener on jsipfs daemon
1 parent dab845f commit d88de11

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@
7474
"ipfs-merkle-dag": "^0.2.1",
7575
"ipfs-repo": "^0.5.0",
7676
"joi": "^8.0.2",
77+
"libp2p-ipfs": "^0.1.0",
7778
"lodash.get": "^4.0.0",
7879
"lodash.set": "^4.0.0",
80+
"multiaddr": "^1.1.1",
7981
"peer-id": "^0.5.0",
82+
"peer-info": "^0.4.0",
8083
"ronin": "^0.3.11"
8184
}
8285
}

src/http-api/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ exports.start = (callback) => {
4848
// load routes
4949
require('./routes')
5050

51+
// start libp2p
52+
ipfs.libp2p.start({}, (err) => {
53+
if (err) {
54+
console.log(err)
55+
}
56+
ipfs.nodeInfo.multiaddrs.forEach((multiaddr) => {
57+
console.log('Swarm listening on', multiaddr.toString())
58+
})
59+
})
60+
61+
// start http-api
5162
server.start((err) => {
5263
if (err) {
5364
return callback(err)

src/ipfs-core/index.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
'use strict'
2-
31
const defaultRepo = require('./default-repo')
4-
// const bl = require('bl')
52
const blocks = require('ipfs-blocks')
63
const BlockService = blocks.BlockService
74
const Block = blocks.Block
85
const mDAG = require('ipfs-merkle-dag')
96
const DAGNode = mDAG.DAGNode
107
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')
1212

1313
exports = module.exports = IPFS
1414

@@ -24,6 +24,8 @@ function IPFS (repo) {
2424
const blockS = new BlockService(repo)
2525
const dagS = new DAGService(blockS)
2626

27+
var libp2pNode
28+
2729
this.version = (opts, callback) => {
2830
if (typeof opts === 'function') {
2931
callback = opts
@@ -53,7 +55,7 @@ function IPFS (repo) {
5355
if (err) {
5456
return callback(err)
5557
}
56-
var pid = peerId.createFromPrivKey(config.Identity.PrivKey)
58+
var pid = PeerId.createFromPrivKey(config.Identity.PrivKey)
5759
callback(null, {
5860
ID: config.Identity.PeerID,
5961
PublicKey: pid.pubKey,
@@ -124,7 +126,7 @@ function IPFS (repo) {
124126
replace: (config, callback) => {
125127
repo.config.set(config, callback)
126128
},
127-
show: callback => {
129+
show: (callback) => {
128130
repo.config.get((err, config) => {
129131
if (err) { return callback(err) }
130132
callback(null, config)
@@ -281,4 +283,28 @@ function IPFS (repo) {
281283
})
282284
}
283285
}
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+
}
284310
}

0 commit comments

Comments
 (0)