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

Commit c567282

Browse files
authored
fix: libp2p now requires encryption module (#3085)
Since the `v0.28.1` patch release, libp2p requires a connection encryption module to be passed in config, otherwise it throws. See: libp2p/js-libp2p#665
1 parent 1d96f82 commit c567282

File tree

7 files changed

+111
-52
lines changed

7 files changed

+111
-52
lines changed

examples/circuit-relaying/README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ A circuit relay address is a [multiaddress](https://multiformats.io/multiaddr/)
6464

6565
Circuit relay addresses are very flexible and can describe many different aspects of how to esablish the relayed connection. In its simplest form, it looks something like this:
6666

67-
- `/p2p-circuit/ipfs/QmPeer`
67+
- `/p2p-circuit/p2p/QmPeer`
6868

6969
If we want to be specific as to which transport we want to use to establish the relay, we can encode that in the address as well:
7070

71-
- `/ip4/127.0.0.1/tcp/65000/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer`
71+
- `/ip4/127.0.0.1/tcp/65000/p2p/QmRelay/p2p-circuit/ipfs/QmPeer`
7272

7373
This tells us that we want to use `QmRelay` located at address 127.0.0.1 and port 65000.
7474

75-
- `/ip4/127.0.0.1/tcp/65000/ipfs/QmRelay/p2p-circuit/ip4/127.0.0.1/tcp/8080/ws/ipfs/QmPeer`
75+
- `/ip4/127.0.0.1/tcp/65000/p2p/QmRelay/p2p-circuit/ip4/127.0.0.1/tcp/8080/ws/ipfs/QmPeer`
7676

7777
We can take it a step further and encode the same information for the destination peer. In this case, we have it located at 127.0.0.1 on port 8080 and using a Web sockets transport!
7878

79-
- `/ip4/127.0.0.1/tcp/65000/ipfs/QmRelay/p2p-circuit`
79+
- `/ip4/127.0.0.1/tcp/65000/p2p/QmRelay/p2p-circuit`
8080

81-
If a node is configured with this address, it will use the specified host (`/ip4/127.0.0.1/tcp/65000/ipfs/QmRelay`) as a relay and it will be reachable over this relay.
81+
If a node is configured with this address, it will use the specified host (`/ip4/127.0.0.1/tcp/65000/p2p/QmRelay`) as a relay and it will be reachable over this relay.
8282
- There could multiple addresses of this sort specified in the config, in which case the node will be reachable over all of them.
8383
- This is useful if, for example, the node is behind a firewall but wants to be reachable from the outside over a specific relay.
8484

@@ -227,36 +227,36 @@ $ ipfs id
227227
"ID": "QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
228228
"PublicKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC84qPFzqajCfnvaJunqt48S1LIBRthXV60q5QClL+dUfOOU/m7v1ZcpNhvFFUN6tVCDaoT5AxEv0czxZiVx/njl6FVIc6tE1J+HWpc8cbAXNY6QbbyzKl/rjp7V8/QClE0JqgjIk84wnWGTwFhOEt0hnpu2XFt9iHaenSfg3EAa8K9MlbxmbawuxNLJJf7VZXkJrUNl6WOglAVU8Sqc4QaahCLVK5Dzo98zDBq1KDBxMbUgH0LTqzr6i+saxkEHZmBKO+mMVT3LzOUx1DQR4pLAw1qgoJstsIZEaJ2XLh975IiI7OKqWYH7+3NyNK2sldJK/4Zko4rH3irmnkAxLcFAgMBAAE=",
229229
"Addresses": [
230-
"/ip4/127.0.0.1/tcp/4001/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
231-
"/ip4/192.168.1.132/tcp/4001/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
232-
"/ip6/::1/tcp/4001/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
233-
"/ip4/186.4.18.182/tcp/13285/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
234-
"/ip4/186.4.18.182/tcp/13285/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF"
230+
"/ip4/127.0.0.1/tcp/4001/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
231+
"/ip4/192.168.1.132/tcp/4001/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
232+
"/ip6/::1/tcp/4001/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
233+
"/ip4/186.4.18.182/tcp/13285/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
234+
"/ip4/186.4.18.182/tcp/13285/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF"
235235
],
236236
"AgentVersion": "go-ipfs/0.4.14-dev/cb5bb7dd8",
237237
"ProtocolVersion": "ipfs/0.1.0"
238238
}
239239
```
240240

241-
We can then grab the resolved multiaddr from the `Addresses` array — `/ip4/127.0.0.1/tcp/4004/ws/ipfs/Qm...`. Let's note it down somewhere and move to the next step.
241+
We can then grab the resolved multiaddr from the `Addresses` array — `/ip4/127.0.0.1/tcp/4004/ws/p2p/Qm...`. Let's note it down somewhere and move to the next step.
242242

243243
**js ipfs**
244244

245245
```console
246246
$ jsipfs daemon
247247
Initializing daemon...
248-
Swarm listening on /p2p-circuit/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
249-
Swarm listening on /p2p-circuit/ip4/0.0.0.0/tcp/4002/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
250-
Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4003/ws/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
251-
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
252-
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
253-
Swarm listening on /ip4/192.168.1.132/tcp/4002/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
248+
Swarm listening on /p2p-circuit/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
249+
Swarm listening on /p2p-circuit/ip4/0.0.0.0/tcp/4002/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
250+
Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4003/ws/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
251+
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
252+
Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
253+
Swarm listening on /ip4/192.168.1.132/tcp/4002/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
254254
API is listening on: /ip4/127.0.0.1/tcp/5002
255255
Gateway (readonly) is listening on: /ip4/127.0.0.1/tcp/9090
256256
Daemon is ready
257257
```
258258

259-
Look out for an address similar to `/ip4/127.0.0.1/tcp/4003/ws/ipfs/Qm...`. Note it down somewhere, and let's move on to the next step.
259+
Look out for an address similar to `/ip4/127.0.0.1/tcp/4003/ws/p2p/Qm...`. Note it down somewhere, and let's move on to the next step.
260260

261261
### 2. Configure and run the bundled example
262262

@@ -333,7 +333,7 @@ const ipfs = await IPFS.create({
333333

334334
- We connected the browser nodes to an external node over its websocket transport using the `/ip4/127.0.0.1/tcp/4003/ws/ipfs/...` multiaddr. That external node happens to be a `HOP` node, meaning that it can relay connections for our browsers (and other nodes) allowing them to connect
335335

336-
- And finally we connected the two browser nodes using the `/p2p-circuit/ipfs/...` multiaddr. Take a look at the code below in [src/app.js](src/app.js#L103...L108) - lines 103-108
336+
- And finally we connected the two browser nodes using the `/p2p-circuit/p2p/...` multiaddr. Take a look at the code below in [src/app.js](src/app.js#L103...L108) - lines 103-108
337337

338338
```js
339339
try {

examples/circuit-relaying/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1>IPFS Simple Messaging</h1>
3434
<ul id="peers"></ul>
3535
</div>
3636
<div class="box peers-box">
37-
<label>Peers Connected:</label>
37+
<label>Swarm Peers:</label>
3838
<ul id="peers-addrs"></ul>
3939
</div>
4040
<div>

examples/circuit-relaying/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"author": "Dmitriy Ryajov <[email protected]>",
1515
"license": "MIT",
1616
"dependencies": {
17+
"delay": "^4.3.0",
1718
"ipfs": "^0.46.0",
1819
"ipfs-pubsub-room": "^2.0.1"
1920
},

examples/circuit-relaying/src/app.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ document.addEventListener('DOMContentLoaded', async () => {
4747
const sendMsg = helpers.sendMsg
4848
const updatePeers = helpers.updatePeers
4949
const updateAddrs = helpers.updateAddrs
50+
const updateSwarmPeers = helpers.updateSwarmPeers
5051

5152
const info = await ipfs.id()
5253
console.log('IPFS node ready with id ' + info.id)
5354

5455
let room = createRoom(roomName)
5556

5657
$peerId.innerHTML = `<li>${info.id}</li>`
57-
updateAddrs(info.addresses)
5858

5959
$send.addEventListener('click', () => {
6060
sendMsg(room)
@@ -95,13 +95,14 @@ document.addEventListener('DOMContentLoaded', async () => {
9595
})
9696

9797
$connect.addEventListener('click', async () => {
98-
const peer = $peer.value
98+
const peer = $peer.value.trim()
9999
$peer.value = ''
100100
try {
101101
await ipfs.swarm.connect(peer)
102+
await updateAddrs(ipfs)
103+
await updateSwarmPeers(ipfs)
102104
} catch (err) {
103105
return console.error(err)
104106
}
105-
$pAddrs.innerHTML += `<li>${peer.trim()}</li>`
106107
})
107108
})

examples/circuit-relaying/src/helpers.js

+52-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const $message = document.querySelector('#message')
66
const $msgs = document.querySelector('#msgs')
77
const $addrs = document.querySelector('#addrs')
88
const $peers = document.querySelector('#peers')
9+
const $pAddrs = document.querySelector('#peers-addrs')
10+
const delay = require('delay')
911

1012
const NAMESPACE = 'ipfs-quick-msg'
1113

@@ -53,22 +55,64 @@ module.exports = (ipfs, peersSet) => {
5355
const tags = Array.from(peersSet).map((p) => {
5456
return `<li>${p}</li>`
5557
})
56-
$peers.innerHTML = tags
58+
$peers.innerHTML = tags.join('')
5759
}
5860

59-
const updateAddrs = (addrs) => {
60-
$addrs.innerHTML = `
61-
<div>
62-
<ul>
63-
${addrs.map((addr) => `<li>${addr.toString()}</li>`)}
64-
<ul>
65-
</div>`
61+
const updateSwarmPeers = async (ipfs) => {
62+
const peers = await ipfs.swarm.peers()
63+
64+
$pAddrs.innerHTML = peers.map(peer => `<li>${peer.peer}</li>`).join('')
65+
}
66+
67+
const updateAddrs = async (ipfs) => {
68+
const info = await ipfs.id()
69+
70+
// see which peers support the circuit relay protocol
71+
const relayAddrs = []
72+
const connections = ipfs.libp2p.connections
73+
const peers = await ipfs.swarm.peers()
74+
75+
for (let i = 0; i < peers.length; i++) {
76+
const {
77+
peer: peerId
78+
} = peers[i]
79+
80+
const cons = connections.get(peerId)
81+
82+
for (let j = 0; j < cons.length; j++) {
83+
const con = cons[j]
84+
const remoteAddr = con.remoteAddr.toString()
85+
86+
for (let k = 0; k < 5; k++) {
87+
// protocols is undefined until the connection is negotiated so try a few times
88+
const protocols = ipfs.libp2p.peerStore.protoBook.get(con.remotePeer)
89+
90+
if (!protocols) {
91+
await delay(500)
92+
continue
93+
}
94+
95+
const isRelay = protocols.find(proto => proto.toString().includes('/circuit/relay'))
96+
97+
// only add addresses for peers that are relays and that we aren't already
98+
// connected to them via another relay
99+
if (isRelay && !remoteAddr.includes('p2p-circuit') && !remoteAddr.includes(info.id)) {
100+
relayAddrs.push(`${remoteAddr}/p2p-circuit/p2p/${info.id}`)
101+
}
102+
103+
break
104+
}
105+
}
106+
}
107+
108+
$addrs.innerHTML = relayAddrs.map((addr) => `<li>${addr.toString()}</li>`).join('')
66109
}
67110

68111
return {
69112
createRoom,
70113
sendMsg,
71114
updatePeers,
115+
updateSwarmPeers,
72116
updateAddrs
73117
}
74118
}

examples/circuit-relaying/test.js

+31-19
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ const {
1515
} = require('test-ipfs-example/utils')
1616
const pkg = require('./package.json')
1717

18-
async function testUI (url, relay, localPeerIdFile, remotePeerIdFile) {
18+
async function testUI (url, relayAddr, relayId, localPeerIdFile, remotePeerIdFile) {
1919
const proc = execa(require.resolve('test-ipfs-example/node_modules/.bin/nightwatch'), ['--config', require.resolve('test-ipfs-example/nightwatch.conf.js'), path.join(__dirname, 'test.js')], {
2020
cwd: path.resolve(__dirname, '../'),
2121
env: {
2222
...process.env,
2323
CI: true,
2424
IPFS_EXAMPLE_TEST_URL: url,
25-
IPFS_RELAY_ADDRESS: relay,
25+
IPFS_RELAY_ADDRESS: relayAddr,
26+
IPFS_RELAY_ID: relayId,
2627
IPFS_LOCAL_PEER_ID_FILE: localPeerIdFile,
2728
IPFS_REMOTE_PEER_ID_FILE: remotePeerIdFile
2829
},
@@ -72,8 +73,8 @@ async function runTest () {
7273
const peerB = path.join(os.tmpdir(), `test-${Date.now()}-b.txt`)
7374

7475
await Promise.all([
75-
testUI(server1.url, id.addresses[0], peerA, peerB),
76-
testUI(server2.url, id.addresses[0], peerB, peerA)
76+
testUI(server1.url, id.addresses[0], id.id, peerA, peerB),
77+
testUI(server2.url, id.addresses[0], id.id, peerB, peerA)
7778
])
7879
} finally {
7980
await ipfsd.stop()
@@ -85,8 +86,8 @@ async function runTest () {
8586
module.exports = runTest
8687

8788
module.exports[pkg.name] = function (browser) {
88-
let localPeerId = null
89-
let remotePeerId = null
89+
let local = null
90+
let remote = null
9091

9192
browser
9293
.url(process.env.IPFS_EXAMPLE_TEST_URL)
@@ -96,25 +97,36 @@ module.exports[pkg.name] = function (browser) {
9697
.pause(1000)
9798
.click('#connect')
9899

99-
browser.expect.element('#peers-addrs').text.to.contain(process.env.IPFS_RELAY_ADDRESS)
100+
browser.expect.element('#peers-addrs').text.to.contain(process.env.IPFS_RELAY_ID)
101+
browser.expect.element('#peer-id').text.to.not.equal('')
100102

101103
// exchange peer info
102104
browser.getText('#addrs', (result) => {
103-
localPeerId = result.value.trim()
104-
console.info('got local peer id', localPeerId) // eslint-disable-line no-console
105+
local = {
106+
addr: result.value.trim()
107+
}
108+
console.info(`got local circuit relay address ${local.addr}`) // eslint-disable-line no-console
105109
})
110+
.getText('#peer-id', (result) => {
111+
local.id = result.value.trim()
112+
console.info(`got local peer id ${local.id}`) // eslint-disable-line no-console
113+
})
106114
.perform(async (browser, done) => {
107-
console.info('writing local peer id') // eslint-disable-line no-console
108-
await fs.writeFile(process.env.IPFS_LOCAL_PEER_ID_FILE, localPeerId)
115+
console.info(`writing local data ${local.addr}`) // eslint-disable-line no-console
116+
await fs.writeJson(process.env.IPFS_LOCAL_PEER_ID_FILE, local)
109117

110-
console.info('reading remote peer id') // eslint-disable-line no-console
118+
console.info('reading remote circuit relay address') // eslint-disable-line no-console
111119
for (let i = 0; i < 100; i++) {
112120
try {
113-
remotePeerId = await fs.readFile(process.env.IPFS_REMOTE_PEER_ID_FILE, {
121+
remote = await fs.readJson(process.env.IPFS_REMOTE_PEER_ID_FILE, {
114122
encoding: 'utf8'
115123
})
116124

117-
console.info('got remote peer id', remotePeerId) // eslint-disable-line no-console
125+
if (!remote || !remote.addr || !remote.id) {
126+
throw new Error('Remote circuit relay address was empty')
127+
}
128+
129+
console.info(`got remote circuit relay address ${remote.addr}`) // eslint-disable-line no-console
118130
done()
119131

120132
break
@@ -125,24 +137,24 @@ module.exports[pkg.name] = function (browser) {
125137
await delay(1000)
126138
}
127139

128-
console.info('connecting to remote peer', remotePeerId) // eslint-disable-line no-console
140+
console.info(`connecting to remote peer ${remote.addr}`) // eslint-disable-line no-console
129141

130142
browser
131143
.clearValue('#peer')
132-
.setValue('#peer', remotePeerId)
144+
.setValue('#peer', remote.addr)
133145
.pause(1000)
134146
.click('#connect')
135147

136-
browser.expect.element('#peers-addrs').text.to.contain(remotePeerId)
148+
browser.expect.element('#peers-addrs').text.to.contain(remote.id)
137149

138150
browser
139151
.clearValue('#message')
140152
.setValue('#message', 'hello')
141153
.pause(1000)
142154
.click('#send')
143155

144-
browser.expect.element('#msgs').text.to.contain(`${remotePeerId.substr(-4)}: hello`)
145-
browser.expect.element('#msgs').text.to.contain(`${localPeerId.substr(-4)}: hello`)
156+
browser.expect.element('#msgs').text.to.contain(`${remote.id.substr(-4)}: hello`)
157+
browser.expect.element('#msgs').text.to.contain(`${local.id.substr(-4)}: hello`)
146158
})
147159

148160
browser.end()

packages/ipfs/test/core/libp2p.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const PeerId = require('peer-id')
77
const Libp2p = require('libp2p')
88
const EE = require('events')
99
const libp2pComponent = require('../../src/core/components/libp2p')
10+
const Crypto = require('libp2p-secio')
1011

1112
class DummyTransport {
1213
get [Symbol.toStringTag] () {
@@ -77,7 +78,7 @@ describe('libp2p customization', function () {
7778
libp2p: (opts) => {
7879
return new Libp2p({
7980
peerId: opts.peerId,
80-
modules: { transport: [DummyTransport] },
81+
modules: { transport: [DummyTransport], connEncryption: [Crypto] },
8182
config: { relay: { enabled: false } }
8283
})
8384
}
@@ -101,7 +102,7 @@ describe('libp2p customization', function () {
101102
libp2p: (opts) => {
102103
return new Libp2p({
103104
peerId: opts.peerId,
104-
modules: { transport: [DummyTransport] },
105+
modules: { transport: [DummyTransport], connEncryption: [Crypto] },
105106
config: { relay: { enabled: false } }
106107
})
107108
}

0 commit comments

Comments
 (0)