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

Commit 09329f3

Browse files
committed
feat(swarm): interface-ipfs-core swarm compatibility
1 parent 9b7ec1d commit 09329f3

File tree

5 files changed

+50
-25
lines changed

5 files changed

+50
-25
lines changed

src/core/ipfs/id.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ module.exports = function id (self) {
1818
callback(null, {
1919
id: self._peerInfo.id.toB58String(),
2020
publicKey: self._peerInfo.id.pubKey.bytes.toString('base64'),
21-
addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }).sort(),
21+
addresses: self._peerInfo.multiaddrs.map((ma) => {
22+
const addr = ma.toString() + '/ipfs/' + self._peerInfo.id.toB58String()
23+
return addr
24+
}).sort(),
2225
agentVersion: 'js-ipfs',
2326
protocolVersion: '9000'
2427
})

src/core/ipfs/libp2p.js

+37-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multiaddr = require('multiaddr')
44
const Libp2pNode = require('libp2p-ipfs').Node
5+
const promisify = require('promisify-es6')
56

67
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
78

@@ -31,29 +32,50 @@ module.exports = function libp2p (self) {
3132
self._libp2pNode.stop(callback)
3233
},
3334
swarm: {
34-
peers: (callback) => {
35+
peers: promisify((callback) => {
3536
if (!self.isOnline()) {
3637
return callback(OFFLINE_ERROR)
3738
}
3839

39-
callback(null, self._libp2pNode.peerBook.getAll())
40-
},
40+
const peers = self._libp2pNode.peerBook.getAll()
41+
const mas = []
42+
Object
43+
.keys(peers)
44+
.forEach((b58Id) => {
45+
peers[b58Id].multiaddrs.forEach((ma) => {
46+
// TODO this should only print the addr we are using
47+
mas.push(ma)
48+
})
49+
})
50+
51+
callback(null, mas)
52+
}),
4153
// all the addrs we know
42-
addrs: (callback) => {
54+
addrs: promisify((callback) => {
4355
if (!self.isOnline()) {
4456
return callback(OFFLINE_ERROR)
4557
}
46-
// TODO
47-
throw new Error('Not implemented')
48-
},
49-
localAddrs: (callback) => {
58+
const peers = self._libp2pNode.peerBook.getAll()
59+
const mas = []
60+
Object
61+
.keys(peers)
62+
.forEach((b58Id) => {
63+
peers[b58Id].multiaddrs.forEach((ma) => {
64+
// TODO this should only print the addr we are using
65+
mas.push(ma)
66+
})
67+
})
68+
69+
callback(null, mas)
70+
}),
71+
localAddrs: promisify((callback) => {
5072
if (!self.isOnline()) {
5173
return callback(OFFLINE_ERROR)
5274
}
5375

5476
callback(null, self._libp2pNode.peerInfo.multiaddrs)
55-
},
56-
connect: (maddr, callback) => {
77+
}),
78+
connect: promisify((maddr, callback) => {
5779
if (!self.isOnline()) {
5880
return callback(OFFLINE_ERROR)
5981
}
@@ -63,8 +85,8 @@ module.exports = function libp2p (self) {
6385
}
6486

6587
self._libp2pNode.dialByMultiaddr(maddr, callback)
66-
},
67-
disconnect: (maddr, callback) => {
88+
}),
89+
disconnect: promisify((maddr, callback) => {
6890
if (!self.isOnline()) {
6991
return callback(OFFLINE_ERROR)
7092
}
@@ -74,11 +96,11 @@ module.exports = function libp2p (self) {
7496
}
7597

7698
self._libp2pNode.hangUpByMultiaddr(maddr, callback)
77-
},
78-
filters: () => {
99+
}),
100+
filters: promisify((callback) => {
79101
// TODO
80102
throw new Error('Not implemented')
81-
}
103+
})
82104
},
83105
routing: {},
84106
records: {},

test/core/both/test-bitswap.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ describe('bitswap', () => {
5353
targetNode.id((err, identity) => {
5454
expect(err).to.not.exist
5555
const addr = identity.addresses
56-
.map((addr) => multiaddr(addr))
56+
.map((addr) => {
57+
const ma = multiaddr(addr.toString().split('ipfs')[0])
58+
return ma
59+
})
5760
.filter((addr) => {
5861
return _.includes(addr.protoNames(), 'ws')
5962
})[0]

test/core/node-only/test-swarm-2.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
'use strict'
44

5-
/*
65
const test = require('interface-ipfs-core')
76
const IPFSFactory = require('../../utils/factory-core')
87

@@ -17,7 +16,5 @@ const common = {
1716
factory.dismantle(cb)
1817
}
1918
}
20-
*/
21-
// TODO
22-
// Needs: https://github.com./ipfs/js-libp2p-ipfs/pull/16
23-
// test.swarm(common)
19+
20+
test.swarm(common)

test/core/node-only/test-swarm.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('swarm', function () {
4646
(cb) => {
4747
nodeB.id((err, res) => {
4848
expect(err).to.not.exist
49-
nodeBMultiaddr = `${res.addresses[0]}/ipfs/${res.id}`
49+
nodeBMultiaddr = res.addresses[0]
5050
cb()
5151
})
5252
}
@@ -73,14 +73,14 @@ describe('swarm', function () {
7373
(cb) => {
7474
nodeA.libp2p.swarm.peers((err, res) => {
7575
expect(err).to.not.exist
76-
expect(Object.keys(res)).to.have.length(1)
76+
expect(Object.keys(res)).to.have.length.above(0)
7777
cb()
7878
})
7979
},
8080
(cb) => {
8181
nodeB.libp2p.swarm.peers((err, res) => {
8282
expect(err).to.not.exist
83-
expect(Object.keys(res)).to.have.length(1)
83+
expect(Object.keys(res)).to.have.length.above(0)
8484
cb()
8585
})
8686
}

0 commit comments

Comments
 (0)