11'use strict'
22
33const series = require ( 'async/series' )
4- const lp = require ( 'pull-length-prefixed' )
5- const nacl = require ( 'tweetnacl' )
64const pull = require ( 'pull-stream' )
75const handshake = require ( 'pull-handshake' )
6+ const debug = require ( 'debug' )
87
98const crypto = require ( './crypto' )
109const Errors = require ( './errors' )
10+ const NONCE_LENGTH = require ( './key-generator' ) . NONCE_LENGTH
11+
12+ const log = debug ( 'libp2p:pnet' )
13+ log . err = debug ( 'libp2p:pnet:err' )
14+ log . trace = debug ( 'libp2p:pnet:trace' )
1115
1216/**
1317 * Exchanges nonces with a connected peer. The nonce read from the peer
@@ -18,21 +22,13 @@ const Errors = require('./errors')
1822 * @returns {void }
1923 */
2024function exchangeNonce ( state , callback ) {
21- pull (
22- // Write the nonce
23- pull . values ( [ state . local . nonce ] ) ,
24- // Append 4 bytes
25- lp . encode ( { fixed : true , bytes : 4 } ) ,
26- // Collect the data and write it to the handshake
27- pull . collect ( ( err , res ) => {
28- if ( err ) { return callback ( err ) }
29- state . shake . write ( res [ 0 ] )
30- } )
31- )
25+ // Write the nonce
26+ state . shake . write ( state . local . nonce )
3227
3328 // Read the nonce from the handshake
34- lp . decodeFromReader ( state . shake , { fixed : true , bytes : 4 } , ( err , nonce ) => {
29+ state . shake . read ( NONCE_LENGTH , ( err , nonce ) => {
3530 state . remote . nonce = nonce || null
31+ log . trace ( 'nonce exchange complete' )
3632 callback ( err )
3733 } )
3834}
@@ -46,11 +42,7 @@ function exchangeNonce (state, callback) {
4642 */
4743function protectConnection ( state , callback ) {
4844 const stream = state . shake . rest ( )
49- const shake = handshake ( { timeout : state . timeout } , ( err ) => {
50- if ( err ) {
51- callback ( err )
52- }
53- } )
45+ const shake = handshake ( { timeout : state . timeout } )
5446
5547 // Bridge the handshakes to form the encrypted connection
5648 pull (
@@ -61,23 +53,8 @@ function protectConnection (state, callback) {
6153 stream
6254 )
6355
64- // Exchange nonces via the handshake
65- shake . handshake . write ( state . local . nonce )
66- shake . handshake . read ( nacl . box . nonceLength , ( err , nonceBack ) => {
67- if ( err ) {
68- state . secure . resolve ( {
69- source : pull . error ( err ) ,
70- sink : ( ) => { }
71- } )
72- return callback ( err )
73- }
74-
75- // Resolve the deferred connection with the completed handshake
76- // This will be set as the inner connection in the final protected
77- // connection result
78- state . secure . resolve ( shake . handshake . rest ( ) )
79- callback ( )
80- } )
56+ state . secure . resolve ( shake . handshake . rest ( ) )
57+ callback ( )
8158}
8259
8360/**
@@ -88,6 +65,8 @@ function protectConnection (state, callback) {
8865 * @returns {PullStream } a handshake stream
8966 */
9067module . exports = function handshake ( state , callback ) {
68+ log . trace ( 'begin handshake' )
69+
9170 series ( [
9271 ( cb ) => exchangeNonce ( state , cb ) ,
9372 ( cb ) => protectConnection ( state , cb )
0 commit comments