Create test

This commit is contained in:
morrigan
2019-11-27 14:19:35 +01:00
parent b5941c750c
commit d03f4974ba
6 changed files with 48 additions and 17 deletions

View File

@ -2,7 +2,7 @@ declare module "it-pb-rpc" {
import { Buffer } from "buffer";
import { Duplex } from "it-pair";
type WrappedDuplex = {
read(bytes: number): Promise<Buffer>,
read(bytes?: number): Promise<Buffer>,
readLP(): Promise<Buffer>,
write(input: Buffer): void,
writeLP(input: Buffer): void,

View File

@ -32,6 +32,7 @@ export class Handshake {
prologue: bytes32,
staticKeys: KeyPair,
connection: WrappedConnection,
handshake?: XXHandshake,
) {
this.type = type;
this.isInitiator = isInitiator;
@ -40,7 +41,7 @@ export class Handshake {
this.staticKeys = staticKeys;
this.connection = connection;
this.xx = new XXHandshake();
this.xx = handshake || new XXHandshake();
}
// stage 0
@ -55,8 +56,7 @@ export class Handshake {
earlyData,
this.staticKeys.privateKey
);
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
const messageBuffer = await this.xx.sendMessage(ns, message);
const messageBuffer = await this.xx.sendMessage(ns, handshakePayload);
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
logger("Stage 0 - Initiator finished proposing");
@ -80,8 +80,7 @@ export class Handshake {
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
const handshakePayload = await createHandshakePayload(this.remotePublicKey, signedPayload);
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
const messageBuffer = await this.xx.sendMessage(session, message);
const messageBuffer = await this.xx.sendMessage(session, handshakePayload);
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
logger('Stage 1 - Responder sent the message.')
}

View File

@ -5,10 +5,9 @@ import DuplexPair from 'it-pair/duplex';
import ensureBuffer from 'it-buffer';
import pipe from 'it-pipe';
import lp from 'it-length-prefixed';
const { int16BEEncode, int16BEDecode } = lp;
import { Handshake } from "./handshake";
import { generateKeypair } from "./utils";
import { generateKeypair, int16BEDecode, int16BEEncode } from "./utils";
import { decryptStream, encryptStream } from "./crypto";
import { bytes } from "./@types/basic";
import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";

View File

@ -74,3 +74,13 @@ export function decodeMessageBuffer(message: bytes) : MessageBuffer {
}
}
export const int16BEEncode = (value, target, offset) => {
target = target || Buffer.allocUnsafe(2);
return target.writeInt16BE(value, offset);
};
int16BEEncode.bytes = 2;
export const int16BEDecode = data => {
if (data.length < 2) throw RangeError('Could not decode int16BE');
return data.readInt16BE(0);}
int16BEDecode.bytes = 2;

View File

@ -308,6 +308,7 @@ export class XXHandshake {
throw new Error("Handshake state `e` param is missing.");
}
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.rs));
const plaintext = await this.decryptAndHash(hs.ss, message.ciphertext);
const { cs1, cs2 } = this.split(hs.ss);