diff --git a/src/noise.ts b/src/noise.ts index 8a5f30a..5e94cc6 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -48,7 +48,7 @@ export class Noise implements NoiseConnection { public async secureOutbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise { const wrappedConnection = Wrap(connection); const libp2pPublicKey = localPeer.marshalPubKey(); - const handshake = await this.performXXHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer); + const handshake = await this.performHandshake(wrappedConnection, true, libp2pPublicKey, remotePeer); const conn = await this.createSecureConnection(wrappedConnection, handshake); return { @@ -67,7 +67,7 @@ export class Noise implements NoiseConnection { public async secureInbound(localPeer: PeerId, connection: any, remotePeer: PeerId): Promise { const wrappedConnection = Wrap(connection); const libp2pPublicKey = localPeer.marshalPubKey(); - const handshake = await this.performXXHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer); + const handshake = await this.performHandshake(wrappedConnection, false, libp2pPublicKey, remotePeer); const conn = await this.createSecureConnection(wrappedConnection, handshake); return { @@ -76,6 +76,28 @@ export class Noise implements NoiseConnection { }; } + /** + * If Noise pipes supported, tries IK handshake first with XX as fallback if it fails. + * If remote peer static key is unknown, use XX. + * @param connection + * @param isInitiator + * @param libp2pPublicKey + * @param remotePeer + */ + private async performHandshake( + connection: WrappedConnection, + isInitiator: boolean, + libp2pPublicKey: bytes, + remotePeer: PeerId, + ): Promise { + if (false) { + // TODO: Implement noise pipes + + } else { + return await this.performXXHandshake(connection, isInitiator, libp2pPublicKey, remotePeer) + } + } + private async performXXHandshake( connection: WrappedConnection, isInitiator: boolean,