Written handshake exchange

This commit is contained in:
morrigan
2019-11-21 13:38:39 +01:00
parent f6bc40baf4
commit b2d058291c
3 changed files with 47 additions and 24 deletions

View File

@ -2,56 +2,79 @@ import { bytes, bytes32 } from "./@types/basic";
import { NoiseSession, XXHandshake } from "./xx";
import { KeyPair } from "./@types/libp2p";
import { Buffer } from "buffer";
import {createHandshakePayload, getHandshakePayload, signPayload} from "./utils";
type handshakeType = "XX";
export class Handshake {
private type: handshakeType;
private remotePublicKey: bytes;
private signedPayload: bytes;
private prologue: bytes32;
private staticKeys: KeyPair;
private connection: any;
private xx: XXHandshake;
constructor(
type: handshakeType,
remotePublicKey: bytes,
prologue: bytes32,
signedPayload: bytes,
staticKeys: KeyPair,
connection,
) {
this.type = type;
this.remotePublicKey = remotePublicKey;
this.signedPayload = signedPayload;
this.prologue = prologue;
this.staticKeys = staticKeys;
this.connection = connection;
this.xx = new XXHandshake();
}
// stage 0
async propose(isInitiator: boolean) : Promise<NoiseSession> {
const xx = new XXHandshake();
const ns = await xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
async propose(isInitiator: boolean, earlyData?: bytes) : Promise<NoiseSession> {
const ns = await this.xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
if (isInitiator) {
const message = Buffer.concat([Buffer.alloc(0), this.signedPayload]);
const messageBuffer = await xx.sendMessage(ns, message);
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
const handshakePayload = await createHandshakePayload(
this.staticKeys.publicKey,
signedPayload,
earlyData,
this.staticKeys.privateKey
);
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
const messageBuffer = await this.xx.sendMessage(ns, message);
this.connection.writeLP(messageBuffer);
} else {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await xx.recvMessage(ns, receivedMessageBuffer);
const plaintext = await this.xx.recvMessage(ns, receivedMessageBuffer);
}
return ns;
}
async exchange() : Promise<NoiseSession> {
async exchange(isInitiator: boolean, session: NoiseSession) : Promise<void> {
if (isInitiator) {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await this.xx.recvMessage(session, receivedMessageBuffer);
} else {
// create payload as responder
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);
this.connection.writeLP(messageBuffer);
}
}
async finish() : Promise<NoiseSession> {
async finish(isInitiator: boolean, session: NoiseSession) : Promise<void> {
if (isInitiator) {
const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0));
this.connection.writeLP(messageBuffer);
} else {
const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await this.xx.recvMessage(session, receivedMessageBuffer);
}
}
}