From ac12fbf987e9de912c3d499619832ef411da876d Mon Sep 17 00:00:00 2001 From: Belma Gutlic Date: Fri, 3 Jan 2020 15:07:46 +0100 Subject: [PATCH] Update noise interface --- src/noise.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) 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,