diff --git a/src/handshake-ik.ts b/src/handshake-ik.ts index ea29a4c..73b6c9b 100644 --- a/src/handshake-ik.ts +++ b/src/handshake-ik.ts @@ -54,7 +54,7 @@ export class IKHandshake implements IHandshake { logger("IK Stage 0 - Responder receiving message..."); const receivedMsg = await this.connection.readLP(); try { - const receivedMessageBuffer = decode1(receivedMsg); + const receivedMessageBuffer = decode1(receivedMsg.slice()); const plaintext = this.ik.recvMessage(this.session, receivedMessageBuffer); logger("IK Stage 0 - Responder got message, going to verify payload."); const decodedPayload = await decodePayload(plaintext); diff --git a/src/handshake-xx.ts b/src/handshake-xx.ts index 0085f13..fed9e4c 100644 --- a/src/handshake-xx.ts +++ b/src/handshake-xx.ts @@ -57,7 +57,7 @@ export class XXHandshake implements IHandshake { logger("Stage 0 - Initiator finished sending first message."); } else { logger("Stage 0 - Responder waiting to receive first message..."); - const receivedMessageBuffer = decode0(await this.connection.readLP()); + const receivedMessageBuffer = decode0((await this.connection.readLP()).slice()); this.xx.recvMessage(this.session, receivedMessageBuffer); logger("Stage 0 - Responder received first message."); } @@ -67,7 +67,7 @@ export class XXHandshake implements IHandshake { public async exchange(): Promise { if (this.isInitiator) { logger('Stage 1 - Initiator waiting to receive first message from responder...'); - const receivedMessageBuffer = decode1(await this.connection.readLP()); + const receivedMessageBuffer = decode1((await this.connection.readLP()).slice()); const plaintext = this.xx.recvMessage(this.session, receivedMessageBuffer); logger('Stage 1 - Initiator received the message. Got remote\'s static key.'); @@ -97,7 +97,7 @@ export class XXHandshake implements IHandshake { logger('Stage 2 - Initiator sent message with signed payload.'); } else { logger('Stage 2 - Responder waiting for third handshake message...'); - const receivedMessageBuffer = decode1(await this.connection.readLP()); + const receivedMessageBuffer = decode1((await this.connection.readLP()).slice()); const plaintext = this.xx.recvMessage(this.session, receivedMessageBuffer); logger('Stage 2 - Responder received the message, finished handshake. Got remote\'s static key.'); diff --git a/src/utils.ts b/src/utils.ts index d4e847d..ca14f1b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -74,7 +74,9 @@ export async function decodePayload(payload: bytes): Promise { ) as INoisePayload; } -export const getHandshakePayload = (publicKey: bytes ) => Buffer.concat([Buffer.from("noise-libp2p-static-key:"), publicKey]); +export function getHandshakePayload(publicKey: bytes): bytes { + return Buffer.concat([Buffer.from("noise-libp2p-static-key:"), publicKey]); +} async function isValidPeerId(peerId: bytes, publicKeyProtobuf: bytes) { const generatedPeerId = await PeerId.createFromPubKey(publicKeyProtobuf);