2016-05-20 21:29:53 +02:00
|
|
|
'use strict'
|
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
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')
|
2016-06-13 19:10:38 +02:00
|
|
|
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',
|
2016-11-03 10:23:33 +01:00
|
|
|
encrypt (local, key, insecure, callback) {
|
2016-06-13 19:10:38 +02:00
|
|
|
if (!local) {
|
2016-05-22 01:51:15 +02:00
|
|
|
throw new Error('no local id provided')
|
|
|
|
}
|
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
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')
|
|
|
|
}
|
2016-05-23 17:33:16 +02:00
|
|
|
|
2016-11-03 10:23:33 +01:00
|
|
|
if (!callback) {
|
|
|
|
callback = (err) => {
|
2016-11-09 11:05:13 +01:00
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2016-11-03 10:23:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 13:37:35 +01:00
|
|
|
const state = new State(local, key, 60 * 1000 * 5, callback)
|
2016-05-23 17:33:16 +02:00
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
pull(
|
2016-09-07 12:22:04 +02:00
|
|
|
insecure,
|
|
|
|
handshake(state),
|
|
|
|
insecure
|
2016-06-13 19:10:38 +02:00
|
|
|
)
|
2016-05-22 22:39:36 +02:00
|
|
|
|
2016-09-07 12:22:04 +02:00
|
|
|
return new Connection(state.secure, insecure)
|
2016-05-22 01:51:15 +02:00
|
|
|
}
|
|
|
|
}
|