Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

Commit c28b845

Browse files
update to latest dependencies and add new crypto api
1 parent 381d3c2 commit c28b845

9 files changed

+93
-81
lines changed

README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ const sw = new Swarm(peerInfo)
5959

6060
## API
6161

62-
peerInfo is a [PeerInfo](https://github.com/diasdavid/js-peer-info) object that represents the peer creating this swarm instance.
62+
peerInfo is a [PeerInfo](https://github.com/libp2p/js-peer-info) object that represents the peer creating this swarm instance.
6363

6464
### Transports
6565

6666
##### `swarm.transport.add(key, transport, options, callback)`
6767

68-
libp2p-swarm expects transports that implement [interface-transport](https://github.com/diasdavid/abstract-transport). For example [libp2p-tcp](https://github.com/diasdavid/js-libp2p-tcp).
68+
libp2p-swarm expects transports that implement [interface-transport](https://github.com/libp2p/abstract-transport). For example [libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp).
6969

7070
- `key` - the transport identifier.
7171
- `transport` -
@@ -100,20 +100,31 @@ Close the listeners of a given transport.
100100

101101
##### `swarm.connection.addUpgrade()`
102102

103-
A connection upgrade must be able to receive and return something that implements the [interface-connection](https://github.com/diasdavid/interface-connection) specification.
103+
A connection upgrade must be able to receive and return something that implements the [interface-connection](https://github.com/libp2p/interface-connection) specification.
104104

105105
> **WIP**
106106
107107
##### `swarm.connection.addStreamMuxer(muxer)`
108108

109-
Upgrading a connection to use a stream muxer is still considered an upgrade, but a special case since once this connection is applied, the returned obj will implement the [interface-stream-muxer](https://github.com/diasdavid/interface-stream-muxer) spec.
109+
Upgrading a connection to use a stream muxer is still considered an upgrade, but a special case since once this connection is applied, the returned obj will implement the [interface-stream-muxer](https://github.com/libp2p/interface-stream-muxer) spec.
110110

111111
- `muxer`
112112

113113
##### `swarm.connection.reuse()`
114114

115115
Enable the identify protocol.
116116

117+
##### `swarm.connection.crypto([tag, encrypt])`
118+
119+
Enable a specified crypto protocol. By default no encryption is used, aka `plaintext`. If called with no arguments it resets to use `plaintext`.
120+
121+
You can use for example [libp2p-secio](https://github.com/libp2p/js-libp2p-secio) like this
122+
123+
```js
124+
const secio = require('libp2p-secio')
125+
swarm.connection.crypto(secio.tag, secio.encrypt)
126+
```
127+
117128
### `swarm.dial(pi, protocol, callback)`
118129

119130
dial uses the best transport (whatever works first, in the future we can have some criteria), and jump starts the connection until the point where we have to negotiate the protocol. If a muxer is available, then drop the muxer onto that connection. Good to warm up connections or to check for connectivity. If we have already a muxer for that peerInfo, then do nothing.
@@ -158,11 +169,11 @@ Close all the listeners and muxers.
158169

159170
libp2p is designed to support multiple transports at the same time. While peers are identified by their ID (which are generated from their public keys), the addresses of each pair may vary, depending the device where they are being run or the network in which they are accessible through.
160171

161-
In order for a transport to be supported, it has to follow the [interface-transport](https://github.com/diasdavid/interface-transport) spec.
172+
In order for a transport to be supported, it has to follow the [interface-transport](https://github.com/libp2p/interface-transport) spec.
162173

163174
### Connection upgrades
164175

165-
Each connection in libp2p follows the [interface-connection](https://github.com/diasdavid/interface-connection) spec. This design decision enables libp2p to have upgradable transports.
176+
Each connection in libp2p follows the [interface-connection](https://github.com/libp2p/interface-connection) spec. This design decision enables libp2p to have upgradable transports.
166177

167178
We think of `upgrade` as a very important notion when we are talking about connections, we can see mechanisms like: stream multiplexing, congestion control, encrypted channels, multipath, simulcast, etc, as `upgrades` to a connection. A connection can be a simple and with no guarantees, drop a packet on the network with a destination thing, a transport in the other hand can be a connection and or a set of different upgrades that are mounted on top of each other, giving extra functionality to that connection and therefore `upgrading` it.
168179

package.json

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "libp2p-swarm",
33
"version": "0.22.3",
44
"description": "libp2p swarm implementation in JavaScript",
5-
"main": "src/index.js",
5+
"main": "lib/index.js",
66
"jsnext:main": "src/index.js",
77
"scripts": {
88
"lint": "gulp lint",
@@ -37,35 +37,36 @@
3737
"node": "^4.3.0"
3838
},
3939
"devDependencies": {
40-
"aegir": "^6.0.1",
40+
"aegir": "^8.0.0",
4141
"buffer-loader": "0.0.1",
4242
"chai": "^3.5.0",
4343
"gulp": "^3.9.1",
4444
"libp2p-multiplex": "^0.2.1",
45-
"libp2p-spdy": "^0.8.1",
46-
"libp2p-tcp": "^0.7.4",
47-
"libp2p-webrtc-star": "^0.3.2",
48-
"libp2p-websockets": "^0.7.1",
45+
"libp2p-spdy": "^0.9.0",
46+
"libp2p-tcp": "^0.8.1",
47+
"libp2p-webrtc-star": "^0.4.1",
48+
"libp2p-websockets": "^0.8.1",
4949
"pre-commit": "^1.1.3",
5050
"pull-goodbye": "0.0.1",
51-
"pull-stream": "^3.4.3",
51+
"pull-stream": "^3.4.5",
5252
"webrtcsupport": "^2.2.0"
5353
},
5454
"dependencies": {
55-
"babel-runtime": "^6.9.0",
55+
"babel-runtime": "^6.11.6",
5656
"browserify-zlib": "github:ipfs/browserify-zlib",
5757
"debug": "^2.2.0",
58-
"interface-connection": "^0.1.7",
59-
"ip-address": "^5.8.0",
60-
"libp2p-identify": "^0.1.3",
61-
"libp2p-secio": "^0.3.0",
58+
"interface-connection": "^0.2.1",
59+
"ip-address": "^5.8.2",
60+
"libp2p-identify": "^0.2.0",
61+
"libp2p-secio": "^0.4.2",
6262
"lodash.contains": "^2.4.3",
6363
"multiaddr": "^2.0.2",
64-
"multistream-select": "^0.10.0",
64+
"multistream-select": "^0.11.0",
6565
"peer-id": "^0.7.0",
6666
"peer-info": "^0.7.0",
6767
"protocol-buffers": "^3.1.6",
68-
"run-parallel": "^1.1.6"
68+
"run-parallel": "^1.1.6",
69+
"run-waterfall": "^1.1.3"
6970
},
7071
"contributors": [
7172
"David Dias <[email protected]>",

src/connection.js

+31-17
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
const identify = require('libp2p-identify')
44
const multistream = require('multistream-select')
5+
const waterfall = require('run-waterfall')
56
const debug = require('debug')
67
const log = debug('libp2p:swarm:connection')
78

89
const protocolMuxer = require('./protocol-muxer')
10+
const plaintext = require('./plaintext')
911

1012
module.exports = function connection (swarm) {
1113
return {
@@ -17,7 +19,7 @@ module.exports = function connection (swarm) {
1719

1820
// for listening
1921
swarm.handle(muxer.multicodec, (conn) => {
20-
const muxedConn = muxer.listen(conn)
22+
const muxedConn = muxer.listener(conn)
2123

2224
muxedConn.on('stream', (conn) => {
2325
protocolMuxer(swarm.protocols, conn)
@@ -32,23 +34,18 @@ module.exports = function connection (swarm) {
3234
conn.getPeerInfo = (cb) => {
3335
const conn = muxedConn.newStream()
3436
const ms = new multistream.Dialer()
35-
ms.handle(conn, (err) => {
36-
if (err) { return cb(err) }
3737

38-
ms.select(identify.multicodec, (err, conn) => {
39-
if (err) { return cb(err) }
40-
41-
identify.listen(conn, (err, peerInfo, observedAddrs) => {
42-
if (err) { return cb(err) }
43-
44-
observedAddrs.forEach((oa) => {
45-
swarm._peerInfo.multiaddr.addSafe(oa)
46-
})
47-
48-
cb(null, peerInfo)
38+
waterfall([
39+
(cb) => ms.handle(conn, cb),
40+
(cb) => ms.select(identify.multicodec, cb),
41+
(conn, cb) => identify.dialer(conn, cb),
42+
(peerInfo, observedAddrs, cb) => {
43+
observedAddrs.forEach((oa) => {
44+
swarm._peerInfo.multiaddr.addSafe(oa)
4945
})
50-
})
51-
})
46+
cb(null, peerInfo)
47+
}
48+
], cb)
5249
}
5350

5451
conn.getPeerInfo((err, peerInfo) => {
@@ -74,8 +71,25 @@ module.exports = function connection (swarm) {
7471
reuse () {
7572
swarm.identify = true
7673
swarm.handle(identify.multicodec, (conn) => {
77-
identify.dial(conn, swarm._peerInfo)
74+
identify.listener(conn, swarm._peerInfo)
75+
})
76+
},
77+
78+
crypto (tag, encrypt) {
79+
if (!tag && !encrypt) {
80+
tag = plaintext.tag
81+
encrypt = plaintext.encrypt
82+
}
83+
84+
swarm.unhandle(swarm.crypto.tag)
85+
swarm.handle(tag, (conn) => {
86+
const id = swarm._peerInfo.id
87+
const secure = encrypt(id, id.privKey, conn)
88+
89+
protocolMuxer(swarm.protocols, secure)
7890
})
91+
92+
swarm.crypto = {tag, encrypt}
7993
}
8094
}
8195
}

src/dial.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const debug = require('debug')
66
const log = debug('libp2p:swarm:dial')
77

88
const protocolMuxer = require('./protocol-muxer')
9-
const secio = require('./secio')
10-
const tags = require('./tags')
119

1210
module.exports = function dial (swarm) {
1311
return (pi, protocol, callback) => {
@@ -108,16 +106,15 @@ module.exports = function dial (swarm) {
108106
}
109107

110108
const id = swarm._peerInfo.id
111-
if (id.privKey == null || swarm.encrypt === false) {
112-
return ms.select(tags.plaintext, cb)
113-
}
114109

115-
ms.select(tags.secio, (err, conn) => {
110+
log('selecting crypto: %s', swarm.crypto.tag)
111+
ms.select(swarm.crypto.tag, (err, conn) => {
116112
if (err) {
117113
return cb(err)
118114
}
119115

120-
cb(null, secio.create(id, conn))
116+
const wrapped = swarm.crypto.encrypt(id, id.privKey, conn)
117+
cb(null, wrapped)
121118
})
122119
})
123120
}
@@ -155,7 +152,7 @@ module.exports = function dial (swarm) {
155152
return
156153
}
157154

158-
const muxedConn = swarm.muxers[key].dial(conn)
155+
const muxedConn = swarm.muxers[key].dialer(conn)
159156
swarm.muxedConns[b58Id] = {}
160157
swarm.muxedConns[b58Id].muxer = muxedConn
161158
// should not be needed anymore - swarm.muxedConns[b58Id].conn = conn

src/index.js

+8-17
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const transport = require('./transport')
99
const connection = require('./connection')
1010
const dial = require('./dial')
1111
const protocolMuxer = require('./protocol-muxer')
12-
const secio = require('./secio')
13-
const tags = require('./tags')
12+
const plaintext = require('./plaintext')
1413

1514
exports = module.exports = Swarm
1615

@@ -52,8 +51,8 @@ function Swarm (peerInfo) {
5251
// is the Identify protocol enabled?
5352
this.identify = false
5453

55-
// is encryption enabled?
56-
this.encrypt = true
54+
// Crypto details
55+
this.crypto = plaintext
5756

5857
this.transport = transport(this)
5958
this.connection = connection(this)
@@ -95,21 +94,13 @@ function Swarm (peerInfo) {
9594
this.protocols[protocol] = handler
9695
}
9796

98-
let cryptoTag = tags.secio
99-
if (this.encrypt === false) {
100-
cryptoTag = tags.plaintext
101-
}
102-
103-
this.handle(cryptoTag, (conn) => {
104-
if (this.encrypt === false) {
105-
return protocolMuxer(this.protocols, conn)
106-
}
107-
108-
const secure = secio.create(this._peerInfo.id, conn)
109-
protocolMuxer(this.protocols, secure)
97+
this.handle(this.crypto.tag, (conn) => {
98+
const id = this._peerInfo.id
99+
const wrapped = this.crypto.encrypt(id, id.privKey, conn)
100+
return protocolMuxer(this.protocols, wrapped)
110101
})
111102

112-
this.unhandle = (protocol, handler) => {
103+
this.unhandle = (protocol) => {
113104
if (this.protocols[protocol]) {
114105
delete this.protocols[protocol]
115106
}

src/plaintext.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = {
4+
tag: '/plaintext/1.0.0',
5+
encrypt (id, privKey, conn) {
6+
return conn
7+
}
8+
}

src/secio.js

-10
This file was deleted.

src/tags.js

-6
This file was deleted.

test/06-conn-upgrade-secio.node.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Peer = require('peer-info')
99
const TCP = require('libp2p-tcp')
1010
const multiplex = require('libp2p-spdy')
1111
const pull = require('pull-stream')
12+
const secio = require('libp2p-secio')
1213

1314
const Swarm = require('../src')
1415

@@ -37,9 +38,9 @@ describe('secio conn upgrade (on TCP)', () => {
3738
swarmB = new Swarm(peerB)
3839
swarmC = new Swarm(peerC)
3940

40-
swarmA.encrypt = true
41-
swarmB.encrypt = true
42-
swarmC.encrypt = true
41+
swarmA.connection.crypto(secio.tag, secio.encrypt)
42+
swarmB.connection.crypto(secio.tag, secio.encrypt)
43+
swarmC.connection.crypto(secio.tag, secio.encrypt)
4344

4445
swarmA.transport.add('tcp', new TCP())
4546
swarmB.transport.add('tcp', new TCP())
@@ -113,4 +114,9 @@ describe('secio conn upgrade (on TCP)', () => {
113114
}, 500)
114115
})
115116
})
117+
118+
it('switch back to plaintext if no arguments passed in', () => {
119+
swarmA.connection.crypto()
120+
expect(swarmA.crypto.tag).to.be.eql('/plaintext/1.0.0')
121+
})
116122
})

0 commit comments

Comments
 (0)