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

Commit 71132f7

Browse files
authored
Merge pull request #17 from libp2p/api
refactor: better public facing api
2 parents 480c2b3 + c686ea7 commit 71132f7

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ const secio = require('libp2p-secio')
3535

3636
## API
3737

38-
### `SecureSession`
38+
### `tag`
3939

40-
#### `constructor(id, key, insecure)`
40+
The current `secio` tag, usable in `multistream`.
41+
42+
### `encrypt(id, key, insecure)`
4143

4244
- `id: PeerId` - The id of the node.
4345
- `key: RSAPrivateKey` - The private key of the node.
4446
- `insecure: PullStream` - The insecure connection.
4547

46-
### `.secure`
47-
4848
Returns the `insecure` connection provided, wrapped with secio. This is a pull-stream.
4949

5050
### This module uses `pull-streams`

src/index.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const Connection = require('interface-connection').Connection
66
const handshake = require('./handshake')
77
const State = require('./state')
88

9-
exports.SecureSession = class SecureSession {
10-
constructor (local, key, insecure) {
9+
module.exports = {
10+
tag: '/secio/1.0.0',
11+
encrypt (local, key, insecure) {
1112
if (!local) {
1213
throw new Error('no local id provided')
1314
}
@@ -20,17 +21,14 @@ exports.SecureSession = class SecureSession {
2021
throw new Error('no insecure stream provided')
2122
}
2223

23-
this.state = new State(local, key)
24-
this.insecure = insecure
24+
const state = new State(local, key)
2525

2626
pull(
27-
this.insecure,
28-
handshake(this.state),
29-
this.insecure
27+
insecure,
28+
handshake(state),
29+
insecure
3030
)
31-
}
3231

33-
get secure () {
34-
return new Connection(this.state.secure, this.insecure)
32+
return new Connection(state.secure, insecure)
3533
}
3634
}

test/index.spec.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@ const pull = require('pull-stream')
1212
const Listener = ms.Listener
1313
const Dialer = ms.Dialer
1414

15-
const SecureSession = require('../src').SecureSession
15+
const secio = require('../src')
1616

1717
describe('libp2p-secio', () => {
18+
it('exports a tag', () => {
19+
expect(secio.tag).to.be.eql('/secio/1.0.0')
20+
})
21+
1822
it('upgrades a connection', (done) => {
1923
const p = pair()
2024

2125
const local = createSession(p[0])
2226
const remote = createSession(p[1])
23-
const localSecure = local.session.secure
2427

2528
pull(
2629
pull.values(['hello world']),
27-
localSecure
30+
local
2831
)
2932

30-
const remoteSecure = remote.session.secure
3133
pull(
32-
remoteSecure,
34+
remote,
3335
pull.collect((err, chunks) => {
3436
expect(err).to.not.exist
3537
expect(chunks).to.be.eql([new Buffer('hello world')])
@@ -52,7 +54,7 @@ describe('libp2p-secio', () => {
5254
], cb),
5355
(cb) => {
5456
listener.addHandler('/banana/1.0.0', (conn) => {
55-
local = createSession(conn).session.secure
57+
local = createSession(conn)
5658
pull(
5759
local,
5860
pull.collect((err, chunks) => {
@@ -65,7 +67,7 @@ describe('libp2p-secio', () => {
6567
cb()
6668
},
6769
(cb) => dialer.select('/banana/1.0.0', (err, conn) => {
68-
remote = createSession(conn).session.secure
70+
remote = createSession(conn)
6971
pull(
7072
pull.values(['hello world']),
7173
remote
@@ -81,10 +83,6 @@ describe('libp2p-secio', () => {
8183
function createSession (insecure) {
8284
const key = crypto.generateKeyPair('RSA', 2048)
8385
const id = PeerId.createFromPrivKey(key.bytes)
84-
return {
85-
id,
86-
key,
87-
insecure,
88-
session: new SecureSession(id, key, insecure)
89-
}
86+
87+
return secio.encrypt(id, key, insecure)
9088
}

0 commit comments

Comments
 (0)