js-libp2p-secio/src/index.js

43 lines
821 B
JavaScript
Raw Normal View History

2016-05-20 21:29:53 +02:00
'use strict'
const pull = require('pull-stream')
const Connection = require('interface-connection').Connection
2016-05-22 13:19:17 +02:00
2016-05-22 01:51:15 +02:00
const handshake = require('./handshake')
const State = require('./state')
2016-05-22 01:51:15 +02:00
2016-09-07 12:22:04 +02:00
module.exports = {
tag: '/secio/1.0.0',
encrypt (local, key, insecure, callback) {
if (!local) {
2016-05-22 01:51:15 +02:00
throw new Error('no local id provided')
}
if (!key) {
2016-05-22 01:51:15 +02:00
throw new Error('no local private key provided')
}
if (!insecure) {
throw new Error('no insecure stream provided')
}
if (!callback) {
callback = (err) => {
2016-11-09 11:05:13 +01:00
if (err) {
console.error(err)
}
}
}
const state = new State(local, key, 60 * 1000 * 5, callback)
pull(
2016-09-07 12:22:04 +02:00
insecure,
handshake(state),
insecure
)
2016-09-07 12:22:04 +02:00
return new Connection(state.secure, insecure)
2016-05-22 01:51:15 +02:00
}
}