Convert earlyData to Buffer

This commit is contained in:
Matija Petrunic 2020-04-17 10:56:31 +02:00
parent 09c5df3b0d
commit cb7eee7407
5 changed files with 11 additions and 7 deletions

View File

@ -5,7 +5,7 @@ import PeerId from "peer-id";
export interface IHandshake { export interface IHandshake {
session: NoiseSession; session: NoiseSession;
remotePeer: PeerId; remotePeer: PeerId;
earlyData: Uint8Array; earlyData: Buffer;
encrypt(plaintext: bytes, session: NoiseSession): bytes; encrypt(plaintext: bytes, session: NoiseSession): bytes;
decrypt(ciphertext: bytes, session: NoiseSession): {plaintext: bytes; valid: boolean}; decrypt(ciphertext: bytes, session: NoiseSession): {plaintext: bytes; valid: boolean};
} }

View File

@ -14,7 +14,7 @@ export interface INoiseConnection {
export type SecureOutbound = { export type SecureOutbound = {
conn: any; conn: any;
earlyData: Uint8Array; earlyData: Buffer;
remotePeer: PeerId; remotePeer: PeerId;
} }

View File

@ -15,7 +15,7 @@ export class IKHandshake implements IHandshake {
public isInitiator: boolean; public isInitiator: boolean;
public session: NoiseSession; public session: NoiseSession;
public remotePeer!: PeerId; public remotePeer!: PeerId;
public earlyData: Uint8Array; public earlyData: Buffer;
private payload: bytes; private payload: bytes;
private prologue: bytes32; private prologue: bytes32;
@ -135,7 +135,7 @@ export class IKHandshake implements IHandshake {
private setEarlyData(data: Uint8Array|null|undefined): void { private setEarlyData(data: Uint8Array|null|undefined): void {
if(data){ if(data){
this.earlyData = data; this.earlyData = Buffer.from(data.buffer, data.byteOffset, data.length);
} }
} }
} }

View File

@ -67,7 +67,11 @@ export class XXFallbackHandshake extends XXHandshake {
this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload); this.remotePeer = this.remotePeer || await getPeerIdFromPayload(decodedPayload);
await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer); await verifySignedPayload(this.session.hs.rs, decodedPayload, this.remotePeer);
if(decodedPayload.data){ if(decodedPayload.data){
this.earlyData = decodedPayload.data; this.earlyData = Buffer.from(
decodedPayload.data.buffer,
decodedPayload.data.byteOffset,
decodedPayload.data.length
);
} }
} catch (e) { } catch (e) {
throw new Error(`Error occurred while verifying signed payload from responder: ${e.message}`); throw new Error(`Error occurred while verifying signed payload from responder: ${e.message}`);

View File

@ -19,7 +19,7 @@ export class XXHandshake implements IHandshake {
public isInitiator: boolean; public isInitiator: boolean;
public session: NoiseSession; public session: NoiseSession;
public remotePeer!: PeerId; public remotePeer!: PeerId;
public earlyData: Uint8Array; public earlyData: Buffer;
protected payload: bytes; protected payload: bytes;
protected connection: WrappedConnection; protected connection: WrappedConnection;
@ -153,7 +153,7 @@ export class XXHandshake implements IHandshake {
private setEarlyData(data: Uint8Array|null|undefined): void { private setEarlyData(data: Uint8Array|null|undefined): void {
if(data){ if(data){
this.earlyData = data this.earlyData = Buffer.from(data.buffer, data.byteOffset, data.length);
} }
} }
} }