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

Refresh #95

Merged
merged 8 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const WS = require('libp2p-websockets')
const PeerId = require('peer-id')

const secio = require('./src')

const peerNodeJSON = require('./test/fixtures/peer-node.json')
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
let listener

module.exports = {
hooks: {
browser: {
pre: (done) => {
PeerId.createFromJSON(peerNodeJSON, (err, peerId) => {
if (err) { throw err }

const ws = new WS()

listener = ws.createListener((conn) => {
const encryptedConn = secio.encrypt(peerId, conn, undefined, (err) => {
if (err) { throw err }
})

// echo
pull(encryptedConn, encryptedConn)
})

listener.listen(ma, done)
})
},
post: (done) => {
listener.close(done)
}
}
}
}
52 changes: 11 additions & 41 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
**/node_modules/
**/*.log
test/repo-tests*
**/bundle.js

# Logs
logs
*.log

coverage

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

build

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
package-lock.json
yarn.lock
docs

lib
**/node_modules
**/*.log
test/setup/tmp-disposable-nodes-addrs.json
dist
test/test-data/go-ipfs-repo/LOCK
test/test-data/go-ipfs-repo/LOG
test/test-data/go-ipfs-repo/LOG.old

# while testing npm5
package-lock.json
yarn.lock
coverage
**/*.swp
examples/sub-module/**/bundle.js
examples/sub-module/**/*-minified.js
examples/sub-module/*-bundle.js
31 changes: 1 addition & 30 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
**/node_modules/
**/*.log
test/repo-tests*

# Logs
logs
node_modules
*.log

coverage

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

build

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

test
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ matrix:
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8

script:
- npm run lint
- npm run test
- npm run coverage

before_script:
- export DISPLAY=:99.0
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
![](https://img.shields.io/badge/Node.js-%3E%3D6.0.0-orange.svg?style=flat-square)


> Secio implementation in JavaScript
> SECIO implementation in JavaScript

This repo contains the JavaScript implementation of secio, an encryption protocol used in libp2p. This is based on this [go implementation](https://github.com/libp2p/go-libp2p-secio).

Expand All @@ -39,18 +39,20 @@ const secio = require('libp2p-secio')

## API

### `tag`
### `.tag`

The current `secio` tag, usable in `multistream`.

### `encrypt(id, key, insecure[, callback])`
### `const encryptedConnection = secio.encrypt(localPeerId, plainTextConnection [, remotePeerId] [, callback])`

- `id: PeerId` - The id of the node.
- `key: RSAPrivateKey` - The private key of the node.
- `insecure: PullStream` - The insecure connection.
- `callback: Function` - Called if an error happens during the initialization.
- `localPeerId: PeerId` - A PeerId object containing the Private, Public and Id of our node.
- `plainTextConnection: Connection` - The insecure connection to be secured.
- `remotePeerId: PeerId` - A PeerId object containing the Public and/or Id of the node we are doing the SECIO handshake with.
- `callback: Function` - Optional, Called if an error happens during the initialization.

Returns the `insecure` connection provided, wrapped with secio. This is a pull-stream.
Returns an encrypted [Connection object](https://github.com/libp2p/interface-connection) that is the upgraded `plainTextConnection` with now having every byte encripted.

Both plainTextConnection and encryptedConnection are at their base, PullStreams.

### This module uses `pull-streams`

Expand All @@ -76,8 +78,6 @@ const nodeStreamInstance = pullToStream(pullStreamInstance)

To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.



## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-secio/issues)!
Expand Down
5 changes: 5 additions & 0 deletions benchmarks/peer-a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "QmVbkvMswbUZbmRHbmBicg5sdzDYBnvGPC5PhZFnkozdoP",
"privKey": "CAASpgkwggSiAgEAAoIBAQCX1xFsZIszqLYXaJ7QZUbotUcWC6SB0CZGUamCPWD5Idfa+LiMByQKLxJrttNkcx+85NRIYPzlV6ToHz6OH/WzUCXEOFy0iKmq/Jz15KKuzzDuyzToqPI0EPRTKWoxI4rj7Ppx6wFQvancgLvxxmPslaqxST+9tsdDw5W2cQnRAIBPoJZtrS9AL9dya6bt6GiLQKKHox4jxVE0P/QYj6/LX7e5wjZMfeS6y4yIdfSiu9UueLI78pixRn5qXcn0zL0L/+xWNsZG2tSVG7BbHFGm4PjF/WdELSOHj9R2YWH+snzdkxPcX3zwle+JKJW/AJGjJGMIoNkIuDg/1uizSRzhAgMBAAECggEAa90gR3v1VzFWibKMf0Yy5UVBxmtm5cDUL8f7PsYTiWd9S9S8ZNz9wS5HS7WI3ibaOPnlw7UCcnmccDkh2xzhEgyxh37yfF1JCmqBpjxRRRVykXBRmaXKySnyJtGY2meBVwEVuK9VM+JINJhO6yYf/gUkSOTcHw0k6QKb7ArJCmrMVH/b/mjqbzQpUM74M6f/5DDU2IYCAaGu5QB7VZsAFSocb5aVvbW3uEAwJ9AmoqYO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CPWD5Idfa+LiMByQKLxJrttNkcx+85NRIYPzlV6ToHz6OH/WzUCXEOFy0iKmq/Jz15KKuzzDuyzToqPI0EPRTKWoxI4rj7Ppx6wFQvancgLvxxmPslaqxST+9tsdDw5W2cQnRAIBPoJZtrS9AL9dya6bt6GiLQKKHox4jxVE0P/QYj6/LX7e5wjZMfeS6y4yIdfSiu9UueLI78pixRn5qXcn0zL0L/+xWNsZG2tSVG7BbHFGm4PjF/WdELSOHj9R2YWH+snzdkxPcX3zwle+JKJW/AJGjJGMIoNkIuDg/1uizSRzhAgMBAAE="
}
5 changes: 5 additions & 0 deletions benchmarks/peer-b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "QmTY6txxqgf7E3ChpYEXqz6QSVa32qjDbyeKuiaPYT1vx9",
"privKey": "CAASqQkwggSlAgEAAoIBAQDLAL7+npPinALH9GE0SSSgi89fIWnbZXO1e94cdpJsR6WoZgGeHNZMTUew3x7TeJV1PoX7DFyM2ZxnclJx/C2DLQzQbRnmbMNqfq+3WfvRBk/e2D0UtwALSCPvM5qKBj7JWCAleAw7ypfJYXL1rSa5tYtLprQd6mt9IZsBCHLnfPibMM8X+8QOkw0Y40NONFaTRPN3PXznAmMekFkAMpSf1QyhNlS2WCX5+aARz8uEZYVdycjcA/ISq/WYxEeFCoyRVdEvjeq+NS5b6pFV5nZ4PnOJzFmvEyGBqndpNHEMgCWsvlspXN/jFMY1pTBds/35b4Vqd6ui7lZTiIEr/3nZAgMBAAECggEBALaLMTRc/Lale4q6oj4WF62Wix8MONhk3iuv+wa1SO29b6WCNKotXDa4t2LBAENBB4SqeGhLsV2mwRH0p1aAYZy3gfJaaRPfccntqazCacKIXoIRCLv74zpB4K5URP8gpoz/hcirrrSFdQBNdMaesVgFfADHIuhTx9x5v5yqMEkfaOe5SDxGLhYGEfBGm/+a3zJK9BXUkQ8gCiqRXlCYM2x9emzBBOKDRBUYTJeSStggHKMJwrCm+XYyAMeYf9CLGXa0ziG4QU7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nclJx/C2DLQzQbRnmbMNqfq+3WfvRBk/e2D0UtwALSCPvM5qKBj7JWCAleAw7ypfJYXL1rSa5tYtLprQd6mt9IZsBCHLnfPibMM8X+8QOkw0Y40NONFaTRPN3PXznAmMekFkAMpSf1QyhNlS2WCX5+aARz8uEZYVdycjcA/ISq/WYxEeFCoyRVdEvjeq+NS5b6pFV5nZ4PnOJzFmvEyGBqndpNHEMgCWsvlspXN/jFMY1pTBds/35b4Vqd6ui7lZTiIEr/3nZAgMBAAE="
}
132 changes: 76 additions & 56 deletions benchmarks/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,97 @@

const Benchmark = require('benchmark')
const pull = require('pull-stream')
const parallel = require('async/parallel')
const pair = require('pull-pair/duplex')
const PeerId = require('peer-id')
const crypto = require('libp2p-crypto')

const secio = require('../src')

const suite = new Benchmark.Suite('secio')
const ids = []
let peers

suite.add('createKey', function (d) {
crypto.keys.generateKeyPair('RSA', 2048, (err, key) => {
if (err) { throw err }
PeerId.createFromPrivKey(key.bytes, (err, id) => {
if (err) { throw err }
function sendData (a, b, opts, finish) {
opts = Object.assign({ times: 1, size: 100 }, opts)

pull(
pull.infinite(() => Buffer.allocUnsafe(opts.size)),
pull.take(opts.times),
a
)

ids.push(id)
d.resolve()
let length = 0

pull(
b,
pull.drain((data) => {
length += data.length
}, () => {
if (length !== opts.times * opts.size) {
throw new Error('Did not receive enough chunks')
}
finish.resolve()
})
})
}, {
defer: true
})
.add('send', function (deferred) {
const p = pair()
)
}

createSession(p[0], (err, local) => {
suite.add('create peers for test', (deferred) => {
parallel([
(cb) => PeerId.createFromJSON(require('./peer-a'), cb),
(cb) => PeerId.createFromJSON(require('./peer-b'), cb)
], (err, _peers) => {
if (err) { throw err }
createSession(p[1], (err, remote) => {
if (err) { throw err }
peers = _peers

sendMessages(local, remote)
})
deferred.resolve()
})
}, { defer: true })

suite.add('establish an encrypted channel', (deferred) => {
const p = pair()

const peerA = peers[0]
const peerB = peers[1]

const aToB = secio.encrypt(peerA, p[0], peerB, (err) => { throw err })
const bToA = secio.encrypt(peerB, p[1], peerA, (err) => { throw err })

sendData(aToB, bToA, {}, deferred)
}, { defer: true })

const cases = [
[10, 262144],
[100, 262144],
[1000, 262144]
// [10000, 262144],
// [100000, 262144],
// [1000000, 262144]
]
cases.forEach((el) => {
const times = el[0]
const size = el[1]

function sendMessages (local, remote) {
pull(
pull.infinite(),
pull.take(100),
pull.map((val) => Buffer.from(val.toString())),
local
)

pull(
remote,
pull.take(100),
pull.collect((err, chunks) => {
if (err) throw err
if (chunks.length !== 100) throw new Error('Did not receive enough chunks')
deferred.resolve()
})
)
}
}, {
defer: true
suite.add(`send plaintext ${times} x ${size} bytes`, (deferred) => {
const p = pair()

sendData(p[0], p[1], { times: times, size: size }, deferred)
}, { defer: true })

suite.add(`send encrypted ${times} x ${size} bytes`, (deferred) => {
const p = pair()

const peerA = peers[0]
const peerB = peers[1]

const aToB = secio.encrypt(peerA, p[0], peerB, (err) => { throw err })
const bToA = secio.encrypt(peerB, p[1], peerA, (err) => { throw err })

sendData(aToB, bToA, { times: times, size: size }, deferred)
}, { defer: true })
})
.on('cycle', (event) => {

suite.on('cycle', (event) => {
console.log(String(event.target))
})
// run async
.run({
async: true
})

function createSession (insecure, cb) {
crypto.keys.generateKeyPair('RSA', 2048, (err, key) => {
if (err) { return cb(err) }

PeerId.createFromPrivKey(key.bytes, (err, id) => {
if (err) { return cb(err) }

cb(null, secio.encrypt(id, key, insecure))
})
})
}
// run async
suite.run({ async: true })
4 changes: 4 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ machine:
node:
version: stable

test:
post:
- npm run coverage -- --upload --providers coveralls

dependencies:
pre:
- google-chrome --version
Expand Down
Loading