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

Commit 8ad4c15

Browse files
mkg20001jacobheun
authored andcommitted
refactor: use async await (#108)
* chore: upgrade deps * feat: first iteration of the idea * feat: iterate a layer deeper * feat: rewrite handshake * feat: rewrite propose * feat: rewrite finish * feat: rewrite exchange * feat: rewrite low-level stuff * feat: work on rewriting tests * refactor: browser tests * refactor: .aegir.js * feat: refactor benchmarks * fix: try to make it work * fix: lint * refactor: move tests * refactor: switch deps * refactor: entry file * refactor: a bit more * fix: tests * feat: inital iterables refactor * refactor: streaming * refactor: cleanup * fix: turn bufferlist into buffer * fix: use errors from interfaces * refactor: etm * fix: typo * fix: .read error * fix: satisfy output expectations * fix: it works - WARNING: using varint instead of fixed lp, tests lie * fix: use errors * refactor: benchmarks * fix: add suggestions from review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * fix: upgrade deps and use correct lp-encoder * refactor: apply changes from review * refactor: apply changes from review * refactor: apply changes from review * chore: remove old tests test: add support tests back * test: fix async benchmarks * chore: clean up deps * fix: use fixed encoding/decoding everywhere fix: exchange final nonce handshake over encryption * test: add verify inbound and outbound secio * test: verify nonces are boxed * chore: add node 12 to ci
1 parent 774267f commit 8ad4c15

24 files changed

+501
-820
lines changed

.aegir.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ stages:
99

1010
node_js:
1111
- '10'
12+
- '12'
1213

1314
os:
1415
- linux

README.md

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,44 +42,9 @@ const secio = require('libp2p-secio')
4242

4343
## API
4444

45-
### `.tag`
45+
This module exposes a crypto interface, as defined in the [js-interfaces](https://github.com/libp2p/js-interfaces)
4646

47-
The current `secio` tag, usable in `multistream`.
48-
49-
### `const encryptedConnection = secio.encrypt(localPeerId, plainTextConnection [, remotePeerId] [, callback])`
50-
51-
- `localPeerId: PeerId` - A PeerId object containing the Private, Public and Id of our node.
52-
- `plainTextConnection: Connection` - The insecure connection to be secured.
53-
- `remotePeerId: PeerId` - A PeerId object containing the Public and/or Id of the node we are doing the SECIO handshake with.
54-
- `callback: Function` - Optional, Called if an error happens during the initialization.
55-
56-
Returns an encrypted [Connection object](https://github.com/libp2p/interface-connection) that is the upgraded `plainTextConnection` with now having every byte encrypted.
57-
58-
Both plainTextConnection and encryptedConnection are at their base, PullStreams.
59-
60-
### This module uses `pull-streams`
61-
62-
We expose a streaming interface based on `pull-streams`, rather then on the Node.js core streams implementation (aka Node.js streams). `pull-streams` offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).
63-
64-
You can learn more about pull-streams at:
65-
66-
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ)
67-
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams)
68-
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple)
69-
- [pull-streams documentation](https://pull-stream.github.io/)
70-
71-
#### Converting `pull-streams` to Node.js Streams
72-
73-
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
74-
75-
```js
76-
const pullToStream = require('pull-stream-to-stream')
77-
78-
const nodeStreamInstance = pullToStream(pullStreamInstance)
79-
// nodeStreamInstance is an instance of a Node.js Stream
80-
```
81-
82-
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.
47+
[ » API Docs ](https://github.com/libp2p/js-interfaces/tree/master/src/crypto#api)
8348

8449
## Contribute
8550

benchmarks/send.js

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,70 @@
11
'use strict'
22

3+
/* eslint-disable no-console */
4+
35
const Benchmark = require('benchmark')
4-
const pull = require('pull-stream/pull')
5-
const infinite = require('pull-stream/sources/infinite')
6-
const take = require('pull-stream/throughs/take')
7-
const drain = require('pull-stream/sinks/drain')
8-
const Connection = require('interface-connection').Connection
9-
const parallel = require('async/parallel')
10-
const pair = require('pull-pair/duplex')
116
const PeerId = require('peer-id')
127

13-
const secio = require('../src')
8+
const pipe = require('it-pipe')
9+
const { reduce } = require('streaming-iterables')
10+
const DuplexPair = require('it-pair/duplex')
11+
12+
const secio = require('..')
1413

1514
const suite = new Benchmark.Suite('secio')
1615
let peers
1716

18-
function sendData (a, b, opts, finish) {
17+
async function sendData (a, b, opts) {
1918
opts = Object.assign({ times: 1, size: 100 }, opts)
2019

21-
pull(
22-
infinite(() => Buffer.allocUnsafe(opts.size)),
23-
take(opts.times),
20+
let i = opts.times
21+
22+
pipe(
23+
function * () {
24+
while (i--) {
25+
yield Buffer.allocUnsafe(opts.size)
26+
}
27+
},
2428
a
2529
)
2630

27-
let length = 0
28-
29-
pull(
31+
const res = await pipe(
3032
b,
31-
drain((data) => {
32-
length += data.length
33-
}, () => {
34-
if (length !== opts.times * opts.size) {
35-
throw new Error('Did not receive enough chunks')
36-
}
37-
finish.resolve()
38-
})
33+
reduce((acc, val) => acc + val.length, 0)
3934
)
40-
}
4135

42-
function ifErr (err) {
43-
if (err) {
44-
throw err
36+
if (res !== opts.times * opts.size) {
37+
throw new Error('Did not receive enough chunks')
4538
}
4639
}
4740

48-
suite.add('create peers for test', (deferred) => {
49-
parallel([
50-
(cb) => PeerId.createFromJSON(require('./peer-a'), cb),
51-
(cb) => PeerId.createFromJSON(require('./peer-b'), cb)
52-
], (err, _peers) => {
53-
if (err) { throw err }
54-
peers = _peers
55-
41+
suite.add('create peers for test', {
42+
defer: true,
43+
fn: async (deferred) => {
44+
peers = await Promise.all([
45+
PeerId.createFromJSON(require('./peer-a')),
46+
PeerId.createFromJSON(require('./peer-b'))
47+
])
5648
deferred.resolve()
57-
})
58-
}, { defer: true })
59-
60-
suite.add('establish an encrypted channel', (deferred) => {
61-
const p = pair()
49+
}
50+
})
51+
suite.add('establish an encrypted channel', {
52+
defer: true,
53+
fn: async (deferred) => {
54+
const p = DuplexPair()
6255

63-
const peerA = peers[0]
64-
const peerB = peers[1]
56+
const peerA = peers[0]
57+
const peerB = peers[1]
6558

66-
const aToB = secio.encrypt(peerA, new Connection(p[0]), peerB, ifErr)
67-
const bToA = secio.encrypt(peerB, new Connection(p[1]), peerA, ifErr)
59+
const [aToB, bToA] = await Promise.all([
60+
secio.secureInbound(peerA, p[0], peerB),
61+
secio.secureOutbound(peerB, p[1], peerA)
62+
])
6863

69-
sendData(aToB, bToA, {}, deferred)
70-
}, { defer: true })
64+
await sendData(aToB.conn, bToA.conn, {})
65+
deferred.resolve()
66+
}
67+
})
7168

7269
const cases = [
7370
[10, 262144],
@@ -81,23 +78,32 @@ cases.forEach((el) => {
8178
const times = el[0]
8279
const size = el[1]
8380

84-
suite.add(`send plaintext ${times} x ${size} bytes`, (deferred) => {
85-
const p = pair()
81+
suite.add(`send plaintext ${times} x ${size} bytes`, {
82+
defer: true,
83+
fn: async (deferred) => {
84+
const p = DuplexPair()
85+
await sendData(p[0], p[1], { times: times, size: size })
86+
deferred.resolve()
87+
}
88+
})
8689

87-
sendData(p[0], p[1], { times: times, size: size }, deferred)
88-
}, { defer: true })
90+
suite.add(`send encrypted ${times} x ${size} bytes`, {
91+
defer: true,
92+
fn: async (deferred) => {
93+
const p = DuplexPair()
8994

90-
suite.add(`send encrypted ${times} x ${size} bytes`, (deferred) => {
91-
const p = pair()
95+
const peerA = peers[0]
96+
const peerB = peers[1]
9297

93-
const peerA = peers[0]
94-
const peerB = peers[1]
98+
const [aToB, bToA] = await Promise.all([
99+
secio.secureInbound(peerA, p[0], peerB),
100+
secio.secureOutbound(peerB, p[1], peerA)
101+
])
95102

96-
const aToB = secio.encrypt(peerA, new Connection(p[0]), peerB, ifErr)
97-
const bToA = secio.encrypt(peerB, new Connection(p[1]), peerA, ifErr)
98-
99-
sendData(aToB, bToA, { times: times, size: size }, deferred)
100-
}, { defer: true })
103+
await sendData(aToB.conn, bToA.conn, { times: times, size: size })
104+
deferred.resolve()
105+
}
106+
})
101107
})
102108

103109
suite.on('cycle', (event) => {

package.json

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,26 @@
2525
],
2626
"license": "MIT",
2727
"dependencies": {
28-
"async": "^2.6.2",
28+
"bl": "^4.0.0",
2929
"debug": "^4.1.1",
30-
"interface-connection": "~0.3.3",
31-
"libp2p-crypto": "~0.16.1",
32-
"multiaddr": "^6.0.6",
33-
"multihashing-async": "~0.6.0",
34-
"once": "^1.4.0",
35-
"peer-id": "~0.12.2",
36-
"peer-info": "~0.15.1",
37-
"protons": "^1.0.1",
38-
"pull-defer": "~0.2.3",
39-
"pull-handshake": "^1.1.4",
40-
"pull-length-prefixed": "^1.3.2",
41-
"pull-stream": "^3.6.9",
42-
"safe-buffer": "^5.1.2"
30+
"it-buffer": "^0.1.1",
31+
"it-length-prefixed": "^3.0.0",
32+
"it-pair": "^1.0.0",
33+
"it-pb-rpc": "^0.1.4",
34+
"it-pipe": "^1.1.0",
35+
"libp2p-crypto": "~0.17.1",
36+
"libp2p-interfaces": "~0.1.3",
37+
"multiaddr": "^7.2.1",
38+
"multihashing-async": "~0.8.0",
39+
"peer-id": "~0.13.5",
40+
"protons": "^1.0.1"
4341
},
4442
"devDependencies": {
45-
"aegir": "^18.2.2",
43+
"aegir": "^20.4.1",
4644
"benchmark": "^2.1.4",
4745
"chai": "^4.2.0",
4846
"dirty-chai": "^2.0.1",
49-
"libp2p-websockets": "~0.12.2",
50-
"multistream-select": "~0.14.4",
51-
"pull-goodbye": "~0.0.2",
52-
"pull-pair": "^1.1.0"
47+
"streaming-iterables": "^4.1.1"
5348
},
5449
"engines": {
5550
"node": ">=6.0.0",

0 commit comments

Comments
 (0)