mirror of
https://github.com/fluencelabs/js-libp2p-secio
synced 2025-05-11 22:07:13 +00:00
refactor: better public facing api
This commit is contained in:
parent
480c2b3465
commit
c686ea7ffb
@ -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`
|
||||
|
18
src/index.js
18
src/index.js
@ -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')
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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')])
|
||||
@ -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) => {
|
||||
@ -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
|
||||
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user