Skip to content

Commit 689f90a

Browse files
vasco-santosjacobheun
authored andcommitted
revert: reapply "fix: throw if no conn encryption module provided (#665)"
This reapplies commit b621fbd.
1 parent 0e3cc58 commit 689f90a

File tree

8 files changed

+91
-22
lines changed

8 files changed

+91
-22
lines changed

doc/API.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ Creates an instance of Libp2p.
8585
| Name | Type | Description |
8686
|------|------|-------------|
8787
| options | `object` | libp2p options |
88-
| options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p modules to use |
88+
| options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#modules) to use |
8989
| [options.addresses] | `{ listen: Array<string>, announce: Array<string>, noAnnounce: Array<string> }` | Addresses for transport listening and to advertise to the network |
9090
| [options.config] | `object` | libp2p modules configuration and core configuration |
91-
| [options.connectionManager] | [`object`](./CONFIGURATION.md#configuring-connection-manager) | libp2p Connection Manager configuration |
92-
| [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager configuration |
91+
| [options.connectionManager] | [`object`](./CONFIGURATION.md#configuring-connection-manager) | libp2p Connection Manager [configuration](./CONFIGURATION.md#configuring-connection-manager) |
92+
| [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager [configuration]((./CONFIGURATION.md#configuring-transport-manager)) |
9393
| [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |
94-
| [options.dialer] | [`object`](./CONFIGURATION.md#configuring-dialing) | libp2p Dialer configuration
95-
| [options.keychain] | [`object`](./CONFIGURATION.md#setup-with-keychain) | keychain configuration |
96-
| [options.metrics] | [`object`](./CONFIGURATION.md#configuring-metrics) | libp2p Metrics configuration
94+
| [options.dialer] | [`object`](./CONFIGURATION.md#configuring-dialing) | libp2p Dialer [configuration]((./CONFIGURATION.md#configuring-dialing))
95+
| [options.keychain] | [`object`](./CONFIGURATION.md#setup-with-keychain) | keychain [configuration]((./CONFIGURATION.md#setup-with-keychain)) |
96+
| [options.metrics] | [`object`](./CONFIGURATION.md#configuring-metrics) | libp2p Metrics [configuration]((./CONFIGURATION.md#configuring-metrics)) |
9797
| [options.peerId] | [`PeerId`][peer-id] | peerId instance (it will be created if not provided) |
98-
| [options.peerStore] | [`object`](./CONFIGURATION.md#configuring-peerstore) | libp2p PeerStore configuration |
98+
| [options.peerStore] | [`object`](./CONFIGURATION.md#configuring-peerstore) | libp2p PeerStore [configuration]((./CONFIGURATION.md#configuring-peerstore)) |
9999

100100
For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
101101

doc/CONFIGURATION.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The libp2p ecosystem contains at least one module for each of these subsystems.
5252

5353
After selecting the modules to use, it is also possible to configure each one according to your needs.
5454

55-
Bear in mind that only a **transport** and **connection encryption** are required, while all the other subsystems are optional.
55+
Bear in mind that a **transport** and **connection encryption** are **required**, while all the other subsystems are optional.
5656

5757
### Transport
5858

examples/discovery-mechanisms/2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const createNode = async () => {
2121
},
2222
config: {
2323
peerDiscovery: {
24-
mdns: {
24+
[MulticastDNS.tag]: {
2525
interval: 20e3,
2626
enabled: true
2727
}

src/errors.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
exports.messages = {
44
NOT_STARTED_YET: 'The libp2p node is not started yet',
5-
DHT_DISABLED: 'DHT is not available'
5+
DHT_DISABLED: 'DHT is not available',
6+
CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required'
67
}
78

89
exports.codes = {
910
DHT_DISABLED: 'ERR_DHT_DISABLED',
1011
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
1112
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
13+
CONN_ENCRYPTION_REQUIRED: 'ERR_CONN_ENCRYPTION_REQUIRED',
1214
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
1315
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
1416
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',

src/index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ const globalThis = require('ipfs-utils/src/globalthis')
66
const log = debug('libp2p')
77
log.error = debug('libp2p:error')
88

9+
const errCode = require('err-code')
910
const PeerId = require('peer-id')
1011

1112
const peerRouting = require('./peer-routing')
1213
const contentRouting = require('./content-routing')
1314
const pubsub = require('./pubsub')
1415
const getPeer = require('./get-peer')
1516
const { validate: validateConfig } = require('./config')
16-
const { codes } = require('./errors')
17+
const { codes, messages } = require('./errors')
1718

1819
const AddressManager = require('./address-manager')
1920
const ConnectionManager = require('./connection-manager')
@@ -121,12 +122,13 @@ class Libp2p extends EventEmitter {
121122
this.registrar.handle = this.handle
122123

123124
// Attach crypto channels
124-
if (this._modules.connEncryption) {
125-
const cryptos = this._modules.connEncryption
126-
cryptos.forEach((crypto) => {
127-
this.upgrader.cryptos.set(crypto.protocol, crypto)
128-
})
125+
if (!this._modules.connEncryption || !this._modules.connEncryption.length) {
126+
throw errCode(new Error(messages.CONN_ENCRYPTION_REQUIRED), codes.CONN_ENCRYPTION_REQUIRED)
129127
}
128+
const cryptos = this._modules.connEncryption
129+
cryptos.forEach((crypto) => {
130+
this.upgrader.cryptos.set(crypto.protocol, crypto)
131+
})
130132

131133
this.dialer = new Dialer({
132134
transportManager: this.transportManager,

test/core/encryption.spec.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict'
2+
/* eslint-env mocha */
3+
4+
const chai = require('chai')
5+
chai.use(require('dirty-chai'))
6+
chai.use(require('chai-as-promised'))
7+
const { expect } = chai
8+
9+
const Transport = require('libp2p-websockets')
10+
const { NOISE: Crypto } = require('libp2p-noise')
11+
12+
const Libp2p = require('../../src')
13+
const { codes: ErrorCodes } = require('../../src/errors')
14+
const { createPeerId } = require('../utils/creators/peer')
15+
16+
describe('Connection encryption configuration', () => {
17+
let peerId
18+
19+
before(async () => {
20+
[peerId] = await createPeerId()
21+
})
22+
23+
it('is required', async () => {
24+
const config = {
25+
peerId,
26+
modules: {
27+
transport: [Transport]
28+
}
29+
}
30+
31+
await expect(Libp2p.create(config)).to.eventually.be.rejected()
32+
.and.to.have.property('code', ErrorCodes.CONN_ENCRYPTION_REQUIRED)
33+
})
34+
35+
it('is required and needs at least one module', async () => {
36+
const config = {
37+
peerId,
38+
modules: {
39+
transport: [Transport],
40+
connEncryption: []
41+
}
42+
}
43+
await expect(Libp2p.create(config)).to.eventually.be.rejected()
44+
.and.to.have.property('code', ErrorCodes.CONN_ENCRYPTION_REQUIRED)
45+
})
46+
47+
it('can be created', async () => {
48+
const config = {
49+
peerId,
50+
modules: {
51+
transport: [Transport],
52+
connEncryption: [Crypto]
53+
}
54+
}
55+
await Libp2p.create(config)
56+
})
57+
})

test/core/listening.node.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ chai.use(require('dirty-chai'))
66
const { expect } = chai
77

88
const Transport = require('libp2p-tcp')
9+
const { NOISE: Crypto } = require('libp2p-noise')
910

1011
const { create } = require('../../src')
1112
const peerUtils = require('../utils/creators/peer')
@@ -31,7 +32,8 @@ describe('Listening', () => {
3132
listen: [listenAddr]
3233
},
3334
modules: {
34-
transport: [Transport]
35+
transport: [Transport],
36+
connEncryption: [Crypto]
3537
}
3638
})
3739

test/transports/transport-manager.spec.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const sinon = require('sinon')
99

1010
const multiaddr = require('multiaddr')
1111
const Transport = require('libp2p-websockets')
12+
const { NOISE: Crypto } = require('libp2p-noise')
1213
const AddressManager = require('../../src/address-manager')
1314
const TransportManager = require('../../src/transport-manager')
1415
const mockUpgrader = require('../utils/mockUpgrader')
@@ -110,7 +111,8 @@ describe('libp2p.transportManager', () => {
110111
libp2p = new Libp2p({
111112
peerId,
112113
modules: {
113-
transport: [Transport]
114+
transport: [Transport],
115+
connEncryption: [Crypto]
114116
}
115117
})
116118

@@ -128,7 +130,8 @@ describe('libp2p.transportManager', () => {
128130
libp2p = new Libp2p({
129131
peerId,
130132
modules: {
131-
transport: [spy]
133+
transport: [spy],
134+
connEncryption: [Crypto]
132135
},
133136
config: {
134137
transport: {
@@ -152,7 +155,8 @@ describe('libp2p.transportManager', () => {
152155
libp2p = new Libp2p({
153156
peerId,
154157
modules: {
155-
transport: [Transport]
158+
transport: [Transport],
159+
connEncryption: [Crypto]
156160
}
157161
})
158162

@@ -188,7 +192,8 @@ describe('libp2p.transportManager (dial only)', () => {
188192
listen: [multiaddr('/ip4/127.0.0.1/tcp/0')]
189193
},
190194
modules: {
191-
transport: [Transport]
195+
transport: [Transport],
196+
connEncryption: [Crypto]
192197
}
193198
})
194199

@@ -212,7 +217,8 @@ describe('libp2p.transportManager (dial only)', () => {
212217
faultTolerance: FaultTolerance.NO_FATAL
213218
},
214219
modules: {
215-
transport: [Transport]
220+
transport: [Transport],
221+
connEncryption: [Crypto]
216222
}
217223
})
218224

0 commit comments

Comments
 (0)