@@ -7,6 +7,7 @@ log.error = debug('libp2p:secio:error')
77const DuplexPair = require ( 'it-pair/duplex' )
88const pipe = require ( 'it-pipe' )
99const lp = require ( 'it-length-prefixed' )
10+ const Wrap = require ( 'it-pb-rpc' )
1011const { int32BEEncode, int32BEDecode } = lp
1112const ensureBuffer = require ( 'it-buffer' )
1213
@@ -20,32 +21,29 @@ module.exports = async function finish (state, wrapped) {
2021
2122 const proto = state . protocols
2223
23- wrapped . write ( state . proposal . in . rand )
24- const nonceBack = await wrapped . read ( state . proposal . in . rand . length )
25-
26- crypto . verifyNonce ( state , nonceBack . slice ( ) )
27-
28- log ( '3. finish - finish' )
29-
30- const network = wrapped . unwrap ( )
3124 const [ secure , user ] = DuplexPair ( )
25+ const network = wrapped . unwrap ( )
3226
3327 pipe (
34- secure . source , // this is FROM the user
28+ secure , // this is FROM the user
3529 ensureBuffer ,
3630 etm . createBoxStream ( proto . local . cipher , proto . local . mac ) ,
3731 lp . encode ( { lengthEncoder : int32BEEncode } ) ,
38- network . sink // and gets piped INTO the network
39- )
40-
41- pipe (
42- network . source , // this is FROM the network
32+ network , // and gets piped INTO and FROM the network
4333 lp . decode ( { lengthDecoder : int32BEDecode } ) ,
4434 ensureBuffer ,
4535 etm . createUnboxStream ( proto . remote . cipher , proto . remote . mac ) ,
46- secure . sink // and gets piped TO the user
36+ secure // and gets piped TO the user
4737 )
4838
39+ // Exchange nonces over the encrypted stream for final verification
40+ const shake = Wrap ( user )
41+ shake . write ( state . proposal . in . rand )
42+ const nonceBack = await shake . read ( state . proposal . in . rand . length )
43+ crypto . verifyNonce ( state , nonceBack . slice ( ) )
44+
45+ log ( '3. finish - finish' )
46+
4947 // Awesome that's all folks.
50- state . secure = user
48+ state . secure = shake . unwrap ( )
5149}
0 commit comments