refactor: better public facing api

This commit is contained in:
Friedel Ziegelmayer 2016-09-07 12:22:04 +02:00
parent 480c2b3465
commit c686ea7ffb
3 changed files with 23 additions and 27 deletions

View File

@ -35,16 +35,16 @@ const secio = require('libp2p-secio')
## API ## API
### `SecureSession` ### `tag`
#### `constructor(id, key, insecure)` The current `secio` tag, usable in `multistream`.
### `encrypt(id, key, insecure)`
- `id: PeerId` - The id of the node. - `id: PeerId` - The id of the node.
- `key: RSAPrivateKey` - The private key of the node. - `key: RSAPrivateKey` - The private key of the node.
- `insecure: PullStream` - The insecure connection. - `insecure: PullStream` - The insecure connection.
### `.secure`
Returns the `insecure` connection provided, wrapped with secio. This is a pull-stream. Returns the `insecure` connection provided, wrapped with secio. This is a pull-stream.
### This module uses `pull-streams` ### This module uses `pull-streams`

View File

@ -6,8 +6,9 @@ const Connection = require('interface-connection').Connection
const handshake = require('./handshake') const handshake = require('./handshake')
const State = require('./state') const State = require('./state')
exports.SecureSession = class SecureSession { module.exports = {
constructor (local, key, insecure) { tag: '/secio/1.0.0',
encrypt (local, key, insecure) {
if (!local) { if (!local) {
throw new Error('no local id provided') throw new Error('no local id provided')
} }
@ -20,17 +21,14 @@ exports.SecureSession = class SecureSession {
throw new Error('no insecure stream provided') throw new Error('no insecure stream provided')
} }
this.state = new State(local, key) const state = new State(local, key)
this.insecure = insecure
pull( pull(
this.insecure, insecure,
handshake(this.state), handshake(state),
this.insecure insecure
) )
}
get secure () { return new Connection(state.secure, insecure)
return new Connection(this.state.secure, this.insecure)
} }
} }

View File

@ -12,24 +12,26 @@ const pull = require('pull-stream')
const Listener = ms.Listener const Listener = ms.Listener
const Dialer = ms.Dialer const Dialer = ms.Dialer
const SecureSession = require('../src').SecureSession const secio = require('../src')
describe('libp2p-secio', () => { describe('libp2p-secio', () => {
it('exports a tag', () => {
expect(secio.tag).to.be.eql('/secio/1.0.0')
})
it('upgrades a connection', (done) => { it('upgrades a connection', (done) => {
const p = pair() const p = pair()
const local = createSession(p[0]) const local = createSession(p[0])
const remote = createSession(p[1]) const remote = createSession(p[1])
const localSecure = local.session.secure
pull( pull(
pull.values(['hello world']), pull.values(['hello world']),
localSecure local
) )
const remoteSecure = remote.session.secure
pull( pull(
remoteSecure, remote,
pull.collect((err, chunks) => { pull.collect((err, chunks) => {
expect(err).to.not.exist expect(err).to.not.exist
expect(chunks).to.be.eql([new Buffer('hello world')]) expect(chunks).to.be.eql([new Buffer('hello world')])
@ -52,7 +54,7 @@ describe('libp2p-secio', () => {
], cb), ], cb),
(cb) => { (cb) => {
listener.addHandler('/banana/1.0.0', (conn) => { listener.addHandler('/banana/1.0.0', (conn) => {
local = createSession(conn).session.secure local = createSession(conn)
pull( pull(
local, local,
pull.collect((err, chunks) => { pull.collect((err, chunks) => {
@ -65,7 +67,7 @@ describe('libp2p-secio', () => {
cb() cb()
}, },
(cb) => dialer.select('/banana/1.0.0', (err, conn) => { (cb) => dialer.select('/banana/1.0.0', (err, conn) => {
remote = createSession(conn).session.secure remote = createSession(conn)
pull( pull(
pull.values(['hello world']), pull.values(['hello world']),
remote remote
@ -81,10 +83,6 @@ describe('libp2p-secio', () => {
function createSession (insecure) { function createSession (insecure) {
const key = crypto.generateKeyPair('RSA', 2048) const key = crypto.generateKeyPair('RSA', 2048)
const id = PeerId.createFromPrivKey(key.bytes) const id = PeerId.createFromPrivKey(key.bytes)
return {
id, return secio.encrypt(id, key, insecure)
key,
insecure,
session: new SecureSession(id, key, insecure)
}
} }