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

refactor: better public facing api #17

Merged
merged 1 commit into from
Sep 7, 2016
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ const secio = require('libp2p-secio')

## 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.
- `key: RSAPrivateKey` - The private key of the node.
- `insecure: PullStream` - The insecure connection.

### `.secure`

Returns the `insecure` connection provided, wrapped with secio. This is a pull-stream.

### This module uses `pull-streams`
Expand Down
18 changes: 8 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const Connection = require('interface-connection').Connection
const handshake = require('./handshake')
const State = require('./state')

exports.SecureSession = class SecureSession {
constructor (local, key, insecure) {
module.exports = {
tag: '/secio/1.0.0',
encrypt (local, key, insecure) {
if (!local) {
throw new Error('no local id provided')
}
Expand All @@ -20,17 +21,14 @@ exports.SecureSession = class SecureSession {
throw new Error('no insecure stream provided')
}

this.state = new State(local, key)
this.insecure = insecure
const state = new State(local, key)

pull(
this.insecure,
handshake(this.state),
this.insecure
insecure,
handshake(state),
insecure
)
}

get secure () {
return new Connection(this.state.secure, this.insecure)
return new Connection(state.secure, insecure)
}
}
24 changes: 11 additions & 13 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@ const pull = require('pull-stream')
const Listener = ms.Listener
const Dialer = ms.Dialer

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

describe('libp2p-secio', () => {
it('exports a tag', () => {
expect(secio.tag).to.be.eql('/secio/1.0.0')
})

it('upgrades a connection', (done) => {
const p = pair()

const local = createSession(p[0])
const remote = createSession(p[1])
const localSecure = local.session.secure

pull(
pull.values(['hello world']),
localSecure
local
)

const remoteSecure = remote.session.secure
pull(
remoteSecure,
remote,
pull.collect((err, chunks) => {
expect(err).to.not.exist
expect(chunks).to.be.eql([new Buffer('hello world')])
Expand All @@ -52,7 +54,7 @@ describe('libp2p-secio', () => {
], cb),
(cb) => {
listener.addHandler('/banana/1.0.0', (conn) => {
local = createSession(conn).session.secure
local = createSession(conn)
pull(
local,
pull.collect((err, chunks) => {
Expand All @@ -65,7 +67,7 @@ describe('libp2p-secio', () => {
cb()
},
(cb) => dialer.select('/banana/1.0.0', (err, conn) => {
remote = createSession(conn).session.secure
remote = createSession(conn)
pull(
pull.values(['hello world']),
remote
Expand All @@ -81,10 +83,6 @@ describe('libp2p-secio', () => {
function createSession (insecure) {
const key = crypto.generateKeyPair('RSA', 2048)
const id = PeerId.createFromPrivKey(key.bytes)
return {
id,
key,
insecure,
session: new SecureSession(id, key, insecure)
}

return secio.encrypt(id, key, insecure)
}