1
0
mirror of https://github.com/fluencelabs/js-libp2p-secio synced 2025-04-02 10:21:11 +00:00
2018-01-07 16:24:43 +00:00

59 lines
1.4 KiB
JavaScript

'use strict'
const pull = require('pull-stream')
const Connection = require('interface-connection').Connection
const assert = require('assert')
const PeerInfo = require('peer-info')
const debug = require('debug')
const once = require('once')
const log = debug('libp2p:secio')
log.error = debug('libp2p:secio:error')
const handshake = require('./handshake')
const State = require('./state')
module.exports = {
tag: '/secio/1.0.0',
encrypt (localId, conn, remoteId, callback) {
assert(localId, 'no local private key provided')
assert(conn, 'no connection for the handshake provided')
if (typeof remoteId === 'function') {
callback = remoteId
remoteId = undefined
}
callback = once(callback || function (err) {
if (err) { log.error(err) }
})
const timeout = 60 * 1000 * 5
const state = new State(localId, remoteId, timeout, callback)
function finish (err) {
if (err) { return callback(err) }
conn.getPeerInfo((err, peerInfo) => {
encryptedConnection.setInnerConn(state.secure)
if (err) { // no peerInfo yet, means I'm the receiver
encryptedConnection.setPeerInfo(new PeerInfo(state.id.remote))
}
callback()
})
}
const encryptedConnection = new Connection(undefined, conn)
pull(
conn,
handshake(state, finish),
conn
)
return encryptedConnection
}
}