1- import { InvalidCryptoExchangeError , UnexpectedPeerError } from '@libp2p/interface/errors'
21import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
32import { decode0 , decode1 , decode2 , encode0 , encode1 , encode2 } from './encoder.js'
3+ import { InvalidCryptoExchangeError , UnexpectedPeerError } from './errors.js'
44import { XX } from './handshakes/xx.js'
55import {
6- logger ,
76 logLocalStaticKeys ,
87 logLocalEphemeralKeys ,
98 logRemoteEphemeralKey ,
@@ -20,8 +19,9 @@ import type { IHandshake } from './@types/handshake-interface.js'
2019import type { CipherState , NoiseSession } from './@types/handshake.js'
2120import type { KeyPair } from './@types/libp2p.js'
2221import type { ICryptoInterface } from './crypto.js'
22+ import type { NoiseComponents } from './index.js'
2323import type { NoiseExtensions } from './proto/payload.js'
24- import type { PeerId } from '@libp2p/interface/peer-id '
24+ import type { Logger , PeerId } from '@libp2p/interface'
2525import type { LengthPrefixedStream } from 'it-length-prefixed-stream'
2626import type { Uint8ArrayList } from 'uint8arraylist'
2727
@@ -37,8 +37,10 @@ export class XXHandshake implements IHandshake {
3737 protected staticKeypair : KeyPair
3838
3939 private readonly prologue : bytes32
40+ private readonly log : Logger
4041
4142 constructor (
43+ components : NoiseComponents ,
4244 isInitiator : boolean ,
4345 payload : bytes ,
4446 prologue : bytes32 ,
@@ -48,6 +50,7 @@ export class XXHandshake implements IHandshake {
4850 remotePeer ?: PeerId ,
4951 handshake ?: XX
5052 ) {
53+ this . log = components . logger . forComponent ( 'libp2p:noise:xxhandshake' )
5154 this . isInitiator = isInitiator
5255 this . payload = payload
5356 this . prologue = prologue
@@ -56,45 +59,45 @@ export class XXHandshake implements IHandshake {
5659 if ( remotePeer ) {
5760 this . remotePeer = remotePeer
5861 }
59- this . xx = handshake ?? new XX ( crypto )
62+ this . xx = handshake ?? new XX ( components , crypto )
6063 this . session = this . xx . initSession ( this . isInitiator , this . prologue , this . staticKeypair )
6164 }
6265
6366 // stage 0
6467 public async propose ( ) : Promise < void > {
65- logLocalStaticKeys ( this . session . hs . s )
68+ logLocalStaticKeys ( this . session . hs . s , this . log )
6669 if ( this . isInitiator ) {
67- logger . trace ( 'Stage 0 - Initiator starting to send first message.' )
70+ this . log . trace ( 'Stage 0 - Initiator starting to send first message.' )
6871 const messageBuffer = this . xx . sendMessage ( this . session , uint8ArrayAlloc ( 0 ) )
6972 await this . connection . write ( encode0 ( messageBuffer ) )
70- logger . trace ( 'Stage 0 - Initiator finished sending first message.' )
71- logLocalEphemeralKeys ( this . session . hs . e )
73+ this . log . trace ( 'Stage 0 - Initiator finished sending first message.' )
74+ logLocalEphemeralKeys ( this . session . hs . e , this . log )
7275 } else {
73- logger . trace ( 'Stage 0 - Responder waiting to receive first message...' )
76+ this . log . trace ( 'Stage 0 - Responder waiting to receive first message...' )
7477 const receivedMessageBuffer = decode0 ( ( await this . connection . read ( ) ) . subarray ( ) )
7578 const { valid } = this . xx . recvMessage ( this . session , receivedMessageBuffer )
7679 if ( ! valid ) {
7780 throw new InvalidCryptoExchangeError ( 'xx handshake stage 0 validation fail' )
7881 }
79- logger . trace ( 'Stage 0 - Responder received first message.' )
80- logRemoteEphemeralKey ( this . session . hs . re )
82+ this . log . trace ( 'Stage 0 - Responder received first message.' )
83+ logRemoteEphemeralKey ( this . session . hs . re , this . log )
8184 }
8285 }
8386
8487 // stage 1
8588 public async exchange ( ) : Promise < void > {
8689 if ( this . isInitiator ) {
87- logger . trace ( 'Stage 1 - Initiator waiting to receive first message from responder...' )
90+ this . log . trace ( 'Stage 1 - Initiator waiting to receive first message from responder...' )
8891 const receivedMessageBuffer = decode1 ( ( await this . connection . read ( ) ) . subarray ( ) )
8992 const { plaintext, valid } = this . xx . recvMessage ( this . session , receivedMessageBuffer )
9093 if ( ! valid ) {
9194 throw new InvalidCryptoExchangeError ( 'xx handshake stage 1 validation fail' )
9295 }
93- logger . trace ( 'Stage 1 - Initiator received the message.' )
94- logRemoteEphemeralKey ( this . session . hs . re )
95- logRemoteStaticKey ( this . session . hs . rs )
96+ this . log . trace ( 'Stage 1 - Initiator received the message.' )
97+ logRemoteEphemeralKey ( this . session . hs . re , this . log )
98+ logRemoteStaticKey ( this . session . hs . rs , this . log )
9699
97- logger . trace ( "Initiator going to check remote's signature..." )
100+ this . log . trace ( "Initiator going to check remote's signature..." )
98101 try {
99102 const decodedPayload = decodePayload ( plaintext )
100103 this . remotePeer = this . remotePeer || await getPeerIdFromPayload ( decodedPayload )
@@ -104,31 +107,31 @@ export class XXHandshake implements IHandshake {
104107 const err = e as Error
105108 throw new UnexpectedPeerError ( `Error occurred while verifying signed payload: ${ err . message } ` )
106109 }
107- logger . trace ( 'All good with the signature!' )
110+ this . log . trace ( 'All good with the signature!' )
108111 } else {
109- logger . trace ( 'Stage 1 - Responder sending out first message with signed payload and static key.' )
112+ this . log . trace ( 'Stage 1 - Responder sending out first message with signed payload and static key.' )
110113 const messageBuffer = this . xx . sendMessage ( this . session , this . payload )
111114 await this . connection . write ( encode1 ( messageBuffer ) )
112- logger . trace ( 'Stage 1 - Responder sent the second handshake message with signed payload.' )
113- logLocalEphemeralKeys ( this . session . hs . e )
115+ this . log . trace ( 'Stage 1 - Responder sent the second handshake message with signed payload.' )
116+ logLocalEphemeralKeys ( this . session . hs . e , this . log )
114117 }
115118 }
116119
117120 // stage 2
118121 public async finish ( ) : Promise < void > {
119122 if ( this . isInitiator ) {
120- logger . trace ( 'Stage 2 - Initiator sending third handshake message.' )
123+ this . log . trace ( 'Stage 2 - Initiator sending third handshake message.' )
121124 const messageBuffer = this . xx . sendMessage ( this . session , this . payload )
122125 await this . connection . write ( encode2 ( messageBuffer ) )
123- logger . trace ( 'Stage 2 - Initiator sent message with signed payload.' )
126+ this . log . trace ( 'Stage 2 - Initiator sent message with signed payload.' )
124127 } else {
125- logger . trace ( 'Stage 2 - Responder waiting for third handshake message...' )
128+ this . log . trace ( 'Stage 2 - Responder waiting for third handshake message...' )
126129 const receivedMessageBuffer = decode2 ( ( await this . connection . read ( ) ) . subarray ( ) )
127130 const { plaintext, valid } = this . xx . recvMessage ( this . session , receivedMessageBuffer )
128131 if ( ! valid ) {
129132 throw new InvalidCryptoExchangeError ( 'xx handshake stage 2 validation fail' )
130133 }
131- logger . trace ( 'Stage 2 - Responder received the message, finished handshake.' )
134+ this . log . trace ( 'Stage 2 - Responder received the message, finished handshake.' )
132135
133136 try {
134137 const decodedPayload = decodePayload ( plaintext )
@@ -140,7 +143,7 @@ export class XXHandshake implements IHandshake {
140143 throw new UnexpectedPeerError ( `Error occurred while verifying signed payload: ${ err . message } ` )
141144 }
142145 }
143- logCipherState ( this . session )
146+ logCipherState ( this . session , this . log )
144147 }
145148
146149 public encrypt ( plaintext : Uint8Array | Uint8ArrayList , session : NoiseSession ) : Uint8Array | Uint8ArrayList {
0 commit comments