2016-05-22 01:03:53 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const debug = require('debug')
|
2016-06-13 19:10:38 +02:00
|
|
|
|
|
|
|
const support = require('../support')
|
|
|
|
const crypto = require('./crypto')
|
2016-05-22 01:33:50 +02:00
|
|
|
|
2016-05-23 12:42:21 +02:00
|
|
|
const log = debug('libp2p:secio')
|
|
|
|
log.error = debug('libp2p:secio:error')
|
2016-05-22 01:03:53 +02:00
|
|
|
|
2016-05-22 18:36:31 +02:00
|
|
|
// step 2. Exchange
|
|
|
|
// -- exchange (signed) ephemeral keys. verify signatures.
|
2016-06-13 19:10:38 +02:00
|
|
|
module.exports = function exchange (state, cb) {
|
2016-05-23 12:42:21 +02:00
|
|
|
log('2. exchange - start')
|
2016-05-22 22:39:36 +02:00
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
log('2. exchange - writing exchange')
|
|
|
|
support.write(state, crypto.createExchange(state))
|
|
|
|
support.read(state.shake, (err, msg) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
2016-05-22 18:36:31 +02:00
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
log('2. exchange - reading exchange')
|
2016-05-23 12:42:21 +02:00
|
|
|
|
2016-05-22 18:36:31 +02:00
|
|
|
try {
|
2016-06-13 19:10:38 +02:00
|
|
|
crypto.verify(state, msg)
|
|
|
|
crypto.generateKeys(state)
|
2016-05-22 18:36:31 +02:00
|
|
|
} catch (err) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log('2. exchange - finish')
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
}
|