2019-11-20 13:23:36 +01:00
|
|
|
import { bytes, bytes32 } from "./@types/basic";
|
2019-11-12 14:07:25 +01:00
|
|
|
import { NoiseSession, XXHandshake } from "./xx";
|
2019-11-20 22:52:08 +01:00
|
|
|
import { KeyPair } from "./@types/libp2p";
|
|
|
|
import { Buffer } from "buffer";
|
2019-11-21 13:38:39 +01:00
|
|
|
import {createHandshakePayload, getHandshakePayload, signPayload} from "./utils";
|
2019-11-20 13:23:36 +01:00
|
|
|
|
|
|
|
type handshakeType = "XX";
|
2019-11-11 21:58:04 +01:00
|
|
|
|
|
|
|
export class Handshake {
|
2019-11-20 13:23:36 +01:00
|
|
|
private type: handshakeType;
|
|
|
|
private remotePublicKey: bytes;
|
|
|
|
private prologue: bytes32;
|
|
|
|
private staticKeys: KeyPair;
|
2019-11-20 22:52:08 +01:00
|
|
|
private connection: any;
|
2019-11-21 13:38:39 +01:00
|
|
|
private xx: XXHandshake;
|
2019-11-20 13:23:36 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
type: handshakeType,
|
2019-11-11 21:58:04 +01:00
|
|
|
remotePublicKey: bytes,
|
|
|
|
prologue: bytes32,
|
|
|
|
staticKeys: KeyPair,
|
2019-11-20 22:52:08 +01:00
|
|
|
connection,
|
2019-11-20 13:23:36 +01:00
|
|
|
) {
|
|
|
|
this.type = type;
|
|
|
|
this.remotePublicKey = remotePublicKey;
|
|
|
|
this.prologue = prologue;
|
|
|
|
this.staticKeys = staticKeys;
|
2019-11-20 22:52:08 +01:00
|
|
|
this.connection = connection;
|
2019-11-21 13:38:39 +01:00
|
|
|
|
|
|
|
this.xx = new XXHandshake();
|
2019-11-20 13:23:36 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 22:52:08 +01:00
|
|
|
// stage 0
|
2019-11-21 13:38:39 +01:00
|
|
|
async propose(isInitiator: boolean, earlyData?: bytes) : Promise<NoiseSession> {
|
|
|
|
const ns = await this.xx.initSession(isInitiator, this.prologue, this.staticKeys, this.remotePublicKey);
|
2019-11-20 22:52:08 +01:00
|
|
|
|
2019-11-20 21:38:14 +01:00
|
|
|
if (isInitiator) {
|
2019-11-21 13:38:39 +01:00
|
|
|
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);
|
2019-11-20 22:52:08 +01:00
|
|
|
this.connection.writeLP(messageBuffer);
|
|
|
|
} else {
|
|
|
|
const receivedMessageBuffer = (await this.connection.readLP()).slice();
|
2019-11-21 13:38:39 +01:00
|
|
|
const plaintext = await this.xx.recvMessage(ns, receivedMessageBuffer);
|
2019-11-20 21:38:14 +01:00
|
|
|
}
|
2019-11-20 22:52:08 +01:00
|
|
|
|
|
|
|
return ns;
|
2019-11-11 21:58:04 +01:00
|
|
|
}
|
2019-11-20 13:23:36 +01:00
|
|
|
|
2019-11-21 13:38:39 +01:00
|
|
|
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);
|
2019-11-20 13:23:36 +01:00
|
|
|
|
2019-11-21 13:38:39 +01:00
|
|
|
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
|
|
|
|
const messageBuffer = await this.xx.sendMessage(session, message);
|
|
|
|
this.connection.writeLP(messageBuffer);
|
|
|
|
}
|
2019-11-20 13:23:36 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 13:38:39 +01:00
|
|
|
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);
|
|
|
|
}
|
2019-11-20 13:23:36 +01:00
|
|
|
}
|
2019-11-11 21:58:04 +01:00
|
|
|
}
|