From cb7eee74074bcd88c0317ab532dfb6a9c86b4c0b Mon Sep 17 00:00:00 2001 From: Matija Petrunic Date: Fri, 17 Apr 2020 10:56:31 +0200 Subject: [PATCH] Convert earlyData to Buffer --- src/@types/handshake-interface.ts | 2 +- src/@types/libp2p.ts | 2 +- src/handshake-ik.ts | 4 ++-- src/handshake-xx-fallback.ts | 6 +++++- src/handshake-xx.ts | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/@types/handshake-interface.ts b/src/@types/handshake-interface.ts index a8de48b..a2d930a 100644 --- a/src/@types/handshake-interface.ts +++ b/src/@types/handshake-interface.ts @@ -5,7 +5,7 @@ import PeerId from "peer-id"; export interface IHandshake { session: NoiseSession; remotePeer: PeerId; - earlyData: Uint8Array; + earlyData: Buffer; encrypt(plaintext: bytes, session: NoiseSession): bytes; decrypt(ciphertext: bytes, session: NoiseSession): {plaintext: bytes; valid: boolean}; } diff --git a/src/@types/libp2p.ts b/src/@types/libp2p.ts index 588c71d..db29558 100644 --- a/src/@types/libp2p.ts +++ b/src/@types/libp2p.ts @@ -14,7 +14,7 @@ export interface INoiseConnection { export type SecureOutbound = { conn: any; - earlyData: Uint8Array; + earlyData: Buffer; remotePeer: PeerId; } diff --git a/src/handshake-ik.ts b/src/handshake-ik.ts index d2675a1..372319f 100644 --- a/src/handshake-ik.ts +++ b/src/handshake-ik.ts @@ -15,7 +15,7 @@ export class IKHandshake implements IHandshake { public isInitiator: boolean; public session: NoiseSession; public remotePeer!: PeerId; - public earlyData: Uint8Array; + public earlyData: Buffer; private payload: bytes; private prologue: bytes32; @@ -135,7 +135,7 @@ export class IKHandshake implements IHandshake { private setEarlyData(data: Uint8Array|null|undefined): void { if(data){ - this.earlyData = data; + this.earlyData = Buffer.from(data.buffer, data.byteOffset, data.length); } } } diff --git a/src/handshake-xx-fallback.ts b/src/handshake-xx-fallback.ts index 9b29e2b..34647a3 100644 --- a/src/handshake-xx-fallback.ts +++ b/src/handshake-xx-fallback.ts @@ -67,7 +67,11 @@ export class XXFallbackHandshake extends XXHandshake { this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload); await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer); if(decodedPayload.data){ - this.earlyData = decodedPayload.data; + this.earlyData = Buffer.from( + decodedPayload.data.buffer, + decodedPayload.data.byteOffset, + decodedPayload.data.length + ); } } catch (e) { throw new Error(`Error occurred while verifying signed payload from responder: ${e.message}`); diff --git a/src/handshake-xx.ts b/src/handshake-xx.ts index 44baa1f..3f217ac 100644 --- a/src/handshake-xx.ts +++ b/src/handshake-xx.ts @@ -19,7 +19,7 @@ export class XXHandshake implements IHandshake { public isInitiator: boolean; public session: NoiseSession; public remotePeer!: PeerId; - public earlyData: Uint8Array; + public earlyData: Buffer; protected payload: bytes; protected connection: WrappedConnection; @@ -153,7 +153,7 @@ export class XXHandshake implements IHandshake { private setEarlyData(data: Uint8Array|null|undefined): void { if(data){ - this.earlyData = data + this.earlyData = Buffer.from(data.buffer, data.byteOffset, data.length); } } }